blob: 33e2e91b00063a0d83421a3e9ca09d26dc95d078 [file] [log] [blame]
efiacor0080fa42020-09-08 16:26:50 +01001# ============LICENSE_START===================================================
ajay_dp001053579b2021-08-25 13:53:27 +05302# Copyright (C) 2020-2021 Nordix Foundation.
efiacor0080fa42020-09-08 16:26:50 +01003# ============================================================================
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=====================================================
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000018import copy
efiacor0080fa42020-09-08 16:26:50 +010019import json
20import os
21from unittest import TestCase
22from unittest.mock import patch, MagicMock
23
24from mod import create_app, db
egernug15dde942021-12-06 13:11:15 +000025from mod.api.db_models import NetworkFunctionFilterModel, MeasurementGroupModel, SubscriptionModel, NfSubRelationalModel
shivasubedi9aa0b662021-01-18 13:59:18 +000026from mod.network_function import NetworkFunctionFilter
efiacor0080fa42020-09-08 16:26:50 +010027from mod.pmsh_utils import AppConfig
ajay_dp001053579b2021-08-25 13:53:27 +053028from mod.pmsh_config import AppConfig as NewAppConfig
efiacor0080fa42020-09-08 16:26:50 +010029
30
shivasubedi9aa0b662021-01-18 13:59:18 +000031def get_pmsh_config(file_path='data/cbs_data_1.json'):
raviteja.karumuri7e304212021-11-24 13:10:09 +000032 """
33 Gets PMSH config from the JSON file
34
35 Args:
36 file_path (String): Name of the file with path
37
38 Returns
39 dict: Dictionary representation of the the service configuration
40 """
shivasubedi9aa0b662021-01-18 13:59:18 +000041 with open(os.path.join(os.path.dirname(__file__), file_path), 'r') as data:
efiacor0080fa42020-09-08 16:26:50 +010042 return json.load(data)
43
44
raviteja.karumuri7e304212021-11-24 13:10:09 +000045def create_subscription_data(subscription_name):
46 """
47 Creates subscription model object
48
49 Args:
50 subscription_name (String): Name of the Subscription
51
52 Returns
53 SubscriptionModel: single subscription model object
54 """
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000055 nf_filter = NetworkFunctionFilterModel(subscription_name, '{^pnf.*,^vnf.*}',
56 '{}', '{}', '{}')
57 mg_first = MeasurementGroupModel(subscription_name, 'MG1', 'UNLOCKED', 15, '/pm/pm.xml',
58 '[{ "measurementType": "countera" }, '
59 '{ "measurementType": "counterb" }]',
60 '[{ "DN":"dna"},{"DN":"dnb"}]')
61 mg_second = copy.deepcopy(mg_first)
62 mg_second.measurement_group_name = 'MG2'
63 mg_second.administrative_state = 'LOCKED'
64 mg_list = [mg_first, mg_second]
65 subscription_model = SubscriptionModel(subscription_name, 'pmsh_operational_policy',
66 'pmsh_control_loop_name', 'LOCKED')
67 subscription_model.network_filter = nf_filter
68 subscription_model.measurement_groups = mg_list
egernug15dde942021-12-06 13:11:15 +000069 nf1 = NfSubRelationalModel(subscription_name, "pnf_101", "LOCKED")
70 nf2 = NfSubRelationalModel(subscription_name, "pnf_102", "LOCKED")
71 subscription_model.nfs = [nf1, nf2]
raviteja.karumuri86f4eb22021-11-09 17:30:03 +000072 return subscription_model
73
74
raviteja.karumuri7e304212021-11-24 13:10:09 +000075def create_multiple_subscription_data(subscription_names):
76 """
77 Creates a list of subscription model objects
78
79 Args:
80 subscription_names (List): Name of the Subscriptions
81
82 Returns
83 list (SubscriptionModel): of subscription model objects
84 """
85 subscriptions = []
86 for subscription_name in subscription_names:
87 subscriptions.append(create_subscription_data(subscription_name))
88 return subscriptions
89
90
efiacor0080fa42020-09-08 16:26:50 +010091class BaseClassSetup(TestCase):
92 app = None
93 app_context = None
94
95 @classmethod
96 @patch('mod.get_db_connection_url', MagicMock(return_value='sqlite://'))
97 @patch('mod.update_logging_config', MagicMock())
98 def setUpClass(cls):
99 os.environ['LOGGER_CONFIG'] = os.path.join(os.path.dirname(__file__), 'log_config.yaml')
100 os.environ['LOGS_PATH'] = '.'
101 cls.app = create_app()
102 cls.app_context = cls.app.app_context()
103 cls.app_context.push()
104
105 @patch('mod.pmsh_utils.AppConfig._get_pmsh_config', MagicMock(return_value=get_pmsh_config()))
106 def setUp(self):
107 os.environ['AAI_SERVICE_PORT'] = '8443'
108 db.create_all()
109 self.app_conf = AppConfig()
shivasubedi9aa0b662021-01-18 13:59:18 +0000110 self.app_conf.nf_filter = NetworkFunctionFilter(**self.app_conf.subscription.nfFilter)
efiacor0080fa42020-09-08 16:26:50 +0100111
ajay_dp001053579b2021-08-25 13:53:27 +0530112 @patch('mod.pmsh_config.AppConfig._get_config', MagicMock(return_value=get_pmsh_config()))
113 def setUpAppConf(self):
114 self.pmsh_app_conf = NewAppConfig()
115
efiacor0080fa42020-09-08 16:26:50 +0100116 def tearDown(self):
117 db.drop_all()
118
119 @classmethod
120 def tearDownClass(cls):
121 db.session.remove()