summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/tests/test_aai_service.py
blob: 97f400c188e078e7101b216ccf3a4bb3eb36857a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# ============LICENSE_START===================================================
#  Copyright (C) 2019-2021 Nordix Foundation.
# ============================================================================
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
import json
import os
from unittest import mock
from unittest.mock import patch

import responses
from requests import Session, HTTPError

import mod.aai_client as aai_client
from tests.base_setup import BaseClassSetup


class AaiClientTestCase(BaseClassSetup):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

    def setUp(self):
        super().setUp()
        with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data:
            self.aai_response_data = data.read()
        with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data:
            self.good_model_info = data.read()

    def tearDown(self):
        super().tearDown()

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()

    @patch('mod.network_function.NetworkFunction.set_nf_model_params')
    @patch.object(Session, 'get')
    @patch.object(Session, 'put')
    def test_aai_client_get_pm_sub_data_success(self, mock_put_session, mock_get_session,
                                                mock_get_sdnc_params):
        mock_put_session.return_value.status_code = 200
        mock_put_session.return_value.text = self.aai_response_data
        mock_get_session.return_value.status_code = 200
        mock_get_session.return_value.text = self.good_model_info
        mock_get_sdnc_params.return_value = True
        xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter)
        self.assertEqual(self.app_conf.subscription.subscriptionName, 'ExtraPM-All-gNB-R2B')
        self.assertEqual(self.app_conf.subscription.administrativeState, 'UNLOCKED')
        self.assertEqual(len(xnfs), 3)

    @patch.object(Session, 'put')
    def test_aai_client_get_pm_sub_data_fail(self, mock_session):
        mock_session.return_value.status_code = 404
        with mock.patch('mod.aai_client._get_all_aai_nf_data', return_value=None):
            with self.assertRaises(RuntimeError):
                aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter)

    @responses.activate
    def test_aai_client_get_all_aai_xnf_data_not_found(self):
        responses.add(responses.PUT,
                      'https://1.2.3.4:8443/aai/v21/query?format=simple&nodesOnly=true',
                      json={'error': 'not found'}, status=404)
        self.assertIsNone(aai_client._get_all_aai_nf_data(self.app_conf))

    @responses.activate
    def test_aai_client_get_all_aai_xnf_data_success(self):
        responses.add(responses.PUT,
                      'https://aai:8443/aai/v21/query?format=simple&nodesOnly=true',
                      json={'dummy_data': 'blah_blah'}, status=200)
        self.assertIsNotNone(aai_client._get_all_aai_nf_data(self.app_conf))

    @responses.activate
    def test_aai_client_get_sdnc_params_success(self):
        responses.add(responses.GET,
                      'https://aai:8443/aai/v21/service-design-and-creation/models/model/'
                      '6fb9f466-7a79-4109-a2a3-72b340aca53d/model-vers/model-ver/'
                      '6d25b637-8bca-47e2-af1a-61258424183d',
                      json=json.loads(self.good_model_info), status=200)
        self.assertIsNotNone(aai_client.get_aai_model_data(self.app_conf,
                                                           '6fb9f466-7a79-4109-a2a3-72b340aca53d',
                                                           '6d25b637-8bca-47e2-af1a-61258424183d',
                                                           'pnf_1'))

    @responses.activate
    def test_aai_client_get_sdnc_params_fail(self):
        responses.add(responses.GET,
                      'https://aai:8443/aai/v21/service-design-and-creation/models/model/'
                      '9fb9f466-7a79-4109-a2a3-72b340aca53d/model-vers/model-ver/'
                      'b7469cc5-be51-41cc-b37f-361537656771', status=404)
        with self.assertRaises(HTTPError):
            aai_client.get_aai_model_data(self.app_conf, '9fb9f466-7a79-4109-a2a3-72b340aca53d',
                                          'b7469cc5-be51-41cc-b37f-361537656771', 'pnf_2')

    def test_aai_client_get_aai_service_url_fail(self):
        os.environ.clear()
        with self.assertRaises(KeyError):
            aai_client._get_aai_service_url()

    def test_aai_client_get_aai_service_url_success(self):
        self.assertEqual('https://aai:8443/aai/v21', aai_client._get_aai_service_url())