def test_serialisation_fipa(self): """Tests a Value Error flag for wrong CFP query.""" with pytest.raises(ValueError): msg = FIPAMessage(performative=FIPAMessage.Performative.CFP, message_id=0, dialogue_reference=(str(0), ''), target=1, query=None) with mock.patch( "aea.protocols.fipa.message.FIPAMessage.Performative" ) as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" FIPASerializer().encode(msg), "Raises Value Error" with pytest.raises(ValueError): msg.set("query", "Hello") # query type is not supported FIPASerializer().encode(msg) with pytest.raises(ValueError): cfp_msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=0, performative=FIPAMessage.Performative.CFP, query=b"hello") cfp_msg.set("query", "hello") fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = cfp_msg.get("message_id") dialogue_reference = cast(Dict[str, str], cfp_msg.get("dialogue_reference")) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() # The encoded message is not a valid FIPA message. FIPASerializer().decode(fipa_bytes) with pytest.raises(ValueError): cfp_msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=0, performative=FIPAMessage.Performative.CFP, query=b"hello") with mock.patch( "aea.protocols.fipa.message.FIPAMessage.Performative" ) as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = cfp_msg.get("message_id") dialogue_reference = cast(Dict[str, str], cfp_msg.get("dialogue_reference")) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() # The encoded message is not a FIPA message FIPASerializer().decode(fipa_bytes)
def test_serialisation_fipa(self): """Tests a Value Error flag for wrong CFP query.""" with pytest.raises(ValueError): msg = FIPAMessage(performative=FIPAMessage.Performative.CFP, message_id=0, dialogue_id=0, destination="publicKey", target=1) with mock.patch("aea.protocols.fipa.message.FIPAMessage.Performative")\ as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" assert FIPASerializer().encode(msg), "Raises Value Error" with pytest.raises(ValueError): msg.set("query", "Hello") assert FIPASerializer().encode(msg), "Query type is Supported!" with pytest.raises(ValueError): cfp_msg = FIPAMessage(message_id=0, dialogue_id=0, target=0, performative=FIPAMessage.Performative.CFP, query=b"hello") cfp_msg.set("query", "hello") fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = cfp_msg.get("id") fipa_msg.dialogue_id = cfp_msg.get("dialogue_id") fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() assert FIPASerializer().decode(fipa_bytes),\ "The encoded message is a valid FIPA message." with pytest.raises(ValueError): cfp_msg = FIPAMessage(message_id=0, dialogue_id=0, target=0, performative=FIPAMessage.Performative.CFP, query=b"hello") with mock.patch("aea.protocols.fipa.message.FIPAMessage.Performative")\ as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = cfp_msg.get("id") fipa_msg.dialogue_id = cfp_msg.get("dialogue_id") fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() assert FIPASerializer().decode(fipa_bytes),\ "The encoded message is a FIPA message"
def decode(self, obj: bytes) -> Message: """Decode bytes into a FIPA message.""" fipa_pb = fipa_pb2.FIPAMessage() fipa_pb.ParseFromString(obj) message_id = fipa_pb.message_id dialogue_reference = (fipa_pb.dialogue_starter_reference, fipa_pb.dialogue_responder_reference) target = fipa_pb.target performative = fipa_pb.WhichOneof("performative") performative_id = FIPAMessage.Performative(str(performative)) performative_content = dict() if performative_id == FIPAMessage.Performative.CFP: query_type = fipa_pb.cfp.WhichOneof("query") if query_type == "nothing": query = None elif query_type == "query_bytes": query = pickle.loads(fipa_pb.cfp.query_bytes) elif query_type == "bytes": query = fipa_pb.cfp.bytes else: raise ValueError("Query type not recognized.") # pragma: no cover performative_content["query"] = query elif performative_id == FIPAMessage.Performative.PROPOSE: descriptions = [] for p_bytes in fipa_pb.propose.proposal: p = pickle.loads(p_bytes) # type: Description descriptions.append(p) performative_content["proposal"] = descriptions elif performative_id == FIPAMessage.Performative.ACCEPT: pass elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: pass elif performative_id == FIPAMessage.Performative.ACCEPT_W_ADDRESS: address = fipa_pb.accept_w_address.address performative_content['address'] = address elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS: address = fipa_pb.match_accept_w_address.address performative_content['address'] = address elif performative_id == FIPAMessage.Performative.DECLINE: pass elif performative_id == FIPAMessage.Performative.INFORM: data = json.loads(fipa_pb.inform.bytes) performative_content["json_data"] = data else: raise ValueError("Performative not valid: {}.".format(performative)) return FIPAMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def encode(self, msg: Message) -> bytes: """Encode a FIPA message into bytes.""" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = msg.get("id") fipa_msg.dialogue_id = msg.get("dialogue_id") fipa_msg.target = msg.get("target") performative_id = FIPAMessage.Performative(msg.get("performative")) if performative_id == FIPAMessage.Performative.CFP: performative = fipa_pb2.FIPAMessage.CFP() # type: ignore query = msg.get("query") if query is None or query == b"": nothing = fipa_pb2.FIPAMessage.CFP.Nothing() # type: ignore performative.nothing.CopyFrom(nothing) elif type(query) == Query: query = pickle.dumps(query) performative.query_bytes = query elif type(query) == bytes: performative.bytes = query else: raise ValueError("Query type not supported: {}".format( type(query))) fipa_msg.cfp.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.PROPOSE: performative = fipa_pb2.FIPAMessage.Propose() # type: ignore proposal = cast(Description, msg.get("proposal")) p_array_bytes = [pickle.dumps(p) for p in proposal] performative.proposal.extend(p_array_bytes) fipa_msg.propose.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT: performative = fipa_pb2.FIPAMessage.Accept() # type: ignore fipa_msg.accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: performative = fipa_pb2.FIPAMessage.MatchAccept() # type: ignore fipa_msg.match_accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.DECLINE: performative = fipa_pb2.FIPAMessage.Decline() # type: ignore fipa_msg.decline.CopyFrom(performative) else: raise ValueError( "Performative not valid: {}".format(performative_id)) fipa_bytes = fipa_msg.SerializeToString() return fipa_bytes
def decode(self, obj: bytes) -> Message: """Decode bytes into a FIPA message.""" fipa_pb = fipa_pb2.FIPAMessage() fipa_pb.ParseFromString(obj) message_id = fipa_pb.message_id dialogue_id = fipa_pb.dialogue_id target = fipa_pb.target performative = fipa_pb.WhichOneof("performative") performative_id = FIPAMessage.Performative(str(performative)) performative_content = dict() if performative_id == FIPAMessage.Performative.CFP: query_type = fipa_pb.cfp.WhichOneof("query") if query_type == "nothing": query = None elif query_type == "query_bytes": query = pickle.loads(fipa_pb.cfp.query_bytes) elif query_type == "bytes": query = fipa_pb.cfp.bytes else: raise ValueError("Query type not recognized.") performative_content["query"] = query elif performative_id == FIPAMessage.Performative.PROPOSE: descriptions = [] for p_bytes in fipa_pb.propose.proposal: p = pickle.loads(p_bytes) # type: Description descriptions.append(p) performative_content["proposal"] = descriptions elif performative_id == FIPAMessage.Performative.ACCEPT: pass elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: pass elif performative_id == FIPAMessage.Performative.DECLINE: pass else: raise ValueError( "Performative not valid: {}.".format(performative)) return FIPAMessage(message_id=message_id, dialogue_id=dialogue_id, target=target, performative=performative, **performative_content)
def encode(self, msg: Message) -> bytes: """Encode a FIPA message into bytes.""" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = msg.get("message_id") dialogue_reference = cast(Tuple[str, str], msg.get("dialogue_reference")) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = msg.get("target") performative_id = FIPAMessage.Performative(msg.get("performative")) if performative_id == FIPAMessage.Performative.CFP: performative = fipa_pb2.FIPAMessage.CFP() # type: ignore query = msg.get("query") if query is None or query == b"": nothing = fipa_pb2.FIPAMessage.CFP.Nothing() # type: ignore performative.nothing.CopyFrom(nothing) elif type(query) == Query: query = pickle.dumps(query) performative.query_bytes = query elif type(query) == bytes: performative.bytes = query else: raise ValueError("Query type not supported: {}".format(type(query))) fipa_msg.cfp.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.PROPOSE: performative = fipa_pb2.FIPAMessage.Propose() # type: ignore proposal = cast(Description, msg.get("proposal")) p_array_bytes = [pickle.dumps(p) for p in proposal] performative.proposal.extend(p_array_bytes) fipa_msg.propose.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT: performative = fipa_pb2.FIPAMessage.Accept() # type: ignore fipa_msg.accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: performative = fipa_pb2.FIPAMessage.MatchAccept() # type: ignore fipa_msg.match_accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT_W_ADDRESS: performative = fipa_pb2.FIPAMessage.AcceptWAddress() # type: ignore address = msg.get("address") if type(address) == str: performative.address = address fipa_msg.accept_w_address.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS: performative = fipa_pb2.FIPAMessage.MatchAcceptWAddress() # type: ignore address = msg.get("address") if type(address) == str: performative.address = address fipa_msg.match_accept_w_address.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.DECLINE: performative = fipa_pb2.FIPAMessage.Decline() # type: ignore fipa_msg.decline.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.INFORM: performative = fipa_pb2.FIPAMessage.Inform() # type: ignore data = msg.get("json_data") data_bytes = json.dumps(data).encode("utf-8") performative.bytes = data_bytes fipa_msg.inform.CopyFrom(performative) else: raise ValueError("Performative not valid: {}".format(performative_id)) fipa_bytes = fipa_msg.SerializeToString() return fipa_bytes