diff options
author | 2021-01-18 13:59:18 +0000 | |
---|---|---|
committer | 2021-02-09 10:57:23 +0000 | |
commit | 9aa0b665b1d8ad6105ea783e176eacc58b26a804 (patch) | |
tree | 2cbb03f5a23a465062f4d3a1940018de3a2f7585 /components/pm-subscription-handler/tests | |
parent | f7be006e7cc638788164fb1028d03898138b8c16 (diff) |
[PMSH] Validate schema of PMSH monitoring policy
Change-Id: I42b002f855a03b39ab85cfcb20d7857d30447e40
Signed-off-by: shivasubedi <shiva.subedi@est.tech>
Issue-ID: DCAEGEN2-2152
Diffstat (limited to 'components/pm-subscription-handler/tests')
5 files changed, 206 insertions, 3 deletions
diff --git a/components/pm-subscription-handler/tests/base_setup.py b/components/pm-subscription-handler/tests/base_setup.py index 2e50dde9..9e12f96e 100755 --- a/components/pm-subscription-handler/tests/base_setup.py +++ b/components/pm-subscription-handler/tests/base_setup.py @@ -21,11 +21,12 @@ from unittest import TestCase from unittest.mock import patch, MagicMock from mod import create_app, db +from mod.network_function import NetworkFunctionFilter 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: +def get_pmsh_config(file_path='data/cbs_data_1.json'): + with open(os.path.join(os.path.dirname(__file__), file_path), 'r') as data: return json.load(data) @@ -48,6 +49,7 @@ class BaseClassSetup(TestCase): os.environ['AAI_SERVICE_PORT'] = '8443' db.create_all() self.app_conf = AppConfig() + self.app_conf.nf_filter = NetworkFunctionFilter(**self.app_conf.subscription.nfFilter) 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 86515343..2e405d09 100644 --- a/components/pm-subscription-handler/tests/data/cbs_data_1.json +++ b/components/pm-subscription-handler/tests/data/cbs_data_1.json @@ -15,6 +15,9 @@ ], "modelVersionIDs": [ + ], + "modelNames": [ + ] }, "measurementGroups":[ diff --git a/components/pm-subscription-handler/tests/data/cbs_invalid_data.json b/components/pm-subscription-handler/tests/data/cbs_invalid_data.json new file mode 100644 index 00000000..92da2b9c --- /dev/null +++ b/components/pm-subscription-handler/tests/data/cbs_invalid_data.json @@ -0,0 +1,116 @@ +{ + "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" + } + ] + } + } + ] + } + }, + "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", + "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/test_pmsh_utils.py b/components/pm-subscription-handler/tests/test_pmsh_utils.py index 602253b8..1711e013 100644 --- a/components/pm-subscription-handler/tests/test_pmsh_utils.py +++ b/components/pm-subscription-handler/tests/test_pmsh_utils.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019-2020 Nordix Foundation. +# 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. @@ -19,6 +19,7 @@ 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 @@ -142,3 +143,66 @@ class PmshUtilsTestCase(BaseClassSetup): 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_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_subscription_handler.py b/components/pm-subscription-handler/tests/test_subscription_handler.py index 31dd0943..2293ee50 100644 --- a/components/pm-subscription-handler/tests/test_subscription_handler.py +++ b/components/pm-subscription-handler/tests/test_subscription_handler.py @@ -162,3 +162,21 @@ class SubscriptionHandlerTest(BaseClassSetup): 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() |