Merge "Handle AAI Update and Delete events for PMSH"
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_network_function.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2019-2020 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 from test.support import EnvironmentVarGuard
19 from unittest import TestCase
20 from unittest.mock import patch
21
22 from mod import db, create_app
23 from mod.network_function import NetworkFunction
24 from mod.subscription import Subscription
25
26
27 class NetworkFunctionTests(TestCase):
28
29     @patch('mod.get_db_connection_url')
30     def setUp(self, mock_get_db_url):
31         mock_get_db_url.return_value = 'sqlite://'
32         self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried')
33         self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active')
34         self.env = EnvironmentVarGuard()
35         self.env.set('LOGS_PATH', './unit_test_logs')
36         self.app = create_app()
37         self.app_context = self.app.app_context()
38         self.app_context.push()
39         db.create_all()
40
41     def tearDown(self):
42         db.session.remove()
43         db.drop_all()
44         self.app_context.pop()
45
46     def test_get_network_function(self):
47         self.nf_1.create()
48         nf = NetworkFunction.get('pnf_1')
49         self.assertEqual(self.nf_1.nf_name, nf.nf_name)
50
51     def test_get_network_function_no_match(self):
52         self.nf_1.create()
53         nf_name = 'nf2_does_not_exist'
54         nf = NetworkFunction.get(nf_name)
55         self.assertEqual(nf, None)
56
57     def test_get_network_functions(self):
58         self.nf_1.create()
59         self.nf_2.create()
60         nfs = NetworkFunction.get_all()
61
62         self.assertEqual(2, len(nfs))
63
64     def test_create_existing_network_function(self):
65         nf = self.nf_1.create()
66         same_nf = self.nf_1.create()
67
68         self.assertEqual(nf, same_nf)
69
70     def test_delete_network_function(self):
71         self.nf_1.create()
72         self.nf_2.create()
73         sub = Subscription(**{"subscriptionName": "sub"})
74         sub.add_network_functions_to_subscription([self.nf_1, self.nf_2])
75
76         NetworkFunction.delete(nf_name=self.nf_1.nf_name)
77
78         nfs = NetworkFunction.get_all()
79         self.assertEqual(1, len(nfs))
80         self.assertEqual(1, len(Subscription.get_all_nfs_subscription_relations()))
81         pnf_1_deleted = [nf for nf in nfs if nf.nf_name != self.nf_1.nf_name]
82         self.assertTrue(pnf_1_deleted)