def encode_amqp_type(amqp_type, test_value): """Encode an AMQP type from a stringified test_value""" if amqp_type == 'null': return None if amqp_type == 'boolean': return True if test_value == 'True' else False if amqp_type == 'ubyte': return proton.ubyte(int(test_value, 16)) if amqp_type == 'ushort': return proton.ushort(int(test_value, 16)) if amqp_type == 'uint': return proton.uint(int(test_value, 16)) if amqp_type == 'ulong': return proton.ulong(int(test_value, 16)) if amqp_type == 'byte': return proton.byte(int(test_value, 16)) if amqp_type == 'short': return proton.short(int(test_value, 16)) if amqp_type == 'int': return proton.int32(int(test_value, 16)) if amqp_type == 'long': return _compat.str2long(test_value, 16) if amqp_type == 'float': return proton.float32(struct.unpack('!f', _compat.decode_hex(test_value[2:]))[0]) if amqp_type == 'double': return struct.unpack('!d', _compat.decode_hex(test_value[2:]))[0] if amqp_type == 'decimal32': return proton.decimal32(int(test_value[2:], 16)) if amqp_type == 'decimal64': return proton.decimal64(_compat.str2long(test_value[2:], 16)) if amqp_type == 'decimal128': return proton.decimal128(_compat.decode_hex(test_value[2:])) if amqp_type == 'char': if len(test_value) == 1: # Format 'a' return proton.char(test_value) return proton.char(_compat.unichr(int(test_value, 16))) if amqp_type == 'timestamp': return proton.timestamp(int(test_value, 16)) if amqp_type == 'uuid': return uuid.UUID(test_value) if amqp_type == 'binary': return test_value.encode('utf-8') if amqp_type == 'string': return _compat.unicode(test_value) if amqp_type == 'symbol': return proton.symbol(test_value) if amqp_type == 'list': return AmqpTypesTestSender.encode_amqp_list(test_value) if amqp_type == 'map': return AmqpTypesTestSender.encode_amqp_map(test_value) if amqp_type == 'array': #return AmqpTypesTestSender.encode_amqp_array(test_value) print('send: Unsupported AMQP type "%s"' % amqp_type) return None print('send: Unknown AMQP type "%s"' % amqp_type) return None
def _add_jms_message_properties(self, message): """Adds message properties to the supplied message from self.test_properties_map""" for property_name in list(self.test_properties_map.keys()): value_map = self.test_properties_map[property_name] value_type = list(value_map.keys())[0] # There is only ever one value in map value = value_map[value_type] if message.properties is None: message.properties = {} if value_type == 'boolean': message.properties[property_name] = value == 'True' elif value_type == 'byte': message.properties[property_name] = proton.byte(int(value, 16)) elif value_type == 'double': message.properties[property_name] = struct.unpack('!d', _compat.decode_hex(value[2:]))[0] elif value_type == 'float': message.properties[property_name] = proton.float32(struct.unpack('!f', _compat.decode_hex(value[2:]))[0]) elif value_type == 'int': message.properties[property_name] = proton.int32(int(value, 16)) elif value_type == 'long': message.properties[property_name] = _compat.str2long(value, 16) elif value_type == 'short': message.properties[property_name] = proton.short(int(value, 16)) elif value_type == 'string': message.properties[property_name] = value else: raise InteropTestError('JmsSenderShim._add_jms_message_properties: ' + 'Unknown or unhandled message property type ?%s"' % value_type)
def _create_jms_streammessage(self, test_value_type, test_value, hdr_kwargs, hdr_annotations): """Create a JMS stream message""" if test_value_type == 'boolean': body_list = [test_value == 'True'] elif test_value_type == 'byte': body_list = [proton.byte(int(test_value, 16))] elif test_value_type == 'bytes': body_list = [base64.b64decode(test_value)] elif test_value_type == 'char': body_list = [proton.char(base64.b64decode(test_value).decode('utf-8'))] elif test_value_type == 'double': body_list = [struct.unpack('!d', test_value[2:].decode('hex'))[0]] elif test_value_type == 'float': body_list = [proton.float32(struct.unpack('!f', test_value[2:].decode('hex'))[0])] elif test_value_type == 'int': body_list = [proton.int32(int(test_value, 16))] elif test_value_type == 'long': body_list = [_compat.str2long(test_value, 16)] elif test_value_type == 'short': body_list = [proton.short(int(test_value, 16))] elif test_value_type == 'string': body_list = [test_value] else: raise InteropTestError('JmsSenderShim._create_jms_streammessage: Unknown or unsupported subtype "%s"' % test_value_type) return proton.Message(id=(self.sent+1), body=body_list, inferred=True, annotations=JmsHdrsPropsTestSender.merge_dicts(create_annotation('JMS_STREAMMESSAGE_TYPE'), hdr_annotations), **hdr_kwargs)
def _create_jms_bytesmessage(self, test_value_type, test_value, hdr_kwargs, hdr_annotations): """Create a JMS bytes message""" # NOTE: test_value contains all unicode strings u'...' as returned by json body_bytes = None if test_value_type == 'boolean': body_bytes = b'\x01' if test_value == 'True' else b'\x00' elif test_value_type == 'byte': body_bytes = struct.pack('b', int(test_value, 16)) elif test_value_type == 'bytes': body_bytes = base64.b64decode(test_value) elif test_value_type == 'char': # JMS expects two-byte chars, ASCII chars can be prefixed with '\x00' body_bytes = b'\x00' + base64.b64decode(test_value) elif test_value_type == 'double' or test_value_type == 'float': body_bytes = test_value[2:].decode('hex') elif test_value_type == 'int': body_bytes = struct.pack('!i', int(test_value, 16)) elif test_value_type == 'long': body_bytes = struct.pack('!q', _compat.str2long(test_value, 16)) elif test_value_type == 'short': body_bytes = struct.pack('!h', proton.short(test_value, 16)) elif test_value_type == 'string': # NOTE: First two bytes must be string length test_value_str = str(test_value) # remove unicode body_bytes = struct.pack('!H', len(test_value_str)) + test_value_str else: raise InteropTestError('JmsSenderShim._create_jms_bytesmessage: Unknown or unsupported subtype "%s"' % test_value_type) return proton.Message(id=(self.sent+1), body=body_bytes, inferred=True, content_type='application/octet-stream', annotations=JmsHdrsPropsTestSender.merge_dicts(create_annotation('JMS_BYTESMESSAGE_TYPE'), hdr_annotations), **hdr_kwargs)
def _create_jms_mapmessage(self, test_value_type, test_value, name): """Create a JMS map message""" if test_value_type == 'boolean': value = test_value == 'True' elif test_value_type == 'byte': value = proton.byte(int(test_value, 16)) elif test_value_type == 'bytes': value = test_value.encode('utf-8') elif test_value_type == 'char': value = proton.char(test_value) elif test_value_type == 'double': value = struct.unpack('!d', _compat.decode_hex(test_value[2:]))[0] elif test_value_type == 'float': value = proton.float32( struct.unpack('!f', _compat.decode_hex(test_value[2:]))[0]) elif test_value_type == 'int': value = proton.int32(int(test_value, 16)) elif test_value_type == 'long': value = _compat.str2long(test_value, 16) elif test_value_type == 'short': value = proton.short(int(test_value, 16)) elif test_value_type == 'string': value = test_value else: raise InteropTestError('JmsMessagesTestSender._create_jms_mapmessage: ' \ 'Unknown or unsupported subtype "%s"' % test_value_type) return proton.Message( id=(self.sent + 1), body={name: value}, inferred=False, annotations=create_annotation('JMS_MAPMESSAGE_TYPE'))
def _create_jms_streammessage(self, test_value_type, test_value): """Create a JMS stream message""" if test_value_type == 'boolean': body_list = [test_value == 'True'] elif test_value_type == 'byte': body_list = [proton.byte(int(test_value, 16))] elif test_value_type == 'bytes': body_list = [test_value.encode('utf-8')] elif test_value_type == 'char': body_list = [proton.char(test_value)] elif test_value_type == 'double': body_list = [ struct.unpack('!d', _compat.decode_hex(test_value[2:]))[0] ] elif test_value_type == 'float': body_list = [ proton.float32( struct.unpack('!f', _compat.decode_hex(test_value[2:]))[0]) ] elif test_value_type == 'int': body_list = [proton.int32(int(test_value, 16))] elif test_value_type == 'long': body_list = [_compat.str2long(test_value, 16)] elif test_value_type == 'short': body_list = [proton.short(int(test_value, 16))] elif test_value_type == 'string': body_list = [test_value] else: raise InteropTestError('JmsMessagesTestSender._create_jms_streammessage: ' \ 'Unknown or unsupported subtype "%s"' % test_value_type) return proton.Message( id=(self.sent + 1), body=body_list, inferred=True, annotations=create_annotation('JMS_STREAMMESSAGE_TYPE'))