diff options
author | 2020-01-24 13:19:01 +0000 | |
---|---|---|
committer | 2020-02-10 15:45:13 +0000 | |
commit | 8b3fc62050a344fe9a9c8909e4c672cb9aa3281d (patch) | |
tree | aa74fef6de378a1914067cb8a3fffa3798cb6a35 /components/pm-subscription-handler/tests/test_subscription.py | |
parent | bbe05d8a65ee0ac698d906b50282406bafe34f80 (diff) |
Adding DB Init and setup
Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Change-Id: Ie32efcf007c9b6fa25b0072019f4a91a841d9d0c
Issue-ID: DCAEGEN2-1828
Diffstat (limited to 'components/pm-subscription-handler/tests/test_subscription.py')
-rwxr-xr-x[-rw-r--r--] | components/pm-subscription-handler/tests/test_subscription.py | 104 |
1 files changed, 92 insertions, 12 deletions
diff --git a/components/pm-subscription-handler/tests/test_subscription.py b/components/pm-subscription-handler/tests/test_subscription.py index cbf930fd..3c7651d4 100644..100755 --- a/components/pm-subscription-handler/tests/test_subscription.py +++ b/components/pm-subscription-handler/tests/test_subscription.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019 Nordix Foundation. +# Copyright (C) 2019-2020 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -18,26 +18,106 @@ import json import os import unittest +from test.support import EnvironmentVarGuard +from unittest import mock -from mod.subscription import Subscription, XnfFilter +from requests import Session +import mod.aai_client as aai_client +from mod import db, create_test_app +from mod.network_function import NetworkFunction +from mod.subscription import Subscription, NetworkFunctionFilter -class SubscriptionTestCase(unittest.TestCase): - def setUp(self): - with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data.json'), 'r') as data: - self.cbs_data = json.load(data) - self.sub = Subscription(**self.cbs_data['policy']['subscription']) - self.xnf_filter = XnfFilter(**self.sub.nfFilter) +class SubscriptionTest(unittest.TestCase): + + @mock.patch.object(Session, 'put') + def setUp(self, mock_session): + with open(os.path.join(os.path.dirname(__file__), 'data/aai_xnfs.json'), 'r') as data: + self.aai_response_data = data.read() + mock_session.return_value.status_code = 200 + mock_session.return_value.text = self.aai_response_data + self.env = EnvironmentVarGuard() + self.env.set('AAI_SERVICE_HOST', '1.2.3.4') + self.env.set('AAI_SERVICE_PORT_AAI_SSL', '8443') + with open(os.path.join(os.path.dirname(__file__), 'data/cbs_data_1.json'), 'r') as data: + self.cbs_data_1 = json.load(data) + with open(os.path.join(os.path.dirname(__file__), + 'data/cbs_data_2.json'), 'r') as data: + self.cbs_data_2 = json.load(data) + self.sub_1, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_1) + self.sub_2, self.xnfs = aai_client.get_pmsh_subscription_data(self.cbs_data_2) + self.nf_1 = NetworkFunction(nf_name='pnf_1', orchestration_status='Inventoried') + self.nf_2 = NetworkFunction(nf_name='pnf_2', orchestration_status='Active') + self.xnf_filter = NetworkFunctionFilter(**self.sub_1.nfFilter) + self.app = create_test_app() + self.app_context = self.app.app_context() + self.app_context.push() + db.create_all() + + def tearDown(self): + db.session.remove() + db.drop_all() + self.app_context.pop() def test_xnf_filter_true(self): - self.assertTrue(self.xnf_filter.is_xnf_in_filter('pnf1')) + self.assertTrue(self.xnf_filter.is_nf_in_filter('pnf1')) def test_xnf_filter_false(self): - self.assertFalse(self.xnf_filter.is_xnf_in_filter('PNF-33')) + self.assertFalse(self.xnf_filter.is_nf_in_filter('PNF-33')) def test_sub_measurement_group(self): - self.assertEqual(len(self.sub.measurementGroups), 2) + self.assertEqual(len(self.sub_1.measurementGroups), 2) def test_sub_file_location(self): - self.assertEqual(self.sub.fileLocation, '/pm/pm.xml') + self.assertEqual(self.sub_1.fileLocation, '/pm/pm.xml') + + def test_get_subscription(self): + sub_name = 'ExtraPM-All-gNB-R2B' + self.sub_1.create() + new_sub = Subscription.get(sub_name) + self.assertEqual(sub_name, new_sub.subscription_name) + + def test_get_subscription_no_match(self): + sub_name = 'sub2_does_not_exist' + sub = Subscription.get(sub_name) + self.assertEqual(sub, None) + + def test_get_subscriptions(self): + self.sub_1.create() + self.sub_2.create() + subs = self.sub_1.get_all() + + self.assertEqual(2, len(subs)) + + def test_create_existing_subscription(self): + sub1 = self.sub_1.create() + same_sub1 = self.sub_1.create() + self.assertEqual(sub1, same_sub1) + self.assertEqual(1, len(self.sub_1.get_all())) + + def test_get_nfs_per_subscription(self): + nf_array = [self.nf_1, self.nf_2] + self.sub_1.add_network_functions_to_subscription(nf_array) + nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations() + self.assertEqual(2, len(nfs_for_sub_1)) + + def test_add_network_functions_per_subscription(self): + nf_array = [self.nf_1, self.nf_2] + self.sub_1.add_network_functions_to_subscription(nf_array) + nfs_for_sub_1 = Subscription.get_all_nfs_subscription_relations() + self.assertEqual(2, len(nfs_for_sub_1)) + new_nf_array = [NetworkFunction(nf_name='vnf_3', orchestration_status='Inventoried')] + self.sub_1.add_network_functions_to_subscription(new_nf_array) + nf_subs = Subscription.get_all_nfs_subscription_relations() + print(nf_subs) + self.assertEqual(3, len(nf_subs)) + + def test_add_duplicate_network_functions_per_subscription(self): + nf_array = [self.nf_1] + self.sub_1.add_network_functions_to_subscription(nf_array) + nf_subs = Subscription.get_all_nfs_subscription_relations() + self.assertEqual(1, len(nf_subs)) + self.sub_1.add_network_functions_to_subscription(nf_array) + nf_subs = Subscription.get_all_nfs_subscription_relations() + self.assertEqual(1, len(nf_subs)) |