OSMENG-987: Implement PrepareVnfWorkflow

Change-Id: Icf3ccfd44160fbc3f2f2d9ec64e360c07fc9bbfa
Signed-off-by: Dario Faccin <dario.faccin@canonical.com>
diff --git a/osm_lcm/tests/test_vnf_activities.py b/osm_lcm/tests/test_vnf_activities.py
new file mode 100644
index 0000000..989a647
--- /dev/null
+++ b/osm_lcm/tests/test_vnf_activities.py
@@ -0,0 +1,48 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import asynctest
+from osm_common.dataclasses.temporal_dataclasses import VnfInstantiateInput
+from osm_common.dbbase import DbException
+from osm_lcm.temporal.vnf_activities import VnfDbActivity
+from temporalio.testing import ActivityEnvironment
+from unittest.mock import Mock
+
+
+vnfr_uuid = "d08d2da5-2120-476c-8538-deaeb4e88b3e"
+model_name = "a-model-name"
+vnf_instantiate_input = VnfInstantiateInput(vnfr_uuid=vnfr_uuid, model_name=model_name)
+
+
+class TestVnfDbActivity(asynctest.TestCase):
+    def setUp(self):
+        self.db = Mock()
+        self.env = ActivityEnvironment()
+        self.vnf_db_activity = VnfDbActivity(self.db)
+
+    async def test_set_vnf_model(self):
+        await self.env.run(self.vnf_db_activity.set_vnf_model, vnf_instantiate_input)
+        self.db.set_one.assert_called_with(
+            "vnfrs", {"_id": vnfr_uuid}, {"namespace": model_name}
+        )
+
+    async def test_db_raises_exception(self):
+        self.db.set_one.side_effect = DbException("not found")
+        with self.assertRaises(DbException):
+            await self.env.run(
+                self.vnf_db_activity.set_vnf_model, vnf_instantiate_input
+            )
diff --git a/osm_lcm/tests/test_vnf_workflows.py b/osm_lcm/tests/test_vnf_workflows.py
new file mode 100644
index 0000000..c5be1e7
--- /dev/null
+++ b/osm_lcm/tests/test_vnf_workflows.py
@@ -0,0 +1,62 @@
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import asynctest
+import logging
+from osm_common.dataclasses.temporal_dataclasses import VnfInstantiateInput
+from osm_common.temporal_constants import (
+    LCM_TASK_QUEUE,
+    ACTIVITY_SET_VNF_MODEL,
+    WORKFLOW_VNF_PREPARE,
+)
+from osm_lcm.temporal.vnf_workflows import VnfPrepareWorkflow
+from temporalio import activity
+from temporalio.testing import WorkflowEnvironment
+from temporalio.worker import Worker
+
+wf_input = VnfInstantiateInput(
+    vnfr_uuid="86b53d92-4f5a-402e-8ac2-585ec6b64698",
+    model_name="a-model-name",
+)
+
+
+@activity.defn(name=ACTIVITY_SET_VNF_MODEL)
+async def set_vnf_model_mocked(set_vnf_model_input: VnfInstantiateInput) -> None:
+    logging.debug(
+        f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
+    )
+
+
+class TestVnfPrepareWorkflow(asynctest.TestCase):
+    async def setUp(self):
+        self.env = await WorkflowEnvironment.start_time_skipping()
+        self.client = self.env.client
+
+    async def test_vnf_prepare_workflow(self):
+        async with self.env:
+            async with Worker(
+                self.client,
+                task_queue=LCM_TASK_QUEUE,
+                workflows=[VnfPrepareWorkflow],
+                activities=[set_vnf_model_mocked],
+            ):
+                await self.client.execute_workflow(
+                    VnfPrepareWorkflow.run,
+                    arg=wf_input,
+                    id=f"{WORKFLOW_VNF_PREPARE}-{wf_input.vnfr_uuid}",
+                    task_queue=LCM_TASK_QUEUE,
+                )