[spade2] r2249 committed - added missing testcases

2 views
Skip to first unread message

spa...@googlecode.com

unread,
Nov 9, 2012, 1:06:40 PM11/9/12
to spade-...@googlegroups.com
Revision: 2249
Author: jpalanca
Date: Fri Nov 9 10:06:25 2012
Log: added missing testcases
http://code.google.com/p/spade2/source/detail?r=2249

Added:
/trunk/test/httpTestCase.py
/trunk/test/jsonTestCase.py

=======================================
--- /dev/null
+++ /trunk/test/httpTestCase.py Fri Nov 9 10:06:25 2012
@@ -0,0 +1,195 @@
+import os
+import sys
+import time
+import unittest
+import BaseHTTPServer
+
+import spade
+
+host = "127.0.0.1"
+
+class MyAgent(spade.Agent.Agent):
+
+ def _setup(self):
+ self.msg = None
+
+class SendMsgBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b@"+host,["http://"+host+":2099/b"]))
+ msg.setContent("HTTPtestSendMsg")
+
+ self.myAgent.send(msg)
+
+class RecvMsgBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ self.myAgent.msg = self._receive(block=True,timeout=20)
+
+class AnswerMsgBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ msg = None
+ msg = self._receive(block=True,timeout=10)
+ if msg != None:
+ content = msg.getContent()
+ msg = msg.createReply()
+ msg.setContent(content)
+ self.myAgent.send(msg)
+
+class SendAndRecvMsgBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b@"+host,["http://"+host+":2099/b"]))
+ msg.setContent("HTTPtestSendAndRecvMsg")
+
+ self.myAgent.send(msg)
+ self.myAgent.msg = None
+ self.myAgent.msg = self._receive(block=True,timeout=20)
+
+class SendJSONMsgBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b@"+host,["http://"+host+":2099/b"]))
+ msg.setContent("HTTPtestSendJSONMsg")
+ msg.setAclRepresentation(spade.ACLMessage.FIPA_ACL_REP_JSON)
+
+ self.myAgent.send(msg)
+
+class Send2HTTPServerBehav(spade.Behaviour.OneShotBehaviour):
+
+ def _process(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b@"+host,["http://"+host+":2100/b"]))
+ msg.setContent("HTTPtestSendJSONMsg")
+ msg.setAclRepresentation(spade.ACLMessage.FIPA_ACL_REP_JSON)
+
+ self.myAgent.send(msg)
+
+class HTTPTestCase(unittest.TestCase):
+
+ def setUp(self):
+
+ self.Aaid = spade.AID.aid("a@"+host,["xmpp://a@"+host])
+ self.Baid = spade.AID.aid("b@"+host,["xmpp://b@"+host])
+
+ self.a = MyAgent("a@"+host, "secret")
+ self.a.start()
+ self.b = MyAgent("b@"+host, "secret")
+ self.b.start()
+ #self.b.setDebugToScreen()
+ #self.a.setDebugToScreen()
+
+ def tearDown(self):
+ self.a.stop()
+ self.b.stop()
+
+ def testSendMsg(self):
+ template = spade.Behaviour.ACLTemplate()
+ #template.setSender(self.Aaid)
+ template.setContent("HTTPtestSendMsg")
+ t = spade.Behaviour.MessageTemplate(template)
+ self.b.addBehaviour(RecvMsgBehav(),t)
+ self.a.addBehaviour(SendMsgBehav(),None)
+ counter = 0
+ while self.b.msg == None and counter < 20:
+ time.sleep(1)
+ counter += 1
+ self.assertNotEqual(self.b.msg,None)
+ self.assertEqual(self.b.msg.getContent(),"HTTPtestSendMsg")
+ self.assertEqual(self.b.msg.getSender(), self.Aaid)
+ self.assertEqual(len(self.b.msg.getReceivers()),1)
+ #self.assertEqual(self.b.msg.getReceivers()[0], self.Baid) #need
to add http address
+
+ def testSendAndRecvMsg(self):
+ template = spade.Behaviour.ACLTemplate()
+ template.setSender(self.Aaid)
+ t = spade.Behaviour.MessageTemplate(template)
+ self.b.addBehaviour(AnswerMsgBehav(),t)
+ template.setSender(self.Baid)
+ t = spade.Behaviour.MessageTemplate(template)
+ self.a.addBehaviour(SendAndRecvMsgBehav(),t)
+ counter = 0
+ while self.a.msg == None and counter < 20:
+ time.sleep(1)
+ counter += 1
+ self.assertNotEqual(self.a.msg,None)
+ self.assertEqual(self.a.msg.getContent(),"HTTPtestSendAndRecvMsg")
+
+ def testSendJSONMsg(self):
+ template = spade.Behaviour.ACLTemplate()
+ #template.setSender(self.Aaid)
+ template.setContent("HTTPtestSendJSONMsg")
+ t = spade.Behaviour.MessageTemplate(template)
+ self.b.addBehaviour(RecvMsgBehav(),t)
+ self.a.addBehaviour(SendJSONMsgBehav(),None)
+ counter = 0
+ while self.b.msg == None and counter < 20:
+ time.sleep(1)
+ counter += 1
+ self.assertNotEqual(self.b.msg,None)
+ self.assertEqual(self.b.msg.getContent(),"HTTPtestSendJSONMsg")
+ self.assertEqual(self.b.msg.getSender(), self.Aaid)
+ self.assertEqual(len(self.b.msg.getReceivers()),1)
+
+
+ class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
+ protocol_version = "HTTP/1.1"
+ def log_message(format,*args): pass
+ def do_POST(self):
+ boundary = self.headers.getparam('boundary')
+ clen = self.headers.getheader('Content-Length')
+ buf1 = self.rfile.read(int(clen)).split("--"+boundary+"--")
+ buf2 = buf1[0].split("--"+boundary)
+ env = buf2[1]
+ pay = buf2[2]
+ index = env.find('\n\n')
+ self.server.type_env =
env[0:index].split(':')[1].split(';')[0].strip()
+ self.server.env = env[index+2:]
+ # the same for payload
+ index = pay.find('\n\n')
+ self.server.type_pay =
pay[0:index].split(':')[1].split()[0].strip()
+ self.server.pay = pay[index+2:]
+
+ self.send_response(200)
+ self.server.done = True
+
+ def testSend2HTTPServer(self):
+ import json
+ template = spade.Behaviour.ACLTemplate()
+ template.setSender(self.Aaid)
+ t = spade.Behaviour.MessageTemplate(template)
+
+ httpd = BaseHTTPServer.HTTPServer(("", 2100), self.Handler)
+ httpd.done = False
+ httpd.env = httpd.pay = None
+ import thread
+ thread.start_new_thread(httpd.handle_request,())
+
+ self.a.addBehaviour(Send2HTTPServerBehav(),None)
+ counter = 0
+ while httpd.done == False and counter < 20:
+ time.sleep(1)
+ counter += 1
+
+ assert httpd.env != None
+ assert httpd.pay != None
+ assert httpd.type_env == "application/json"
+ assert json.loads(httpd.env)["acl-representation"]
== "fipa.acl.rep.json.std"
+ assert httpd.type_pay == "application/json"
+ assert json.loads(httpd.pay)["content"] == "HTTPtestSendJSONMsg"
+ assert json.loads(httpd.pay)["performative"] == "inform"
+ assert json.loads(httpd.pay)["sender"]["name"] == "a@"+host
+
+if __name__ == "__main__":
+ unittest.main()
+
+
+
=======================================
--- /dev/null
+++ /trunk/test/jsonTestCase.py Fri Nov 9 10:06:25 2012
@@ -0,0 +1,105 @@
+import unittest
+
+import spade
+import xmpp
+
+
+class JsonTestCase(unittest.TestCase):
+
+
+ def testDumpACLMessage(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b...@myhost.com",["xmpp://b...@myhost.com", "http://myhost.com:8080"]))
+ msg.setContent("testDumpACLMessage")
+ cid = msg.getConversationId()
+
+ assert msg.asJSON()
== '{"performative":"inform","receivers":[{"name": "b...@myhost.com", "addresses":
["xmpp://b...@myhost.com", "http://myhost.com:8080"]}],"content":"testDumpACLMessage","conversation-id":"'+cid+'"}'
+
+ def testSanityCheckACLMessage(self):
+ msg = spade.ACLMessage.ACLMessage()
+ msg.setPerformative("inform")
+
msg.addReceiver(spade.AID.aid("b...@myhost.com",["xmpp://b...@myhost.com", "http://myhost.com:8080"]))
+ msg.setContent("Sanity Check")
+ cid = msg.getConversationId()
+
+ jsonmsg = msg.asJSON()
+
+ msg2 = spade.ACLMessage.ACLMessage()
+ msg2.loadJSON(jsonmsg)
+
+ assert msg.asString() == msg2.asString()
+
+
+ def testDumpACLEnvelope(self):
+ env = spade.Envelope.Envelope()
+
env.addTo(spade.AID.aid("b...@myhost.com",["xmpp://b...@myhost.com", "http://myhost.com:8080"]))
+
env.setFrom(spade.AID.aid("a...@myotherhost.com",["xmpp://a...@myotherhost.com", "http://myotherhost.com:8080"]))
+ env.setAclRepresentation("fipa.acl.rep.string.std")
+ env.setPayloadLength("100")
+ env.setPayloadEncoding("US-ASCII")
+ env.setDate("20121105T134259368626")
+
+ envjson
= '{"to":[{"name":"b...@myhost.com","addresses":["xmpp://b...@myhost.com","http://myhost.com:8080"]}],"from":{"name":"a...@myotherhost.com","addresses":["xmpp://a...@myotherhost.com","http://myotherhost.com:8080"]},"acl-representation":"fipa.acl.rep.string.std","payload-length":"100","payload-encoding":"US-ASCII","date":"20121105T134259368626","intended-receiver":[{"name":"b...@myhost.com","addresses":["xmpp://b...@myhost.com","http://myhost.com:8080"]}]}'
+
+ assert env.asJSON() == envjson
+
+ def testSanityCheckACLEnvelope(self):
+ env = spade.Envelope.Envelope()
+
env.addTo(spade.AID.aid("b...@myhost.com",["xmpp://b...@myhost.com", "http://myhost.com:8080"]))
+
env.setFrom(spade.AID.aid("a...@myotherhost.com",["xmpp://a...@myotherhost.com", "http://myotherhost.com:8080"]))
+ env.setAclRepresentation("fipa.acl.rep.string.std")
+ env.setPayloadLength("100")
+ env.setPayloadEncoding("US-ASCII")
+ env.setDate("20121105T134259368626")
+
+ jsonmsg = env.asJSON()
+
+ env2 = spade.Envelope.Envelope()
+ env2.loadJSON(jsonmsg)
+
+ assert isEqualXML(env.asXML(), env2.asXML())
+
+
+def isEqualXML(a, b):
+ da,db = xmpp.simplexml.NodeBuilder(a),xmpp.simplexml.NodeBuilder(b)
+ return isEqualElement(da.getDom(),db.getDom())
+
+def isEqualElement(a, b):
+ if a.getName()!=b.getName():
+ return False
+ if sorted(a.getAttrs().items())!=sorted(b.getAttrs().items()):
+ return False
+ if len(a.getChildren())!=len(b.getChildren()):
+ return False
+ if a.getData() and b.getData() and a.getData() != b.getData():
+ return False
+ for ac in a.getChildren():
+ l = []
+ for bc in b.getChildren():
+ if ac.getName() == bc.getName():
+ l.append(bc)
+ if len(l) == 0:
+ return False
+ r = False
+ for n in l:
+ if len(ac.kids)==len(n.kids): r = True
+ if not r:
+ return False
+
+ if ac.getData():
+ for n in l:
+ if n.getData()==ac.getData(): r = True
+ if not r:
+ return False
+
+ if not ac.getData() and (len(ac.kids)>0):
+ for n in l:
+ if isEqualElement(ac,n): r = True
+ if not r:
+ return False
+
+ return True
+
+if __name__ == "__main__":
+ unittest.main()
Reply all
Reply to author
Forward
0 new messages