diff options
author | efiacor <fiachra.corcoran@est.tech> | 2020-09-08 16:26:50 +0100 |
---|---|---|
committer | efiacor <fiachra.corcoran@est.tech> | 2020-09-10 13:00:50 +0100 |
commit | 0080fa4309a40599b7d239b53bab9a74182ae500 (patch) | |
tree | 17af572ecb3e03a3be7115d41e3097985773ed5a /components/pm-subscription-handler/tests | |
parent | 2e3c407d0fcf8c73c5fd6714d6013e37930c92cb (diff) |
[PMSSH] Add sdnc model info to event publish1.1.1-pmsh
Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Change-Id: I89e25e99a2d541a29cf4a8dde986fab5b1812c9e
Issue-ID: DCAEGEN2-2405
Diffstat (limited to 'components/pm-subscription-handler/tests')
17 files changed, 442 insertions, 383 deletions
diff --git a/components/pm-subscription-handler/tests/base_setup.py b/components/pm-subscription-handler/tests/base_setup.py new file mode 100755 index 00000000..2e50dde9 --- /dev/null +++ b/components/pm-subscription-handler/tests/base_setup.py @@ -0,0 +1,57 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2020 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 TestCase +from unittest.mock import patch, MagicMock + +from mod import create_app, db +from mod.pmsh_utils import AppConfig + + +def get_pmsh_config(): + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: + return json.load(data) + + +class BaseClassSetup(TestCase): + app = None + app_context = None + + @classmethod + @patch('mod.get_db_connection_url', MagicMock(return_value='sqlite://')) + @patch('mod.update_logging_config', MagicMock()) + def setUpClass(cls): + os.environ['LOGGER_CONFIG'] = os.path.join(os.path.dirname(__file__), 'log_config.yaml') + os.environ['LOGS_PATH'] = '.' + cls.app = create_app() + cls.app_context = cls.app.app_context() + cls.app_context.push() + + @patch('mod.pmsh_utils.AppConfig._get_pmsh_config', MagicMock(return_value=get_pmsh_config())) + def setUp(self): + os.environ['AAI_SERVICE_PORT'] = '8443' + db.create_all() + self.app_conf = AppConfig() + + def tearDown(self): + db.drop_all() + + @classmethod + def tearDownClass(cls): + db.session.remove() diff --git a/components/pm-subscription-handler/tests/data/aai_model_info.json b/components/pm-subscription-handler/tests/data/aai_model_info.json new file mode 100644 index 00000000..625de52a --- /dev/null +++ b/components/pm-subscription-handler/tests/data/aai_model_info.json @@ -0,0 +1,36 @@ +{ + "model-version-id": "6d25b637-8bca-47e2-af1a-61258424183d", + "model-name": "PNF102", + "model-version": "1.0", + "model-description": "sartgserg", + "sdnc-model-name": "pm_control", + "sdnc-model-version": "1.0.0", + "resource-version": "1598626661947", + "relationship-list": { + "relationship": [ + { + "related-to": "model-element", + "relationship-label": "org.onap.relationships.inventory.IsA", + "related-link": "/aai/v20/service-design-and-creation/models/model/c1a44771-3aa8-4888-a4f4-be89d1caa0cb/model-vers/model-ver/7256a992-10a7-4ac8-8c2c-63c67e5c48c8/model-elements/model-element/fddc70fe-8343-48c1-af2e-b54f551a32ee/model-elements/model-element/7bff45b7-8254-44e5-b7ad-6e10dee6dfc3", + "relationship-data": [ + { + "relationship-key": "model.model-invariant-id", + "relationship-value": "c1a44771-3aa8-4888-a4f4-be89d1caa0cb" + }, + { + "relationship-key": "model-ver.model-version-id", + "relationship-value": "7256a992-10a7-4ac8-8c2c-63c67e5c48c8" + }, + { + "relationship-key": "model-element.model-element-uuid", + "relationship-value": "fddc70fe-8343-48c1-af2e-b54f551a32ee" + }, + { + "relationship-key": "model-element.model-element-uuid", + "relationship-value": "7bff45b7-8254-44e5-b7ad-6e10dee6dfc3" + } + ] + } + ] + } +}
\ No newline at end of file diff --git a/components/pm-subscription-handler/tests/data/aai_model_info_no_sdnc.json b/components/pm-subscription-handler/tests/data/aai_model_info_no_sdnc.json new file mode 100644 index 00000000..fffdfa92 --- /dev/null +++ b/components/pm-subscription-handler/tests/data/aai_model_info_no_sdnc.json @@ -0,0 +1,34 @@ +{ + "model-version-id": "b7469cc5-be51-41cc-b37f-361537656771", + "model-name": "PNF104", + "model-version": "1.0", + "model-description": "aervaer", + "resource-version": "1599053901976", + "relationship-list": { + "relationship": [ + { + "related-to": "model-element", + "relationship-label": "org.onap.relationships.inventory.IsA", + "related-link": "/aai/v20/service-design-and-creation/models/model/9f751399-59f7-46e4-aaf4-f78b7bf8af84/model-vers/model-ver/135b8bab-b24c-4c6d-b042-dcd7fcd937ba/model-elements/model-element/3c323039-c6ab-47bb-b8e0-d1e57a78fbea/model-elements/model-element/8a424916-020f-4c83-b785-7c04ac06567a", + "relationship-data": [ + { + "relationship-key": "model.model-invariant-id", + "relationship-value": "9f751399-59f7-46e4-aaf4-f78b7bf8af84" + }, + { + "relationship-key": "model-ver.model-version-id", + "relationship-value": "135b8bab-b24c-4c6d-b042-dcd7fcd937ba" + }, + { + "relationship-key": "model-element.model-element-uuid", + "relationship-value": "3c323039-c6ab-47bb-b8e0-d1e57a78fbea" + }, + { + "relationship-key": "model-element.model-element-uuid", + "relationship-value": "8a424916-020f-4c83-b785-7c04ac06567a" + } + ] + } + ] + } +}
\ No newline at end of file diff --git a/components/pm-subscription-handler/tests/data/cbs_data_1.json b/components/pm-subscription-handler/tests/data/cbs_data_1.json index 5c2e0296..86515343 100644 --- a/components/pm-subscription-handler/tests/data/cbs_data_1.json +++ b/components/pm-subscription-handler/tests/data/cbs_data_1.json @@ -10,7 +10,7 @@ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ diff --git a/components/pm-subscription-handler/tests/data/filter_test_data.json b/components/pm-subscription-handler/tests/data/filter_test_data.json index 28dc61d1..0d8fc7c8 100644 --- a/components/pm-subscription-handler/tests/data/filter_test_data.json +++ b/components/pm-subscription-handler/tests/data/filter_test_data.json @@ -6,7 +6,7 @@ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ @@ -16,7 +16,6 @@ "nfName": "pnf1", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": true }, { @@ -26,7 +25,7 @@ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ @@ -36,15 +35,14 @@ "nfName": "PNF-33", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": false }, { - "testName": "test_filter_true_on_modelInvariantUUIDs", + "testName": "test_filter_true_on_modelInvariantIDs", "nfFilter":{ "nfNames":[ ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ "5845y423-g654-6fju-po78-8n53154532k6", "7129e420-d396-4efb-af02-6b83499b12f8" ], @@ -55,15 +53,14 @@ "nfName": "pnf1", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": true }, { - "testName": "test_filter_false_on_modelInvariantUUIDs", + "testName": "test_filter_false_on_modelInvariantIDs", "nfFilter":{ "nfNames":[ ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ "5845y423-g654-6fju-po78-8n53154532k6", "7129e420-d396-4efb-af02-6b83499b12f8" ], @@ -74,17 +71,16 @@ "nfName": "pnf1", "modelInvariantUUID": "WrongModelInvariantUUID", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": false }, { - "testName": "test_filter_false_on_modelInvariantUUIDs_being_false_and_pnfname_being_true", + "testName": "test_filter_false_on_modelInvariantIDs_being_false_and_pnfname_being_true", "nfFilter":{ "nfNames":[ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ "7129e420-d396-4efb-af02-6b83499b12f8" ], "modelVersionIDs": [ @@ -94,7 +90,6 @@ "nfName": "pnf1", "modelInvariantUUID": "WrongModelInvariantUUID", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": false }, { @@ -102,7 +97,7 @@ "nfFilter":{ "nfNames":[ ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ "e80a6ae3-cafd-4d24-850d-e14c084a5ca9" @@ -111,7 +106,6 @@ "nfName": "pnf1", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Active", "expectedResult": true }, { @@ -119,7 +113,7 @@ "nfFilter":{ "nfNames":[ ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ "e80a6ae3-cafd-4d24-850d-e14c084a5ca9" @@ -128,7 +122,6 @@ "nfName": "pnf1", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "WrongModelVersionID", - "orchestration_status": "Active", "expectedResult": false }, { @@ -138,7 +131,7 @@ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ ], "modelVersionIDs": [ "e80a6ae3-cafd-4d24-850d-e14c084a5ca9" @@ -147,25 +140,6 @@ "nfName": "pnf1", "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", "modelVersionID": "WrongModelVersionID", - "orchestration_status": "Active", - "expectedResult": false - }, - { - "testName": "test_filter_false_on_OrchestrationStatus", - "nfFilter":{ - "nfNames":[ - "^pnf.*", - "^vnf.*" - ], - "modelInvariantUUIDs": [ - ], - "modelVersionIDs": [ - ] - }, - "nfName": "pnf1", - "modelInvariantUUID": "7129e420-d396-4efb-af02-6b83499b12f8", - "modelVersionID": "e80a6ae3-cafd-4d24-850d-e14c084a5ca9", - "orchestration_status": "Inventoried", "expectedResult": false } - ]
\ No newline at end of file +] diff --git a/components/pm-subscription-handler/tests/data/pm_subscription_event.json b/components/pm-subscription-handler/tests/data/pm_subscription_event.json index e190aa26..9416ec28 100755 --- a/components/pm-subscription-handler/tests/data/pm_subscription_event.json +++ b/components/pm-subscription-handler/tests/data/pm_subscription_event.json @@ -1,5 +1,7 @@ {
"nfName":"pnf_1",
+ "blueprintName": "some-name",
+ "blueprintVersion": "some-version",
"policyName":"pmsh-operational-policy",
"changeType":"CREATE",
"closedLoopControlName":"pmsh-control-loop",
diff --git a/components/pm-subscription-handler/tests/log_config.yaml b/components/pm-subscription-handler/tests/log_config.yaml index 1c3abd8b..f3f39772 100755 --- a/components/pm-subscription-handler/tests/log_config.yaml +++ b/components/pm-subscription-handler/tests/log_config.yaml @@ -5,9 +5,16 @@ disable_existing_loggers: true loggers: onap_logger: level: DEBUG - handlers: [stdout_handler] + handlers: [onap_log_handler, stdout_handler] propagate: false handlers: + onap_log_handler: + class: logging.handlers.RotatingFileHandler + filename: ./application.log + mode: a + maxBytes: 10000000 + backupCount: 10 + formatter: mdcFormatter stdout_handler: class: logging.StreamHandler formatter: mdcFormatter diff --git a/components/pm-subscription-handler/tests/test_aai_event_handler.py b/components/pm-subscription-handler/tests/test_aai_event_handler.py index 9ac76477..dc5243d7 100755 --- a/components/pm-subscription-handler/tests/test_aai_event_handler.py +++ b/components/pm-subscription-handler/tests/test_aai_event_handler.py @@ -16,59 +16,40 @@ # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== import json -import os from os import path -from test.support import EnvironmentVarGuard -from unittest import TestCase from unittest.mock import patch, Mock -from mod import create_app, db from mod.aai_event_handler import process_aai_events -from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig +from tests.base_setup import BaseClassSetup -class AAIEventHandlerTest(TestCase): +class AAIEventHandlerTest(BaseClassSetup): - @patch('mod.get_db_connection_url') - @patch('mod.update_logging_config') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def setUp(self, mock_get_pmsh_config, mock_update_config, mock_get_db_url): - mock_get_db_url.return_value = 'sqlite://' - with open(path.join(path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + @classmethod + def setUpClass(cls): + super().setUpClass() + + def setUp(self): + super().setUp() with open(path.join(path.dirname(__file__), 'data/mr_aai_events.json'), 'r') as data: self.mr_aai_events = json.load(data)["mr_response"] - self.env = EnvironmentVarGuard() - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) self.mock_mr_sub = Mock(get_from_topic=Mock(return_value=self.mr_aai_events)) self.mock_mr_pub = Mock() self.mock_app = Mock() - self.app = create_app() - self.app_context = self.app.app_context() - self.app_context.push() - db.create_all() def tearDown(self): - db.session.remove() - db.drop_all() - self.app_context.pop() + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + @patch('mod.network_function.NetworkFunction.set_sdnc_params') @patch('mod.subscription.Subscription.activate_subscription') @patch('mod.aai_event_handler.NetworkFunction.delete') - @patch('mod.aai_event_handler.NetworkFunction.get') - def test_process_aai_update_and_delete_events(self, mock_nf_get, mock_nf_delete, - mock_activate_sub): - pnf_already_active = NetworkFunction(nf_name='pnf_already_active', - orchestration_status='Active') - mock_nf_get.side_effect = [None, pnf_already_active] - expected_nf_for_processing = NetworkFunction( - nf_name='pnf_newly_discovered', orchestration_status='Active') - + def test_process_aai_update_and_delete_events(self, mock_nf_delete, mock_activate_sub, + mock_set_sdnc_params): + mock_set_sdnc_params.return_value = True process_aai_events(self.mock_mr_sub, self.mock_mr_pub, self.mock_app, self.app_conf) - - mock_activate_sub.assert_called_once_with([expected_nf_for_processing], - self.mock_mr_pub, self.app_conf) - mock_nf_delete.assert_called_once_with(nf_name='pnf_to_be_deleted') + self.assertEqual(mock_activate_sub.call_count, 2) + mock_nf_delete.assert_called_once() diff --git a/components/pm-subscription-handler/tests/test_aai_service.py b/components/pm-subscription-handler/tests/test_aai_service.py index 4b7be153..7a3b846d 100644 --- a/components/pm-subscription-handler/tests/test_aai_service.py +++ b/components/pm-subscription-handler/tests/test_aai_service.py @@ -17,40 +17,46 @@ # ============LICENSE_END===================================================== import json import os -from test.support import EnvironmentVarGuard -from unittest import mock, TestCase +from unittest import mock from unittest.mock import patch import responses -from requests import Session +from requests import Session, HTTPError import mod.aai_client as aai_client -from mod import create_app -from mod.pmsh_utils import AppConfig - - -class AaiClientTestCase(TestCase): - - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - @patch('mod.update_logging_config') - @patch('mod.get_db_connection_url') - def setUp(self, mock_get_db_url, mock_update_config, mock_get_pmsh_config): - mock_get_db_url.return_value = 'sqlite://' - self.env = EnvironmentVarGuard() - self.env.set('AAI_SERVICE_PORT', '8443') - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() +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() - self.app = create_app() + 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_sdnc_params') + @patch.object(Session, 'get') @patch.object(Session, 'put') - def test_aai_client_get_pm_sub_data_success(self, mock_session): - mock_session.return_value.status_code = 200 - mock_session.return_value.text = self.aai_response_data + 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.assertEqual(self.app_conf.subscription.subscriptionName, 'ExtraPM-All-gNB-R2B') self.assertEqual(self.app_conf.subscription.administrativeState, 'UNLOCKED') @@ -61,26 +67,48 @@ class AaiClientTestCase(TestCase): 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.cbs_data) + aai_client.get_pmsh_nfs_from_aai(self.app_conf) @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/v19/query?format=simple&nodesOnly=true', + 'https://1.2.3.4:8443/aai/v20/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/v19/query?format=simple&nodesOnly=true', + 'https://aai:8443/aai/v20/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/v20/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/v20/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): - self.env.clear() + 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_client._get_aai_service_url()) + self.assertEqual('https://aai:8443/aai/v20', aai_client._get_aai_service_url()) diff --git a/components/pm-subscription-handler/tests/test_controller.py b/components/pm-subscription-handler/tests/test_controller.py index 4fcecc37..c38cd976 100755 --- a/components/pm-subscription-handler/tests/test_controller.py +++ b/components/pm-subscription-handler/tests/test_controller.py @@ -17,56 +17,55 @@ # ============LICENSE_END===================================================== import json import os -import unittest -from test.support import EnvironmentVarGuard from unittest.mock import patch +import responses from requests import Session -from mod import aai_client, create_app, db +from mod import aai_client from mod.api.controller import status, get_all_sub_to_nf_relations -from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig +from tests.base_setup import BaseClassSetup -class ControllerTestCase(unittest.TestCase): +class ControllerTestCase(BaseClassSetup): - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - @patch('mod.update_logging_config') - @patch('mod.get_db_connection_url') - @patch.object(Session, 'put') - def setUp(self, mock_session, mock_get_db_url, mock_update_config, mock_get_pmsh_config): - mock_get_db_url.return_value = 'sqlite://' + @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() - mock_session.return_value.status_code = 200 - mock_session.return_value.text = self.aai_response_data - self.env = EnvironmentVarGuard() - self.env.set('AAI_SERVICE_PORT', '8443') - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data - self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') - self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') - self.app = create_app() - self.app_context = self.app.app_context() - self.app_context.push() - db.create_all() - self.app_conf = AppConfig() - self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf) + 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): - db.session.remove() - db.drop_all() + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() def test_status_response_healthy(self): self.assertEqual(status()['status'], 'healthy') - def test_get_all_sub_to_nf_relations(self): + @patch.object(Session, 'get') + @patch.object(Session, 'put') + def test_get_all_sub_to_nf_relations(self, mock_put_session, mock_get_session): + 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 + responses.add(responses.GET, + 'https://aai:8443/aai/v20/service-design-and-creation/models/model/' + '7129e420-d396-4efb-af02-6b83499b12f8/model-vers/model-ver/' + 'e80a6ae3-cafd-4d24-850d-e14c084a5ca9', + json=json.loads(self.good_model_info), status=200) + self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf) sub_model = self.app_conf.subscription.get() - for nf in [self.nf_1, self.nf_2]: + for nf in self.xnfs: self.app_conf.subscription.add_network_function_to_subscription(nf, sub_model) all_subs = get_all_sub_to_nf_relations() - self.assertEqual(len(all_subs[0]['network_functions']), 2) + self.assertEqual(len(all_subs[0]['network_functions']), 3) self.assertEqual(all_subs[0]['subscription_name'], 'ExtraPM-All-gNB-R2B') diff --git a/components/pm-subscription-handler/tests/test_exit_handler.py b/components/pm-subscription-handler/tests/test_exit_handler.py index d41bd03b..c3cc0241 100755 --- a/components/pm-subscription-handler/tests/test_exit_handler.py +++ b/components/pm-subscription-handler/tests/test_exit_handler.py @@ -15,40 +15,38 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== -import json import os from signal import SIGTERM, signal -from test.support import EnvironmentVarGuard -from unittest import TestCase from unittest.mock import patch, Mock -from mod.api.db_models import NetworkFunctionModel from mod.exit_handler import ExitHandler -from mod.pmsh_utils import AppConfig from mod.subscription import Subscription +from tests.base_setup import BaseClassSetup -class ExitHandlerTests(TestCase): - @patch('mod.subscription.Subscription.create') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') +class ExitHandlerTests(BaseClassSetup): + + @classmethod + def setUpClass(cls): + super().setUpClass() + @patch('mod.pmsh_utils.PeriodicTask') - def setUp(self, mock_periodic_task, mock_get_pmsh_config, mock_sub_create): - self.env = EnvironmentVarGuard() - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data + def setUp(self, mock_periodic_task): + super().setUp() self.mock_aai_event_thread = mock_periodic_task - self.app_conf = AppConfig() self.sub = self.app_conf.subscription - @patch('mod.logger.debug') + def tearDown(self): + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + @patch.object(Subscription, 'update_sub_nf_status') @patch.object(Subscription, 'update_subscription_status') - @patch.object(Subscription, '_get_nf_models', - return_value=[NetworkFunctionModel('pnf1', 'ACTIVE')]) - def test_terminate_signal_successful(self, mock_sub_get_nf_models, mock_upd_sub_status, - mock_upd_subnf_status, mock_logger): + def test_terminate_signal_successful(self, mock_upd_sub_status, + mock_upd_subnf_status): handler = ExitHandler(periodic_tasks=[self.mock_aai_event_thread], app_conf=self.app_conf, subscription_handler=Mock()) diff --git a/components/pm-subscription-handler/tests/test_network_function.py b/components/pm-subscription-handler/tests/test_network_function.py index cdfb1eb5..f79ec93d 100755 --- a/components/pm-subscription-handler/tests/test_network_function.py +++ b/components/pm-subscription-handler/tests/test_network_function.py @@ -17,40 +17,38 @@ # ============LICENSE_END===================================================== import json import os -from test.support import EnvironmentVarGuard -from unittest import TestCase from unittest.mock import patch -from mod import db, create_app from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig -from mod.subscription import Subscription - - -class NetworkFunctionTests(TestCase): - - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - @patch('mod.update_logging_config') - @patch('mod.get_db_connection_url') - def setUp(self, mock_get_db_url, mock_update_config, mock_get_pmsh_config): - mock_get_db_url.return_value = 'sqlite://' - self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') - self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data - self.env = EnvironmentVarGuard() - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - self.app = create_app() - self.app_context = self.app.app_context() - self.app_context.push() - db.create_all() - self.app_conf = AppConfig() +from tests.base_setup import BaseClassSetup + + +class NetworkFunctionTests(BaseClassSetup): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + def setUp(self): + super().setUp() + self.nf_1 = NetworkFunction(nf_name='pnf_1', + model_invariant_id='some-id', + model_version_id='some-id') + self.nf_2 = NetworkFunction(nf_name='pnf_2', + model_invariant_id='some-id', + model_version_id='some-id') + with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data: + self.good_model_info = json.loads(data.read()) + with open(os.path.join(os.path.dirname(__file__), + 'data/aai_model_info_no_sdnc.json'), 'r') as data: + self.bad_model_info = json.loads(data.read()) def tearDown(self): - db.session.remove() - db.drop_all() - self.app_context.pop() + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() def test_get_network_function(self): self.nf_1.create() @@ -77,16 +75,21 @@ class NetworkFunctionTests(TestCase): self.assertEqual(nf, same_nf) def test_delete_network_function(self): - self.nf_1.create() - self.nf_2.create() - sub = Subscription(**{"subscriptionName": "sub"}) + sub = self.app_conf.subscription for nf in [self.nf_1, self.nf_2]: sub.add_network_function_to_subscription(nf, self.app_conf.subscription.get()) - + nfs = NetworkFunction.get_all() + self.assertEqual(2, len(nfs)) NetworkFunction.delete(nf_name=self.nf_1.nf_name) - nfs = NetworkFunction.get_all() self.assertEqual(1, len(nfs)) - self.assertEqual(1, len(Subscription.get_all_nfs_subscription_relations())) - pnf_1_deleted = [nf for nf in nfs if nf.nf_name != self.nf_1.nf_name] - self.assertTrue(pnf_1_deleted) + + @patch('mod.aai_client.get_aai_model_data') + def test_set_sdnc_params_true(self, mock_get_aai_model): + mock_get_aai_model.return_value = self.good_model_info + self.assertTrue(self.nf_1.set_sdnc_params(self.app_conf)) + + @patch('mod.aai_client.get_aai_model_data') + def test_set_sdnc_params_false(self, mock_get_aai_model): + mock_get_aai_model.return_value = self.bad_model_info + self.assertFalse(self.nf_1.set_sdnc_params(self.app_conf)) diff --git a/components/pm-subscription-handler/tests/test_network_function_filter.py b/components/pm-subscription-handler/tests/test_network_function_filter.py index 95c9b7bc..2151925d 100644 --- a/components/pm-subscription-handler/tests/test_network_function_filter.py +++ b/components/pm-subscription-handler/tests/test_network_function_filter.py @@ -20,7 +20,7 @@ import json import os from unittest import TestCase from parameterized import parameterized -from mod.network_function import NetworkFunctionFilter +from mod.network_function import NetworkFunctionFilter, NetworkFunction def custom_name_func(testcase_func, param_num, param): @@ -32,7 +32,8 @@ def custom_name_func(testcase_func, param_num, param): def load_test_cases(): test_parameters = [] - with open(os.path.join(os.path.dirname(__file__), 'data/filter_test_data.json'), 'r') as test_data: + with open(os.path.join(os.path.dirname(__file__), + 'data/filter_test_data.json'), 'r') as test_data: loaded_test_data = json.load(test_data) for test in loaded_test_data: params = [value for key, value in test.items()] @@ -43,44 +44,44 @@ def load_test_cases(): class NetworkFunctionFilterTest(TestCase): @parameterized.expand(load_test_cases, name_func=custom_name_func) - def test(self, test_name, nf_filter, nf_name, model_invariant_uuid, model_version_id, orchestration_status, + def test(self, test_name, nf_filter, nf_name, model_invariant_uuid, model_version_id, expected_result): nf_filter = NetworkFunctionFilter(**nf_filter) - self.assertEqual(nf_filter.is_nf_in_filter(nf_name, - model_invariant_uuid, - model_version_id, - orchestration_status), expected_result) + self.assertEqual(nf_filter.is_nf_in_filter(NetworkFunction(nf_name=nf_name, + model_invariant_id=model_invariant_uuid, + model_version_id=model_version_id)), + expected_result) - def test_filter_true_on_multiple_modelInvariantUUIDs(self): + def test_filter_true_on_multiple_modelInvariantIDs(self): nf_filter = NetworkFunctionFilter(**{ "nfNames": [ ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ '5845y423-g654-6fju-po78-8n53154532k6', '7129e420-d396-4efb-af02-6b83499b12f8' ], "modelVersionIDs": [ ] }) - self.assertTrue(nf_filter.is_nf_in_filter('pnf1', - '7129e420-d396-4efb-af02-6b83499b12f8', - 'e80a6ae3-cafd-4d24-850d-e14c084a5ca9', - 'Active')) + self.assertTrue(nf_filter.is_nf_in_filter( + NetworkFunction(nf_name='pnf1', + model_invariant_id='7129e420-d396-4efb-af02-6b83499b12f8', + model_version_id='e80a6ae3-cafd-4d24-850d-e14c084a5ca9'))) - def test_filter_false_on_modelInvariantUUIDs_being_false_and_pnfname_being_true(self): + def test_filter_false_on_modelInvariantIDs_being_false_and_pnfname_being_true(self): nf_filter = NetworkFunctionFilter(**{ "nfNames": [ "^pnf.*", "^vnf.*" ], - "modelInvariantUUIDs": [ + "modelInvariantIDs": [ '5845y423-g654-6fju-po78-8n53154532k6', '7129e420-d396-4efb-af02-6b83499b12f8' ], "modelVersionIDs": [ ] }) - self.assertFalse(nf_filter.is_nf_in_filter('pnf1', - 'WrongModelInvariantUUID', - 'e80a6ae3-cafd-4d24-850d-e14c084a5ca9', - 'Active'))
\ No newline at end of file + self.assertFalse(nf_filter.is_nf_in_filter( + NetworkFunction(nf_name='pnf1', + model_invariant_id='WrongModelInvariantUUID', + model_version_id='e80a6ae3-cafd-4d24-850d-e14c084a5ca9'))) diff --git a/components/pm-subscription-handler/tests/test_pmsh_utils.py b/components/pm-subscription-handler/tests/test_pmsh_utils.py index 6e5585f5..1bc039d2 100644 --- a/components/pm-subscription-handler/tests/test_pmsh_utils.py +++ b/components/pm-subscription-handler/tests/test_pmsh_utils.py @@ -15,88 +15,67 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== -import json -import os from test.support import EnvironmentVarGuard -from unittest import TestCase -from unittest.mock import patch +from unittest.mock import patch, Mock import responses from requests import Session from tenacity import RetryError -from mod import db, get_db_connection_url, create_app +from mod import get_db_connection_url +from mod.network_function import NetworkFunction from mod.pmsh_utils import AppConfig +from tests.base_setup import BaseClassSetup +from tests.base_setup import get_pmsh_config -class PmshUtilsTestCase(TestCase): +class PmshUtilsTestCase(BaseClassSetup): - @patch('mod.update_logging_config') - @patch('mod.create_app') - @patch('mod.get_db_connection_url') - def setUp(self, mock_get_db_url, mock_app, mock_update_config): - mock_get_db_url.return_value = 'sqlite://' - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - self.env = EnvironmentVarGuard() - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - self.mock_app = mock_app - self.app = create_app() - self.app_context = self.app.app_context() - self.app_context.push() - db.create_all() - - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_utils_get_mr_sub(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + @classmethod + def setUpClass(cls): + super().setUpClass() + + def setUp(self): + super().setUp() + self.mock_app = Mock() + + def tearDown(self): + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + + def test_utils_get_mr_sub(self): mr_policy_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') self.assertTrue(mr_policy_sub.aaf_id, 'dcae@dcae.onap.org') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_utils_get_mr_sub_fails_with_invalid_name(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_utils_get_mr_sub_fails_with_invalid_name(self): with self.assertRaises(KeyError): self.app_conf.get_mr_sub('invalid_sub') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_utils_get_mr_pub(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_utils_get_mr_pub(self): mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') self.assertTrue(mr_policy_pub.aaf_pass, 'demo123456!') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_utils_get_mr_pub_fails_with_invalid_name(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_utils_get_mr_pub_fails_with_invalid_name(self): with self.assertRaises(KeyError): self.app_conf.get_mr_pub('invalid_pub') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_utils_get_cert_data(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_utils_get_cert_data(self): self.assertEqual(self.app_conf.cert_params, ('/opt/app/pmsh/etc/certs/cert.pem', '/opt/app/pmsh/etc/certs/key.pem')) - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @patch.object(Session, 'post') - def test_mr_pub_publish_to_topic_success(self, mock_session, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_mr_pub_publish_to_topic_success(self, mock_session): mock_session.return_value.status_code = 200 mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') with patch('requests.Session.post') as session_post_call: mr_policy_pub.publish_to_topic({"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) session_post_call.assert_called_once() - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @responses.activate - def test_mr_pub_publish_to_topic_fail(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_mr_pub_publish_to_topic_fail(self): responses.add(responses.POST, 'https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS', json={'error': 'Client Error'}, status=400) @@ -104,21 +83,19 @@ class PmshUtilsTestCase(TestCase): with self.assertRaises(Exception): mr_policy_pub.publish_to_topic({"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def test_mr_pub_publish_sub_event_data_success(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_mr_pub_publish_sub_event_data_success(self): mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') with patch('mod.pmsh_utils._MrPub.publish_to_topic') as pub_to_topic_call: - mr_policy_pub.publish_subscription_event_data(self.app_conf.subscription, 'pnf201', - self.app_conf) + mr_policy_pub.publish_subscription_event_data( + self.app_conf.subscription, + NetworkFunction(nf_name='pnf_1', + model_invariant_id='some-id', + model_version_id='some-id'), + self.app_conf) pub_to_topic_call.assert_called_once() - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @responses.activate - def test_mr_sub_get_from_topic_success(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_mr_sub_get_from_topic_success(self): policy_mr_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') responses.add(responses.GET, 'https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS/' @@ -127,11 +104,8 @@ class PmshUtilsTestCase(TestCase): mr_topic_data = policy_mr_sub.get_from_topic(1) self.assertIsNotNone(mr_topic_data) - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @responses.activate - def test_mr_sub_get_from_topic_fail(self, mock_get_pmsh_config): - mock_get_pmsh_config.return_value = self.cbs_data - self.app_conf = AppConfig() + def test_mr_sub_get_from_topic_fail(self): policy_mr_sub = self.app_conf.get_mr_sub('policy_pm_subscriber') responses.add(responses.GET, 'https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS/' @@ -158,15 +132,14 @@ class PmshUtilsTestCase(TestCase): @patch('mod.logger.info') @patch('mod.pmsh_utils.get_all') def test_refresh_config_success(self, mock_cbs_client_get_all, mock_logger): - mock_cbs_client_get_all.return_value = self.cbs_data - self.app_conf = AppConfig() + mock_cbs_client_get_all.return_value = get_pmsh_config() self.app_conf.refresh_config() mock_logger.assert_called_with('AppConfig data has been refreshed') @patch('mod.logger.error') @patch('mod.pmsh_utils.get_all') def test_refresh_config_fail(self, mock_cbs_client_get_all, mock_logger): - mock_cbs_client_get_all.return_value = self.cbs_data + mock_cbs_client_get_all.return_value = get_pmsh_config() self.app_conf = AppConfig() mock_cbs_client_get_all.side_effect = Exception with self.assertRaises(RetryError): diff --git a/components/pm-subscription-handler/tests/test_policy_response_handler.py b/components/pm-subscription-handler/tests/test_policy_response_handler.py index 9dd73ee0..26a06fce 100644 --- a/components/pm-subscription-handler/tests/test_policy_response_handler.py +++ b/components/pm-subscription-handler/tests/test_policy_response_handler.py @@ -15,36 +15,37 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== -import json -import os -from unittest import TestCase from unittest.mock import patch from mod.api.db_models import SubscriptionModel from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig from mod.policy_response_handler import PolicyResponseHandler, policy_response_handle_functions from mod.subscription import AdministrativeState, SubNfState +from tests.base_setup import BaseClassSetup -class PolicyResponseHandlerTest(TestCase): +class PolicyResponseHandlerTest(BaseClassSetup): + + @classmethod + def setUpClass(cls): + super().setUpClass() - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @patch('mod.create_app') - @patch('mod.subscription.Subscription') @patch('mod.pmsh_utils._MrSub') - def setUp(self, mock_mr_sub, mock_sub, mock_app, mock_get_app_conf): - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) + def setUp(self, mock_mr_sub, mock_app): + super().setUp() self.mock_policy_mr_sub = mock_mr_sub - mock_get_app_conf.return_value = self.cbs_data - self.app_conf = AppConfig() - self.sub = self.app_conf.subscription - self.mock_app = mock_app self.nf = NetworkFunction(nf_name='nf1') self.policy_response_handler = PolicyResponseHandler(self.mock_policy_mr_sub, self.app_conf, - self.mock_app) + mock_app) + + def tearDown(self): + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() @patch('mod.network_function.NetworkFunction.delete') def test_handle_response_locked_success(self, mock_delete): diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py index c7845e74..9bfe825f 100755 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -17,54 +17,45 @@ # ============LICENSE_END===================================================== import json import os -from test.support import EnvironmentVarGuard -from unittest import TestCase -from unittest.mock import patch +from unittest.mock import patch, Mock from requests import Session import mod.aai_client as aai_client -from mod import db, create_app -from mod.api.db_models import NetworkFunctionModel from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig from mod.subscription import Subscription +from tests.base_setup import BaseClassSetup -class SubscriptionTest(TestCase): - @patch('mod.update_logging_config') - @patch('mod.pmsh_utils._MrPub') - @patch('mod.pmsh_utils._MrSub') - @patch('mod.get_db_connection_url') +class SubscriptionTest(BaseClassSetup): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + @patch.object(Session, 'get') @patch.object(Session, 'put') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') - def setUp(self, mock_get_pmsh_config, mock_session, mock_get_db_url, - mock_mr_sub, mock_mr_pub, mock_update_config): - mock_get_db_url.return_value = 'sqlite://' + def setUp(self, mock_session_put, mock_session_get): + super().setUp() with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: self.aai_response_data = data.read() - mock_session.return_value.status_code = 200 - mock_session.return_value.text = self.aai_response_data - self.env = EnvironmentVarGuard() - self.env.set('AAI_SERVICE_PORT', '8443') - self.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data - self.mock_mr_sub = mock_mr_sub - self.mock_mr_pub = mock_mr_pub - self.app = create_app() - self.app_context = self.app.app_context() - self.app_context.push() - db.create_all() - self.app_conf = AppConfig() + mock_session_put.return_value.status_code = 200 + mock_session_put.return_value.text = self.aai_response_data + with open(os.path.join(os.path.dirname(__file__), 'data/aai_model_info.json'), 'r') as data: + self.aai_model_data = data.read() + mock_session_get.return_value.status_code = 200 + mock_session_get.return_value.text = self.aai_model_data + self.mock_mr_sub = Mock() + self.mock_mr_pub = Mock() self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf) self.sub_model = self.app_conf.subscription.get() def tearDown(self): - db.drop_all() - db.session.remove() - self.app_context.pop() + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() def test_sub_measurement_group(self): self.assertEqual(len(self.app_conf.subscription.measurementGroups), 2) @@ -84,8 +75,6 @@ class SubscriptionTest(TestCase): self.sub_model) self.app_conf.subscription.add_network_function_to_subscription(list(self.xnfs)[1], self.sub_model) - nfs = Subscription.get_nf_names_per_sub(self.app_conf.subscription.subscriptionName) - self.assertEqual(2, len(nfs)) def test_create_existing_subscription(self): sub1 = self.app_conf.subscription.create() @@ -121,16 +110,6 @@ class SubscriptionTest(TestCase): self.assertEqual('new_status', sub.status) - def test_delete_subscription(self): - for nf in self.xnfs: - self.app_conf.subscription.add_network_function_to_subscription(nf, self.sub_model) - self.app_conf.subscription.delete_subscription() - self.assertEqual(0, len(Subscription.get_all())) - self.assertEqual(None, self.app_conf.subscription.get()) - self.assertEqual(0, len(Subscription.get_all_nfs_subscription_relations())) - self.assertEqual(0, len(NetworkFunction.get_all())) - self.assertEqual(None, NetworkFunction.get(nf_name=list(self.xnfs)[0].nf_name)) - def test_update_sub_nf_status(self): sub_name = 'ExtraPM-All-gNB-R2B' for nf in self.xnfs: @@ -172,18 +151,15 @@ class SubscriptionTest(TestCase): with open(os.path.join(os.path.dirname(__file__), 'data/pm_subscription_event.json'), 'r') as data: expected_sub_event = json.load(data) - actual_sub_event = self.app_conf.subscription.prepare_subscription_event('pnf_1', - self.app_conf) + nf = NetworkFunction(nf_name='pnf_1', + model_invariant_id='some-id', + model_version_id='some-id') + nf.sdnc_model_name = 'some-name' + nf.sdnc_model_version = 'some-version' + actual_sub_event = self.app_conf.subscription.prepare_subscription_event(nf, self.app_conf) + print(actual_sub_event) self.assertEqual(expected_sub_event, actual_sub_event) - def test_get_nf_models(self): - for nf in self.xnfs: - self.app_conf.subscription.add_network_function_to_subscription(nf, self.sub_model) - nf_models = self.app_conf.subscription._get_nf_models() - - self.assertEqual(3, len(nf_models)) - self.assertIsInstance(nf_models[0], NetworkFunctionModel) - def test_get_network_functions(self): for nf in self.xnfs: self.app_conf.subscription.add_network_function_to_subscription(nf, self.sub_model) diff --git a/components/pm-subscription-handler/tests/test_subscription_handler.py b/components/pm-subscription-handler/tests/test_subscription_handler.py index a6611666..bf72382a 100644 --- a/components/pm-subscription-handler/tests/test_subscription_handler.py +++ b/components/pm-subscription-handler/tests/test_subscription_handler.py @@ -15,50 +15,39 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== -import json -import os -from test.support import EnvironmentVarGuard -from unittest import TestCase -from unittest.mock import patch +from unittest.mock import patch, Mock -from mod import create_app, db from mod.network_function import NetworkFunction -from mod.pmsh_utils import AppConfig from mod.subscription import AdministrativeState from mod.subscription_handler import SubscriptionHandler +from tests.base_setup import BaseClassSetup -class SubscriptionHandlerTest(TestCase): +class SubscriptionHandlerTest(BaseClassSetup): @classmethod def setUpClass(cls): - cls.env = EnvironmentVarGuard() - cls.env.set('AAI_SERVICE_PORT', '8443') - cls.env.set('LOGGER_CONFIG', os.path.join(os.path.dirname(__file__), 'log_config.yaml')) - cls.nfs = [NetworkFunction(nf_name='pnf_1'), NetworkFunction(nf_name='pnf_2')] + super().setUpClass() - @patch('mod.get_db_connection_url') - @patch('mod.update_logging_config') - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config') @patch('mod.pmsh_utils._MrPub') - @patch('mod.pmsh_utils.PeriodicTask') - def setUp(self, mock_periodic_task, mock_mr_pub, mock_get_pmsh_config, mock_update_config, - mock_get_db_url): - mock_get_db_url.return_value = 'sqlite://' - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: - self.cbs_data = json.load(data) - mock_get_pmsh_config.return_value = self.cbs_data + def setUp(self, mock_mr_pub): + super().setUp() + self.nfs = [NetworkFunction(nf_name='pnf_1', + model_invariant_id='some-id', + model_version_id='some-id'), + NetworkFunction(nf_name='pnf_2', + model_invariant_id='some-id', + model_version_id='some-id')] self.mock_mr_pub = mock_mr_pub - self.mock_aai_event_thread = mock_periodic_task - self.mock_policy_event_thread = mock_periodic_task - self.app = create_app() - self.app.app_context().push() - db.create_all() - self.app_conf = AppConfig() + self.mock_aai_event_thread = Mock() + self.mock_policy_event_thread = Mock() def tearDown(self): - db.drop_all() - db.session.remove() + super().tearDown() + + @classmethod + def tearDownClass(cls): + super().tearDownClass() @patch('mod.subscription.Subscription.get_local_sub_admin_state') @patch('mod.logger.info') |