mirror of https://github.com/OpenKMIP/PyKMIP.git
Merge pull request #150 from viktorTarasov/fix/service-client/batch-item-of-error-response
fix: kmip-client: batch item of error response
This commit is contained in:
commit
0ed92a1ea4
|
@ -21,6 +21,7 @@ from kmip.services.results import DiscoverVersionsResult
|
|||
from kmip.services.results import GetResult
|
||||
from kmip.services.results import GetAttributeListResult
|
||||
from kmip.services.results import LocateResult
|
||||
from kmip.services.results import OperationResult
|
||||
from kmip.services.results import QueryResult
|
||||
from kmip.services.results import RegisterResult
|
||||
from kmip.services.results import RekeyKeyPairResult
|
||||
|
@ -485,14 +486,18 @@ class KMIPProxy(KMIP):
|
|||
def _process_batch_items(self, response):
|
||||
results = []
|
||||
for batch_item in response.batch_items:
|
||||
operation = batch_item.operation.value
|
||||
operation = None
|
||||
if batch_item.operation is not None:
|
||||
operation = batch_item.operation.value
|
||||
processor = self._get_batch_item_processor(operation)
|
||||
result = processor(batch_item)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
def _get_batch_item_processor(self, operation):
|
||||
if operation == OperationEnum.CREATE_KEY_PAIR:
|
||||
if operation is None:
|
||||
return self._process_response_error
|
||||
elif operation == OperationEnum.CREATE_KEY_PAIR:
|
||||
return self._process_create_key_pair_batch_item
|
||||
elif operation == OperationEnum.GET_ATTRIBUTE_LIST:
|
||||
return self._process_get_attribute_list_batch_item
|
||||
|
@ -591,6 +596,12 @@ class KMIPProxy(KMIP):
|
|||
|
||||
return result
|
||||
|
||||
def _process_response_error(self, batch_item):
|
||||
result = OperationResult(
|
||||
batch_item.result_status, batch_item.result_reason,
|
||||
batch_item.result_message)
|
||||
return result
|
||||
|
||||
def _get(self,
|
||||
unique_identifier=None,
|
||||
key_format_type=None,
|
||||
|
|
|
@ -20,6 +20,8 @@ from kmip.core.attributes import PrivateKeyUniqueIdentifier
|
|||
from kmip.core.enums import AuthenticationSuite
|
||||
from kmip.core.enums import ConformanceClause
|
||||
from kmip.core.enums import CredentialType
|
||||
from kmip.core.enums import ResultStatus as ResultStatusEnum
|
||||
from kmip.core.enums import ResultReason as ResultReasonEnum
|
||||
from kmip.core.enums import Operation as OperationEnum
|
||||
from kmip.core.enums import QueryFunction as QueryFunctionEnum
|
||||
|
||||
|
@ -31,6 +33,9 @@ from kmip.core.messages.messages import RequestBatchItem
|
|||
from kmip.core.messages.messages import ResponseBatchItem
|
||||
from kmip.core.messages.messages import ResponseMessage
|
||||
from kmip.core.messages.contents import Operation
|
||||
from kmip.core.messages.contents import ResultStatus
|
||||
from kmip.core.messages.contents import ResultReason
|
||||
from kmip.core.messages.contents import ResultMessage
|
||||
from kmip.core.messages.contents import ProtocolVersion
|
||||
from kmip.core.messages.payloads.create_key_pair import \
|
||||
CreateKeyPairRequestPayload, CreateKeyPairResponsePayload
|
||||
|
@ -56,6 +61,7 @@ from kmip.services.kmip_client import KMIPProxy
|
|||
from kmip.services.results import CreateKeyPairResult
|
||||
from kmip.services.results import DiscoverVersionsResult
|
||||
from kmip.services.results import GetAttributeListResult
|
||||
from kmip.services.results import OperationResult
|
||||
from kmip.services.results import QueryResult
|
||||
from kmip.services.results import RekeyKeyPairResult
|
||||
|
||||
|
@ -373,6 +379,28 @@ class TestKMIPClient(TestCase):
|
|||
msg = "number of results " + base.format(0, len(results))
|
||||
self.assertEqual(0, len(results), msg)
|
||||
|
||||
def test_process_batch_item_with_error(self):
|
||||
result_status = ResultStatus(ResultStatusEnum.OPERATION_FAILED)
|
||||
result_reason = ResultReason(ResultReasonEnum.INVALID_MESSAGE)
|
||||
result_message = ResultMessage("message")
|
||||
|
||||
batch_item = ResponseBatchItem(
|
||||
result_status=result_status,
|
||||
result_reason=result_reason,
|
||||
result_message=result_message)
|
||||
response = ResponseMessage(batch_items=[batch_item])
|
||||
results = self.client._process_batch_items(response)
|
||||
|
||||
base = "expected {0}, received {1}"
|
||||
msg = "number of results " + base.format(1, len(results))
|
||||
self.assertEqual(1, len(results), msg)
|
||||
|
||||
result = results[0]
|
||||
self.assertIsInstance(result, OperationResult)
|
||||
self.assertEqual(result.result_status, result_status)
|
||||
self.assertEqual(result.result_reason, result_reason)
|
||||
self.assertEqual(result.result_message.value, "message")
|
||||
|
||||
def test_get_batch_item_processor(self):
|
||||
base = "expected {0}, received {1}"
|
||||
|
||||
|
@ -389,7 +417,8 @@ class TestKMIPClient(TestCase):
|
|||
self.assertEqual(expected, observed, msg)
|
||||
|
||||
self.assertRaisesRegexp(ValueError, "no processor for operation",
|
||||
self.client._get_batch_item_processor, None)
|
||||
self.client._get_batch_item_processor,
|
||||
0xA5A5A5A5)
|
||||
|
||||
expected = self.client._process_get_attribute_list_batch_item
|
||||
observed = self.client._get_batch_item_processor(
|
||||
|
|
Loading…
Reference in New Issue