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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
22 from mod import db, create_app
23 from mod.network_function import NetworkFunction
24 from mod.subscription import Subscription
27 class NetworkFunctionTests(TestCase):
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()
44 self.app_context.pop()
46 def test_get_network_function(self):
48 nf = NetworkFunction.get('pnf_1')
49 self.assertEqual(self.nf_1.nf_name, nf.nf_name)
51 def test_get_network_function_no_match(self):
53 nf_name = 'nf2_does_not_exist'
54 nf = NetworkFunction.get(nf_name)
55 self.assertEqual(nf, None)
57 def test_get_network_functions(self):
60 nfs = NetworkFunction.get_all()
62 self.assertEqual(2, len(nfs))
64 def test_create_existing_network_function(self):
65 nf = self.nf_1.create()
66 same_nf = self.nf_1.create()
68 self.assertEqual(nf, same_nf)
70 def test_delete_network_function(self):
73 sub = Subscription(**{"subscriptionName": "sub"})
74 sub.add_network_functions_to_subscription([self.nf_1, self.nf_2])
76 NetworkFunction.delete(nf_name=self.nf_1.nf_name)
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)