| 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 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 19 | from osm_lcm.temporal.ns_activities import GetNsRecordImpl, GetVnfDetailsImpl |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 20 | from osm_common.dbbase import DbException |
| 21 | from temporalio.testing import ActivityEnvironment |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 22 | from unittest.mock import Mock |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 23 | |
| 24 | ns_uuid = "00000000-0000-0000-0000-000000000000" |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 25 | get_vnf_details_input = GetVnfDetailsImpl.Input(ns_uuid=ns_uuid) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 26 | sample_vnf_details = [ |
| 27 | { |
| 28 | "id": "00000000-0000-0000-0000-000000000000", |
| 29 | "member-vnf-index-ref": "vnf1", |
| 30 | }, |
| 31 | { |
| 32 | "id": "00000000-0000-0000-0000-000000000000", |
| 33 | "member-vnf-index-ref": "vnf2", |
| 34 | }, |
| 35 | ] |
| 36 | sample_nsr = { |
| 37 | "_id": ns_uuid, |
| 38 | "name": "sol006_juju24", |
| 39 | "name-ref": "sol006_juju24", |
| 40 | "short-name": "sol006_juju24", |
| 41 | "admin-status": "ENABLED", |
| 42 | "nsState": "NOT_INSTANTIATED", |
| 43 | "currentOperation": "IDLE", |
| 44 | } |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 45 | |
| 46 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 47 | class TestException(Exception): |
| 48 | pass |
| 49 | |
| 50 | |
| 51 | class TestGetVnfDetails(asynctest.TestCase): |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 52 | def setUp(self): |
| 53 | self.db = Mock() |
| 54 | self.env = ActivityEnvironment() |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 55 | self.get_vnf_details_impl = GetVnfDetailsImpl(self.db) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 56 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 57 | async def test_activity__succeded__get_expected_result(self): |
| 58 | self.db.get_list.return_value = sample_vnf_details |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 59 | result = await self.env.run(self.get_vnf_details_impl, get_vnf_details_input) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 60 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 61 | self.assertEqual( |
| 62 | result.vnf_details, |
| 63 | [ |
| 64 | ("00000000-0000-0000-0000-000000000000", "vnf1"), |
| 65 | ("00000000-0000-0000-0000-000000000000", "vnf2"), |
| 66 | ], |
| 67 | ) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 68 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 69 | async def test_activity__failed__raise_db_exception(self): |
| 70 | self.db.get_list.side_effect = DbException("not found.") |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 71 | with self.assertRaises(DbException): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 72 | await self.env.run(self.get_vnf_details_impl, get_vnf_details_input) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class TestGetNsRecord(asynctest.TestCase): |
| 76 | async def setUp(self): |
| 77 | self.db = Mock() |
| 78 | self.env = ActivityEnvironment() |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 79 | self.get_ns_record_impl = GetNsRecordImpl(self.db) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 80 | |
| 81 | async def test_activity__succeeded__get_expected_result(self): |
| 82 | self.db.get_one.return_value = sample_nsr |
| 83 | activity_result = await self.env.run( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 84 | self.get_ns_record_impl, |
| 85 | GetNsRecordImpl.Input(nsr_uuid=sample_nsr["_id"]), |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 86 | ) |
| 87 | self.assertEqual(activity_result.nsr, sample_nsr) |
| 88 | |
| 89 | async def test_activity__failed__raise_test_exception(self): |
| 90 | self.db.get_one.side_effect = TestException("Can not connect to Database.") |
| 91 | with self.assertRaises(TestException): |
| 92 | await self.env.run( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 93 | self.get_ns_record_impl, |
| 94 | GetNsRecordImpl.Input(nsr_uuid=sample_nsr["_id"]), |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 95 | ) |