def test_constructor_hasattr_False(self): self.target2 = Request("FlowId2", Request.Method.GET, "flows/FlowId2", self.value) self.assertEqual(self.target2.object_id, "FlowId2") self.assertEqual(self.target2.method, Request.Method.GET) self.assertEqual(self.target2.path, "flows/FlowId2") self.assertEqual(self.target2.body, self.value)
def test_dispatch_request_object_id_not_in_local_objects(self): request = Request("NoneID", "method", "path") remote_object = RemoteObject("remote_object", self.target) self.target._MessageDispatcher__local_objects =\ {"remote_object": remote_object} self.result = self.target.dispatch_request(request) self.assertEqual(self.result.status_code, 404) self.assertEqual(self.result.body, None)
def test_request_sync(self): with patch("org.o3project.odenos.remoteobject.transport." + "remote_message_transport.RemoteMessageTransport." + "send_request_message") as mock_send_request_message: request = Request("object_id", "method", "path") mock_send_request_message.return_value = "send_request_message" self.result = self.target.request_sync(request) self.assertEqual(self.result, "send_request_message")
def test_send_request_message(self): request = Request("object_id", "method", "path") response = Response(200, "body") self.target.dispatcher.dispatch_request =\ MagicMock(return_value=response) result = self.target.send_request_message(request) self.assertEqual(result.status_code, 200) self.assertEqual(result.body, "body")
def __send_request(self, object_id, method, path, body=None): resp = Response(Response.StatusCode.INTERNAL_SERVER_ERROR, None) req = Request(object_id, method, path, body=body) try: resp = self.__dispatcher.request_sync(req) except: logging.error("Exception: Request to " + object_id + " Method:" + method + " Path:" + path) logging.error(traceback.format_exc()) return resp
def subscribe_event(self, event_subscription): self.__subscription_map.remove_subscription( event_subscription.subscriber_id) self.__subscription_map.set_subscription(event_subscription) self.__update_subscriber() request = Request( self.event_manager_id, Request.Method.PUT, "settings/event_subscriptions/%s" % event_subscription.subscriber_id, event_subscription) return self.request_sync(request)
def test_dispatch_request_object_id_in_local_objects(self): request = Request("remote_object", "method", "path") remote_object = RemoteObject("remote_object", self.target) self.target._MessageDispatcher__local_objects =\ {"remote_object": remote_object} with patch("org.o3project.odenos.remoteobject." + "remote_object.RemoteObject." + "dispatch_request") as mock_dispatch_request: mock_dispatch_request.return_value = "mock_dispatch_request" self.result = self.target.dispatch_request(request) self.assertEqual(self.result, "mock_dispatch_request")
def test_action(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "GET", "path/to/param1", None) response = self.target.action(request) self.assertEqual(self.call_func0, 1) self.assertEqual(response.status_code, 101)
def test_action_with_param_attribute_params_is_1(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "PUT", "path/to/param2", "body") response = self.target.action(request) self.assertEqual(self.call_func1, 1) self.assertEqual(self.func1_body, "body") self.assertEqual(response.status_code, 102)
def test_action_with_pattern_not_matched(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "GET", "path/to/param3", "body") response = self.target.action(request) self.assertEqual(self.call_func0, 0) self.assertEqual(self.call_func1, 0) self.assertEqual(self.func1_body, None) self.assertEqual(response.status_code, 404)
def setUp(self): self.target = Request("FlowId1", Request.Method.GET, "flows/FlowId1", self.flow_target)
def test_on_request(self): request = Request("ObjectId", Request.Method.GET, "component_managers", None) self.result = self.target._on_request(request) self.assertEqual(self.result.status_code, 404) self.assertEqual(self.result.body, None)
def _request_sync(self, object_id, method, path, body=None): return self.dispatcher.request_sync( Request(object_id, method, path, body))
def test_redisSubscriberRunnable_TYPE_REQUEST(self): request = Request("object_id", "method", "path", None) request_packed = request.packed_object() response = Response(200, "body") response_packed = response.packed_object() pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(0)) resb.extend(pk.pack(0)) resb.extend(pk.pack("object_id")) resb.extend(pk.pack(request_packed)) self.value = {"type": "message", "data": resb} self.value02 = {"type": "ERROR", "data": resb} resb_check = bytearray() resb_check.extend(pk.pack(1)) resb_check.extend(pk.pack(0)) resb_check.extend(pk.pack("object_id")) resb_check.extend(pk.pack(response_packed)) self.target.thread_pool = futures.ThreadPoolExecutor(max_workers=8) self.target._MessageDispatcher__redisSubscriber = redis.StrictRedis( host=self.target._MessageDispatcher__redisServer, port=self.target._MessageDispatcher__redisPort) self.target._MessageDispatcher__redisSubscriber =\ self.target._MessageDispatcher__redisSubscriber.pubsub() def dummy_request_runnable(arg, request, sno, srcid): arg(request, sno, srcid) with nested(patch("redis.client.PubSub.subscribe"), patch("redis.client.PubSub.unsubscribe"), patch("redis.client.PubSub.listen"), patch("concurrent.futures.ThreadPoolExecutor.submit"), patch(self.REQUEST_PATH + ".create_from_packed"), patch(self.DISPATCHER_PATH + ".dispatch_request")) as (mock_subscribe, mock_unsubscribe, mock_listen, mock_submit, mock_request, mock_dispatch_request): mock_subscribe.return_value = None mock_unsubscribe.return_value = None mock_listen.return_value = [self.value, self.value02] mock_request.return_value = request mock_dispatch_request.return_value = response mock_submit.side_effect = dummy_request_runnable self.target._MessageDispatcher__redisSubscriberRunnable() self.result = self.target._MessageDispatcher__pubsqueue.get() mock_request.assert_called_once_with( ["object_id", "method", "path", None]) self.assertEqual(mock_submit.call_count, 1) self.assertEqual(self.result.trans, None) self.assertEqual(self.result.type, 1) self.assertEqual(self.result.sno, 0) self.assertEqual(self.result.channel, "object_id") self.assertEqual(self.result.data, resb_check)