class RemoteMessageTransportTest(unittest.TestCase):
    Dispatcher = MagicMock()

    def setUp(self):
        self.target = LocalMessageTransport("LocalMessageTransport",
                                            self.Dispatcher)

    def tearDown(self):
        self.target = None

    def test_constructor(self):
        self.assertEqual(self.target.object_id, "LocalMessageTransport")
        self.assertEqual(self.target.dispatcher, self.Dispatcher)

    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 test_close(self):
        self.target.close()
class RemoteMessageTransportTest(unittest.TestCase):
    Dispatcher = MagicMock()

    def setUp(self):
        self.target = LocalMessageTransport(
            "LocalMessageTransport",
            self.Dispatcher)

    def tearDown(self):
        self.target = None

    def test_constructor(self):
        self.assertEqual(self.target.object_id, "LocalMessageTransport")
        self.assertEqual(self.target.dispatcher, self.Dispatcher)

    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 test_close(self):
        self.target.close()
    def test_remove_local_object(self):
        remote_msg_trans = RemoteMessageTransport("remote_object", self.target)
        local_msg_trans = LocalMessageTransport("local_object", self.target)
        self.target._MessageDispatcher__local_objects =\
            {"remote_object": remote_msg_trans,
             "local_object": local_msg_trans}
        subscription_map =\
            self.target._MessageDispatcher__subscription_map
        subscription_map._EventSubscriptionMap__subscriptions =\
            {"remote_object": set(["key01:value01",
                                   "key01:value02"]),
             "local_object": set(["key01:value01",
                                  "key01:value02"])}
        subscription_map._EventSubscriptionMap__subscription_map =\
            {"key01:value01": set(["remote_object",
                                   "local_object"]),
             "key01:value02": set(["remote_object",
                                   "local_object"])}

        self.target.remove_local_object(local_msg_trans)

        self.assertEqual(self.target._MessageDispatcher__local_objects,
                         {"remote_object": remote_msg_trans})
        self.assertEqual(
            subscription_map._EventSubscriptionMap__subscriptions,
            {"remote_object": set(["key01:value01", "key01:value02"])})
        self.assertEqual(
            subscription_map._EventSubscriptionMap__subscription_map, {
                "key01:value01": set(["remote_object"]),
                "key01:value02": set(["remote_object"])
            })
 def __get_message_client(self, object_id):
     if object_id not in self.__clients:
         if object_id in self.__local_objects:
             client = LocalMessageTransport(object_id, self)
         else:
             client = RemoteMessageTransport(object_id, self)
         self.__clients[object_id] = client
     return self.__clients[object_id]
 def setUp(self):
     self.target = LocalMessageTransport("LocalMessageTransport",
                                         self.Dispatcher)
 def setUp(self):
     self.target = LocalMessageTransport(
         "LocalMessageTransport",
         self.Dispatcher)