def test_message_body_data_type(): single_data = b'!@#$%^&*()_+1234567890' single_data_message = Message(body=single_data, body_type=MessageBodyType.Data) check_list = [data for data in single_data_message.get_data()] assert isinstance(single_data_message._body, DataBody) assert len(check_list) == 1 assert check_list[0] == single_data assert str(single_data_message) multiple_data = [ b'!@#$%^&*()_+1234567890', 'abcdefg~123', ] multiple_data_message = Message(body=multiple_data, body_type=MessageBodyType.Data) check_list = [data for data in multiple_data_message.get_data()] assert isinstance(multiple_data_message._body, DataBody) assert len(check_list) == 2 assert check_list[0] == multiple_data[0] assert check_list[1] == multiple_data[1].encode("UTF-8") assert str(multiple_data_message) with pytest.raises(TypeError): Message(body={"key": "value"}, body_type=MessageBodyType.Data) with pytest.raises(TypeError): Message(body=1, body_type=MessageBodyType.Data) with pytest.raises(TypeError): Message(body=['abc', True], body_type=MessageBodyType.Data) with pytest.raises(TypeError): Message(body=True, body_type=MessageBodyType.Data)
def _parse_payload(self, message: Message, origin_device_id, content_type, create_payload_error): payload = "" data = message.get_data() if data: payload = str(next(data), "utf8") if create_payload_error: payload = "Some Random Payload" if "application/json" not in content_type.lower(): self._warnings.append("Content type not supported. " "Content type found: {}. " "Content type expected: application/json. " "DeviceId: {}".format( content_type, origin_device_id)) else: try: payload = json.loads( re.compile(r"(\\r\\n)+|\\r+|\\n+").sub("", payload)) except Exception: self._errors.append("Invalid JSON format. " "DeviceId: {}, Raw payload {}".format( origin_device_id, payload)) return payload
def test_parse_message_bad_content_type_should_warn(self): # setup encoded_payload = json.dumps(self.payload).encode() properties = MessageProperties(content_type=self.bad_content_type) message = Message( body=encoded_payload, properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) parser = _parser.Event3Parser() # act parsed_msg = parser.parse_message(message, None, None, None, None, False) # verify # since the content_encoding header is not present, just dump the raw payload assert parsed_msg["event"]["payload"] == str(encoded_payload, "utf8") assert len(parser._errors) == 1 assert len(parser._warnings) == 1 assert len(parser._info) == 0 warning = parser._warnings[0] assert "Content type not supported." in warning assert self.bad_content_type in warning assert "application/json" in warning assert self.device_id in warning error = parser._errors[0] assert "No encoding found for message" in error
def test_parse_message_pnp_should_succeed(self): # setup interface_name = "interface_name" properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.payload).encode(), properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode(), _parser.INTERFACE_NAME_IDENTIFIER: interface_name.encode(), }, ) parser = _parser.Event3Parser() # act parsed_msg = parser.parse_message( message=message, pnp_context=True, interface_name=interface_name, properties=None, content_type_hint=None, simulate_errors=False, ) # verify assert parsed_msg["event"]["payload"] == self.payload assert parsed_msg["event"]["origin"] == self.device_id assert parsed_msg["event"]["interface"] == interface_name assert len(parser._errors) == 0 assert len(parser._warnings) == 0 assert len(parser._info) == 0
def test_parse_message_bad_json_should_fail(self): # setup properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=self.bad_payload.encode(), properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) parser = _parser.Event3Parser() # act parsed_msg = parser.parse_message(message, None, None, None, None, False) # verify # parsing should attempt to place raw payload into result even if parsing fails assert parsed_msg["event"]["payload"] == self.bad_payload assert len(parser._errors) == 1 assert len(parser._warnings) == 0 assert len(parser._info) == 0 errors = parser._errors[0] assert "Invalid JSON format." in errors assert self.device_id in errors assert self.bad_payload in errors
def xtest_validate_against_bad_template_should_not_throw(self): # setup device_template = "an_unparseable_template" properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.bad_dcm_payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, application_properties=_encode_app_props(self.app_properties), ) args = CommonParserArguments(properties=["all"]) parser = self._create_parser(device_template=device_template, message=message, args=args) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == self.bad_dcm_payload assert parsed_msg["event"]["origin"] == self.device_id expected_details = strings.invalid_template_extract_schema_failed( device_template) _validate_issues(parser, Severity.error, 1, 1, [expected_details])
def test_validate_against_bad_template_should_not_throw(self): # setup device_template = "an_unparseable_template" properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.bad_dcm_payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, application_properties=_encode_app_props(self.app_properties), ) args = CommonParserArguments(properties=["all"]) parser = self._create_parser(device_template=device_template, message=message, args=args) # haven't found a better way to force the error to occur within parser parser._central_template_provider.get_device_template = lambda x: Template( device_template) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == self.bad_dcm_payload assert parsed_msg["event"]["origin"] == self.device_id expected_details = strings.device_template_not_found( "Could not parse iot central device template.") _validate_issues(parser, Severity.error, 1, 1, [expected_details])
def send_message(target, data, auth=None, debug=False): """Send a single message to AMQP endpoint. :param target: The target AMQP endpoint. :type target: str, bytes or ~uamqp.address.Target :param data: The contents of the message to send. :type data: str, bytes or ~uamqp.message.Message :param auth: The authentication credentials for the endpoint. This should be one of the subclasses of uamqp.authentication.AMQPAuth. Currently this includes: - uamqp.authentication.SASLAnonymous - uamqp.authentication.SASLPlain - uamqp.authentication.SASTokenAuth If no authentication is supplied, SASLAnnoymous will be used by default. :type auth: ~uamqp.authentication.common.AMQPAuth :param debug: Whether to turn on network trace logs. If `True`, trace logs will be logged at INFO level. Default is `False`. :type debug: bool :return: A list of states for each message sent. :rtype: list[~uamqp.constants.MessageState] """ message = data if isinstance(data, Message) else Message(body=data) with SendClient(target, auth=auth, debug=debug) as send_client: send_client.queue_message(message) return send_client.send_all_messages()
def test_parse_message_bad_field_name_should_fail(self): # setup device_template = self._get_template() properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.bad_field_name).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) args = CommonParserArguments() parser = self._create_parser(device_template=device_template, message=message, args=args) # act parsed_msg = parser.parse_message() # verify # parsing should attempt to place raw payload into result even if parsing fails assert parsed_msg["event"]["payload"] == self.bad_field_name # field name contains '-' character error expected_details_1 = strings.invalid_field_name( list(self.bad_field_name.keys())) _validate_issues(parser, Severity.error, 2, 1, [expected_details_1]) # field name not present in template warning expected_details_2 = strings.invalid_field_name_mismatch_template( list(self.bad_field_name.keys()), device_template.schema_names) _validate_issues(parser, Severity.warning, 2, 1, [expected_details_2])
def test_parse_message_bad_content_type_should_warn(self): # setup encoded_payload = json.dumps(self.payload).encode() properties = MessageProperties(content_type=self.bad_content_type) message = Message( body=encoded_payload, properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) args = CommonParserArguments(content_type="application/json") parser = common_parser.CommonParser(message=message, common_parser_args=args) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == self.payload expected_details_1 = strings.invalid_encoding_none_found() expected_details_2 = strings.content_type_mismatch( self.bad_content_type, "application/json") _validate_issues( parser, Severity.warning, 2, 2, [expected_details_1, expected_details_2], )
def test_parse_bad_type_and_bad_payload_should_error(self): # setup encoded_payload = self.bad_payload.encode() properties = MessageProperties(content_type=self.bad_content_type, content_encoding=self.encoding) message = Message( body=encoded_payload, properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) args = CommonParserArguments(content_type="application/json") parser = common_parser.CommonParser(message=message, common_parser_args=args) # act parsed_msg = parser.parse_message() # verify # since the content_encoding header is not present, just dump the raw payload payload = str(encoded_payload, "utf8") assert parsed_msg["event"]["payload"] == payload expected_details_1 = strings.content_type_mismatch( self.bad_content_type, "application/json") _validate_issues(parser, Severity.warning, 2, 1, [expected_details_1]) expected_details_2 = strings.invalid_json() _validate_issues(parser, Severity.error, 2, 1, [expected_details_2])
def _mgmt_request_response(self, operation, message, callback, **kwargs): if not self.running: raise InvalidHandlerState("Client connection is closed.") try: application_properties = { ASSOCIATEDLINKPROPERTYNAME: self._handler.message_handler.name } except AttributeError: application_properties = {} mgmt_msg = Message(body=message, properties=MessageProperties( reply_to=self.mgmt_target, encoding=self.encoding, **kwargs), application_properties=application_properties) try: return self._handler.mgmt_request(mgmt_msg, operation, op_type=b"entity-mgmt", node=self.mgmt_target.encode( self.encoding), timeout=5000, callback=callback) except Exception as exp: # pylint: disable=broad-except raise ServiceBusError("Management request failed: {}".format(exp), exp)
def test_type_mismatch_should_error(self): # setup device_template = self._get_template() properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.type_mismatch_payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, application_properties=_encode_app_props(self.app_properties), ) args = CommonParserArguments(properties=["all"]) parser = self._create_parser(device_template=device_template, message=message, args=args) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == self.type_mismatch_payload assert parsed_msg["event"]["origin"] == self.device_id assert parsed_msg["event"]["properties"][ "application"] == self.app_properties field_name = list(self.type_mismatch_payload.keys())[0] data = list(self.type_mismatch_payload.values())[0] data_type = "boolean" expected_details = strings.invalid_primitive_schema_mismatch_template( field_name, data_type, data) _validate_issues(parser, Severity.error, 1, 1, [expected_details])
def _parse_payload(self, message: Message, content_type): payload = "" data = message.get_data() if data: payload = str(next(data), "utf8") if "application/json" in content_type.lower(): return self._try_parse_json(payload) return payload
def test_parse_message_should_succeed( self, device_id, encoding, content_type, interface_name, payload, properties, app_properties, module_id, ): # setup properties = MessageProperties(content_encoding=encoding, content_type=content_type) message = Message( body=json.dumps(payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: device_id.encode(), common_parser.INTERFACE_NAME_IDENTIFIER: interface_name.encode(), common_parser.MODULE_ID_IDENTIFIER: module_id.encode(), }, application_properties=_encode_app_props(app_properties), ) args = CommonParserArguments(properties=["all"], content_type=content_type) parser = common_parser.CommonParser(message=message, common_parser_args=args) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == payload assert parsed_msg["event"]["origin"] == device_id device_identifier = str(common_parser.DEVICE_ID_IDENTIFIER, "utf8") assert parsed_msg["event"]["annotations"][ device_identifier] == device_id module_identifier = str(common_parser.MODULE_ID_IDENTIFIER, "utf8") if module_id: assert parsed_msg["event"]["annotations"][ module_identifier] == module_id else: assert not parsed_msg["event"]["annotations"].get( module_identifier) properties = parsed_msg["event"]["properties"] assert properties["system"]["content_encoding"] == encoding assert properties["system"]["content_type"] == content_type assert properties["application"] == app_properties assert len(parser.issues_handler.get_all_issues()) == 0
def test_message_body_sequence_type(): single_list = [ 1, 2, b'aa', b'bb', True, b"abc", { b"key1": b"value", b"key2": -1.23 } ] single_list_message = Message(body=single_list, body_type=MessageBodyType.Sequence) check_list = [data for data in single_list_message.get_data()] assert isinstance(single_list_message._body, SequenceBody) assert len(check_list) == 1 assert check_list[0] == single_list assert str(single_list_message) multiple_lists = [[1, 2, 3, 4], [b'aa', b'bb', b'cc', b'dd']] multiple_lists_message = Message(body=multiple_lists, body_type=MessageBodyType.Sequence) check_list = [data for data in multiple_lists_message.get_data()] assert isinstance(multiple_lists_message._body, SequenceBody) assert len(check_list) == 2 assert check_list[0] == multiple_lists[0] assert check_list[1] == multiple_lists[1] assert str(multiple_lists_message) with pytest.raises(TypeError): Message(body={"key": "value"}, body_type=MessageBodyType.Sequence) with pytest.raises(TypeError): Message(body=1, body_type=MessageBodyType.Sequence) with pytest.raises(TypeError): Message(body='a', body_type=MessageBodyType.Sequence) with pytest.raises(TypeError): Message(body=True, body_type=MessageBodyType.Sequence)
def test_validate_invalid_telmetry_component_template_should_fail(self): # setup device_template = Template( load_json(FileNames.central_property_validation_template_file)) properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.bad_dcm_payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode(), common_parser.COMPONENT_NAME_IDENTIFIER: list(device_template.components.keys())[1].encode(), }, application_properties=_encode_app_props(self.app_properties), ) args = CommonParserArguments(properties=["all"]) parser = self._create_parser(device_template=device_template, message=message, args=args) # act parsed_msg = parser.parse_message() # verify assert parsed_msg["event"]["payload"] == self.bad_dcm_payload assert parsed_msg["event"]["origin"] == self.device_id device_identifier = str(common_parser.DEVICE_ID_IDENTIFIER, "utf8") assert parsed_msg["event"]["annotations"][ device_identifier] == self.device_id component_identifier = str(common_parser.COMPONENT_NAME_IDENTIFIER, "utf8") assert ( parsed_msg["event"]["annotations"][component_identifier] == list( device_template.components.keys())[1]) properties = parsed_msg["event"]["properties"] assert properties["system"]["content_encoding"] == self.encoding assert properties["system"]["content_type"] == self.content_type assert properties["application"] == self.app_properties expected_details = strings.invalid_field_name_component_mismatch_template( list(self.bad_dcm_payload.keys()), device_template.component_schema_names, ) _validate_issues(parser, Severity.warning, 1, 1, [expected_details])
def test_parse_message_should_succeed(self): # setup app_prop_type = "some_app_prop" app_prop_value = "some_app_value" properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.payload).encode(), properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, application_properties={ app_prop_type.encode(): app_prop_value.encode() }, ) parser = _parser.Event3Parser() # act parsed_msg = parser.parse_message( message=message, pnp_context=False, interface_name=None, properties={"all"}, content_type_hint=None, simulate_errors=False, ) # verify assert parsed_msg["event"]["payload"] == self.payload assert parsed_msg["event"]["origin"] == self.device_id device_identifier = str(_parser.DEVICE_ID_IDENTIFIER, "utf8") assert parsed_msg["event"]["annotations"][ device_identifier] == self.device_id properties = parsed_msg["event"]["properties"] assert properties["system"]["content_encoding"] == self.encoding assert properties["system"]["content_type"] == self.content_type assert properties["application"][app_prop_type] == app_prop_value assert len(parser._errors) == 0 assert len(parser._warnings) == 0 assert len(parser._info) == 0
def xtest_validate_against_template_should_fail(self): # setup device_template = self._get_template() properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.bad_dcm_payload).encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, application_properties=_encode_app_props(self.app_properties), ) args = CommonParserArguments(properties=["all"]) parser = self._create_parser(device_template=device_template, message=message, args=args) # act parsed_msg = parser.parse_message() schema = parser._extract_template_schemas_from_template( device_template) schema = parser._extract_template_schemas_from_template( device_template) # verify assert parsed_msg["event"]["payload"] == self.bad_dcm_payload assert parsed_msg["event"]["origin"] == self.device_id device_identifier = str(common_parser.DEVICE_ID_IDENTIFIER, "utf8") assert parsed_msg["event"]["annotations"][ device_identifier] == self.device_id properties = parsed_msg["event"]["properties"] assert properties["system"]["content_encoding"] == self.encoding assert properties["system"]["content_type"] == self.content_type assert properties["application"] == self.app_properties expected_details = strings.invalid_field_name_mismatch_template( list(self.bad_dcm_payload.keys()), list(schema.keys())) _validate_issues(parser, Severity.warning, 1, 1, [expected_details])
def test_parse_message_bad_encoding_should_warn(self): # setup properties = MessageProperties(content_encoding=self.bad_encoding, content_type=self.content_type) message = Message( body=json.dumps(self.payload).encode(self.bad_encoding), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) args = CommonParserArguments() parser = common_parser.CommonParser(message=message, common_parser_args=args) # act parser.parse_message() expected_details = strings.invalid_encoding(self.bad_encoding) _validate_issues(parser, Severity.warning, 1, 1, [expected_details])
def test_parse_message_pnp_should_fail(self): # setup actual_interface_name = "actual_interface_name" expected_interface_name = "expected_interface_name" properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=json.dumps(self.payload).encode(), properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode(), _parser.INTERFACE_NAME_IDENTIFIER: actual_interface_name.encode(), }, ) parser = _parser.Event3Parser() # act parsed_msg = parser.parse_message( message=message, pnp_context=True, interface_name=expected_interface_name, properties=None, content_type_hint=None, simulate_errors=False, ) # verify # all the items should still be parsed and available, but we should have an error assert parsed_msg["event"]["payload"] == self.payload assert parsed_msg["event"]["origin"] == self.device_id assert parsed_msg["event"]["interface"] == actual_interface_name assert len(parser._errors) == 1 assert len(parser._warnings) == 0 assert len(parser._info) == 0 actual_error = parser._errors[0] expected_error = "Inteface name mismatch. {}. Expected: {}, Actual: {}".format( self.device_id, expected_interface_name, actual_interface_name) assert actual_error == expected_error
async def _mgmt_request_response(self, operation, message, callback, **kwargs): if not self.running: raise InvalidHandlerState("Client connection is closed.") mgmt_msg = Message(body=message, properties=MessageProperties( reply_to=self.mgmt_target, encoding=self.encoding, **kwargs)) try: return await self._handler.mgmt_request_async( mgmt_msg, operation, op_type=b"entity-mgmt", node=self.mgmt_target.encode(self.encoding), timeout=5000, callback=callback) except Exception as exp: # pylint: disable=broad-except raise ServiceBusError("Management request failed: {}".format(exp), exp)
def test_parse_message_bad_encoding_should_fail(self): # setup properties = MessageProperties(content_encoding=self.bad_encoding, content_type=self.content_type) message = Message( body=json.dumps(self.payload).encode(self.bad_encoding), properties=properties, annotations={ _parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) parser = _parser.Event3Parser() # act parser.parse_message(message, None, None, None, None, False) assert len(parser._errors) == 1 assert len(parser._warnings) == 0 assert len(parser._info) == 0 errors = parser._errors[0] assert "Unsupported encoding detected: '{}'".format( self.bad_encoding) in errors
def test_parse_message_bad_json_should_fail(self): # setup properties = MessageProperties(content_encoding=self.encoding, content_type=self.content_type) message = Message( body=self.bad_payload.encode(), properties=properties, annotations={ common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode() }, ) args = CommonParserArguments() parser = common_parser.CommonParser(message=message, common_parser_args=args) # act parsed_msg = parser.parse_message() # verify # parsing should attempt to place raw payload into result even if parsing fails assert parsed_msg["event"]["payload"] == self.bad_payload expected_details = strings.invalid_json() _validate_issues(parser, Severity.error, 1, 1, [expected_details])
def test_deepcopy_batch_message(): ## DEEPCOPY WITH MESSAGES IN BATCH THAT HAVE HEADER/PROPERTIES properties = MessageProperties() properties.message_id = '2' properties.user_id = '1' properties.to = 'dkfj' properties.subject = 'dsljv' properties.reply_to = "kdjfk" properties.correlation_id = 'ienag' properties.content_type = 'b' properties.content_encoding = '39ru' properties.absolute_expiry_time = 24 properties.creation_time = 10 properties.group_id = '3irow' properties.group_sequence = 39 properties.reply_to_group_id = '39rud' header = MessageHeader() header.delivery_count = 3 header.time_to_live = 5 header.first_acquirer = 'dkfj' header.durable = True header.priority = 4 message = Message(body="test", properties=properties, header=header) message.on_send_complete = send_complete_callback message.footer = {'a': 2} message.state = constants.MessageState.ReceivedSettled message_batch = BatchMessage(data=[], multi_messages=False, properties=None) message_batch._body_gen.append(message) message_batch_copy = copy.deepcopy(message_batch) batch_message = list(message_batch._body_gen)[0] batch_copy_message = list(message_batch_copy._body_gen)[0] assert len(list(message_batch._body_gen)) == len( list(message_batch_copy._body_gen)) # check message attributes are equal to deepcopied message attributes assert list(batch_message.get_data()) == list( batch_copy_message.get_data()) assert batch_message.footer == batch_copy_message.footer assert batch_message.state == batch_copy_message.state assert batch_message.application_properties == batch_copy_message.application_properties assert batch_message.annotations == batch_copy_message.annotations assert batch_message.delivery_annotations == batch_copy_message.delivery_annotations assert batch_message.settled == batch_copy_message.settled assert batch_message.properties.message_id == batch_copy_message.properties.message_id assert batch_message.properties.user_id == batch_copy_message.properties.user_id assert batch_message.properties.to == batch_copy_message.properties.to assert batch_message.properties.subject == batch_copy_message.properties.subject assert batch_message.properties.reply_to == batch_copy_message.properties.reply_to assert batch_message.properties.correlation_id == batch_copy_message.properties.correlation_id assert batch_message.properties.content_type == batch_copy_message.properties.content_type assert batch_message.properties.content_encoding == batch_copy_message.properties.content_encoding assert batch_message.properties.absolute_expiry_time == batch_copy_message.properties.absolute_expiry_time assert batch_message.properties.creation_time == batch_copy_message.properties.creation_time assert batch_message.properties.group_id == batch_copy_message.properties.group_id assert batch_message.properties.group_sequence == batch_copy_message.properties.group_sequence assert batch_message.properties.reply_to_group_id == batch_copy_message.properties.reply_to_group_id assert batch_message.header.delivery_count == batch_copy_message.header.delivery_count assert batch_message.header.time_to_live == batch_copy_message.header.time_to_live assert batch_message.header.first_acquirer == batch_copy_message.header.first_acquirer assert batch_message.header.durable == batch_copy_message.header.durable assert batch_message.header.priority == batch_copy_message.header.priority
def test_message_pickle(): properties = MessageProperties() properties.message_id = '2' properties.user_id = '1' properties.to = 'dkfj' properties.subject = 'dsljv' properties.reply_to = "kdjfk" properties.correlation_id = 'ienag' properties.content_type = 'b' properties.content_encoding = '39ru' properties.absolute_expiry_time = 24 properties.creation_time = 10 properties.group_id = '3irow' properties.group_sequence = 39 properties.reply_to_group_id = '39rud' header = MessageHeader() header.delivery_count = 3 header.time_to_live = 5 header.first_acquirer = 'dkfj' header.durable = True header.priority = 4 data_message = Message(body=[b'testmessage1', b'testmessage2']) pickled = pickle.loads(pickle.dumps(data_message)) body = list(pickled.get_data()) assert len(body) == 2 assert body == [b'testmessage1', b'testmessage2'] sequence_message = Message(body=[[1234.56, b'testmessage2', True], [-1234.56, { b'key': b'value' }, False]], body_type=MessageBodyType.Sequence) pickled = pickle.loads(pickle.dumps(sequence_message)) body = list(pickled.get_data()) assert len(body) == 2 assert body == [[1234.56, b'testmessage2', True], [-1234.56, { b'key': b'value' }, False]] value_message = Message(body={b'key': [1, b'str', False]}, body_type=MessageBodyType.Value) pickled = pickle.loads(pickle.dumps(value_message)) body = pickled.get_data() assert body == {b'key': [1, b'str', False]} error = errors.MessageModified(False, False, {b'key': b'value'}) pickled_error = pickle.loads(pickle.dumps(error)) assert pickled_error._annotations == {b'key': b'value'} # pylint: disable=protected-access message = Message(body="test", properties=properties, header=header) message.on_send_complete = send_complete_callback message.footer = {'a': 2} message.state = constants.MessageState.ReceivedSettled pickled = pickle.loads(pickle.dumps(message)) assert list(message.get_data()) == [b"test"] assert message.footer == pickled.footer assert message.state == pickled.state assert message.application_properties == pickled.application_properties assert message.annotations == pickled.annotations assert message.delivery_annotations == pickled.delivery_annotations assert message.settled == pickled.settled assert message.properties.message_id == pickled.properties.message_id assert message.properties.user_id == pickled.properties.user_id assert message.properties.to == pickled.properties.to assert message.properties.subject == pickled.properties.subject assert message.properties.reply_to == pickled.properties.reply_to assert message.properties.correlation_id == pickled.properties.correlation_id assert message.properties.content_type == pickled.properties.content_type assert message.properties.content_encoding == pickled.properties.content_encoding assert message.properties.absolute_expiry_time == pickled.properties.absolute_expiry_time assert message.properties.creation_time == pickled.properties.creation_time assert message.properties.group_id == pickled.properties.group_id assert message.properties.group_sequence == pickled.properties.group_sequence assert message.properties.reply_to_group_id == pickled.properties.reply_to_group_id assert message.header.delivery_count == pickled.header.delivery_count assert message.header.time_to_live == pickled.header.time_to_live assert message.header.first_acquirer == pickled.header.first_acquirer assert message.header.durable == pickled.header.durable assert message.header.priority == pickled.header.priority # send with message param settler = errors.MessageAlreadySettled internal_message = c_uamqp.create_message() internal_message.add_body_data(b"hi") message_w_message_param = Message(message=internal_message, settler=settler, delivery_no=1) pickled = pickle.loads(pickle.dumps(message_w_message_param)) message_data = str(message_w_message_param.get_data()) pickled_data = str(pickled.get_data()) assert message_data == pickled_data assert message_w_message_param.footer == pickled.footer assert message_w_message_param.state == pickled.state assert message_w_message_param.application_properties == pickled.application_properties assert message_w_message_param.annotations == pickled.annotations assert message_w_message_param.delivery_annotations == pickled.delivery_annotations assert message_w_message_param.settled == pickled.settled assert pickled.delivery_no == 1 assert type(pickled._settler()) == type(settler()) # pylint: disable=protected-access
def test_message_body_value_type(): string_value = b'!@#$%^&*()_+1234567890' string_value_message = Message(body=string_value, body_type=MessageBodyType.Value) assert string_value_message.get_data() == string_value assert isinstance(string_value_message._body, ValueBody) assert str(string_value_message) float_value = 1.23 float_value_message = Message(body=float_value, body_type=MessageBodyType.Value) assert float_value_message.get_data() == float_value assert isinstance(string_value_message._body, ValueBody) assert str(float_value_message) dict_value = {b"key1 ": b"value1", b'key2': -1, b'key3': False} dict_value_message = Message(body=dict_value, body_type=MessageBodyType.Value) assert dict_value_message.get_data() == dict_value assert isinstance(string_value_message._body, ValueBody) assert str(dict_value_message) compound_list_value = [ 1, b'abc', True, [1.23, b'abc', False], { b"key1 ": b"value1", b'key2': -1 } ] compound_list_value_message = Message(body=compound_list_value, body_type=MessageBodyType.Value) assert compound_list_value_message.get_data() == compound_list_value assert isinstance(string_value_message._body, ValueBody) assert str(compound_list_value_message)
def test_message_auto_body_type(): single_data = b'!@#$%^&*()_+1234567890' single_data_message = Message(body=single_data) check_list = [data for data in single_data_message.get_data()] assert isinstance(single_data_message._body, DataBody) assert len(check_list) == 1 assert check_list[0] == single_data assert (str(single_data_message)) multiple_data = [b'!@#$%^&*()_+1234567890', 'abcdefg~123'] multiple_data_message = Message(body=multiple_data) check_list = [data for data in multiple_data_message.get_data()] assert isinstance(multiple_data_message._body, DataBody) assert len(check_list) == 2 assert check_list[0] == multiple_data[0] assert check_list[1] == multiple_data[1].encode("UTF-8") assert (str(multiple_data_message)) list_mixed_body = [b'!@#$%^&*()_+1234567890', 'abcdefg~123', False, 1.23] list_mixed_body_message = Message(body=list_mixed_body) check_data = list_mixed_body_message.get_data() assert isinstance(list_mixed_body_message._body, ValueBody) assert isinstance(check_data, list) assert len(check_data) == 4 assert check_data[0] == list_mixed_body[0] assert check_data[1] == list_mixed_body[1].encode("UTF-8") assert check_data[2] == list_mixed_body[2] assert check_data[3] == list_mixed_body[3] assert (str(list_mixed_body_message)) dic_mixed_body = {b'key1': b'value', b'key2': False, b'key3': -1.23} dic_mixed_body_message = Message(body=dic_mixed_body) check_data = dic_mixed_body_message.get_data() assert isinstance(dic_mixed_body_message._body, ValueBody) assert isinstance(check_data, dict) assert len(check_data) == 3 assert check_data == dic_mixed_body assert (str(dic_mixed_body_message))