Raised coverage of serviceHandler.py from 0% to 81%!
Signed-off-by: Travis Miller <raphtee@google.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@1838 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/json_rpc/common.py b/frontend/afe/json_rpc/common.py
new file mode 100644
index 0000000..1d171c6
--- /dev/null
+++ b/frontend/afe/json_rpc/common.py
@@ -0,0 +1,8 @@
+import os, sys
+dirname = os.path.dirname(sys.modules[__name__].__file__)
+autotest_dir = os.path.abspath(os.path.join(dirname, "../../../"))
+client_dir = os.path.join(autotest_dir, "client")
+sys.path.insert(0, client_dir)
+import setup_modules
+sys.path.pop(0)
+setup_modules.setup(base_path=autotest_dir, root_module_name="autotest_lib")
diff --git a/frontend/afe/json_rpc/serviceHandler_unittest.py b/frontend/afe/json_rpc/serviceHandler_unittest.py
new file mode 100644
index 0000000..b8d24be
--- /dev/null
+++ b/frontend/afe/json_rpc/serviceHandler_unittest.py
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+
+import unittest
+import common
+import serviceHandler
+
+
+class RpcMethodHolder(object):
+ @staticmethod
+ def service_1(x, y):
+ return x + y
+
+ @staticmethod
+ def service_2(path):
+ return path.split('/')[-1]
+
+
+json_request1 = """
+{
+ "method": "service_1",
+ "params": [7, 9],
+ "id": null
+}
+"""
+
+expected_response1 = '{"error": null, "result": 16, "id": null}'
+
+
+json_request2 = """
+{
+ "method": "service_2",
+ "params": ["http://www.some.url.com/path/to/package.rpm"],
+ "id": null
+}
+"""
+
+expected_response2 = '{"error": null, "result": "package.rpm", "id": null}'
+
+json_request3 = """
+{
+ "method": "service_3",
+ "params": ["I wonder if this works"],
+ "id": null
+}
+"""
+
+
+class TestServiceHandler(unittest.TestCase):
+ def setUp(self):
+ holder = RpcMethodHolder()
+ self.serviceHandler = serviceHandler.ServiceHandler(holder)
+
+
+ def test_handleRequest1(self):
+ response = self.serviceHandler.handleRequest(json_request1)
+ self.assertEquals(response, expected_response1)
+
+
+ def test_handleRequest2(self):
+ response = self.serviceHandler.handleRequest(json_request2)
+ self.assertEquals(response, expected_response2)
+
+
+ def test_handleRequest3(self):
+ response = self.serviceHandler.handleRequest(json_request3)
+ response_obj = eval(response.replace('null', 'None'))
+ self.assertNotEquals(response_obj['error'], 'None')
+
+
+if __name__ == "__main__":
+ unittest.main()