efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 1 | # ============LICENSE_START=================================================== |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 2 | # Copyright (C) 2019-2020 Nordix Foundation. |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 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 | import unittest |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 21 | from test.support import EnvironmentVarGuard |
| 22 | from unittest import mock |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 23 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 24 | from requests import Session |
| 25 | |
| 26 | import mod.aai_client as aai_client |
efiacor | bc9f41e | 2020-02-12 23:31:16 +0000 | [diff] [blame^] | 27 | from mod import db, create_app |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 28 | from mod.network_function import NetworkFunction |
| 29 | from mod.subscription import Subscription, NetworkFunctionFilter |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 32 | class SubscriptionTest(unittest.TestCase): |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 33 | |
efiacor | bc9f41e | 2020-02-12 23:31:16 +0000 | [diff] [blame^] | 34 | @mock.patch('mod.get_db_connection_url') |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 35 | @mock.patch.object(Session, 'put') |
efiacor | bc9f41e | 2020-02-12 23:31:16 +0000 | [diff] [blame^] | 36 | def setUp(self, mock_session, mock_get_db_url): |
| 37 | mock_get_db_url.return_value = 'sqlite://' |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 38 | with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: |
| 39 | self.aai_response_data = data.read() |
| 40 | mock_session.return_value.status_code = 200 |
| 41 | mock_session.return_value.text = self.aai_response_data |
| 42 | self.env = EnvironmentVarGuard() |
| 43 | self.env.set('AAI_SERVICE_HOST', '1.2.3.4') |
| 44 | self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443') |
efiacor | bc9f41e | 2020-02-12 23:31:16 +0000 | [diff] [blame^] | 45 | self.env.set('TESTING', 'True') |
| 46 | self.env.set('LOGS_PATH', './unit_test_logs') |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 47 | with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: |
| 48 | self.cbs_data_1 = json.load(data) |
| 49 | with open(os.path.join(os.path.dirname(__file__), |
| 50 | 'data/cbs_data_2.json'), 'r') as data: |
| 51 | self.cbs_data_2 = json.load(data) |
| 52 | self.sub_1, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_1) |
| 53 | self.sub_2, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_2) |
| 54 | self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') |
| 55 | self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') |
| 56 | self.xnf_filter = NetworkFunctionFilter(**self.sub_1.nfFilter) |
efiacor | bc9f41e | 2020-02-12 23:31:16 +0000 | [diff] [blame^] | 57 | self.app = create_app() |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 58 | self.app_context = self.app.app_context() |
| 59 | self.app_context.push() |
| 60 | db.create_all() |
| 61 | |
| 62 | def tearDown(self): |
| 63 | db.session.remove() |
| 64 | db.drop_all() |
| 65 | self.app_context.pop() |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 66 | |
| 67 | def test_xnf_filter_true(self): |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 68 | self.assertTrue(self.xnf_filter.is_nf_in_filter('pnf1')) |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 69 | |
| 70 | def test_xnf_filter_false(self): |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 71 | self.assertFalse(self.xnf_filter.is_nf_in_filter('PNF-33')) |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 72 | |
| 73 | def test_sub_measurement_group(self): |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 74 | self.assertEqual(len(self.sub_1.measurementGroups), 2) |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 75 | |
| 76 | def test_sub_file_location(self): |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 77 | self.assertEqual(self.sub_1.fileLocation, '/pm/pm.xml') |
| 78 | |
| 79 | def test_get_subscription(self): |
| 80 | sub_name = 'ExtraPM-All-gNB-R2B' |
| 81 | self.sub_1.create() |
| 82 | new_sub = Subscription.get(sub_name) |
| 83 | self.assertEqual(sub_name, new_sub.subscription_name) |
| 84 | |
| 85 | def test_get_subscription_no_match(self): |
| 86 | sub_name = 'sub2_does_not_exist' |
| 87 | sub = Subscription.get(sub_name) |
| 88 | self.assertEqual(sub, None) |
| 89 | |
| 90 | def test_get_subscriptions(self): |
| 91 | self.sub_1.create() |
| 92 | self.sub_2.create() |
| 93 | subs = self.sub_1.get_all() |
| 94 | |
| 95 | self.assertEqual(2, len(subs)) |
| 96 | |
| 97 | def test_create_existing_subscription(self): |
| 98 | sub1 = self.sub_1.create() |
| 99 | same_sub1 = self.sub_1.create() |
| 100 | self.assertEqual(sub1, same_sub1) |
| 101 | self.assertEqual(1, len(self.sub_1.get_all())) |
| 102 | |
| 103 | def test_get_nfs_per_subscription(self): |
| 104 | nf_array = [self.nf_1, self.nf_2] |
| 105 | self.sub_1.add_network_functions_to_subscription(nf_array) |
| 106 | nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations() |
| 107 | self.assertEqual(2, len(nfs_for_sub_1)) |
| 108 | |
| 109 | def test_add_network_functions_per_subscription(self): |
| 110 | nf_array = [self.nf_1, self.nf_2] |
| 111 | self.sub_1.add_network_functions_to_subscription(nf_array) |
| 112 | nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations() |
| 113 | self.assertEqual(2, len(nfs_for_sub_1)) |
| 114 | new_nf_array = [NetworkFunction(nf_name='vnf_3', orchestration_status='Inventoried')] |
| 115 | self.sub_1.add_network_functions_to_subscription(new_nf_array) |
| 116 | nf_subs = Subscription.get_all_nfs_subscription_relations() |
| 117 | print(nf_subs) |
| 118 | self.assertEqual(3, len(nf_subs)) |
| 119 | |
| 120 | def test_add_duplicate_network_functions_per_subscription(self): |
| 121 | nf_array = [self.nf_1] |
| 122 | self.sub_1.add_network_functions_to_subscription(nf_array) |
| 123 | nf_subs = Subscription.get_all_nfs_subscription_relations() |
| 124 | self.assertEqual(1, len(nf_subs)) |
| 125 | self.sub_1.add_network_functions_to_subscription(nf_array) |
| 126 | nf_subs = Subscription.get_all_nfs_subscription_relations() |
| 127 | self.assertEqual(1, len(nf_subs)) |