Merge "Add db initial pod docker image auto-build."
[dcaegen2/services.git] / components / pm-subscription-handler / tests / test_aai_service.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 import unittest
21 from test.support import EnvironmentVarGuard
22 from unittest import mock
23
24 import responses
25 from requests import Session
26
27 import mod.aai_client as aai_client
28
29
30 class AaiClientTestCase(unittest.TestCase):
31
32     def setUp(self):
33         self.env = EnvironmentVarGuard()
34         self.env.set('AAI_SERVICE_HOST', '1.2.3.4')
35         self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443')
36         with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data:
37             self.cbs_data = json.load(data)
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
41     @mock.patch.object(Session, 'put')
42     def test_aai_client_get_pm_sub_data_success(self, mock_session):
43         mock_session.return_value.status_code = 200
44         mock_session.return_value.text = self.aai_response_data
45         sub, xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data)
46         self.assertEqual(sub.subscriptionName, 'ExtraPM-All-gNB-R2B')
47         self.assertEqual(sub.administrativeState, 'UNLOCKED')
48         self.assertEqual(len(xnfs), 6)
49
50     @mock.patch.object(Session, 'put')
51     def test_aai_client_get_pm_sub_data_fail(self, mock_session):
52         mock_session.return_value.status_code = 404
53         with mock.patch('mod.aai_client._get_all_aai_nf_data', return_value=None):
54             with self.assertRaises(RuntimeError):
55                 aai_client.get_pmsh_subscription_data(self.cbs_data)
56
57     @responses.activate
58     def test_aai_client_get_all_aai_xnf_data_not_found(self):
59         responses.add(responses.PUT,
60                       'https://1.2.3.4:8443/aai/v16/query?format=simple&nodesOnly=true',
61                       json={'error': 'not found'}, status=404)
62         self.assertIsNone(aai_client._get_all_aai_nf_data())
63
64     @responses.activate
65     def test_aai_client_get_all_aai_xnf_data_success(self):
66         responses.add(responses.PUT,
67                       'https://1.2.3.4:8443/aai/v16/query?format=simple&nodesOnly=true',
68                       json={'dummy_data': 'blah_blah'}, status=200)
69         self.assertIsNotNone(aai_client._get_all_aai_nf_data())
70
71     def test_aai_client_get_aai_service_url_fail(self):
72         self.env.clear()
73         with self.assertRaises(KeyError):
74             aai_client._get_aai_service_url()
75
76     def test_aai_client_get_aai_service_url_succses(self):
77         self.assertEqual('https://1.2.3.4:8443', aai_client._get_aai_service_url())