blob: 4fcecc3731335c500c6222b87ad29ce610cb0c80 [file] [log] [blame]
efiacor5ed9b7d2020-05-20 15:18:41 +01001# ============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=====================================================
18import json
19import os
20import unittest
21from test.support import EnvironmentVarGuard
22from unittest.mock import patch
23
24from requests import Session
25
26from mod import aai_client, create_app, db
27from mod.api.controller import status, get_all_sub_to_nf_relations
28from mod.network_function import NetworkFunction
efiacor80ff1482020-06-09 19:20:22 +010029from mod.pmsh_utils import AppConfig
efiacor5ed9b7d2020-05-20 15:18:41 +010030
31
32class ControllerTestCase(unittest.TestCase):
efiacor80ff1482020-06-09 19:20:22 +010033
34 @patch('mod.pmsh_utils.AppConfig._get_pmsh_config')
35 @patch('mod.update_logging_config')
efiacor5ed9b7d2020-05-20 15:18:41 +010036 @patch('mod.get_db_connection_url')
37 @patch.object(Session, 'put')
efiacor80ff1482020-06-09 19:20:22 +010038 def setUp(self, mock_session, mock_get_db_url, mock_update_config, mock_get_pmsh_config):
efiacor5ed9b7d2020-05-20 15:18:41 +010039 mock_get_db_url.return_value = 'sqlite://'
40 with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data:
41 self.aai_response_data = data.read()
42 mock_session.return_value.status_code = 200
43 mock_session.return_value.text = self.aai_response_data
44 self.env = EnvironmentVarGuard()
efiacor5ed9b7d2020-05-20 15:18:41 +010045 self.env.set('AAI_SERVICE_PORT', '8443')
ERIMROB27605192020-05-12 12:56:56 +010046 self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml'))
efiacor5ed9b7d2020-05-20 15:18:41 +010047 with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data:
efiacor80ff1482020-06-09 19:20:22 +010048 self.cbs_data = json.load(data)
49 mock_get_pmsh_config.return_value = self.cbs_data
efiacor5ed9b7d2020-05-20 15:18:41 +010050 self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried')
51 self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active')
52 self.app = create_app()
53 self.app_context = self.app.app_context()
54 self.app_context.push()
55 db.create_all()
efiacor38ccb472020-08-05 10:12:04 +010056 self.app_conf = AppConfig()
57 self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf)
efiacor5ed9b7d2020-05-20 15:18:41 +010058
59 def tearDown(self):
60 db.session.remove()
61 db.drop_all()
efiacor5ed9b7d2020-05-20 15:18:41 +010062
63 def test_status_response_healthy(self):
64 self.assertEqual(status()['status'], 'healthy')
65
66 def test_get_all_sub_to_nf_relations(self):
efiacor38ccb472020-08-05 10:12:04 +010067 sub_model = self.app_conf.subscription.get()
efiacor5ed9b7d2020-05-20 15:18:41 +010068 for nf in [self.nf_1, self.nf_2]:
efiacor38ccb472020-08-05 10:12:04 +010069 self.app_conf.subscription.add_network_function_to_subscription(nf, sub_model)
efiacor5ed9b7d2020-05-20 15:18:41 +010070 all_subs = get_all_sub_to_nf_relations()
71 self.assertEqual(len(all_subs[0]['network_functions']), 2)
72 self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B')