efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 1 | # ============LICENSE_START=================================================== |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 2 | # Copyright (C) 2019-2021 Nordix Foundation. |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [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 |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 20 | from unittest.mock import patch, MagicMock |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 21 | import responses |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 22 | from requests import Session |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 23 | from mod import aai_client |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 24 | from mod.api.controller import status, get_all_sub_to_nf_relations, post_subscription |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 25 | from tests.base_setup import BaseClassSetup |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 26 | from mod.api.db_models import SubscriptionModel, NfMeasureGroupRelationalModel |
| 27 | from mod.subscription import SubNfState |
| 28 | from mod.network_function import NetworkFunctionFilter |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 29 | |
| 30 | |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 31 | class ControllerTestCase(BaseClassSetup): |
efiacor | 80ff148 | 2020-06-09 19:20:22 +0100 | [diff] [blame] | 32 | |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 33 | @classmethod |
| 34 | def setUpClass(cls): |
| 35 | super().setUpClass() |
| 36 | |
| 37 | def setUp(self): |
| 38 | super().setUp() |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 39 | super().setUpAppConf() |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 40 | with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: |
| 41 | self.aai_response_data = data.read() |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 42 | with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data: |
| 43 | self.good_model_info = data.read() |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 44 | with open(os.path.join(os.path.dirname(__file__), |
| 45 | 'data/create_subscription_request.json'), 'r') as data: |
| 46 | self.subscription_request = data.read() |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 47 | |
| 48 | def tearDown(self): |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 49 | super().tearDown() |
| 50 | |
| 51 | @classmethod |
| 52 | def tearDownClass(cls): |
| 53 | super().tearDownClass() |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 54 | |
| 55 | def test_status_response_healthy(self): |
| 56 | self.assertEqual(status()['status'], 'healthy') |
| 57 | |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 58 | @patch.object(Session, 'get') |
| 59 | @patch.object(Session, 'put') |
| 60 | def test_get_all_sub_to_nf_relations(self, mock_put_session, mock_get_session): |
| 61 | mock_put_session.return_value.status_code = 200 |
| 62 | mock_put_session.return_value.text = self.aai_response_data |
| 63 | mock_get_session.return_value.status_code = 200 |
| 64 | mock_get_session.return_value.text = self.good_model_info |
| 65 | responses.add(responses.GET, |
| 66 | 'https://aai:8443/aai/v20/service-design-and-creation/models/model/' |
| 67 | '7129e420-d396-4efb-af02-6b83499b12f8/model-vers/model-ver/' |
| 68 | 'e80a6ae3-cafd-4d24-850d-e14c084a5ca9', |
| 69 | json=json.loads(self.good_model_info), status=200) |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 70 | self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter) |
efiacor | 38ccb47 | 2020-08-05 10:12:04 +0100 | [diff] [blame] | 71 | sub_model = self.app_conf.subscription.get() |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 72 | for nf in self.xnfs: |
efiacor | 38ccb47 | 2020-08-05 10:12:04 +0100 | [diff] [blame] | 73 | self.app_conf.subscription.add_network_function_to_subscription(nf, sub_model) |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 74 | all_subs = get_all_sub_to_nf_relations() |
efiacor | 0080fa4 | 2020-09-08 16:26:50 +0100 | [diff] [blame] | 75 | self.assertEqual(len(all_subs[0]['network_functions']), 3) |
efiacor | 5ed9b7d | 2020-05-20 15:18:41 +0100 | [diff] [blame] | 76 | self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B') |
SagarS | 70de6a2 | 2021-09-08 14:46:32 +0100 | [diff] [blame^] | 77 | |
| 78 | def create_test_subs(self, new_sub_name, new_msrmt_grp_name): |
| 79 | subscription = self.subscription_request.replace('ExtraPM-All-gNB-R2B', new_sub_name) |
| 80 | subscription = subscription.replace('msrmt_grp_name', new_msrmt_grp_name) |
| 81 | return subscription |
| 82 | |
| 83 | @patch('mod.api.services.subscription_service.save_nf_filter', MagicMock(return_value=None)) |
| 84 | @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None)) |
| 85 | @patch.object(aai_client, '_get_all_aai_nf_data') |
| 86 | @patch.object(aai_client, 'get_aai_model_data') |
| 87 | @patch.object(NetworkFunctionFilter, 'get_network_function_filter') |
| 88 | def test_post_subscription(self, mock_filter_call, mock_model_aai, mock_aai): |
| 89 | mock_aai.return_value = json.loads(self.aai_response_data) |
| 90 | mock_model_aai.return_value = json.loads(self.good_model_info) |
| 91 | subscription = self.create_test_subs('xtraPM-All-gNB-R2B-post', 'msrmt_grp_name-post') |
| 92 | subscription = json.loads(subscription) |
| 93 | mock_filter_call.return_value = NetworkFunctionFilter( |
| 94 | **subscription['subscription']["nfFilter"]) |
| 95 | sub_name = subscription['subscription']['subscriptionName'] |
| 96 | mes_grp = subscription['subscription']['measurementGroups'][0]['measurementGroup'] |
| 97 | mes_grp_name = mes_grp['measurementGroupName'] |
| 98 | response = post_subscription(subscription) |
| 99 | subscription = (SubscriptionModel.query.filter( |
| 100 | SubscriptionModel.subscription_name == sub_name).one_or_none()) |
| 101 | self.assertIsNotNone(subscription) |
| 102 | msr_grp_nf_rel = (NfMeasureGroupRelationalModel.query.filter( |
| 103 | NfMeasureGroupRelationalModel.measurement_grp_name == mes_grp_name)).all() |
| 104 | for published_event in msr_grp_nf_rel: |
| 105 | self.assertEqual(published_event.nf_measure_grp_status, |
| 106 | SubNfState.PENDING_CREATE.value) |
| 107 | self.assertEqual(response[1], 201) |
| 108 | |
| 109 | def test_post_subscription_duplicate_sub(self): |
| 110 | # Posting the same subscription request stored in previous test to get duplicate response |
| 111 | response = post_subscription(json.loads(self.subscription_request)) |
| 112 | self.assertEqual(response[1], 409) |
| 113 | self.assertEqual(response[0], 'subscription Name: ExtraPM-All-gNB-R2B already exists.') |
| 114 | |
| 115 | def test_post_subscription_invalid_filter(self): |
| 116 | subscription = self.create_test_subs('xtraPM-All-gNB-R2B-invalid', 'msrmt_grp_name-invalid') |
| 117 | subscription = json.loads(subscription) |
| 118 | subscription['subscription']['nfFilter']['nfNames'] = [] |
| 119 | subscription['subscription']['nfFilter']['modelInvariantIDs'] = [] |
| 120 | subscription['subscription']['nfFilter']['modelVersionIDs'] = [] |
| 121 | subscription['subscription']['nfFilter']['modelNames'] = [] |
| 122 | response = post_subscription(subscription) |
| 123 | self.assertEqual(response[1], 400) |
| 124 | self.assertEqual(response[0], 'At least one filter within nfFilter must not be empty') |
| 125 | |
| 126 | def test_post_subscription_missing(self): |
| 127 | subscription = json.loads(self.subscription_request) |
| 128 | subscription['subscription']['subscriptionName'] = '' |
| 129 | response = post_subscription(subscription) |
| 130 | self.assertEqual(response[1], 400) |
| 131 | self.assertEqual(response[0], 'No value provided in subscription name') |