blob: cc95655598dc044d323748b5b164c4a6b8231cea [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import print_function
7
8from chromite.api import build_api
9from chromite.api.gen import build_api_test_pb2
10from chromite.lib import cros_test_lib
11
12
13class RouterTest(cros_test_lib.MockTestCase):
14 """Test Router functionality."""
15 _INPUT_JSON = '{"id":"Input ID"}'
16
17 def setUp(self):
18 self.router = build_api.Router()
19 self.router.Register(build_api_test_pb2)
20
21 def testInputOutputMethod(self):
22 """Test input/output handling."""
23 def impl(input_msg, output_msg):
24 self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage)
25 self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage)
26
27 self.PatchObject(self.router, '_GetMethod', return_value=impl)
28
29 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
30 self._INPUT_JSON)
31
32 # Implementation expecting two methods with definitions that have Empty
33 # for the input, output, and both messages.
34 with self.assertRaises(TypeError):
35 self.router.Route('chromite.api.TestApiService', 'InputMethod',
36 self._INPUT_JSON)
37 with self.assertRaises(TypeError):
38 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
39 self._INPUT_JSON)
40 with self.assertRaises(TypeError):
41 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
42 self._INPUT_JSON)
43
44 def testInputMethod(self):
45 """Test methods with only an input message."""
46 def impl(input_msg):
47 self.assertIsInstance(input_msg, build_api_test_pb2.TestRequestMessage)
48
49 self.PatchObject(self.router, '_GetMethod', return_value=impl)
50
51 self.router.Route('chromite.api.TestApiService', 'InputMethod',
52 self._INPUT_JSON)
53
54 # Test errors raised for implementations expecting input message with
55 # definitions that expect both messages, output only, and neither.
56 with self.assertRaises(TypeError):
57 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
58 self._INPUT_JSON)
59 with self.assertRaises(AssertionError):
60 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
61 self._INPUT_JSON)
62 with self.assertRaises(TypeError):
63 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
64 self._INPUT_JSON)
65
66 def testOutputMethod(self):
67 """Test methods with only an output message."""
68 def impl(output_msg):
69 self.assertIsInstance(output_msg, build_api_test_pb2.TestResultMessage)
70
71 self.PatchObject(self.router, '_GetMethod', return_value=impl)
72
73 self.router.Route('chromite.api.TestApiService', 'OutputMethod',
74 self._INPUT_JSON)
75
76 # Test errors raised for implementations expecting input message with
77 # definitions that expect both messages, output only, and neither.
78 with self.assertRaises(TypeError):
79 self.router.Route('chromite.api.TestApiService', 'InputOutputMethod',
80 self._INPUT_JSON)
81 with self.assertRaises(AssertionError):
82 self.router.Route('chromite.api.TestApiService', 'InputMethod',
83 self._INPUT_JSON)
84 with self.assertRaises(TypeError):
85 self.router.Route('chromite.api.TestApiService', 'NoIoMethod',
86 self._INPUT_JSON)