[PMSH] Validate schema of PMSH monitoring policy
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_controller.py
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 from unittest.mock import patch
21
22 import responses
23 from requests import Session
24
25 from mod import aai_client
26 from mod.api.controller import status, get_all_sub_to_nf_relations
27 from tests.base_setup import BaseClassSetup
28
29
30 class ControllerTestCase(BaseClassSetup):
31
32     @classmethod
33     def setUpClass(cls):
34         super().setUpClass()
35
36     def setUp(self):
37         super().setUp()
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         with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data:
41             self.good_model_info = data.read()
42
43     def tearDown(self):
44         super().tearDown()
45
46     @classmethod
47     def tearDownClass(cls):
48         super().tearDownClass()
49
50     def test_status_response_healthy(self):
51         self.assertEqual(status()['status'], 'healthy')
52
53     @patch.object(Session, 'get')
54     @patch.object(Session, 'put')
55     def test_get_all_sub_to_nf_relations(self, mock_put_session, mock_get_session):
56         mock_put_session.return_value.status_code = 200
57         mock_put_session.return_value.text = self.aai_response_data
58         mock_get_session.return_value.status_code = 200
59         mock_get_session.return_value.text = self.good_model_info
60         responses.add(responses.GET,
61                       'https://aai:8443/aai/v20/service-design-and-creation/models/model/'
62                       '7129e420-d396-4efb-af02-6b83499b12f8/model-vers/model-ver/'
63                       'e80a6ae3-cafd-4d24-850d-e14c084a5ca9',
64                       json=json.loads(self.good_model_info), status=200)
65         self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf)
66         sub_model = self.app_conf.subscription.get()
67         for nf in self.xnfs:
68             self.app_conf.subscription.add_network_function_to_subscription(nf, sub_model)
69         all_subs = get_all_sub_to_nf_relations()
70         self.assertEqual(len(all_subs[0]['network_functions']), 3)
71         self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B')