[DCAEGEN2] PMSH Create Subscription public API
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_aai_service.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2019-2021 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 import mock
21 from unittest.mock import patch
22
23 import responses
24 from requests import Session, HTTPError
25
26 import mod.aai_client as aai_client
27 from tests.base_setup import BaseClassSetup
28
29
30 class AaiClientTestCase(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     @patch('mod.network_function.NetworkFunction.set_nf_model_params')
51     @patch.object(Session, 'get')
52     @patch.object(Session, 'put')
53     def test_aai_client_get_pm_sub_data_success(self, mock_put_session, mock_get_session,
54                                                 mock_get_sdnc_params):
55         mock_put_session.return_value.status_code = 200
56         mock_put_session.return_value.text = self.aai_response_data
57         mock_get_session.return_value.status_code = 200
58         mock_get_session.return_value.text = self.good_model_info
59         mock_get_sdnc_params.return_value = True
60         xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter)
61         self.assertEqual(self.app_conf.subscription.subscriptionName, 'ExtraPM-All-gNB-R2B')
62         self.assertEqual(self.app_conf.subscription.administrativeState, 'UNLOCKED')
63         self.assertEqual(len(xnfs), 3)
64
65     @patch.object(Session, 'put')
66     def test_aai_client_get_pm_sub_data_fail(self, mock_session):
67         mock_session.return_value.status_code = 404
68         with mock.patch('mod.aai_client._get_all_aai_nf_data', return_value=None):
69             with self.assertRaises(RuntimeError):
70                 aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter)
71
72     @responses.activate
73     def test_aai_client_get_all_aai_xnf_data_not_found(self):
74         responses.add(responses.PUT,
75                       'https://1.2.3.4:8443/aai/v21/query?format=simple&nodesOnly=true',
76                       json={'error': 'not found'}, status=404)
77         self.assertIsNone(aai_client._get_all_aai_nf_data(self.app_conf))
78
79     @responses.activate
80     def test_aai_client_get_all_aai_xnf_data_success(self):
81         responses.add(responses.PUT,
82                       'https://aai:8443/aai/v21/query?format=simple&nodesOnly=true',
83                       json={'dummy_data': 'blah_blah'}, status=200)
84         self.assertIsNotNone(aai_client._get_all_aai_nf_data(self.app_conf))
85
86     @responses.activate
87     def test_aai_client_get_sdnc_params_success(self):
88         responses.add(responses.GET,
89                       'https://aai:8443/aai/v21/service-design-and-creation/models/model/'
90                       '6fb9f466-7a79-4109-a2a3-72b340aca53d/model-vers/model-ver/'
91                       '6d25b637-8bca-47e2-af1a-61258424183d',
92                       json=json.loads(self.good_model_info), status=200)
93         self.assertIsNotNone(aai_client.get_aai_model_data(self.app_conf,
94                                                            '6fb9f466-7a79-4109-a2a3-72b340aca53d',
95                                                            '6d25b637-8bca-47e2-af1a-61258424183d',
96                                                            'pnf_1'))
97
98     @responses.activate
99     def test_aai_client_get_sdnc_params_fail(self):
100         responses.add(responses.GET,
101                       'https://aai:8443/aai/v21/service-design-and-creation/models/model/'
102                       '9fb9f466-7a79-4109-a2a3-72b340aca53d/model-vers/model-ver/'
103                       'b7469cc5-be51-41cc-b37f-361537656771', status=404)
104         with self.assertRaises(HTTPError):
105             aai_client.get_aai_model_data(self.app_conf, '9fb9f466-7a79-4109-a2a3-72b340aca53d',
106                                           'b7469cc5-be51-41cc-b37f-361537656771', 'pnf_2')
107
108     def test_aai_client_get_aai_service_url_fail(self):
109         os.environ.clear()
110         with self.assertRaises(KeyError):
111             aai_client._get_aai_service_url()
112
113     def test_aai_client_get_aai_service_url_success(self):
114         self.assertEqual('https://aai:8443/aai/v21', aai_client._get_aai_service_url())