diff options
Diffstat (limited to 'components/pm-subscription-handler/tests')
17 files changed, 135 insertions, 1085 deletions
diff --git a/components/pm-subscription-handler/tests/base_setup.py b/components/pm-subscription-handler/tests/base_setup.py index 14f813d4..90919ff1 100755 --- a/components/pm-subscription-handler/tests/base_setup.py +++ b/components/pm-subscription-handler/tests/base_setup.py @@ -24,9 +24,7 @@ from unittest.mock import patch, MagicMock from mod import create_app, db from mod.api.db_models import NetworkFunctionFilterModel, MeasurementGroupModel, \ SubscriptionModel, NetworkFunctionModel, NfSubRelationalModel -from mod.network_function import NetworkFunctionFilter -from mod.pmsh_utils import AppConfig -from mod.pmsh_config import AppConfig as NewAppConfig +from mod.pmsh_config import AppConfig def get_pmsh_config(file_path='data/cbs_data_1.json'): @@ -124,16 +122,11 @@ class BaseClassSetup(TestCase): 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())) + @patch('mod.pmsh_config.AppConfig._get_config', MagicMock(return_value=get_pmsh_config())) def setUp(self): os.environ['AAI_SERVICE_PORT'] = '8443' db.create_all() self.app_conf = AppConfig() - self.app_conf.nf_filter = NetworkFunctionFilter(**self.app_conf.subscription.nfFilter) - - @patch('mod.pmsh_config.AppConfig._get_config', MagicMock(return_value=get_pmsh_config())) - def setUpAppConf(self): - self.pmsh_app_conf = NewAppConfig() def tearDown(self): db.drop_all() 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 f872bb50..6efda4f8 100644 --- a/components/pm-subscription-handler/tests/data/cbs_data_1.json +++ b/components/pm-subscription-handler/tests/data/cbs_data_1.json @@ -8,71 +8,6 @@ "key_path":"/opt/app/pmsh/etc/certs/key.pem", "ca_cert_path":"/opt/app/pmsh/etc/certs/cacert.pem", "enable_tls":"true", - "pmsh_policy":{ - "subscription":{ - "subscriptionName":"ExtraPM-All-gNB-R2B", - "administrativeState":"UNLOCKED", - "fileBasedGP":15, - "fileLocation":"\/pm\/pm.xml", - "nfFilter":{ - "nfNames":[ - "^pnf.*", - "^vnf.*" - ], - "modelInvariantIDs":[ - - ], - "modelVersionIDs":[ - - ], - "modelNames":[ - - ] - }, - "measurementGroups":[ - { - "measurementGroup":{ - "measurementTypes":[ - { - "measurementType":"countera" - }, - { - "measurementType":"counterb" - } - ], - "managedObjectDNsBasic":[ - { - "DN":"dna" - }, - { - "DN":"dnb" - } - ] - } - }, - { - "measurementGroup":{ - "measurementTypes":[ - { - "measurementType":"counterc" - }, - { - "measurementType":"counterd" - } - ], - "managedObjectDNsBasic":[ - { - "DN":"dnc" - }, - { - "DN":"dnd" - } - ] - } - } - ] - } - }, "streams_subscribes":{ "aai_subscriber":{ "type":"message_router", @@ -114,4 +49,4 @@ } } } -}
\ No newline at end of file +} diff --git a/components/pm-subscription-handler/tests/data/cbs_invalid_data.json b/components/pm-subscription-handler/tests/data/cbs_invalid_data.json deleted file mode 100644 index a6f63680..00000000 --- a/components/pm-subscription-handler/tests/data/cbs_invalid_data.json +++ /dev/null @@ -1,116 +0,0 @@ -{ - "config":{ - "control_loop_name":"pmsh-control-loop", - "operational_policy_name":"pmsh-operational-policy", - "aaf_password":"demo123456!", - "aaf_identity":"dcae@dcae.onap.org", - "cert_path":"/opt/app/pmsh/etc/certs/cert.pem", - "key_path":"/opt/app/pmsh/etc/certs/key.pem", - "ca_cert_path":"/opt/app/pmsh/etc/certs/cacert.pem", - "enable_tls":"true", - "pmsh_policy":{ - "subscription":{ - "subscriptionName":"ExtraPM-All-gNB-R2B", - "administrativeState":"UNLOCKED", - "fileBasedGP":15, - "fileLocation":"\/pm\/pm.xml", - "nfFilter":{ - "nfNames":[ - - ], - "modelInvariantIDs":[ - - ], - "modelVersionIDs":[ - - ], - "modelNames":[ - - ] - }, - "measurementGroups":[ - { - "measurementGroup":{ - "measurementTypes":[ - { - "measurementType":"countera" - }, - { - "measurementType":"counterb" - } - ], - "managedObjectDNsBasic":[ - { - "DN":"dna" - }, - { - "DN":"dnb" - } - ] - } - }, - { - "measurementGroup":{ - "measurementTypes":[ - { - "measurementType":"counterc" - }, - { - "measurementType":"counterd" - } - ], - "managedObjectDNsBasic":[ - { - "DN":"dnc" - }, - { - "DN":"dnd" - } - ] - } - } - ] - } - }, - "streams_subscribes":{ - "aai_subscriber":{ - "type":"message_router", - "dmaap_info":{ - "topic_url":"https://message-router:3905/events/AAI_EVENT", - "client_role":"org.onap.dcae.aaiSub", - "location":"san-francisco", - "client_id":"1575976809466" - } - }, - "policy_pm_subscriber":{ - "type":"message_router", - "dmaap_info":{ - "topic_url":"https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS", - "client_role":"org.onap.dcae.pmSubscriber", - "location":"san-francisco", - "client_id":"1575876809456" - } - } - }, - "streams_publishes":{ - "policy_pm_publisher":{ - "type":"message_router", - "dmaap_info":{ - "topic_url":"https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS", - "client_role":"org.onap.dcae.pmPublisher", - "location":"san-francisco", - "client_id":"1475976809466" - } - }, - "other_publisher":{ - "type":"message_router", - "dmaap_info":{ - "topic_url":"https://message-router:3905/events/org.onap.dmaap.mr.SOME_OTHER_TOPIC", - "client_role":"org.onap.dcae.pmControlPub", - "location":"san-francisco", - "client_id":"1875976809466" - } - } - } - } -}
\ 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 deleted file mode 100755 index 6a7fe340..00000000 --- a/components/pm-subscription-handler/tests/data/pm_subscription_event.json +++ /dev/null @@ -1,57 +0,0 @@ -{
- "nfName":"pnf_1",
- "blueprintName": "some-name",
- "blueprintVersion": "some-version",
- "operationalPolicyName":"pmsh-operational-policy",
- "changeType":"CREATE",
- "controlLoopName":"pmsh-control-loop",
- "ipAddress": "204.120.0.15",
- "subscription":{
- "subscriptionName":"ExtraPM-All-gNB-R2B",
- "administrativeState":"UNLOCKED",
- "fileBasedGP":15,
- "fileLocation":"/pm/pm.xml",
- "measurementGroups":[
- {
- "measurementGroup":{
- "measurementTypes":[
- {
- "measurementType":"countera"
- },
- {
- "measurementType":"counterb"
- }
- ],
- "managedObjectDNsBasic":[
- {
- "DN":"dna"
- },
- {
- "DN":"dnb"
- }
- ]
- }
- },
- {
- "measurementGroup":{
- "measurementTypes":[
- {
- "measurementType":"counterc"
- },
- {
- "measurementType":"counterd"
- }
- ],
- "managedObjectDNsBasic":[
- {
- "DN":"dnc"
- },
- {
- "DN":"dnd"
- }
- ]
- }
- }
- ]
- }
-}
diff --git a/components/pm-subscription-handler/tests/services/test_measurement_group_service.py b/components/pm-subscription-handler/tests/services/test_measurement_group_service.py index 1dbe84a9..25ab2581 100644 --- a/components/pm-subscription-handler/tests/services/test_measurement_group_service.py +++ b/components/pm-subscription-handler/tests/services/test_measurement_group_service.py @@ -21,6 +21,7 @@ import os from unittest.mock import patch from mod.api.custom_exception import InvalidDataException, DataConflictException +from mod.api.services.measurement_group_service import MgNfState from mod.network_function import NetworkFunction, NetworkFunctionFilter from mod.pmsh_config import AppConfig from mod import db, aai_client @@ -29,13 +30,9 @@ from tests.base_setup import BaseClassSetup, create_subscription_data, \ from mod.api.services import measurement_group_service, nf_service from mod.api.db_models import MeasurementGroupModel, NfMeasureGroupRelationalModel, \ SubscriptionModel, NetworkFunctionModel -from mod.subscription import SubNfState class MeasurementGroupServiceTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() def setUp(self): super().setUp() @@ -48,16 +45,9 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): 'r') as data: self.good_model_info = data.read() - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - @patch.object(AppConfig, 'publish_to_topic') def test_publish_measurement_group(self, mock_mr): - super().setUpAppConf() + super().setUp() nf_1 = NetworkFunction(**{'nf_name': 'pnf_1', 'ipv4_address': '204.120.0.15', 'ipv6_address': '2001:db8:3333:4444:5555:6666:7777:8888', @@ -104,38 +94,38 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): def test_apply_nf_to_measurement_group_status(self): measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test", "measure_grp_name", SubNfState.PENDING_CREATE.value) + "pnf_test", "measure_grp_name", MgNfState.PENDING_CREATE.value) db.session.commit() measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'measure_grp_name', NfMeasureGroupRelationalModel.nf_name == 'pnf_test').one_or_none()) self.assertIsNotNone(measurement_grp_rel) self.assertEqual(measurement_grp_rel.nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) def test_update_measurement_group_nf_status(self): measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test", "measure_grp_name", SubNfState.PENDING_CREATE.value) + "pnf_test", "measure_grp_name", MgNfState.PENDING_CREATE.value) measurement_group_service.update_measurement_group_nf_status( - "measure_grp_name", SubNfState.CREATED.value, "pnf_test") + "measure_grp_name", MgNfState.CREATED.value, "pnf_test") db.session.commit() measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'measure_grp_name', NfMeasureGroupRelationalModel.nf_name == 'pnf_test').one_or_none()) self.assertIsNotNone(measurement_grp_rel) self.assertEqual(measurement_grp_rel.nf_measure_grp_status, - SubNfState.CREATED.value) + MgNfState.CREATED.value) def test_delete_nf_to_measurement_group_without_nf_delete(self): nf = NetworkFunction(nf_name='pnf_test1') nf_service.save_nf(nf) db.session.commit() measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test1", "measure_grp_name1", SubNfState.PENDING_CREATE.value) + "pnf_test1", "measure_grp_name1", MgNfState.PENDING_CREATE.value) measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test1", "measure_grp_name2", SubNfState.PENDING_CREATE.value) + "pnf_test1", "measure_grp_name2", MgNfState.PENDING_CREATE.value) measurement_group_service.delete_nf_to_measurement_group( - "pnf_test1", "measure_grp_name1", SubNfState.DELETED.value) + "pnf_test1", "measure_grp_name1", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'measure_grp_name1', NfMeasureGroupRelationalModel.nf_name == 'pnf_test1').one_or_none()) @@ -149,9 +139,9 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): nf_service.save_nf(nf) db.session.commit() measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test2", "measure_grp_name2", SubNfState.PENDING_CREATE.value) + "pnf_test2", "measure_grp_name2", MgNfState.PENDING_CREATE.value) measurement_group_service.delete_nf_to_measurement_group( - "pnf_test2", "measure_grp_name2", SubNfState.DELETED.value) + "pnf_test2", "measure_grp_name2", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'measure_grp_name2', NfMeasureGroupRelationalModel.nf_name == 'pnf_test2').one_or_none()) @@ -167,10 +157,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): nf_service.save_nf(nf) db.session.commit() measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test2", "measure_grp_name2", SubNfState.PENDING_CREATE.value) + "pnf_test2", "measure_grp_name2", MgNfState.PENDING_CREATE.value) nf_delete_func.side_effect = Exception('delete failed') measurement_group_service.delete_nf_to_measurement_group( - "pnf_test2", "measure_grp_name2", SubNfState.DELETED.value) + "pnf_test2", "measure_grp_name2", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'measure_grp_name2', NfMeasureGroupRelationalModel.nf_name == 'pnf_test2').one_or_none()) @@ -186,7 +176,7 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): def test_update_nf_to_measurement_group_failure(self, mock_logger, db_commit_call): db_commit_call.side_effect = Exception('update failed') measurement_group_service.update_measurement_group_nf_status( - "measure_grp_name2", SubNfState.CREATE_FAILED.value, "pnf_test2") + "measure_grp_name2", MgNfState.CREATE_FAILED.value, "pnf_test2") mock_logger.assert_called_with('Failed to update nf: pnf_test2 for ' 'measurement group: measure_grp_name2 due to: update failed') @@ -195,9 +185,8 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): subscription = subscription.replace('msrmt_grp_name', new_msrmt_grp_name) return subscription - @patch.object(AppConfig, 'publish_to_topic') - def test_update_admin_status_to_locking(self, mock_mr): - super().setUpAppConf() + def test_update_admin_status_to_locking(self): + super().setUp() sub = create_subscription_data('sub') nf_list = create_multiple_network_function_data(['pnf_101', 'pnf_102']) db.session.add(sub) @@ -206,7 +195,7 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): measurement_group_service. \ apply_nf_status_to_measurement_group(nf.nf_name, sub.measurement_groups[0]. measurement_group_name, - SubNfState.CREATED.value) + MgNfState.CREATED.value) db.session.commit() measurement_group_service.update_admin_status(sub.measurement_groups[0], 'LOCKED') meas_grp = measurement_group_service.query_meas_group_by_name('sub', 'MG1') @@ -217,11 +206,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): NfMeasureGroupRelationalModel.measurement_grp_name == meas_grp.measurement_group_name)\ .all() for nf in meas_group_nfs: - self.assertEqual(nf.nf_measure_grp_status, SubNfState.PENDING_DELETE.value) + self.assertEqual(nf.nf_measure_grp_status, MgNfState.PENDING_DELETE.value) - @patch.object(AppConfig, 'publish_to_topic') - def test_update_admin_status_to_locked(self, mock_mr): - super().setUpAppConf() + def test_update_admin_status_to_locked(self): + super().setUp() sub = create_subscription_data('sub') db.session.add(sub) measurement_group_service.update_admin_status(sub.measurement_groups[0], 'LOCKED') @@ -230,15 +218,14 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): self.assertEqual(meas_grp.measurement_group_name, 'MG1') self.assertEqual(meas_grp.administrative_state, 'LOCKED') - @patch.object(AppConfig, 'publish_to_topic') @patch.object(aai_client, '_get_all_aai_nf_data') @patch.object(aai_client, 'get_aai_model_data') @patch.object(NetworkFunctionFilter, 'get_network_function_filter') def test_update_admin_status_to_unlocked_with_no_nfs(self, mock_filter_call, - mock_model_aai, mock_aai, mock_mr): + mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) - super().setUpAppConf() + super().setUp() sub = create_subscription_data('sub') sub.nfs = [] db.session.add(sub) @@ -253,17 +240,16 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): NfMeasureGroupRelationalModel.measurement_grp_name == meas_grp.measurement_group_name)\ .all() for nf in meas_group_nfs: - self.assertEqual(nf.nf_measure_grp_status, SubNfState.PENDING_CREATE.value) + self.assertEqual(nf.nf_measure_grp_status, MgNfState.PENDING_CREATE.value) - @patch.object(AppConfig, 'publish_to_topic') @patch.object(aai_client, '_get_all_aai_nf_data') @patch.object(aai_client, 'get_aai_model_data') @patch.object(NetworkFunctionFilter, 'get_network_function_filter') def test_update_admin_status_to_unlocking(self, mock_filter_call, - mock_model_aai, mock_aai, mock_mr): + mock_model_aai, mock_aai): mock_aai.return_value = json.loads(self.aai_response_data) mock_model_aai.return_value = json.loads(self.good_model_info) - super().setUpAppConf() + super().setUp() sub = create_subscription_data('sub') db.session.add(sub) db.session.commit() @@ -277,7 +263,7 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): NfMeasureGroupRelationalModel.measurement_grp_name == meas_grp.measurement_group_name)\ .all() for nf in meas_group_nfs: - self.assertEqual(nf.nf_measure_grp_status, SubNfState.PENDING_CREATE.value) + self.assertEqual(nf.nf_measure_grp_status, MgNfState.PENDING_CREATE.value) def test_update_admin_status_for_missing_measurement_group(self): try: @@ -287,7 +273,7 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): 'for admin status update') def test_update_admin_status_for_data_conflict(self): - super().setUpAppConf() + super().setUp() sub = create_subscription_data('sub1') sub.measurement_groups[0].administrative_state = 'LOCKING' try: @@ -298,7 +284,7 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): 'meas group name: MG1') def test_update_admin_status_for_same_state(self): - super().setUpAppConf() + super().setUp() sub = create_subscription_data('sub1') try: measurement_group_service.update_admin_status(sub.measurement_groups[0], 'UNLOCKED') @@ -317,10 +303,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): measurement_group_service. \ apply_nf_status_to_measurement_group(nf.nf_name, sub.measurement_groups[1]. measurement_group_name, - SubNfState.PENDING_DELETE.value) + MgNfState.PENDING_DELETE.value) db.session.commit() measurement_group_service.lock_nf_to_meas_grp( - "pnf_101", "MG2", SubNfState.DELETED.value) + "pnf_101", "MG2", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'MG2', NfMeasureGroupRelationalModel.nf_name == 'pnf_101').one_or_none()) @@ -343,10 +329,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): measurement_group_service. \ apply_nf_status_to_measurement_group(nf.nf_name, sub.measurement_groups[1]. measurement_group_name, - SubNfState.PENDING_DELETE.value) + MgNfState.PENDING_DELETE.value) db.session.commit() measurement_group_service.lock_nf_to_meas_grp( - "pnf_101", "MG2", SubNfState.DELETED.value) + "pnf_101", "MG2", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'MG2', NfMeasureGroupRelationalModel.nf_name == 'pnf_101').one_or_none()) @@ -365,10 +351,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): nf = NetworkFunction(nf_name='pnf_test2') nf_service.save_nf(nf) measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test2", "MG2", SubNfState.PENDING_DELETE.value) + "pnf_test2", "MG2", MgNfState.PENDING_DELETE.value) db.session.commit() measurement_group_service.filter_nf_to_meas_grp( - "pnf_test2", "MG2", SubNfState.DELETED.value) + "pnf_test2", "MG2", MgNfState.DELETED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'MG2', NfMeasureGroupRelationalModel.nf_name == 'pnf_test2').one_or_none()) @@ -387,10 +373,10 @@ class MeasurementGroupServiceTestCase(BaseClassSetup): nf = NetworkFunction(nf_name='pnf_test2') nf_service.save_nf(nf) measurement_group_service.apply_nf_status_to_measurement_group( - "pnf_test2", "MG2", SubNfState.PENDING_CREATE.value) + "pnf_test2", "MG2", MgNfState.PENDING_CREATE.value) db.session.commit() measurement_group_service.filter_nf_to_meas_grp( - "pnf_test2", "MG2", SubNfState.CREATED.value) + "pnf_test2", "MG2", MgNfState.CREATED.value) measurement_grp_rel = (NfMeasureGroupRelationalModel.query.filter( NfMeasureGroupRelationalModel.measurement_grp_name == 'MG2', NfMeasureGroupRelationalModel.nf_name == 'pnf_test2').one_or_none()) diff --git a/components/pm-subscription-handler/tests/services/test_nf_service.py b/components/pm-subscription-handler/tests/services/test_nf_service.py index d5824992..2297ff8d 100644 --- a/components/pm-subscription-handler/tests/services/test_nf_service.py +++ b/components/pm-subscription-handler/tests/services/test_nf_service.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2021 Nordix Foundation. +# Copyright (C) 2021-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -28,9 +28,6 @@ from mod.network_function import NetworkFunctionFilter class NetworkFunctionServiceTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() def setUp(self): super().setUp() @@ -44,13 +41,6 @@ class NetworkFunctionServiceTestCase(BaseClassSetup): 'r') as data: self.good_model_info = data.read() - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - def create_test_subs(self, new_sub_name, new_msrmt_grp_name): subscription = self.subscription_request.replace('ExtraPM-All-gNB-R2B', new_sub_name) subscription = subscription.replace('msrmt_grp_name', new_msrmt_grp_name) diff --git a/components/pm-subscription-handler/tests/services/test_subscription_service.py b/components/pm-subscription-handler/tests/services/test_subscription_service.py index a0f3297c..4129bd81 100644 --- a/components/pm-subscription-handler/tests/services/test_subscription_service.py +++ b/components/pm-subscription-handler/tests/services/test_subscription_service.py @@ -22,8 +22,8 @@ from unittest.mock import patch, MagicMock from mod.api.db_models import SubscriptionModel, MeasurementGroupModel, \ NfMeasureGroupRelationalModel, NetworkFunctionModel, NfSubRelationalModel, \ convert_db_string_to_list, NetworkFunctionFilterModel +from mod.api.services.measurement_group_service import MgNfState from mod.network_function import NetworkFunctionFilter -from mod.subscription import SubNfState from mod import aai_client, db from mod.api.custom_exception import DuplicateDataException, InvalidDataException, \ DataConflictException @@ -34,9 +34,6 @@ from tests.base_setup import create_multiple_subscription_data class SubscriptionServiceTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() def setUp(self): super().setUp() @@ -49,13 +46,6 @@ class SubscriptionServiceTestCase(BaseClassSetup): 'r') as data: self.good_model_info = data.read() - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - def create_test_subs(self, new_sub_name, new_msrmt_grp_name): subscription = self.subscription_request.replace('ExtraPM-All-gNB-R2B', new_sub_name) subscription = subscription.replace('msrmt_grp_name', new_msrmt_grp_name) @@ -84,7 +74,7 @@ class SubscriptionServiceTestCase(BaseClassSetup): NfMeasureGroupRelationalModel.measurement_grp_name == 'msrmt_grp_name-new')).all() for pubslished_event in msr_grp_nf_rel: self.assertEqual(pubslished_event.nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) @patch('mod.api.services.subscription_service.save_nf_filter', MagicMock(return_value=None)) @patch.object(AppConfig, 'publish_to_topic') @@ -132,6 +122,9 @@ class SubscriptionServiceTestCase(BaseClassSetup): def test_perform_validation_existing_sub(self): try: + subscription = create_subscription_data('ExtraPM-All-gNB-R2B') + db.session.add(subscription) + db.session.commit() subscription_service.create_subscription(json.loads(self.subscription_request) ['subscription']) except DuplicateDataException as exception: @@ -341,10 +334,10 @@ class SubscriptionServiceTestCase(BaseClassSetup): "No value provided for measurement group name") def test_validate_nf_filter_with_no_filter_values(self): - nfFilter = '{"nfNames": [],"modelInvariantIDs": [], ' \ - '"modelVersionIDs": [],"modelNames": []}' + nf_filter = '{"nfNames": [],"modelInvariantIDs": [], ' \ + '"modelVersionIDs": [],"modelNames": []}' try: - subscription_service.validate_nf_filter(json.loads(nfFilter)) + subscription_service.validate_nf_filter(json.loads(nf_filter)) except InvalidDataException as invalidEx: self.assertEqual(invalidEx.args[0], "At least one filter within nfFilter must not be empty") @@ -423,10 +416,10 @@ class SubscriptionServiceTestCase(BaseClassSetup): self.assertEqual(len(meas_group_nfs), 2) self.assertEqual(meas_group_nfs[0].nf_name, 'pnf201') self.assertEqual(meas_group_nfs[0].nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) self.assertEqual(meas_group_nfs[1].nf_name, 'pnf_33_ericsson') self.assertEqual(meas_group_nfs[1].nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) meas_grp = measurement_group_service.query_meas_group_by_name('sub_01', 'msg_01') self.assertEqual(meas_grp.administrative_state, 'UNLOCKED') # Creating test data for update filter function @@ -442,13 +435,13 @@ class SubscriptionServiceTestCase(BaseClassSetup): .all() self.assertEqual(meas_group_nfs[0].nf_name, 'pnf201') self.assertEqual(meas_group_nfs[0].nf_measure_grp_status, - SubNfState.PENDING_DELETE.value) + MgNfState.PENDING_DELETE.value) self.assertEqual(meas_group_nfs[1].nf_name, 'pnf_33_ericsson') self.assertEqual(meas_group_nfs[1].nf_measure_grp_status, - SubNfState.PENDING_DELETE.value) + MgNfState.PENDING_DELETE.value) self.assertEqual(meas_group_nfs[2].nf_name, 'xnf111') self.assertEqual(meas_group_nfs[2].nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) meas_grp = measurement_group_service.query_meas_group_by_name('sub_01', 'msg_01') self.assertEqual(meas_grp.administrative_state, 'FILTERING') 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 5fc38c52..ba0e5e9d 100755 --- a/components/pm-subscription-handler/tests/test_aai_event_handler.py +++ b/components/pm-subscription-handler/tests/test_aai_event_handler.py @@ -20,31 +20,26 @@ import json from os import path from unittest.mock import patch, MagicMock from mod.aai_event_handler import AAIEventHandler -from mod.api.db_models import NetworkFunctionModel, NetworkFunctionFilterModel, \ - MeasurementGroupModel, SubscriptionModel -from mod.subscription import AdministrativeState -from tests.base_setup import BaseClassSetup +from mod.api.db_models import NetworkFunctionModel +from tests.base_setup import BaseClassSetup, create_subscription_data from mod import db class AAIEventHandlerTest(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() def setUp(self): super().setUp() - super().setUpAppConf() + subscription = create_subscription_data('aai_event_handler') + subscription.measurement_groups[1].administravtive_sate = 'UNLOCKED' + db.session.add(subscription) + db.session.add(subscription.measurement_groups[0]) + db.session.add(subscription.measurement_groups[1]) + db.session.add(subscription.network_filter) + db.session.add(subscription.nfs[0]) + db.session.commit() with open(path.join(path.dirname(__file__), 'data/mr_aai_events.json'), 'r') as data: self.mr_aai_events = json.load(data)["mr_response"] - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - @patch('mod.pmsh_config.AppConfig.get_from_topic') @patch('mod.network_function.NetworkFunction.set_nf_model_params') @patch('mod.network_function.NetworkFunction.delete') @@ -87,16 +82,14 @@ class AAIEventHandlerTest(BaseClassSetup): apply_nfs_to_measure_grp): mock_set_sdnc_params.return_value = True mr_aai_mock.return_value = self.mr_aai_events - aai_handler = AAIEventHandler(self.app) - subscription = SubscriptionModel(subscription_name='ExtraPM-All-gNB-R2B2', - operational_policy_name='operation_policy', - control_loop_name="control-loop", - status=AdministrativeState.UNLOCKED.value) - db.session.add(subscription) - db.session.commit() - generate_nf_filter_measure_grp('ExtraPM-All-gNB-R2B', 'msr_grp_name') - generate_nf_filter_measure_grp('ExtraPM-All-gNB-R2B2', 'msr_grp_name2') + nf_to_subscription = create_subscription_data('nf_to_subscription') + nf_to_subscription.measurement_groups[0].measurement_group_name = 'NF_MG_ONE' + nf_to_subscription.measurement_groups[1].measurement_group_name = 'NF_MG_TWO' + db.session.add(nf_to_subscription) + db.session.add(nf_to_subscription.measurement_groups[0]) + db.session.add(nf_to_subscription.network_filter) db.session.commit() + aai_handler = AAIEventHandler(self.app) aai_handler.execute() self.assertEqual(apply_nfs_to_measure_grp.call_count, 2) @@ -111,11 +104,9 @@ class AAIEventHandlerTest(BaseClassSetup): mr_aai_mock.return_value = self.mr_aai_events apply_nfs_to_measure_grp.side_effect = Exception("publish failed") aai_handler = AAIEventHandler(self.app) - generate_nf_filter_measure_grp('ExtraPM-All-gNB-R2B', 'msr_grp_name3') - db.session.commit() aai_handler.execute() mock_logger.assert_called_with('Failed to process AAI event for ' - 'subscription: ExtraPM-All-gNB-R2B ' + 'subscription: aai_event_handler ' 'due to: publish failed') @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None)) @@ -126,21 +117,5 @@ class AAIEventHandlerTest(BaseClassSetup): mock_set_sdnc_params.return_value = True mr_aai_mock.side_effect = Exception("AAI failure") aai_handler = AAIEventHandler(self.app) - generate_nf_filter_measure_grp('ExtraPM-All-gNB-R2B', 'msr_grp_name3') - db.session.commit() aai_handler.execute() mock_logger.assert_called_with('Failed to process AAI event due to: AAI failure') - - -def generate_nf_filter_measure_grp(sub_name, msg_name): - nf_filter = NetworkFunctionFilterModel( - subscription_name=sub_name, nf_names='{^pnf.*, ^vnf.*}', - model_invariant_ids='{}', - model_version_ids='{}', - model_names='{}') - measurement_group = MeasurementGroupModel( - subscription_name=sub_name, measurement_group_name=msg_name, - administrative_state='UNLOCKED', file_based_gp=15, file_location='pm.xml', - measurement_type=[], managed_object_dns_basic=[]) - db.session.add(nf_filter) - db.session.add(measurement_group) diff --git a/components/pm-subscription-handler/tests/test_aai_service.py b/components/pm-subscription-handler/tests/test_aai_service.py index 97f400c1..0d0bd962 100644 --- a/components/pm-subscription-handler/tests/test_aai_service.py +++ b/components/pm-subscription-handler/tests/test_aai_service.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019-2021 Nordix Foundation. +# Copyright (C) 2019-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. @@ -23,30 +23,21 @@ 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 +from mod import aai_client +from mod.network_function import NetworkFunctionFilter +from tests.base_setup import BaseClassSetup, create_subscription_data class AaiClientTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() - def setUp(self): super().setUp() + self.subscription = create_subscription_data('ExtraPM-All-gNB-R2B') 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') @@ -57,9 +48,10 @@ class AaiClientTestCase(BaseClassSetup): 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') + nf_filter = NetworkFunctionFilter(**self.subscription.network_filter.serialize()) + xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, nf_filter) + self.assertEqual(self.subscription.subscription_name, 'ExtraPM-All-gNB-R2B') + self.assertEqual(self.subscription.measurement_groups[0].administrative_state, 'UNLOCKED') self.assertEqual(len(xnfs), 3) @patch.object(Session, 'put') @@ -67,7 +59,7 @@ class AaiClientTestCase(BaseClassSetup): 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) + aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.subscription.network_filter) @responses.activate def test_aai_client_get_all_aai_xnf_data_not_found(self): diff --git a/components/pm-subscription-handler/tests/test_controller.py b/components/pm-subscription-handler/tests/test_controller.py index 7b0a8b19..797666d9 100755 --- a/components/pm-subscription-handler/tests/test_controller.py +++ b/components/pm-subscription-handler/tests/test_controller.py @@ -28,22 +28,17 @@ from mod.api.services.measurement_group_service import query_meas_group_by_name from tests.base_setup import BaseClassSetup from mod.api.custom_exception import InvalidDataException, DataConflictException from mod.api.db_models import SubscriptionModel, NfMeasureGroupRelationalModel -from mod.subscription import SubNfState from mod.network_function import NetworkFunctionFilter from tests.base_setup import create_subscription_data, create_multiple_subscription_data, \ create_multiple_network_function_data from mod.api.services import measurement_group_service, nf_service, subscription_service +from mod.api.services.measurement_group_service import MgNfState class ControllerTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() - def setUp(self): super().setUp() - super().setUpAppConf() 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: @@ -52,13 +47,6 @@ class ControllerTestCase(BaseClassSetup): 'data/create_subscription_request.json'), 'r') as data: self.subscription_request = data.read() - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - def test_status_response_healthy(self): self.assertEqual(status()['status'], 'healthy') @@ -90,11 +78,14 @@ class ControllerTestCase(BaseClassSetup): NfMeasureGroupRelationalModel.measurement_grp_name == mes_grp_name)).all() for published_event in msr_grp_nf_rel: self.assertEqual(published_event.nf_measure_grp_status, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) self.assertEqual(response[1], 201) def test_post_subscription_duplicate_sub(self): # Posting the same subscription request stored in previous test to get duplicate response + subscription = create_subscription_data('ExtraPM-All-gNB-R2B') + db.session.add(subscription) + db.session.commit() response = post_subscription(json.loads(self.subscription_request)) self.assertEqual(response[1], 409) self.assertEqual(response[0], 'subscription Name: ExtraPM-All-gNB-R2B already exists.') @@ -185,7 +176,7 @@ class ControllerTestCase(BaseClassSetup): measurement_group_service. \ apply_nf_status_to_measurement_group(nf.nf_name, sub.measurement_groups[0]. measurement_group_name, - SubNfState.PENDING_CREATE.value) + MgNfState.PENDING_CREATE.value) db.session.commit() mg_with_nfs, status_code = get_meas_group_with_nfs('sub1', 'MG1') self.assertEqual(status_code, HTTPStatus.OK.value) @@ -305,7 +296,7 @@ class ControllerTestCase(BaseClassSetup): measurement_group_service. \ apply_nf_status_to_measurement_group(nf.nf_name, sub.measurement_groups[0]. measurement_group_name, - SubNfState.CREATED.value) + MgNfState.CREATED.value) db.session.commit() response = update_admin_state('sub1', 'MG1', {'administrativeState': 'LOCKED'}) self.assertEqual(response[1], HTTPStatus.OK.value) @@ -315,7 +306,7 @@ class ControllerTestCase(BaseClassSetup): self.assertEqual(mg_with_nfs['measurementGroupName'], 'MG1') self.assertEqual(mg_with_nfs['administrativeState'], 'LOCKING') for nf in mg_with_nfs['networkFunctions']: - self.assertEqual(nf['nfMgStatus'], SubNfState.PENDING_DELETE.value) + self.assertEqual(nf['nfMgStatus'], MgNfState.PENDING_DELETE.value) @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None)) @patch.object(aai_client, '_get_all_aai_nf_data') @@ -340,7 +331,7 @@ class ControllerTestCase(BaseClassSetup): self.assertEqual(mg_with_nfs['measurementGroupName'], 'MG2') self.assertEqual(mg_with_nfs['administrativeState'], 'UNLOCKED') for nf in mg_with_nfs['networkFunctions']: - self.assertEqual(nf['nfMgStatus'], SubNfState.PENDING_CREATE.value) + self.assertEqual(nf['nfMgStatus'], MgNfState.PENDING_CREATE.value) @patch('mod.api.services.measurement_group_service.update_admin_status', MagicMock(side_effect=InvalidDataException('Bad request'))) diff --git a/components/pm-subscription-handler/tests/test_exit_handler.py b/components/pm-subscription-handler/tests/test_exit_handler.py index c98be634..d88b4752 100755 --- a/components/pm-subscription-handler/tests/test_exit_handler.py +++ b/components/pm-subscription-handler/tests/test_exit_handler.py @@ -17,32 +17,31 @@ # ============LICENSE_END===================================================== import os from signal import SIGTERM, signal -from unittest.mock import patch +from unittest.mock import patch, MagicMock +from mod import db from mod.exit_handler import ExitHandler -from tests.base_setup import BaseClassSetup +from tests.base_setup import BaseClassSetup, create_subscription_data class ExitHandlerTests(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() - - @patch('mod.pmsh_utils.PeriodicTask') - @patch('mod.pmsh_utils.PeriodicTask') + @patch('pmsh_service_main.PeriodicTask') + @patch('pmsh_service_main.PeriodicTask') def setUp(self, mock_periodic_task_aai, mock_periodic_task_policy): super().setUp() + subscription = create_subscription_data('aai_event_handler') + subscription.measurement_groups[1].administravtive_sate = 'UNLOCKED' + db.session.add(subscription) + db.session.add(subscription.measurement_groups[0]) + db.session.add(subscription.measurement_groups[1]) + db.session.add(subscription.network_filter) + db.session.add(subscription.nfs[0]) + db.session.commit() self.mock_aai_event_thread = mock_periodic_task_aai self.mock_policy_resp_handler_thread = mock_periodic_task_policy - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - + @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None)) def test_terminate_signal_successful(self): handler = ExitHandler(periodic_tasks=[self.mock_aai_event_thread, self.mock_policy_resp_handler_thread]) diff --git a/components/pm-subscription-handler/tests/test_network_function.py b/components/pm-subscription-handler/tests/test_network_function.py index 3e38b9c6..d80f2c40 100755 --- a/components/pm-subscription-handler/tests/test_network_function.py +++ b/components/pm-subscription-handler/tests/test_network_function.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019-2021 Nordix Foundation. +# Copyright (C) 2019-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ # ============LICENSE_END===================================================== import json import os -from unittest.mock import patch, Mock +from unittest.mock import patch from mod.network_function import NetworkFunction from tests.base_setup import BaseClassSetup @@ -25,10 +25,6 @@ from tests.base_setup import BaseClassSetup class NetworkFunctionTests(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() - def setUp(self): super().setUp() self.nf_1 = NetworkFunction(sdnc_model_name='blah', sdnc_model_version=1.0, @@ -49,13 +45,6 @@ class NetworkFunctionTests(BaseClassSetup): 'data/aai_model_info_no_sdnc.json'), 'r') as data: self.bad_model_info = json.loads(data.read()) - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - def test_get_network_function(self): self.nf_1.create() nf = NetworkFunction.get('pnf_1') @@ -82,7 +71,7 @@ class NetworkFunctionTests(BaseClassSetup): def test_delete_network_function(self): for nf in [self.nf_1, self.nf_2]: - self.app_conf.subscription.add_network_function_to_subscription(nf, Mock()) + nf.create() nfs = NetworkFunction.get_all() self.assertEqual(2, len(nfs)) NetworkFunction.delete(nf_name=self.nf_1.nf_name) diff --git a/components/pm-subscription-handler/tests/test_pmsh_config.py b/components/pm-subscription-handler/tests/test_pmsh_config.py index deb867bf..84f86fb4 100644 --- a/components/pm-subscription-handler/tests/test_pmsh_config.py +++ b/components/pm-subscription-handler/tests/test_pmsh_config.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2021 Nordix Foundation. +# Copyright (C) 2021-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== -from unittest.mock import Mock, patch +from unittest.mock import patch import responses from requests import Session @@ -26,30 +26,15 @@ from tests.base_setup import BaseClassSetup class PmshConfigTestCase(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() - - def setUp(self): - super().setUpAppConf() - self.mock_app = Mock() - - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - def test_config_get_aaf_creds(self): - self.assertEqual(self.pmsh_app_conf.enable_tls, 'true') - self.assertEqual(self.pmsh_app_conf.aaf_id, 'dcae@dcae.onap.org') - self.assertEqual(self.pmsh_app_conf.aaf_pass, 'demo123456!') + self.assertEqual(self.app_conf.enable_tls, 'true') + self.assertEqual(self.app_conf.aaf_id, 'dcae@dcae.onap.org') + self.assertEqual(self.app_conf.aaf_pass, 'demo123456!') def test_config_get_cert_data(self): - self.assertEqual(self.pmsh_app_conf.key_path, '/opt/app/pmsh/etc/certs/key.pem') - self.assertEqual(self.pmsh_app_conf.cert_path, '/opt/app/pmsh/etc/certs/cert.pem') - self.assertEqual(self.pmsh_app_conf.ca_cert_path, '/opt/app/pmsh/etc/certs/cacert.pem') + self.assertEqual(self.app_conf.key_path, '/opt/app/pmsh/etc/certs/key.pem') + self.assertEqual(self.app_conf.cert_path, '/opt/app/pmsh/etc/certs/cert.pem') + self.assertEqual(self.app_conf.ca_cert_path, '/opt/app/pmsh/etc/certs/cacert.pem') def test_singleton_instance_is_accessible_using_class_method(self): my_singleton_instance = AppConfig.get_instance() @@ -60,8 +45,8 @@ class PmshConfigTestCase(BaseClassSetup): def test_mr_pub_publish_to_topic_success(self, mock_session): mock_session.return_value.status_code = 200 with patch('requests.Session.post') as session_post_call: - self.pmsh_app_conf.publish_to_topic(MRTopic.POLICY_PM_PUBLISHER.value, - {"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) + self.app_conf.publish_to_topic(MRTopic.POLICY_PM_PUBLISHER.value, + {"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) session_post_call.assert_called_once() @responses.activate @@ -70,8 +55,8 @@ class PmshConfigTestCase(BaseClassSetup): 'https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS', json={"error": "Client Error"}, status=400) with self.assertRaises(Exception): - self.pmsh_app_conf.publish_to_topic(MRTopic.POLICY_PM_PUBLISHER.value, - {"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) + self.app_conf.publish_to_topic(MRTopic.POLICY_PM_PUBLISHER.value, + {"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) @responses.activate def test_mr_sub_get_from_topic_success(self): @@ -79,7 +64,7 @@ class PmshConfigTestCase(BaseClassSetup): 'https://message-router:3905/events/org.onap.dmaap.mr.PM_SUBSCRIPTIONS/' 'dcae_pmsh_cg/1?timeout=5000', json={"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=200) - mr_topic_data = self.pmsh_app_conf.get_from_topic(MRTopic.POLICY_PM_SUBSCRIBER.value, 1) + mr_topic_data = self.app_conf.get_from_topic(MRTopic.POLICY_PM_SUBSCRIBER.value, 1) self.assertIsNotNone(mr_topic_data) @responses.activate @@ -89,4 +74,4 @@ class PmshConfigTestCase(BaseClassSetup): 'dcae_pmsh_cg/1?timeout=5000', json={"key": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=400) with self.assertRaises(Exception): - self.pmsh_app_conf.get_from_topic(MRTopic.POLICY_PM_SUBSCRIBER.value, 1) + self.app_conf.get_from_topic(MRTopic.POLICY_PM_SUBSCRIBER.value, 1) diff --git a/components/pm-subscription-handler/tests/test_pmsh_utils.py b/components/pm-subscription-handler/tests/test_pmsh_utils.py deleted file mode 100644 index 57f20ddf..00000000 --- a/components/pm-subscription-handler/tests/test_pmsh_utils.py +++ /dev/null @@ -1,245 +0,0 @@ -# ============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===================================================== -from test.support import EnvironmentVarGuard -from unittest.mock import patch, Mock - -import responses -from jsonschema import ValidationError -from requests import Session -from tenacity import RetryError - -from mod import get_db_connection_url -from mod.network_function import NetworkFunction -from tests.base_setup import BaseClassSetup -from tests.base_setup import get_pmsh_config - - -class PmshUtilsTestCase(BaseClassSetup): - - @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') - - def test_utils_get_mr_sub_fails_with_invalid_name(self): - with self.assertRaises(KeyError): - self.app_conf.get_mr_sub('invalid_sub') - - 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!') - - def test_utils_get_mr_pub_fails_with_invalid_name(self): - with self.assertRaises(KeyError): - self.app_conf.get_mr_pub('invalid_pub') - - 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.object(Session, 'post') - 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() - - @responses.activate - 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) - mr_policy_pub = self.app_conf.get_mr_pub('policy_pm_publisher') - with self.assertRaises(Exception): - mr_policy_pub.publish_to_topic({"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}) - - 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, - NetworkFunction(nf_name='pnf_1', - model_invariant_id='some-id', - model_version_id='some-id')) - pub_to_topic_call.assert_called_once() - - @responses.activate - 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/' - 'dcae_pmsh_cg/1?timeout=1000', - json={"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=200) - mr_topic_data = policy_mr_sub.get_from_topic(1) - self.assertIsNotNone(mr_topic_data) - - @responses.activate - 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/' - 'dcae_pmsh_cg/1?timeout=1000', - json={"dummy_val": "43c4ee19-6b8d-4279-a80f-c507850aae47"}, status=400) - with self.assertRaises(Exception): - policy_mr_sub.get_from_topic(1) - - def test_get_db_connection_url_success(self): - self.env = EnvironmentVarGuard() - self.env.set('PMSH_PG_URL', '1.2.3.4') - self.env.set('PMSH_PG_USERNAME', 'pmsh') - self.env.set('PMSH_PG_PASSWORD', 'pass') - db_url = get_db_connection_url() - self.assertEqual(db_url, 'postgresql+psycopg2://pmsh:pass@1.2.3.4:5432/pmsh') - - def test_get_db_connection_url_fail(self): - self.env = EnvironmentVarGuard() - self.env.set('PMSH_PG_USERNAME', 'pmsh') - self.env.set('PMSH_PG_PASSWORD', 'pass') - with self.assertRaises(Exception): - get_db_connection_url() - - @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 = 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.side_effect = ValueError - with self.assertRaises(RetryError): - self.app_conf.refresh_config() - mock_logger.assert_called_with('Failed to refresh PMSH AppConfig') - - @patch('mod.logger.debug') - def test_utils_validate_config_subscription(self, mock_logger): - self.app_conf.validate_sub_schema() - mock_logger.assert_called_with("Subscription schema is valid.") - - @patch('mod.logger.debug') - def test_utils_validate_config_subscription_administrativeState_locked(self, mock_logger): - self.app_conf.subscription.administrativeState = "LOCKED" - self.app_conf.validate_sub_schema() - mock_logger.assert_called_with("Subscription schema is valid.") - - def test_utils_validate_config_subscription_administrativeState_invalid_value(self): - self.app_conf.subscription.administrativeState = "FAILED" - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() - - def test_utils_validate_config_subscription_nfFilter_failed(self): - self.app_conf.subscription.nfFilter = {} - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() - - def test_utils_validate_config_subscription_nfFilter_not_empty(self): - self.app_conf.subscription.nfFilter = { - "nfNames": [ - - ], - "modelInvariantIDs": [ - - ], - "modelVersionIDs": [ - - ], - "modelNames": [ - - ] - } - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() - - @patch('mod.logger.debug') - def test_utils_validate_config_subscription_nfFilter_with_empty_property(self, mock_logger): - self.app_conf.subscription.nfFilter = { - "nfNames": [ - "^pnf.*", - "^vnf.*" - ], - "modelInvariantIDs": [ - "7129e420-d396-4efb-af02-6b83499b12f8" - ], - "modelVersionIDs": [ - - ], - "modelNames": [ - "pnf102" - ] - } - self.app_conf.validate_sub_schema() - mock_logger.assert_called_with("Subscription schema is valid.") - - def test_utils_validate_config_subscription_where_measurementTypes_is_empty(self): - self.app_conf.subscription.measurementGroups = [{ - "measurementGroup": { - "measurementTypes": [ - ], - "managedObjectDNsBasic": [ - { - "DN": "dna" - }, - { - "DN": "dnb" - } - ] - } - }] - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() - - def test_utils_validate_config_subscription_where_managedObjectDNsBasic_is_empty(self): - self.app_conf.subscription.measurementGroups = [{ - "measurementGroup": { - "measurementTypes": [ - { - "measurementType": "countera" - }, - { - "measurementType": "counterb" - } - ], - "managedObjectDNsBasic": [ - - ] - } - }] - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() - - def test_utils_validate_config_subscription_where_measurementGroups_is_empty(self): - self.app_conf.subscription.measurementGroups = [] - with self.assertRaises(ValidationError): - self.app_conf.validate_sub_schema() 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 d5ae5ce1..735e9381 100644 --- a/components/pm-subscription-handler/tests/test_policy_response_handler.py +++ b/components/pm-subscription-handler/tests/test_policy_response_handler.py @@ -18,32 +18,21 @@ from unittest.mock import patch, MagicMock from mod import db +from mod.api.services.measurement_group_service import MgNfState, AdministrativeState from mod.api.services import measurement_group_service from mod.network_function import NetworkFunction from mod.policy_response_handler import PolicyResponseHandler, policy_response_handle_functions -from mod.subscription import AdministrativeState, SubNfState from tests.base_setup import BaseClassSetup, create_subscription_data class PolicyResponseHandlerTest(BaseClassSetup): - @classmethod - def setUpClass(cls): - super().setUpClass() @patch('mod.create_app') def setUp(self, mock_app): super().setUp() - super().setUpAppConf() self.nf = NetworkFunction(nf_name='nf1') self.policy_response_handler = PolicyResponseHandler(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): with patch.dict(policy_response_handle_functions, @@ -64,7 +53,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'failed') mock_update_sub_nf.assert_called_with( measurement_group_name='msr_grp_name', - status=SubNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) + status=MgNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) @patch('mod.network_function.NetworkFunction.delete') def test_handle_response_locking_success(self, mock_delete): @@ -86,7 +75,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'failed') mock_update_sub_nf.assert_called_with( measurement_group_name='msr_grp_name', - status=SubNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) + status=MgNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_unlocked_success(self, mock_update_sub_nf): @@ -98,7 +87,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'success') mock_update_sub_nf.assert_called_with( measurement_group_name='msr_grp_name', - status=SubNfState.CREATED.value, nf_name=self.nf.nf_name) + status=MgNfState.CREATED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_unlocked_success_filtering(self, mock_update_sub_nf): @@ -107,7 +96,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): sub = create_subscription_data('sub') db.session.add(sub) measurement_group_service.apply_nf_status_to_measurement_group( - self.nf.nf_name, "MG2", SubNfState.PENDING_CREATE.value) + self.nf.nf_name, "MG2", MgNfState.PENDING_CREATE.value) db.session.commit() self.policy_response_handler._handle_response( 'MG2', @@ -115,7 +104,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'success') mock_update_sub_nf.assert_called_with( measurement_group_name='MG2', - status=SubNfState.CREATED.value, nf_name=self.nf.nf_name) + status=MgNfState.CREATED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_locking_success_filtering(self, mock_update_sub_nf): @@ -124,7 +113,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): sub = create_subscription_data('sub') db.session.add(sub) measurement_group_service.apply_nf_status_to_measurement_group( - self.nf.nf_name, "MG2", SubNfState.PENDING_DELETE.value) + self.nf.nf_name, "MG2", MgNfState.PENDING_DELETE.value) db.session.commit() self.policy_response_handler._handle_response( 'MG2', @@ -132,7 +121,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'success') mock_update_sub_nf.assert_called_with( measurement_group_name='MG2', - status=SubNfState.DELETED.value, nf_name=self.nf.nf_name) + status=MgNfState.DELETED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_unlocked_failed(self, mock_update_sub_nf): @@ -144,7 +133,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'failed') mock_update_sub_nf.assert_called_with( measurement_group_name='msr_grp_name', - status=SubNfState.CREATE_FAILED.value, nf_name=self.nf.nf_name) + status=MgNfState.CREATE_FAILED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_create_failed_filtering(self, mock_update_sub_nf): @@ -153,7 +142,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): sub = create_subscription_data('sub') db.session.add(sub) measurement_group_service.apply_nf_status_to_measurement_group( - self.nf.nf_name, "MG2", SubNfState.PENDING_CREATE.value) + self.nf.nf_name, "MG2", MgNfState.PENDING_CREATE.value) db.session.commit() self.policy_response_handler._handle_response( 'MG2', @@ -161,7 +150,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'failed') mock_update_sub_nf.assert_called_with( measurement_group_name='MG2', - status=SubNfState.CREATE_FAILED.value, nf_name=self.nf.nf_name) + status=MgNfState.CREATE_FAILED.value, nf_name=self.nf.nf_name) @patch('mod.api.services.measurement_group_service.update_measurement_group_nf_status') def test_handle_response_delete_failed_filtering(self, mock_update_sub_nf): @@ -170,7 +159,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): sub = create_subscription_data('sub') db.session.add(sub) measurement_group_service.apply_nf_status_to_measurement_group( - self.nf.nf_name, "MG2", SubNfState.PENDING_DELETE.value) + self.nf.nf_name, "MG2", MgNfState.PENDING_DELETE.value) db.session.commit() self.policy_response_handler._handle_response( 'MG2', @@ -178,7 +167,7 @@ class PolicyResponseHandlerTest(BaseClassSetup): self.nf.nf_name, 'failed') mock_update_sub_nf.assert_called_with( measurement_group_name='MG2', - status=SubNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) + status=MgNfState.DELETE_FAILED.value, nf_name=self.nf.nf_name) def test_handle_response_exception(self): self.assertRaises(Exception, self.policy_response_handler._handle_response, 'sub1', diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py deleted file mode 100755 index 5c40c4fd..00000000 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ /dev/null @@ -1,168 +0,0 @@ -# ============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.mock import patch, Mock - -from requests import Session - -import mod.aai_client as aai_client -from mod.network_function import NetworkFunction -from mod.subscription import Subscription -from tests.base_setup import BaseClassSetup - - -class SubscriptionTest(BaseClassSetup): - - @classmethod - def setUpClass(cls): - super().setUpClass() - - @patch.object(Session, 'get') - @patch.object(Session, 'put') - 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_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.app_conf.subscription.create() - self.xnfs = aai_client.get_pmsh_nfs_from_aai(self.app_conf, self.app_conf.nf_filter) - self.sub_model = self.app_conf.subscription.get() - - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - - def test_sub_measurement_group(self): - self.assertEqual(len(self.app_conf.subscription.measurementGroups), 2) - - def test_sub_file_location(self): - self.assertEqual(self.app_conf.subscription.fileLocation, '/pm/pm.xml') - - def test_get_subscription(self): - sub_name = 'ExtraPM-All-gNB-R2B' - new_sub = self.app_conf.subscription.get() - self.assertEqual(sub_name, new_sub.subscription_name) - - def test_get_nf_names_per_sub(self): - self.app_conf.subscription.add_network_function_to_subscription(list(self.xnfs)[0], - self.sub_model) - self.app_conf.subscription.add_network_function_to_subscription(list(self.xnfs)[1], - self.sub_model) - - def test_add_duplicate_network_functions_per_subscription(self): - self.app_conf.subscription.add_network_function_to_subscription(list(self.xnfs)[0], - self.sub_model) - nf_subs = Subscription.get_all_nfs_subscription_relations() - self.assertEqual(1, len(nf_subs)) - self.app_conf.subscription.add_network_function_to_subscription(list(self.xnfs)[0], - self.sub_model) - nf_subs = Subscription.get_all_nfs_subscription_relations() - self.assertEqual(1, len(nf_subs)) - - def test_update_subscription_status(self): - self.app_conf.subscription.administrativeState = 'new_status' - self.app_conf.subscription.update_subscription_status() - sub = self.app_conf.subscription.get() - - self.assertEqual('new_status', sub.status) - - def test_update_sub_nf_status(self): - sub_name = 'ExtraPM-All-gNB-R2B' - for nf in self.xnfs: - self.app_conf.subscription.add_network_function_to_subscription(nf, self.sub_model) - sub_nfs = Subscription.get_all_nfs_subscription_relations() - self.assertEqual('PENDING_CREATE', sub_nfs[0].nf_sub_status) - - Subscription.update_sub_nf_status(sub_name, 'Active', 'pnf_23') - sub_nfs = Subscription.get_all_nfs_subscription_relations() - self.assertEqual('PENDING_CREATE', sub_nfs[0].nf_sub_status) - self.assertEqual('PENDING_CREATE', sub_nfs[1].nf_sub_status) - - @patch('mod.subscription.Subscription.add_network_function_to_subscription') - @patch('mod.subscription.Subscription.update_sub_nf_status') - def test_process_activate_subscription(self, mock_update_sub_nf, mock_add_nfs): - self.app_conf.subscription.create_subscription_on_nfs([list(self.xnfs)[0]], - self.mock_mr_pub) - - mock_add_nfs.assert_called() - self.assertTrue(self.mock_mr_pub.publish_subscription_event_data.called) - mock_update_sub_nf.assert_called_with(self.app_conf.subscription.subscriptionName, - 'PENDING_CREATE', list(self.xnfs)[0].nf_name) - - @patch('mod.subscription.Subscription.get_network_functions') - @patch('mod.subscription.Subscription.update_sub_nf_status') - def test_process_deactivate_subscription(self, mock_update_sub_nf, mock_get_nfs): - self.app_conf.subscription.administrativeState = 'LOCKED' - mock_get_nfs.return_value = [list(self.xnfs)[0]] - self.app_conf.subscription.delete_subscription_from_nfs(self.xnfs, self.mock_mr_pub) - self.assertTrue(self.mock_mr_pub.publish_subscription_event_data.called) - self.assertEqual(mock_update_sub_nf.call_count, 3) - - def test_activate_subscription_exception(self): - self.assertRaises(Exception, self.app_conf.subscription.create_subscription_on_nfs, - [list(self.xnfs)[0]], 'not_mr_pub', 'app_config') - - def test_prepare_subscription_event(self): - with open(os.path.join(os.path.dirname(__file__), - 'data/pm_subscription_event.json'), 'r') as data: - expected_sub_event = json.load(data) - nf = NetworkFunction(nf_name='pnf_1', - ipv4_address='204.120.0.15', - ipv6_address='', - 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) - print(actual_sub_event) - self.assertEqual(expected_sub_event, actual_sub_event) - - def test_prepare_subscription_event_with_ipv6(self): - with open(os.path.join(os.path.dirname(__file__), - 'data/pm_subscription_event.json'), 'r') as data: - expected_sub_event = json.load(data) - expected_sub_event['ipAddress'] = '2001:db8:3333:4444:5555:6666:7777:8888' - nf = NetworkFunction(nf_name='pnf_1', - ipv4_address='204.120.0.15', - ipv6_address='2001:db8:3333:4444:5555:6666:7777:8888', - 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) - print(actual_sub_event) - self.assertEqual(expected_sub_event, actual_sub_event) - - def test_get_network_functions(self): - for nf in self.xnfs: - self.app_conf.subscription.add_network_function_to_subscription(nf, self.sub_model) - nfs = self.app_conf.subscription.get_network_functions() - - self.assertEqual(3, len(nfs)) - self.assertIsInstance(nfs[0], NetworkFunction) diff --git a/components/pm-subscription-handler/tests/test_subscription_handler.py b/components/pm-subscription-handler/tests/test_subscription_handler.py deleted file mode 100644 index 1843eb44..00000000 --- a/components/pm-subscription-handler/tests/test_subscription_handler.py +++ /dev/null @@ -1,181 +0,0 @@ -# ============LICENSE_START=================================================== -# Copyright (C) 2020-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===================================================== -from unittest.mock import patch, Mock, MagicMock - -from mod.api.db_models import NetworkFunctionModel -from mod.network_function import NetworkFunction -from mod.subscription import AdministrativeState -from mod.subscription_handler import SubscriptionHandler -from tests.base_setup import BaseClassSetup, get_pmsh_config - - -class SubscriptionHandlerTest(BaseClassSetup): - 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')] - - @classmethod - def setUpClass(cls): - super().setUpClass() - - @patch('mod.pmsh_utils._MrSub') - @patch('mod.pmsh_utils._MrPub') - def setUp(self, mock_mr_pub, mock_mr_sub): - super().setUp() - self.mock_mr_pub = mock_mr_pub - self.mock_mr_sub = mock_mr_sub - self.mock_policy_event_thread = Mock() - - def tearDown(self): - super().tearDown() - - @classmethod - def tearDownClass(cls): - super().tearDownClass() - - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.logger.info') - @patch('mod.aai_client.get_pmsh_nfs_from_aai') - def test_execute_no_change_of_state(self, mock_get_aai, mock_logger, mock_get_sub_status): - mock_get_sub_status.return_value = AdministrativeState.UNLOCKED.value - mock_get_aai.return_value = self.nfs - sub_handler = SubscriptionHandler(self.mock_mr_pub, - self.mock_mr_sub, self.app, self.app_conf) - sub_handler.execute() - mock_logger.assert_called_with('Administrative State did not change ' - 'in the app config: UNLOCKED') - - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.subscription.Subscription.create_subscription_on_nfs') - @patch('mod.aai_client.get_pmsh_nfs_from_aai') - def test_execute_change_of_state_to_unlocked(self, mock_get_aai, mock_activate_sub, - mock_get_sub_status): - mock_get_aai.return_value = self.nfs - mock_get_sub_status.return_value = AdministrativeState.LOCKED.value - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - self.assertEqual(AdministrativeState.UNLOCKED.value, - self.app_conf.subscription.administrativeState) - mock_activate_sub.assert_called_with(self.nfs, self.mock_mr_pub) - - @patch('mod.subscription.Subscription.get_network_functions', MagicMock(return_value=nfs)) - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.subscription.Subscription.delete_subscription_from_nfs') - def test_execute_change_of_state_to_locked(self, mock_deactivate_sub, mock_get_sub_status): - mock_get_sub_status.return_value = AdministrativeState.UNLOCKED.value - self.app_conf.subscription.administrativeState = AdministrativeState.LOCKED.value - self.app_conf.subscription.update_subscription_status() - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_deactivate_sub.assert_called_with(self.nfs, self.mock_mr_pub) - - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.create_subscription_on_nfs') - @patch('mod.logger.error') - @patch('mod.aai_client.get_pmsh_nfs_from_aai') - def test_execute_exception(self, mock_get_aai, mock_logger, mock_activate_sub): - mock_get_aai.return_value = self.nfs - mock_activate_sub.side_effect = Exception - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_logger.assert_called_with('Error occurred during the activation/deactivation process ', - exc_info=True) - - @patch('mod.network_function.NetworkFunction.get', - MagicMock(return_value=NetworkFunctionModel(nf_name='pnf_1', - model_invariant_id='some-id', - model_version_id='some-id', - ipv4_address='ip_address4', - ipv6_address='ip_address6', - model_name='model_name', - sdnc_model_name='sdnc_model_name', - sdnc_model_version='sdnc_model_version'))) - @patch('mod.subscription.Subscription.get_delete_failed_nfs', MagicMock(return_value=nfs)) - @patch('mod.subscription.Subscription.get_network_functions', MagicMock(return_value=nfs)) - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.subscription.Subscription.delete_subscription_from_nfs') - @patch('mod.network_function.NetworkFunction.increment_retry_count') - def test_execute_change_of_state_to_locking_retry_delete(self, mock_retry_inc, mock_delete_sub, - mock_get_sub_status): - mock_get_sub_status.return_value = AdministrativeState.LOCKING.value - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - self.assertEqual(mock_delete_sub.call_count, 2) - self.assertEqual(mock_retry_inc.call_count, 2) - - @patch('mod.subscription.Subscription.get_delete_failed_nfs', MagicMock(return_value=[])) - @patch('mod.subscription.Subscription.get_network_functions', MagicMock(return_value=nfs)) - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.subscription.Subscription.update_subscription_status') - def test_execute_change_of_state_to_locking_success(self, mock_update_sub, - mock_get_sub_status): - mock_get_sub_status.return_value = AdministrativeState.LOCKING.value - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_update_sub.assert_called_once() - - @patch('mod.network_function.NetworkFunction.get', - MagicMock(return_value=NetworkFunctionModel(nf_name='pnf_1', - model_invariant_id='some-id', - model_version_id='some-id', - ipv4_address='ip_address4', - ipv6_address='ip_address6', - model_name='model_name', - sdnc_model_name='sdnc_model_name', - sdnc_model_version='sdnc_model_version', - retry_count=3))) - @patch('mod.subscription.Subscription.get_delete_failed_nfs', MagicMock(return_value=[nfs[0]])) - @patch('mod.subscription.Subscription.get_network_functions', MagicMock(return_value=nfs[0])) - @patch('mod.pmsh_utils.AppConfig.refresh_config', MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription.Subscription.get_local_sub_admin_state') - @patch('mod.network_function.NetworkFunction.delete') - def test_execute_change_of_state_to_locking_retry_failed(self, mock_nf_del, - mock_get_sub_status): - mock_get_sub_status.return_value = AdministrativeState.LOCKING.value - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_nf_del.assert_called_once() - - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config', - MagicMock(return_value=get_pmsh_config('data/cbs_invalid_data.json'))) - @patch('mod.subscription_handler.SubscriptionHandler._check_state_change') - def test_execute_invalid_schema(self, mock_change_state_check): - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_change_state_check.assert_not_called() - - @patch('mod.pmsh_utils.AppConfig._get_pmsh_config', - MagicMock(return_value=get_pmsh_config())) - @patch('mod.subscription_handler.SubscriptionHandler._check_state_change') - def test_execute_valid_schema(self, mock_change_state_check): - sub_handler = SubscriptionHandler(self.mock_mr_pub, self.mock_mr_sub, self.app, - self.app_conf) - sub_handler.execute() - mock_change_state_check.assert_called_once() |