def _cast(self, value: RecordMap) -> Optional['_Envelope']: headers = value._get_headers(self._tag) node_uri = None lane_uri = None prio = 0.0 rate = 0.0 for index in range(0, headers.size): header = headers.get_item(index) key = header.key.get_string_value() if key is not None: if key == 'node': node_uri = header.value.get_string_value() elif key == 'lane': lane_uri = header.value.get_string_value() elif key == 'prio': prio = header.value.get_num_value() elif key == 'rate': rate = header.value.get_num_value() if node_uri is not None and lane_uri is not None: body = value.get_body() return self._create_envelope_from(node_uri, lane_uri, prio, rate, body)
def test_link_form_cast(self): # Given form = _LinkRequestForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('link_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('link_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(1))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(3))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('link_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('link'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _LinkRequest) self.assertEqual('link_node', actual._node_uri) self.assertEqual('link_lane', actual._lane_uri) self.assertEqual(1, actual._prio) self.assertEqual(3, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_unlinked_form_cast(self): # Given form = _UnlinkedResponseForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('unlinked_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('unlinked_lane'))) items.add( Slot.create_slot(Text.create_from('prio'), Num.create_from(20))) items.add( Slot.create_slot(Text.create_from('rate'), Num.create_from(25))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('unlinked_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('unlinked'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _UnlinkedResponse) self.assertEqual('unlinked_node', actual._node_uri) self.assertEqual('unlinked_lane', actual._lane_uri) self.assertEqual(20, actual._prio) self.assertEqual(25, actual._rate) self.assertEqual(body, actual._body.get_item(0))
def test_lane_addressed_form_cast_missing_headers(self): # Given form = _SyncedResponseForm() items = RecordMap.create() record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('synced'), items)) # When actual = form._cast(record_map) # Then self.assertEqual(None, actual)
def test_link_addressed_cast_missing_headers(self): # Given form = SyncRequestForm() items = RecordMap.create() record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) # When actual = form.cast(record_map) # Then self.assertEqual(None, actual)
def test_to_string(self): # Given value = RecordMap.create() value.add( Attr.create_attr( Text.create_from('remove'), RecordMap.create_record_map( Slot.create_slot(Text.create_from('key'), Text.create_from('foo'))))) # When actual = Recon.to_string(value) # Then self.assertEqual('@remove(key:foo)', actual)
def test_link_addressed_form_cast_missing_lane(self): # Given form = _SyncRequestForm() items = RecordMap.create() items.add( Attr.create_attr(Text.create_from('node'), Text.create_from('sync_foo_node'))) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) # When actual = form._cast(record_map) # Then self.assertEqual(None, actual)
async def test_downlink_model_receive_message_unlinked(self, mock_warn): # Given with SwimClient(execute_on_exception=MockExecuteOnException. get_mock_execute_on_exception()) as client: downlink = EventDownlinkModel(client) downlink.connection = MockConnection.get_mock_connection() downlink.connection.owner = downlink body = RecordMap.create() body.add(Attr.create_attr('laneNotFound', 'foo')) unlinked_message = UnlinkedResponse('unlinked_node', 'unlinked_lane', body=body) downlink.connection.messages_to_receive.append(unlinked_message) # When actual = downlink.open() while not MockExecuteOnException.get_mock_execute_on_exception( ).called: pass # Then self.assertEqual(downlink, actual) self.assertEqual('Lane "None" was not found on the remote agent!', mock_warn.call_args_list[0][0][0])
def to_record(self) -> '_Record': key_slot = self.get_key_item() value_slot = self.get_value_item() update_record = RecordMap.create_record_map( Attr.create_attr(Text.create_from('update'), key_slot)) update_record.add(value_slot) return update_record
def get_key_item(self) -> 'Record': key_slot = RecordMap.create() key_slot.add( Slot.create_slot( Text.create_from('key'), RecordConverter.get_converter().object_to_record(self.key))) return key_slot
def test_event_form_cast(self): # Given form = EventMessageForm() items = RecordMap.create() items.add(Slot.create_slot(Text.create_from('node'), Text.create_from('event_node'))) items.add(Slot.create_slot(Text.create_from('lane'), Text.create_from('event_lane'))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('event_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('event'), items)) record_map.add(body) # When actual = form.cast(record_map) # Then self.assertIsInstance(actual, EventMessage) self.assertEqual('event_node', actual.node_uri) self.assertEqual('event_lane', actual.lane_uri) self.assertEqual(body, actual.body.get_item(0))
def test_write_command_body_remove(self): # Given envelope = _CommandMessage( '/unit/foo', 'shoppingCart', body=RecordMap.create_record_map( Attr.create_attr( Text.create_from('remove'), RecordMap.create_record_map( Slot.create_slot( Text.create_from('key'), Text.create_from('FromClientLink')))))) expected = '@command(node:"/unit/foo",lane:shoppingCart)@remove(key:FromClientLink)' # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
def test_sync_form_cast(self): # Given form = SyncRequestForm() items = RecordMap.create() items.add(Slot.create_slot(Text.create_from('node'), Text.create_from('sync_node'))) items.add(Slot.create_slot(Text.create_from('lane'), Text.create_from('sync_lane'))) items.add(Slot.create_slot(Text.create_from('prio'), Num.create_from(33.12))) items.add(Slot.create_slot(Text.create_from('rate'), Num.create_from(12.33))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('sync_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('sync'), items)) record_map.add(body) # When actual = form.cast(record_map) # Then self.assertIsInstance(actual, SyncRequest) self.assertEqual('sync_node', actual.node_uri) self.assertEqual('sync_lane', actual.lane_uri) self.assertEqual(33.12, actual.prio) self.assertEqual(12.33, actual.rate) self.assertEqual(body, actual.body.get_item(0))
def get_key_item(self) -> '_Record': """ Convert the request key into an Item object. :return: - Request key as an Item object. """ key_slot = RecordMap.create() key_slot.add( Slot.create_slot( Text.create_from('key'), RecordConverter.get_converter().object_to_record(self.key))) return key_slot
def test_command_form_cast(self): # Given form = _CommandMessageForm() items = RecordMap.create() items.add( Slot.create_slot(Text.create_from('node'), Text.create_from('command_node'))) items.add( Slot.create_slot(Text.create_from('lane'), Text.create_from('command_lane'))) body = Attr.create_attr(Text.create_from('body'), Text.create_from('command_body')) record_map = RecordMap.create() record_map.add(Attr.create_attr(Text.create_from('command'), items)) record_map.add(body) # When actual = form._cast(record_map) # Then self.assertIsInstance(actual, _CommandMessage) self.assertEqual('command_node', actual._node_uri) self.assertEqual('command_lane', actual._lane_uri) self.assertEqual(body, actual._body.get_item(0))
def test_write_event_body_nested(self): # Given friend = RecordMap.create() friend.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) friend.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Sam/Spam'))) friend.add( Slot.create_slot(Text.create_from('age'), Num.create_from(1))) friend.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(22))) body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), _Extant._get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Par'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(11))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) body.add(Slot.create_slot(Text.create_from('friend'), friend)) envelope = _EventMessage('/this/is/spam', 'hello', body=body) person = '@Person{name:Par,age:11,salary:-5.9,friend:@Person{name:"Sam/Spam",age:1,salary:22}}' expected = '@event(node:"/this/is/spam",lane:hello)' + person # When actual = envelope._to_recon() # Then self.assertEqual(expected, actual)
async def test_attr_expression_parser_parse_attribute_existing_field(self): # Given message = await InputMessage.create('@test') parser = ReconParser() builder = RecordMap.create() builder.add(Text.create_from('Moo')) field = Text.create_from('Boo') # When actual = await parser.parse_attr_expression(message, builder=builder, field_output=field) # Then self.assertIsInstance(actual, RecordMap) self.assertEqual(3, actual.size) self.assertEqual('Moo', actual.get_item(0).value) self.assertEqual('Boo', actual.get_item(1).value) self.assertEqual('test', actual.get_item(2).key.value)
async def test_attr_expression_parser_parse_literal_existing_value(self): # Given message = await InputMessage.create('literal') parser = ReconParser() builder = RecordMap.create() builder.add(Text.create_from('Moo')) value = Text.create_from('Dog') # When actual = await parser.parse_attr_expression(message, builder=builder, value_output=value) # Then self.assertIsInstance(actual, RecordMap) self.assertEqual(3, actual.size) self.assertEqual('Moo', actual.get_item(0).value) self.assertEqual('Dog', actual.get_item(1).value) self.assertEqual('literal', actual.get_item(2).value)
async def test_write_event_body_object(self): # Given body = RecordMap.create() body.add( Attr.create_attr(Text.create_from('Person'), Extant.get_extant())) body.add( Slot.create_slot(Text.create_from('name'), Text.create_from('Bar'))) body.add(Slot.create_slot(Text.create_from('age'), Num.create_from(14))) body.add( Slot.create_slot(Text.create_from('salary'), Num.create_from(-5.9))) envelope = EventMessage('/this/is/spam', 'hello', body=body) expected = '@event(node:"/this/is/spam",lane:hello)@Person{name:Bar,age:14,salary:-5.9}' # When actual = await envelope.to_recon() # Then self.assertEqual(expected, actual)
def to_record(self) -> '_Record': key_slot = self.get_key_item() remove_record = RecordMap.create_record_map( Attr.create_attr(Text.create_from('remove'), key_slot)) return remove_record