a651dad5219df60b170df16f4a6e7d829593e215
[ccsdk/cds.git] /
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 kotlinx.coroutines.runBlocking
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
38 import org.springframework.context.annotation.ComponentScan
39 import org.springframework.test.annotation.DirtiesContext
40 import org.springframework.test.context.TestPropertySource
41 import org.springframework.test.context.junit4.SpringRunner
42 import kotlin.test.assertNotNull
43
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
46 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
47 @DirtiesContext
48 @TestPropertySource(properties =
49 ["server.port=9111",
50     "blueprintsprocessor.restconfEnabled=true",
51     "blueprintsprocessor.restclient.odlPrimary.type=basic-auth",
52     "blueprintsprocessor.restclient.odlPrimary.url=http://127.0.0.1:9111",
53     "blueprintsprocessor.restclient.odlPrimary.userId=sampleuser",
54     "blueprintsprocessor.restclient.odlPrimary.token=sampletoken"],
55         locations = ["classpath:application-test.properties"])
56 class ComponentRestconfExecutorTest {
57
58     @Autowired
59     lateinit var componentRestconfExecutor: ComponentRestconfExecutor
60
61     @Test
62     fun `test Restconf Component Instance`() {
63         runBlocking {
64             assertNotNull(componentRestconfExecutor, "failed to get ComponentRestconfExecutor instance")
65             val executionServiceInput = ExecutionServiceInput().apply {
66                 commonHeader = CommonHeader().apply {
67                     requestId = "1234"
68                 }
69                 actionIdentifiers = ActionIdentifiers().apply {
70                     actionName = "activate"
71                 }
72                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
73             }
74             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
75             componentRestconfExecutor.bluePrintRuntimeService = bluePrintRuntime
76             componentRestconfExecutor.stepName = "sample-step"
77
78             val operationInputs = hashMapOf<String, JsonNode>()
79             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-restconf".asJsonPrimitive()
80             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
81             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
82             operationInputs[ComponentRestconfExecutor.SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
83             operationInputs[ComponentRestconfExecutor.SCRIPT_CLASS_REFERENCE] =
84                     "InternalSimpleRestconf_cba\$TestRestconfConfigure".asJsonPrimitive()
85             operationInputs[ComponentRestconfExecutor.INSTANCE_DEPENDENCIES] = JacksonUtils.jsonNode("[]") as ArrayNode
86
87             val stepInputData = StepData().apply {
88                 name = "activate-restconf"
89                 properties = operationInputs
90             }
91             executionServiceInput.stepData = stepInputData
92
93             val blueprintContext = mockk<BluePrintContext>()
94             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
95             every {
96                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs("activate-restconf",
97                         "interfaceName", "operationName")
98             } returns operationInputs
99
100             val operationOutputs = hashMapOf<String, JsonNode>()
101             every {
102                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs("activate-restconf",
103                         "interfaceName", "operationName")
104             } returns operationOutputs
105
106             componentRestconfExecutor.applyNB(executionServiceInput)
107         }
108     }
109 }