blob: a3a28163f1041cbcf66199d6f7ad0af7b1ccada8 [file] [log] [blame]
efiacor5ed9b7d2020-05-20 15:18:41 +01001# ============LICENSE_START===================================================
SagarS70de6a22021-09-08 14:46:32 +01002# Copyright (C) 2019-2021 Nordix Foundation.
efiacor5ed9b7d2020-05-20 15:18:41 +01003# ============================================================================
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
SagarS70de6a22021-09-08 14:46:32 +010020from unittest.mock import patch, MagicMock
efiacor0080fa42020-09-08 16:26:50 +010021import responses
efiacor5ed9b7d2020-05-20 15:18:41 +010022from requests import Session
efiacor0080fa42020-09-08 16:26:50 +010023from mod import aai_client
SagarS70de6a22021-09-08 14:46:32 +010024from mod.api.controller import status, get_all_sub_to_nf_relations, post_subscription
efiacor0080fa42020-09-08 16:26:50 +010025from tests.base_setup import BaseClassSetup
SagarS70de6a22021-09-08 14:46:32 +010026from mod.api.db_models import SubscriptionModel, NfMeasureGroupRelationalModel
27from mod.subscription import SubNfState
28from mod.network_function import NetworkFunctionFilter
efiacor5ed9b7d2020-05-20 15:18:41 +010029
30
efiacor0080fa42020-09-08 16:26:50 +010031class ControllerTestCase(BaseClassSetup):
efiacor80ff1482020-06-09 19:20:22 +010032
efiacor0080fa42020-09-08 16:26:50 +010033 @classmethod
34 def setUpClass(cls):
35 super().setUpClass()
36
37 def setUp(self):
38 super().setUp()
SagarS70de6a22021-09-08 14:46:32 +010039 super().setUpAppConf()
efiacor5ed9b7d2020-05-20 15:18:41 +010040 with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data:
41 self.aai_response_data = data.read()
efiacor0080fa42020-09-08 16:26:50 +010042 with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data:
43 self.good_model_info = data.read()
SagarS70de6a22021-09-08 14:46:32 +010044 with open(os.path.join(os.path.dirname(__file__),
45 'data/create_subscription_request.json'), 'r') as data:
46 self.subscription_request = data.read()
efiacor5ed9b7d2020-05-20 15:18:41 +010047
48 def tearDown(self):
efiacor0080fa42020-09-08 16:26:50 +010049 super().tearDown()
50
51 @classmethod
52 def tearDownClass(cls):
53 super().tearDownClass()
efiacor5ed9b7d2020-05-20 15:18:41 +010054
55 def test_status_response_healthy(self):
56 self.assertEqual(status()['status'], 'healthy')
57
efiacor0080fa42020-09-08 16:26:50 +010058 @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)
SagarS70de6a22021-09-08 14:46:32 +010070 self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter)
efiacor38ccb472020-08-05 10:12:04 +010071 sub_model = self.app_conf.subscription.get()
efiacor0080fa42020-09-08 16:26:50 +010072 for nf in self.xnfs:
efiacor38ccb472020-08-05 10:12:04 +010073 self.app_conf.subscription.add_network_function_to_subscription(nf, sub_model)
efiacor5ed9b7d2020-05-20 15:18:41 +010074 all_subs = get_all_sub_to_nf_relations()
efiacor0080fa42020-09-08 16:26:50 +010075 self.assertEqual(len(all_subs[0]['network_functions']), 3)
efiacor5ed9b7d2020-05-20 15:18:41 +010076 self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B')
SagarS70de6a22021-09-08 14:46:32 +010077
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')