1#! /usr/bin/env python
2#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc.  All rights reserved.
5# https://developers.google.com/protocol-buffers/
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11#     * Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13#     * Redistributions in binary form must reproduce the above
14# copyright notice, this list of conditions and the following disclaimer
15# in the documentation and/or other materials provided with the
16# distribution.
17#     * Neither the name of Google Inc. nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33"""Tests for google.protobuf.internal.service_reflection."""
34
35__author__ = 'petar@google.com (Petar Petrov)'
36
37
38try:
39  import unittest2 as unittest  #PY26
40except ImportError:
41  import unittest
42
43from google.protobuf import unittest_pb2
44from google.protobuf import service_reflection
45from google.protobuf import service
46
47
48class FooUnitTest(unittest.TestCase):
49
50  def testService(self):
51    class MockRpcChannel(service.RpcChannel):
52      def CallMethod(self, method, controller, request, response, callback):
53        self.method = method
54        self.controller = controller
55        self.request = request
56        callback(response)
57
58    class MockRpcController(service.RpcController):
59      def SetFailed(self, msg):
60        self.failure_message = msg
61
62    self.callback_response = None
63
64    class MyService(unittest_pb2.TestService):
65      pass
66
67    self.callback_response = None
68
69    def MyCallback(response):
70      self.callback_response = response
71
72    rpc_controller = MockRpcController()
73    channel = MockRpcChannel()
74    srvc = MyService()
75    srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
76    self.assertEqual('Method Foo not implemented.',
77                     rpc_controller.failure_message)
78    self.assertEqual(None, self.callback_response)
79
80    rpc_controller.failure_message = None
81
82    service_descriptor = unittest_pb2.TestService.GetDescriptor()
83    srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
84                    unittest_pb2.BarRequest(), MyCallback)
85    self.assertEqual('Method Bar not implemented.',
86                     rpc_controller.failure_message)
87    self.assertEqual(None, self.callback_response)
88
89    class MyServiceImpl(unittest_pb2.TestService):
90      def Foo(self, rpc_controller, request, done):
91        self.foo_called = True
92      def Bar(self, rpc_controller, request, done):
93        self.bar_called = True
94
95    srvc = MyServiceImpl()
96    rpc_controller.failure_message = None
97    srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
98    self.assertEqual(None, rpc_controller.failure_message)
99    self.assertEqual(True, srvc.foo_called)
100
101    rpc_controller.failure_message = None
102    srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
103                    unittest_pb2.BarRequest(), MyCallback)
104    self.assertEqual(None, rpc_controller.failure_message)
105    self.assertEqual(True, srvc.bar_called)
106
107  def testServiceStub(self):
108    class MockRpcChannel(service.RpcChannel):
109      def CallMethod(self, method, controller, request,
110                     response_class, callback):
111        self.method = method
112        self.controller = controller
113        self.request = request
114        callback(response_class())
115
116    self.callback_response = None
117
118    def MyCallback(response):
119      self.callback_response = response
120
121    channel = MockRpcChannel()
122    stub = unittest_pb2.TestService_Stub(channel)
123    rpc_controller = 'controller'
124    request = 'request'
125
126    # GetDescriptor now static, still works as instance method for compatibility
127    self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(),
128                     stub.GetDescriptor())
129
130    # Invoke method.
131    stub.Foo(rpc_controller, request, MyCallback)
132
133    self.assertIsInstance(self.callback_response, unittest_pb2.FooResponse)
134    self.assertEqual(request, channel.request)
135    self.assertEqual(rpc_controller, channel.controller)
136    self.assertEqual(stub.GetDescriptor().methods[0], channel.method)
137
138
139if __name__ == '__main__':
140  unittest.main()
141