summaryrefslogtreecommitdiffstats
path: root/components/pm-subscription-handler/tests/test_aai_event_handler.py
diff options
context:
space:
mode:
authorSagarS <sagar.shetty@est.tech>2021-12-09 16:59:51 +0000
committerSagarS <sagar.shetty@est.tech>2021-12-09 17:01:04 +0000
commitadcbd374f47858f02a5155ec63d678442fb7f4a0 (patch)
treecdb0449b2c08748845bf92254b45ee31619339c9 /components/pm-subscription-handler/tests/test_aai_event_handler.py
parent15dde94eccad074dda76c420f900c977c184b66f (diff)
[DCAEGEN2] PMSH AAI changes with new subscription format
Issue-ID: DCAEGEN2-2912 Change-Id: Ibd21f8f7a01a31bd7db19a39af010d6805b41622 Signed-off-by: SagarS <sagar.shetty@est.tech>
Diffstat (limited to 'components/pm-subscription-handler/tests/test_aai_event_handler.py')
-rwxr-xr-xcomponents/pm-subscription-handler/tests/test_aai_event_handler.py119
1 files changed, 104 insertions, 15 deletions
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 0ae19429..d06b7728 100755
--- a/components/pm-subscription-handler/tests/test_aai_event_handler.py
+++ b/components/pm-subscription-handler/tests/test_aai_event_handler.py
@@ -1,5 +1,5 @@
# ============LICENSE_START===================================================
-# Copyright (C) 2020 Nordix Foundation.
+# 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.
@@ -15,27 +15,28 @@
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
+import copy
import json
from os import path
-from unittest.mock import patch, Mock
-
-from mod.aai_event_handler import process_aai_events
+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 import db
class AAIEventHandlerTest(BaseClassSetup):
-
@classmethod
def setUpClass(cls):
super().setUpClass()
def setUp(self):
super().setUp()
+ super().setUpAppConf()
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.mock_mr_sub = Mock(get_from_topic=Mock(return_value=self.mr_aai_events))
- self.mock_mr_pub = Mock()
- self.mock_app = Mock()
def tearDown(self):
super().tearDown()
@@ -44,12 +45,100 @@ class AAIEventHandlerTest(BaseClassSetup):
def tearDownClass(cls):
super().tearDownClass()
- @patch('mod.network_function.NetworkFunction.set_nf_model_params')
- @patch('mod.subscription.Subscription.create_subscription_on_nfs')
+ @patch('mod.pmsh_config.AppConfig.get_from_topic')
@patch('mod.aai_event_handler.NetworkFunction.delete')
- def test_process_aai_update_and_delete_events(self, mock_nf_delete, mock_activate_sub,
- mock_set_sdnc_params):
+ def test_process_aai_delete_events(self, mock_nf_delete, mr_aai_mock):
+ mr_aai_mock.return_value = self.mr_aai_events
+ aai_handler = AAIEventHandler(self.app)
+ network_function = NetworkFunctionModel(
+ nf_name="pnf_to_be_deleted1",
+ ipv4_address='204.120.0.15',
+ ipv6_address='2001:db8:3333:4444:5555:6666:7777:8888',
+ model_invariant_id='123',
+ model_version_id='234',
+ model_name='pnf',
+ sdnc_model_name='p-node',
+ sdnc_model_version='v1')
+ db.session.add(network_function)
+ network_function2 = copy.deepcopy(network_function)
+ network_function2.nf_name = "pnf_to_be_deleted2"
+ db.session.add(network_function2)
+ db.session.commit()
+ aai_handler.execute()
+ self.assertEqual(mock_nf_delete.call_count, 2)
+
+ @patch('mod.aai_event_handler.AAIEventHandler.apply_nfs_to_subscriptions')
+ @patch('mod.network_function.NetworkFunction.set_nf_model_params')
+ @patch('mod.pmsh_config.AppConfig.get_from_topic')
+ def test_process_aai_update_events(self, mr_aai_mock, mock_set_sdnc_params, mock_apply_nfs):
+ mock_set_sdnc_params.return_value = True
+ mr_aai_mock.return_value = self.mr_aai_events
+ aai_handler = AAIEventHandler(self.app)
+ aai_handler.execute()
+ self.assertEqual(mock_apply_nfs.call_count, 1)
+
+ @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None))
+ @patch('mod.api.services.subscription_service.apply_measurement_grp_to_nfs')
+ @patch('mod.network_function.NetworkFunction.set_nf_model_params')
+ @patch('mod.pmsh_config.AppConfig.get_from_topic')
+ def test_process_aai_apply_nfs_to_subscriptions(self, mr_aai_mock, mock_set_sdnc_params,
+ apply_nfs_to_measure_grp):
mock_set_sdnc_params.return_value = True
- process_aai_events(self.mock_mr_sub, self.mock_mr_pub, self.mock_app, self.app_conf)
- self.assertEqual(mock_activate_sub.call_count, 2)
- mock_nf_delete.assert_called_once()
+ 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')
+ db.session.commit()
+ aai_handler.execute()
+ self.assertEqual(apply_nfs_to_measure_grp.call_count, 2)
+
+ @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None))
+ @patch('mod.api.services.subscription_service.apply_measurement_grp_to_nfs')
+ @patch('mod.network_function.NetworkFunction.set_nf_model_params')
+ @patch('mod.logger.error')
+ @patch('mod.pmsh_config.AppConfig.get_from_topic')
+ def test_process_aai_apply_msg_failure(self, mr_aai_mock, mock_logger, mock_set_sdnc_params,
+ apply_nfs_to_measure_grp):
+ mock_set_sdnc_params.return_value = True
+ 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 '
+ 'due to: publish failed')
+
+ @patch('mod.pmsh_config.AppConfig.publish_to_topic', MagicMock(return_value=None))
+ @patch('mod.network_function.NetworkFunction.set_nf_model_params')
+ @patch('mod.logger.error')
+ @patch('mod.pmsh_config.AppConfig.get_from_topic')
+ def test_process_aai__failure(self, mr_aai_mock, mock_logger, mock_set_sdnc_params):
+ 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)