2 # -------------------------------------------------------------------------
3 # Copyright (C) 2020 Wipro Limited.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
23 from mock import patch
25 from conductor.data.plugins.inventory_provider.utils import aai_utils
27 class TestUtils(unittest.TestCase):
35 def test_convert_hyphen_to_under_score(self):
36 slice_profile_file = './conductor/tests/unit/data/plugins/inventory_provider/slice_profile.json'
37 slice_profile_hyphened = json.loads(open(slice_profile_file).read())
39 converted_slice_profile_file = './conductor/tests/unit/data/plugins/inventory_provider' \
40 '/slice_profile_converted.json'
41 converted_slice_profile = json.loads(open(converted_slice_profile_file).read())
42 self.assertEqual(converted_slice_profile, aai_utils.convert_hyphen_to_under_score(slice_profile_hyphened))
44 def test_get_first_level_and_second_level_filter(self):
45 first_level_filter_file = './conductor/tests/unit/data/plugins/inventory_provider/first_level_filter.json'
46 first_level_filter = json.loads(open(first_level_filter_file).read())
47 filtering_attributes = dict()
48 filtering_attributes['orchestration-status'] = "active"
49 filtering_attributes['service-role'] = "nssi"
50 filtering_attributes['model-invariant-id'] = "123644"
51 filtering_attributes['model-version-id'] = "524846"
52 filtering_attributes['environment-context'] = 'shared'
54 second_level_filter = {'service-role': 'nssi'}
56 self.assertEqual(second_level_filter, aai_utils.get_first_level_and_second_level_filter(filtering_attributes,
59 self.assertEqual(first_level_filter, filtering_attributes)
61 self.assertEqual({}, aai_utils.get_first_level_and_second_level_filter(filtering_attributes,
64 def test_add_query_params(self):
65 first_level_filter_file = './conductor/tests/unit/data/plugins/inventory_provider/first_level_filter.json'
66 first_level_filter = json.loads(open(first_level_filter_file).read())
68 query_params = "?orchestration-status=active&model-invariant-id=123644&model-version-id=524846&" \
69 "environment-context=shared&"
71 self.assertEqual(query_params, aai_utils.add_query_params(first_level_filter))
72 first_level_filter = {}
73 self.assertEqual('', aai_utils.add_query_params(first_level_filter))
75 def test_add_query_params_and_depth(self):
76 first_level_filter_file = './conductor/tests/unit/data/plugins/inventory_provider/first_level_filter.json'
77 first_level_filter = json.loads(open(first_level_filter_file).read())
79 query_params_with_depth = "?orchestration-status=active&model-invariant-id=123644&model-version-id=524846&" \
80 "environment-context=shared&depth=2"
82 self.assertEqual(query_params_with_depth, aai_utils.add_query_params_and_depth(first_level_filter, "2"))
84 only_depth = "?depth=2"
85 first_level_filter = {}
86 self.assertEqual(only_depth, aai_utils.add_query_params_and_depth(first_level_filter, "2"))
88 def test_get_inv_values_for_second_level_filter(self):
89 nssi_response_file = './conductor/tests/unit/data/plugins/inventory_provider/nssi_response.json'
90 nssi_response = json.loads(open(nssi_response_file).read())
91 nssi_instance = nssi_response.get("service-instance")[0]
92 second_level_filter = {'service-role': 'nsi'}
93 inventory_attribute = {'service-role': 'nssi'}
94 self.assertEqual(inventory_attribute, aai_utils.get_inv_values_for_second_level_filter(second_level_filter,