blob: 38518578a38f7546d34b6b09f6a0e79456d441b2 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo485b7132017-06-30 11:46:47 -070017import unittest
18from mock import patch, MagicMock
19from grpc_client.models_accessor import GRPCModelsAccessor
20from grpc_client.resources import RESOURCES
21
22class FakeObj:
23 new = None
24 filter = None
25
26class FakeResource:
27 objects = FakeObj
28
29class FakeModel:
30 pass
31
32class FakeExistingModel:
33 pass
34
35mock_resources = {
Matteo Scandolo21dde412017-07-11 18:54:12 -070036 'username~pass': {
37 'test-model': FakeResource
38 }
Matteo Scandolo485b7132017-06-30 11:46:47 -070039}
40
Matteo Scandolo21dde412017-07-11 18:54:12 -070041USERNAME = 'username'
42PASSWORD = 'pass'
43
Matteo Scandolo485b7132017-06-30 11:46:47 -070044class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
45
Matteo Scandolo21dde412017-07-11 18:54:12 -070046 def test_unkown_user(self):
47 """
48 [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise
49 """
50 data = {
51 "name": "test"
52 }
53 with self.assertRaises(Exception) as e:
54 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
55 self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources")
56
57 @patch.dict(RESOURCES, mock_resources, clear=True)
Matteo Scandolo485b7132017-06-30 11:46:47 -070058 def test_unkown_module(self):
59 """
60 [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise
61 """
62 data = {
63 "name": "test"
64 }
65 with self.assertRaises(Exception) as e:
Matteo Scandolo21dde412017-07-11 18:54:12 -070066 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
Matteo Scandolod12be212017-07-07 10:44:34 -070067 self.assertEqual(e.exception.message, "[XOS-TOSCA] The model you are trying to create (name: test, class: i-do-not-exists) is not know by xos-core")
Matteo Scandolo485b7132017-06-30 11:46:47 -070068
69 @patch.object(FakeResource.objects, "filter")
70 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
71 def test_new_model(self, mock_filter):
72 """
73 [GRPCModelsAccessor] get_model_from_classname: should create a new model
74 """
75 data = {
76 "name": "test"
77 }
78 with patch.dict(RESOURCES, mock_resources, clear=True):
Matteo Scandolo21dde412017-07-11 18:54:12 -070079 model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -070080 mock_filter.assert_called_with(name="test")
81 self.assertEqual(model, FakeModel)
82
83 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel]))
84 def test_existing_model(self):
85 """
86 [GRPCModelsAccessor] get_model_from_classname: should update an existing model
87 """
88 data = {
89 "name": "test"
90 }
91 with patch.dict(RESOURCES, mock_resources, clear=True):
Matteo Scandolo21dde412017-07-11 18:54:12 -070092 model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -070093 self.assertEqual(model, FakeExistingModel)
94
95 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b']))
96 def test_multiple_models(self):
97 """
98 [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found
99 """
100 data = {
101 "name": "test"
102 }
103 with patch.dict(RESOURCES, mock_resources, clear=True):
104 with self.assertRaises(Exception) as e:
Matteo Scandolo21dde412017-07-11 18:54:12 -0700105 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700106 self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it")
107
108 @patch.dict(RESOURCES, mock_resources, clear=True)
109 @patch.object(FakeResource.objects, "filter")
Matteo Scandolo21dde412017-07-11 18:54:12 -0700110 @patch.object(FakeResource.objects, "new")
111 def test_find_model_without_name_property(self, mock_new, mock_filter):
Matteo Scandolo485b7132017-06-30 11:46:47 -0700112 """
113 [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property
114 """
115 data = {
116 'foo': 'bar',
117 'something': 'else'
118 }
Matteo Scandolo21dde412017-07-11 18:54:12 -0700119 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700120 mock_filter.assert_called_with(foo="bar")
Matteo Scandolo21dde412017-07-11 18:54:12 -0700121 mock_new.assert_called()
Matteo Scandolo485b7132017-06-30 11:46:47 -0700122
Matteo Scandolo1fedfae2017-10-09 13:57:00 -0700123 @patch.dict(RESOURCES, mock_resources, clear=True)
124 @patch.object(FakeResource.objects, "new")
125 def test_model_without_properties(self, mock_new):
126 """
127 [GRPCModelsAccessor] get_model_from_classname: should create a new model if not properties are specified
128 """
129 data = {
130 }
131 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
132 mock_new.assert_called()
133
Matteo Scandolo485b7132017-06-30 11:46:47 -0700134if __name__ == '__main__':
135 unittest.main()