ERIMROB | 26b76c0 | 2020-02-12 11:35:20 +0000 | [diff] [blame] | 1 | # ============LICENSE_START=================================================== |
| 2 | # Copyright (C) 2020 Nordix Foundation. |
| 3 | # ============================================================================ |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # SPDX-License-Identifier: Apache-2.0 |
| 17 | # ============LICENSE_END===================================================== |
| 18 | import os |
| 19 | import json |
| 20 | from unittest import TestCase |
| 21 | from unittest.mock import patch |
| 22 | |
| 23 | import pmsh_service_main as pmsh_service |
| 24 | from mod.network_function import NetworkFunction |
| 25 | |
| 26 | |
| 27 | class PMSHServiceTest(TestCase): |
| 28 | |
| 29 | @patch('mod.create_app') |
| 30 | @patch('mod.subscription.Subscription') |
| 31 | @patch('mod.pmsh_utils._MrPub') |
| 32 | @patch('mod.config_handler.ConfigHandler') |
| 33 | def setUp(self, mock_config_handler, mock_mr_pub, |
| 34 | mock_sub, mock_app): |
| 35 | with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: |
| 36 | self.cbs_data_1 = json.load(data) |
| 37 | self.mock_app = mock_app |
| 38 | self.mock_sub = mock_sub |
| 39 | self.mock_mr_pub = mock_mr_pub |
| 40 | self.mock_config_handler = mock_config_handler |
| 41 | self.nf_1 = NetworkFunction(nf_name='pnf_1') |
| 42 | self.nf_2 = NetworkFunction(nf_name='pnf_2') |
| 43 | self.nfs = [self.nf_1, self.nf_2] |
| 44 | |
| 45 | @patch('threading.Timer') |
| 46 | @patch('mod.aai_client.get_pmsh_subscription_data') |
| 47 | def test_subscription_processor_changed_state(self, mock_get_aai, mock_thread): |
| 48 | self.mock_config_handler.get_config.return_value = self.cbs_data_1 |
| 49 | mock_get_aai.return_value = self.mock_sub, self.nfs |
| 50 | mock_thread.start.return_value = 1 |
| 51 | |
| 52 | pmsh_service.subscription_processor(self.mock_config_handler, 'LOCKED', |
| 53 | self.mock_mr_pub, self.mock_app) |
| 54 | |
| 55 | self.mock_sub.process_subscription.assert_called_with(self.nfs, self.mock_mr_pub) |
| 56 | |
| 57 | @patch('threading.Timer') |
| 58 | @patch('mod.pmsh_logging.debug') |
| 59 | @patch('mod.aai_client.get_pmsh_subscription_data') |
| 60 | def test_subscription_processor_unchanged_state(self, mock_get_aai, mock_logger, mock_thread): |
| 61 | self.mock_config_handler.get_config.return_value = self.cbs_data_1 |
| 62 | mock_get_aai.return_value = self.mock_sub, self.nfs |
| 63 | mock_thread.start.return_value = 1 |
| 64 | |
| 65 | pmsh_service.subscription_processor(self.mock_config_handler, 'UNLOCKED', self.mock_mr_pub, |
| 66 | self.mock_app) |
| 67 | |
| 68 | mock_logger.assert_called_with('Administrative State did not change in the Config') |
| 69 | |
| 70 | @patch('threading.Timer') |
| 71 | @patch('mod.pmsh_logging.debug') |
| 72 | @patch('mod.aai_client.get_pmsh_subscription_data') |
| 73 | def test_subscription_processor_exception(self, mock_get_aai, mock_logger, mock_thread): |
| 74 | self.mock_config_handler.get_config.return_value = self.cbs_data_1 |
| 75 | mock_get_aai.return_value = self.mock_sub, self.nfs |
| 76 | mock_thread.start.return_value = 1 |
| 77 | self.mock_sub.process_subscription.side_effect = Exception |
| 78 | |
| 79 | pmsh_service.subscription_processor(self.mock_config_handler, 'LOCKED', self.mock_mr_pub, |
| 80 | self.mock_app) |
| 81 | mock_logger.assert_called_with(f'Error occurred during the ' |
| 82 | f'activation/deactivation process ') |