def test_fipa_cfp_serialization_bytes(): """Test that the serialization - deserialization for the 'fipa' protocol works.""" query = b"Hello" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=query, ) msg.counterparty = "sender" msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FIPASerializer().decode(actual_envelope.message) actual_msg.counterparty = "sender" expected_msg = msg assert expected_msg == actual_msg deserialised_msg = FIPASerializer().decode(envelope.message) deserialised_msg.counterparty = "sender" assert msg.get("performative") == deserialised_msg.get("performative")
def test_propose(self): """Test that a Propose can be sent correctly.""" propose_empty = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.PROPOSE, proposal=[], ) propose_empty.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(propose_empty), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) expected_propose_empty = FIPASerializer().decode(envelope.message) expected_propose_empty.counterparty = self.crypto2.address assert expected_propose_empty == propose_empty propose_descriptions = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.PROPOSE, proposal=[ Description( {"foo": "bar"}, DataModel("foobar", [Attribute("foo", str, True)]) ) ], ) propose_descriptions.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(propose_descriptions), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) expected_propose_descriptions = FIPASerializer().decode(envelope.message) expected_propose_descriptions.counterparty = self.crypto2.address assert expected_propose_descriptions == propose_descriptions
def test_fipa_accept_serialization(): """Test that the serialization for the 'fipa' protocol works.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg.counterparty = "sender" msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FIPASerializer().decode(actual_envelope.message) actual_msg.counterparty = "sender" expected_msg = msg assert expected_msg == actual_msg
def test_cfp(self): """Test that a CFP can be sent correctly.""" cfp_message = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=Query([Constraint("something", ConstraintType(">", 1))]), ) cfp_message.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(cfp_message), ) ) envelope = self.multiplexer2.get(block=True, timeout=5.0) expected_cfp_message = FIPASerializer().decode(envelope.message) expected_cfp_message.counterparty = self.crypto2.address assert expected_cfp_message == cfp_message cfp_none = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=None, ) cfp_none.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(cfp_none), ) ) envelope = self.multiplexer2.get(block=True, timeout=5.0) expected_cfp_none = FIPASerializer().decode(envelope.message) expected_cfp_none.counterparty = self.crypto2.address assert expected_cfp_none == cfp_none
def test_decline(self): """Test that a Decline can be sent correctly.""" decline = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.DECLINE, ) decline.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(decline), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) expected_decline = FIPASerializer().decode(envelope.message) expected_decline.counterparty = self.crypto2.address assert expected_decline == decline
def test_accept(self): """Test that an Accept can be sent correctly.""" accept = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) accept.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(accept), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) expected_accept = FIPASerializer().decode(envelope.message) expected_accept.counterparty = self.crypto2.address assert expected_accept == accept
def test_accept_w_inform(self): """Test that an accept with address can be sent correctly.""" accept_w_inform = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT_W_INFORM, info={"address": "my_address"}, ) accept_w_inform.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(accept_w_inform), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) returned_accept_w_inform = FIPASerializer().decode(envelope.message) returned_accept_w_inform.counterparty = self.crypto2.address assert returned_accept_w_inform == accept_w_inform
def test_match_accept(self): """Test that a match accept can be sent correctly.""" # NOTE since the OEF SDK doesn't support the match accept, we have to use a fixed message id! match_accept = FIPAMessage( message_id=4, dialogue_reference=(str(0), ""), target=3, performative=FIPAMessage.Performative.MATCH_ACCEPT, ) match_accept.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(match_accept), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) expected_match_accept = FIPASerializer().decode(envelope.message) expected_match_accept.counterparty = self.crypto2.address assert expected_match_accept == match_accept
def test_inform(self): """Test that an inform can be sent correctly.""" payload = {"foo": "bar"} inform = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.INFORM, info=payload, ) inform.counterparty = self.crypto2.address self.multiplexer1.put( Envelope( to=self.crypto2.address, sender=self.crypto1.address, protocol_id=FIPAMessage.protocol_id, message=FIPASerializer().encode(inform), ) ) envelope = self.multiplexer2.get(block=True, timeout=2.0) returned_inform = FIPASerializer().decode(envelope.message) returned_inform.counterparty = self.crypto2.address assert returned_inform == inform