From a55310368f1673006816708777ec2b5ad45a7aff Mon Sep 17 00:00:00 2001 From: Peter Hamilton Date: Sun, 27 Nov 2016 11:17:54 -0500 Subject: [PATCH] Updating the ApplicationSpecificInformation attribute This change adds some utility methods, including equality operators and string methods, to the ApplicationSpecificInformation attribute. Unit tests covering the changes are included. --- kmip/core/attributes.py | 34 ++++ kmip/core/primitives.py | 2 +- .../test_application_specific_information.py | 169 ++++++++++++++++++ 3 files changed, 204 insertions(+), 1 deletion(-) diff --git a/kmip/core/attributes.py b/kmip/core/attributes.py index 0eaea6e..f53bf94 100644 --- a/kmip/core/attributes.py +++ b/kmip/core/attributes.py @@ -757,6 +757,40 @@ class ApplicationSpecificInformation(Struct): """ self.__validate() + def __repr__(self): + application_namespace = "application_namespace={0}".format( + repr(self.application_namespace) + ) + application_data = "application_data={0}".format( + repr(self.application_data) + ) + return "ApplicationSpecificInformation({0}, {1})".format( + application_namespace, + application_data + ) + + def __str__(self): + return str({ + "application_namespace": str(self.application_namespace), + "application_data": str(self.application_data) + }) + + def __eq__(self, other): + if isinstance(other, ApplicationSpecificInformation): + if self.application_namespace != other.application_namespace: + return False + if self.application_data != other.application_data: + return False + return True + else: + return NotImplemented + + def __ne__(self, other): + if isinstance(other, ApplicationSpecificInformation): + return not self.__eq__(other) + else: + return NotImplemented + def __validate(self): if not isinstance(self.application_namespace, ApplicationNamespace): msg = "invalid application namespace" diff --git a/kmip/core/primitives.py b/kmip/core/primitives.py index a36759f..3bfc8e7 100644 --- a/kmip/core/primitives.py +++ b/kmip/core/primitives.py @@ -839,7 +839,7 @@ class TextString(Base): return "{0}(value={1})".format(type(self).__name__, repr(self.value)) def __str__(self): - return "{0}".format(repr(self.value)) + return "{0}".format(str(self.value)) def __eq__(self, other): if isinstance(other, TextString): diff --git a/kmip/tests/unit/core/attributes/test_application_specific_information.py b/kmip/tests/unit/core/attributes/test_application_specific_information.py index ee67ac6..69535a3 100644 --- a/kmip/tests/unit/core/attributes/test_application_specific_information.py +++ b/kmip/tests/unit/core/attributes/test_application_specific_information.py @@ -188,6 +188,175 @@ class TestApplicationSpecificInformation(TestCase): self._test_write(self.encoding, application_namespace, application_data) + def test_repr(self): + """ + Test that an ApplicationSpecificInformation object can be represented + using repr correctly. + """ + application_specific_info = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace("ssl"), + application_data=ApplicationData("www.example.com") + ) + s = repr(application_specific_info) + + self.assertEqual( + "ApplicationSpecificInformation(" + "application_namespace=ApplicationNamespace(value='ssl'), " + "application_data=ApplicationData(value='www.example.com'))", + s + ) + + def test_str(self): + """ + Test that an ApplicationSpecificInformation object can be turned into + a string correctly. + """ + application_specific_info = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace("ssl"), + application_data=ApplicationData("www.example.com") + ) + s = str(application_specific_info) + + self.assertEqual( + str({'application_namespace': 'ssl', + 'application_data': 'www.example.com'} + ), + s + ) + + def test_equal_on_equal(self): + """ + Test that the equality operator returns True when comparing two + ApplicationSpecificInformation objects with the same data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + + self.assertTrue(a == b) + self.assertTrue(b == a) + + def test_equal_on_not_equal_namespace(self): + """ + Test that the equality operator returns False when comparing two + ApplicationSpecificInformation objects with different data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace_1'), + application_data=ApplicationData('test_data') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace_2'), + application_data=ApplicationData('test_data') + ) + + self.assertFalse(a == b) + self.assertFalse(b == a) + + def test_equal_on_not_equal_data(self): + """ + Test that the equality operator returns False when comparing two + ApplicationSpecificInformation objects with different data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data_1') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data_2') + ) + + self.assertFalse(a == b) + self.assertFalse(b == a) + + def test_equal_on_type_mismatch(self): + """ + Test that the equality operator returns False when comparing a + ApplicationSpecificInformation object to a + non-ApplicationSpecificInformation object. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + b = "invalid" + + self.assertFalse(a == b) + self.assertFalse(b == a) + + def test_not_equal_on_equal(self): + """ + Test that the inequality operator returns False when comparing + two ApplicationSpecificInformation objects with the same internal + data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + + self.assertFalse(a != b) + self.assertFalse(b != a) + + def test_not_equal_on_not_equal_namespace(self): + """ + Test that the inequality operator returns True when comparing two + ApplicationSpecificInformation objects with different data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace_1'), + application_data=ApplicationData('test_data') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace_2'), + application_data=ApplicationData('test_data') + ) + + self.assertTrue(a != b) + self.assertTrue(b != a) + + def test_not_equal_on_not_equal_data(self): + """ + Test that the inequality operator returns True when comparing two + ApplicationSpecificInformation objects with different data. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data_1') + ) + b = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data_2') + ) + + self.assertTrue(a != b) + self.assertTrue(b != a) + + def test_not_equal_on_type_mismatch(self): + """ + Test that the equality operator returns True when comparing a + ApplicationSpecificInformation object to a + non-ApplicationSpecificInformation object. + """ + a = ApplicationSpecificInformation( + application_namespace=ApplicationNamespace('test_namespace'), + application_data=ApplicationData('test_data') + ) + b = "invalid" + + self.assertTrue(a != b) + self.assertTrue(b != a) + def _test_create(self, application_namespace, application_data): application_specific_info = ApplicationSpecificInformation.create( application_namespace, application_data)