Merge "UT for execution service"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / scripts / AbstractComponentFunctionTest.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts;
22
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.node.ObjectNode
25 import io.mockk.every
26 import io.mockk.mockk
27 import kotlinx.coroutines.runBlocking
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
34 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
37 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
38 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
41 import org.springframework.test.context.junit4.SpringRunner
42 import kotlin.test.BeforeTest
43 import kotlin.test.assertEquals
44 import kotlin.test.assertNotNull
45
46 /**
47  * Unit test cases for abstract component function.
48  */
49 @RunWith(SpringRunner::class)
50 class AbstractComponentFunctionTest {
51
52     lateinit var blueprintContext: BluePrintContext
53
54     @BeforeTest
55     fun init() {
56         blueprintContext = mockk<BluePrintContext>()
57         every { blueprintContext.rootPath } returns normalizedPathName("target")
58     }
59
60     /**
61      * Tests the abstract component functionality.
62      */
63     @Test
64     fun testAbstractComponent() {
65         runBlocking {
66             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
67             val samp = SampleComponent()
68             val comp = samp as AbstractComponentFunction
69
70             comp.bluePrintRuntimeService = bluePrintRuntime
71             comp.stepName = "sample-step"
72             assertNotNull(comp, "failed to get kotlin instance")
73
74             val input = getMockedInput(bluePrintRuntime)
75
76             val output = comp.applyNB(input)
77
78             assertEquals(output.actionIdentifiers.actionName, "activate")
79             assertEquals(output.commonHeader.requestId, "1234")
80             assertEquals(output.stepData!!.name, "activate-restconf")
81             assertEquals(output.status.message, "success")
82         }
83     }
84
85
86     /**
87      * Mocked input for abstract function test.
88      */
89     private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
90             ExecutionServiceInput {
91         val operationInputs = hashMapOf<String, JsonNode>()
92         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
93                 "activate-restconf".asJsonPrimitive()
94         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
95                 "interfaceName".asJsonPrimitive()
96         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
97                 "operationName".asJsonPrimitive()
98
99         val stepInputData = StepData().apply {
100             name = "activate-restconf"
101             properties = operationInputs
102         }
103         val executionServiceInput = ExecutionServiceInput().apply {
104             commonHeader = CommonHeader().apply {
105                 requestId = "1234"
106             }
107             actionIdentifiers = ActionIdentifiers().apply {
108                 actionName = "activate"
109             }
110             payload = JacksonUtils.jsonNode("{}") as ObjectNode
111         }
112         executionServiceInput.stepData = stepInputData
113
114         every {
115             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
116                     "activate-restconf", "interfaceName", "operationName")
117         } returns operationInputs
118
119         val operationOutputs = hashMapOf<String, JsonNode>()
120         every {
121             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
122                     "activate-restconf", "interfaceName", "operationName")
123         } returns operationOutputs
124         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
125
126         return executionServiceInput
127     }
128 }