blob: 97c1d6a1aba749523272099ecf2cf451642bbe45 [file] [log] [blame]
efiacorbbe05d82019-12-11 12:00:26 +00001# ============LICENSE_START===================================================
efiacor8b3fc622020-01-24 13:19:01 +00002# Copyright (C) 2019-2020 Nordix Foundation.
efiacorbbe05d82019-12-11 12:00:26 +00003# ============================================================================
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=====================================================
18import json
19import os
20import unittest
efiacor8b3fc622020-01-24 13:19:01 +000021from test.support import EnvironmentVarGuard
22from unittest import mock
efiacorbbe05d82019-12-11 12:00:26 +000023
efiacor8b3fc622020-01-24 13:19:01 +000024from requests import Session
25
26import mod.aai_client as aai_client
efiacorbc9f41e2020-02-12 23:31:16 +000027from mod import db, create_app
efiacor8b3fc622020-01-24 13:19:01 +000028from mod.network_function import NetworkFunction
29from mod.subscription import Subscription, NetworkFunctionFilter
efiacorbbe05d82019-12-11 12:00:26 +000030
31
efiacor8b3fc622020-01-24 13:19:01 +000032class SubscriptionTest(unittest.TestCase):
efiacorbbe05d82019-12-11 12:00:26 +000033
efiacorbc9f41e2020-02-12 23:31:16 +000034 @mock.patch('mod.get_db_connection_url')
efiacor8b3fc622020-01-24 13:19:01 +000035 @mock.patch.object(Session, 'put')
efiacorbc9f41e2020-02-12 23:31:16 +000036 def setUp(self, mock_session, mock_get_db_url):
37 mock_get_db_url.return_value = 'sqlite://'
efiacor8b3fc622020-01-24 13:19:01 +000038 with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data:
39 self.aai_response_data = data.read()
40 mock_session.return_value.status_code = 200
41 mock_session.return_value.text = self.aai_response_data
42 self.env = EnvironmentVarGuard()
43 self.env.set('AAI_SERVICE_HOST', '1.2.3.4')
44 self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443')
efiacorbc9f41e2020-02-12 23:31:16 +000045 self.env.set('TESTING', 'True')
46 self.env.set('LOGS_PATH', './unit_test_logs')
efiacor8b3fc622020-01-24 13:19:01 +000047 with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data:
48 self.cbs_data_1 = json.load(data)
49 with open(os.path.join(os.path.dirname(__file__),
50 'data/cbs_data_2.json'), 'r') as data:
51 self.cbs_data_2 = json.load(data)
52 self.sub_1, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_1)
53 self.sub_2, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_2)
54 self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried')
55 self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active')
56 self.xnf_filter = NetworkFunctionFilter(**self.sub_1.nfFilter)
efiacorbc9f41e2020-02-12 23:31:16 +000057 self.app = create_app()
efiacor8b3fc622020-01-24 13:19:01 +000058 self.app_context = self.app.app_context()
59 self.app_context.push()
60 db.create_all()
61
62 def tearDown(self):
63 db.session.remove()
64 db.drop_all()
65 self.app_context.pop()
efiacorbbe05d82019-12-11 12:00:26 +000066
67 def test_xnf_filter_true(self):
efiacor8b3fc622020-01-24 13:19:01 +000068 self.assertTrue(self.xnf_filter.is_nf_in_filter('pnf1'))
efiacorbbe05d82019-12-11 12:00:26 +000069
70 def test_xnf_filter_false(self):
efiacor8b3fc622020-01-24 13:19:01 +000071 self.assertFalse(self.xnf_filter.is_nf_in_filter('PNF-33'))
efiacorbbe05d82019-12-11 12:00:26 +000072
73 def test_sub_measurement_group(self):
efiacor8b3fc622020-01-24 13:19:01 +000074 self.assertEqual(len(self.sub_1.measurementGroups), 2)
efiacorbbe05d82019-12-11 12:00:26 +000075
76 def test_sub_file_location(self):
efiacor8b3fc622020-01-24 13:19:01 +000077 self.assertEqual(self.sub_1.fileLocation, '/pm/pm.xml')
78
79 def test_get_subscription(self):
80 sub_name = 'ExtraPM-All-gNB-R2B'
81 self.sub_1.create()
82 new_sub = Subscription.get(sub_name)
83 self.assertEqual(sub_name, new_sub.subscription_name)
84
85 def test_get_subscription_no_match(self):
86 sub_name = 'sub2_does_not_exist'
87 sub = Subscription.get(sub_name)
88 self.assertEqual(sub, None)
89
90 def test_get_subscriptions(self):
91 self.sub_1.create()
92 self.sub_2.create()
93 subs = self.sub_1.get_all()
94
95 self.assertEqual(2, len(subs))
96
97 def test_create_existing_subscription(self):
98 sub1 = self.sub_1.create()
99 same_sub1 = self.sub_1.create()
100 self.assertEqual(sub1, same_sub1)
101 self.assertEqual(1, len(self.sub_1.get_all()))
102
103 def test_get_nfs_per_subscription(self):
104 nf_array = [self.nf_1, self.nf_2]
105 self.sub_1.add_network_functions_to_subscription(nf_array)
106 nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations()
107 self.assertEqual(2, len(nfs_for_sub_1))
108
109 def test_add_network_functions_per_subscription(self):
110 nf_array = [self.nf_1, self.nf_2]
111 self.sub_1.add_network_functions_to_subscription(nf_array)
112 nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations()
113 self.assertEqual(2, len(nfs_for_sub_1))
114 new_nf_array = [NetworkFunction(nf_name='vnf_3', orchestration_status='Inventoried')]
115 self.sub_1.add_network_functions_to_subscription(new_nf_array)
116 nf_subs = Subscription.get_all_nfs_subscription_relations()
117 print(nf_subs)
118 self.assertEqual(3, len(nf_subs))
119
120 def test_add_duplicate_network_functions_per_subscription(self):
121 nf_array = [self.nf_1]
122 self.sub_1.add_network_functions_to_subscription(nf_array)
123 nf_subs = Subscription.get_all_nfs_subscription_relations()
124 self.assertEqual(1, len(nf_subs))
125 self.sub_1.add_network_functions_to_subscription(nf_array)
126 nf_subs = Subscription.get_all_nfs_subscription_relations()
127 self.assertEqual(1, len(nf_subs))