| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 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 |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 17 | import asynctest |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 18 | |
| 19 | from osm_common.dataclasses.temporal_dataclasses import ( |
| 20 | GetNsRecordInput, |
| 21 | GetVnfDetailsInput, |
| 22 | ) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 23 | from osm_common.dbbase import DbException |
| 24 | from temporalio.testing import ActivityEnvironment |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 25 | from osm_lcm.temporal.ns_activities import NsOperations |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 26 | from unittest.mock import Mock |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 27 | |
| 28 | ns_uuid = "00000000-0000-0000-0000-000000000000" |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 29 | get_vnf_details_input = GetVnfDetailsInput(ns_uuid=ns_uuid) |
| 30 | sample_vnf_details = [ |
| 31 | { |
| 32 | "id": "00000000-0000-0000-0000-000000000000", |
| 33 | "member-vnf-index-ref": "vnf1", |
| 34 | }, |
| 35 | { |
| 36 | "id": "00000000-0000-0000-0000-000000000000", |
| 37 | "member-vnf-index-ref": "vnf2", |
| 38 | }, |
| 39 | ] |
| 40 | sample_nsr = { |
| 41 | "_id": ns_uuid, |
| 42 | "name": "sol006_juju24", |
| 43 | "name-ref": "sol006_juju24", |
| 44 | "short-name": "sol006_juju24", |
| 45 | "admin-status": "ENABLED", |
| 46 | "nsState": "NOT_INSTANTIATED", |
| 47 | "currentOperation": "IDLE", |
| 48 | } |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 49 | |
| 50 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 51 | class TestException(Exception): |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | class TestGetVnfDetails(asynctest.TestCase): |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 56 | def setUp(self): |
| 57 | self.db = Mock() |
| 58 | self.env = ActivityEnvironment() |
| 59 | self.ns_operations_activity = NsOperations(self.db) |
| 60 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 61 | async def test_activity__succeded__get_expected_result(self): |
| 62 | self.db.get_list.return_value = sample_vnf_details |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 63 | result = await self.env.run( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 64 | self.ns_operations_activity.get_vnf_details, get_vnf_details_input |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 65 | ) |
| 66 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 67 | self.assertEqual( |
| 68 | result.vnf_details, |
| 69 | [ |
| 70 | ("00000000-0000-0000-0000-000000000000", "vnf1"), |
| 71 | ("00000000-0000-0000-0000-000000000000", "vnf2"), |
| 72 | ], |
| 73 | ) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 74 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 75 | async def test_activity__failed__raise_db_exception(self): |
| 76 | self.db.get_list.side_effect = DbException("not found.") |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 77 | with self.assertRaises(DbException): |
| 78 | await self.env.run( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 79 | self.ns_operations_activity.get_vnf_details, get_vnf_details_input |
| 80 | ) |
| 81 | |
| 82 | |
| 83 | class TestGetNsRecord(asynctest.TestCase): |
| 84 | async def setUp(self): |
| 85 | self.db = Mock() |
| 86 | self.env = ActivityEnvironment() |
| 87 | self.ns_operations_activity = NsOperations(self.db) |
| 88 | |
| 89 | async def test_activity__succeeded__get_expected_result(self): |
| 90 | self.db.get_one.return_value = sample_nsr |
| 91 | activity_result = await self.env.run( |
| 92 | self.ns_operations_activity.get_ns_record, |
| 93 | GetNsRecordInput(nsr_uuid=sample_nsr["_id"]), |
| 94 | ) |
| 95 | self.assertEqual(activity_result.nsr, sample_nsr) |
| 96 | |
| 97 | async def test_activity__failed__raise_test_exception(self): |
| 98 | self.db.get_one.side_effect = TestException("Can not connect to Database.") |
| 99 | with self.assertRaises(TestException): |
| 100 | await self.env.run( |
| 101 | self.ns_operations_activity.get_ns_record, |
| 102 | GetNsRecordInput(nsr_uuid=sample_nsr["_id"]), |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 103 | ) |