Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restconf / executor / ComponentRestconfExecutorTest.kt
1 /*
2  *  Copyright © 2018 IBM.
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 implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ArrayNode
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import io.mockk.every
23 import io.mockk.mockk
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
30 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
31 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
37 import org.springframework.context.annotation.ComponentScan
38 import org.springframework.test.annotation.DirtiesContext
39 import org.springframework.test.context.TestPropertySource
40 import org.springframework.test.context.junit4.SpringRunner
41 import kotlin.test.assertNotNull
42
43 @RunWith(SpringRunner::class)
44 @EnableAutoConfiguration
45 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
46 @DirtiesContext
47 @TestPropertySource(properties =
48 ["server.port=9111",
49     "blueprintsprocessor.restconfEnabled=true",
50     "blueprintsprocessor.restclient.odlPrimary.type=basic-auth",
51     "blueprintsprocessor.restclient.odlPrimary.url=http://127.0.0.1:9111",
52     "blueprintsprocessor.restclient.odlPrimary.userId=sampleuser",
53     "blueprintsprocessor.restclient.odlPrimary.token=sampletoken"],
54     locations = ["classpath:application-test.properties"])
55 class ComponentRestconfExecutorTest {
56
57     @Autowired
58     lateinit var componentRestconfExecutor: ComponentRestconfExecutor
59
60     @Test
61     fun `test Restconf Component Instance`() {
62         assertNotNull(componentRestconfExecutor, "failed to get ComponentRestconfExecutor instance")
63         val executionServiceInput = ExecutionServiceInput().apply {
64             commonHeader = CommonHeader().apply {
65                 requestId = "1234"
66             }
67             actionIdentifiers = ActionIdentifiers().apply {
68                 actionName = "activate"
69             }
70             payload = JacksonUtils.jsonNode("{}") as ObjectNode
71         }
72         val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
73         componentRestconfExecutor.bluePrintRuntimeService = bluePrintRuntime
74         componentRestconfExecutor.stepName = "sample-step"
75
76         val operationInputs = hashMapOf<String, JsonNode>()
77         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-restconf".asJsonPrimitive()
78         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
79         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
80         operationInputs[ComponentRestconfExecutor.SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
81         operationInputs[ComponentRestconfExecutor.SCRIPT_CLASS_REFERENCE] =
82             "InternalSimpleRestconf_cba\$TestRestconfConfigure".asJsonPrimitive()
83         operationInputs[ComponentRestconfExecutor.INSTANCE_DEPENDENCIES] = JacksonUtils.jsonNode("[]") as ArrayNode
84
85         val blueprintContext = mockk<BluePrintContext>()
86         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
87         every { bluePrintRuntime.get("sample-step-step-inputs") } returns operationInputs.asJsonNode()
88         every {
89             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs("activate-restconf",
90                 "interfaceName", "operationName")
91         } returns operationInputs
92
93         val operationOutputs = hashMapOf<String, JsonNode>()
94         every {
95             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs("activate-restconf",
96                 "interfaceName", "operationName")
97         } returns operationOutputs
98         every { bluePrintRuntime.put("sample-step-step-outputs", any()) } returns Unit
99
100         componentRestconfExecutor.apply(executionServiceInput)
101     }
102 }