efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame^] | 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 | import json |
| 19 | import os |
| 20 | import unittest |
| 21 | from test.support import EnvironmentVarGuard |
| 22 | from unittest.mock import patch |
| 23 | |
| 24 | from requests import Session |
| 25 | |
| 26 | from mod import aai_client, create_app, db |
| 27 | from mod.api.controller import status, get_all_sub_to_nf_relations |
| 28 | from mod.network_function import NetworkFunction |
| 29 | |
| 30 | |
| 31 | class ControllerTestCase(unittest.TestCase): |
| 32 | @patch('mod.get_db_connection_url') |
| 33 | @patch.object(Session, 'put') |
| 34 | def setUp(self, mock_session, mock_get_db_url): |
| 35 | mock_get_db_url.return_value = 'sqlite://' |
| 36 | with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: |
| 37 | self.aai_response_data = data.read() |
| 38 | mock_session.return_value.status_code = 200 |
| 39 | mock_session.return_value.text = self.aai_response_data |
| 40 | self.env = EnvironmentVarGuard() |
| 41 | self.env.set('AAI_SERVICE_HOST', '1.2.3.4') |
| 42 | self.env.set('AAI_SERVICE_PORT', '8443') |
| 43 | self.env.set('TESTING', 'True') |
| 44 | self.env.set('LOGS_PATH', './unit_test_logs') |
| 45 | with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: |
| 46 | self.cbs_data_1 = json.load(data) |
| 47 | self.sub_1, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_1) |
| 48 | self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') |
| 49 | self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') |
| 50 | self.app = create_app() |
| 51 | self.app_context = self.app.app_context() |
| 52 | self.app_context.push() |
| 53 | db.create_all() |
| 54 | |
| 55 | def tearDown(self): |
| 56 | db.session.remove() |
| 57 | db.drop_all() |
| 58 | self.app_context.pop() |
| 59 | |
| 60 | def test_status_response_healthy(self): |
| 61 | self.assertEqual(status()['status'], 'healthy') |
| 62 | |
| 63 | def test_get_all_sub_to_nf_relations(self): |
| 64 | self.sub_1.create() |
| 65 | for nf in [self.nf_1, self.nf_2]: |
| 66 | self.sub_1.add_network_function_to_subscription(nf) |
| 67 | all_subs = get_all_sub_to_nf_relations() |
| 68 | self.assertEqual(len(all_subs[0]['network_functions']), 2) |
| 69 | self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B') |