[DCAEGEN2] PMSH Create Subscription public API
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_network_function.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2019-2021 Nordix 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 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
18 import json
19 import os
20 from unittest.mock import patch, Mock
21
22 from mod.network_function import NetworkFunction
23 from tests.base_setup import BaseClassSetup
24
25
26 class NetworkFunctionTests(BaseClassSetup):
27
28     @classmethod
29     def setUpClass(cls):
30         super().setUpClass()
31
32     def setUp(self):
33         super().setUp()
34         self.nf_1 = NetworkFunction(sdnc_model_name='blah', sdnc_model_version=1.0,
35                                     **{'nf_name': 'pnf_1',
36                                        'ipv4_address': '204.120.0.15',
37                                        'ipv6_address': '2001:db8:3333:4444:5555:6666:7777:8888',
38                                        'model_invariant_id': 'some_id',
39                                        'model_version_id': 'some_other_id'})
40         self.nf_2 = NetworkFunction(sdnc_model_name='blah', sdnc_model_version=2.0,
41                                     **{'nf_name': 'pnf_2',
42                                        'ipv4_address': '204.120.0.15',
43                                        'ipv6_address': '2001:db8:3333:4444:5555:6666:7777:8888',
44                                        'model_invariant_id': 'some_id',
45                                        'model_version_id': 'some_other_id'})
46         with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data:
47             self.good_model_info = json.loads(data.read())
48         with open(os.path.join(os.path.dirname(__file__),
49                                'data/aai_model_info_no_sdnc.json'), 'r') as data:
50             self.bad_model_info = json.loads(data.read())
51
52     def tearDown(self):
53         super().tearDown()
54
55     @classmethod
56     def tearDownClass(cls):
57         super().tearDownClass()
58
59     def test_get_network_function(self):
60         self.nf_1.create()
61         nf = NetworkFunction.get('pnf_1')
62         self.assertEqual(self.nf_1.nf_name, nf.nf_name)
63
64     def test_get_network_function_no_match(self):
65         self.nf_1.create()
66         nf_name = 'nf2_does_not_exist'
67         nf = NetworkFunction.get(nf_name)
68         self.assertEqual(nf, None)
69
70     def test_get_network_functions(self):
71         self.nf_1.create()
72         self.nf_2.create()
73         nfs = NetworkFunction.get_all()
74
75         self.assertEqual(2, len(nfs))
76
77     def test_create_existing_network_function(self):
78         nf = self.nf_1.create()
79         same_nf = self.nf_1.create()
80
81         self.assertEqual(nf, same_nf)
82
83     def test_delete_network_function(self):
84         for nf in [self.nf_1, self.nf_2]:
85             self.app_conf.subscription.add_network_function_to_subscription(nf, Mock())
86         nfs = NetworkFunction.get_all()
87         self.assertEqual(2, len(nfs))
88         NetworkFunction.delete(nf_name=self.nf_1.nf_name)
89         nfs = NetworkFunction.get_all()
90         self.assertEqual(1, len(nfs))
91
92     @patch('mod.aai_client.get_aai_model_data')
93     def test_set_sdnc_params_true(self, mock_get_aai_model):
94         mock_get_aai_model.return_value = self.good_model_info
95         self.assertTrue(self.nf_1.set_nf_model_params(self.app_conf))
96
97     @patch('mod.aai_client.get_aai_model_data')
98     def test_set_sdnc_params_false(self, mock_get_aai_model):
99         mock_get_aai_model.return_value = self.bad_model_info
100         self.assertFalse(self.nf_1.set_nf_model_params(self.app_conf))