Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / BlueprintWorkflowExecutionServiceImplTest.kt
1 /*
2  *  Copyright © 2019 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.services.workflow
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ObjectNode
21 import io.mockk.every
22 import io.mockk.mockk
23 import io.mockk.mockkObject
24 import io.mockk.unmockkAll
25 import kotlinx.coroutines.runBlocking
26 import org.junit.After
27 import org.junit.Before
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.ExecutionServiceOutput
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BlueprintClusterService
35 import org.onap.ccsdk.cds.blueprintsprocessor.services.workflow.mock.MockComponentFunction
36 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
37 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintError
38 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
39 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
40 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
41 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
42 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintWorkflowExecutionService
43 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
44 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintDependencyService
45 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
46 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BlueprintMetadataUtils
47 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
48 import org.springframework.beans.factory.annotation.Autowired
49 import org.springframework.boot.test.mock.mockito.MockBean
50 import org.springframework.test.context.ContextConfiguration
51 import org.springframework.test.context.junit4.SpringRunner
52 import kotlin.test.assertEquals
53 import kotlin.test.assertFailsWith
54 import kotlin.test.assertNotNull
55
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(classes = [WorkflowServiceConfiguration::class])
58 class BlueprintWorkflowExecutionServiceImplTest {
59
60     @Autowired
61     lateinit var bluePrintWorkflowExecutionService: BlueprintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>
62
63     @MockBean
64     lateinit var bluePrintClusterService: BlueprintClusterService
65
66     @Before
67     fun init() {
68         mockkObject(BlueprintDependencyService)
69         every { BlueprintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
70     }
71
72     @After
73     fun afterTests() {
74         unmockkAll()
75     }
76
77     @Test
78     fun testBlueprintWorkflowExecutionService() {
79         runBlocking {
80             val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
81                 "1234",
82                 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
83             )
84
85             val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
86                 "execution-input/resource-assignment-input.json",
87                 ExecutionServiceInput::class.java
88             )!!
89
90             val executionServiceOutput = bluePrintWorkflowExecutionService
91                 .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
92
93             assertNotNull(executionServiceOutput, "failed to get response")
94             assertEquals(
95                 BlueprintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
96                 "failed to get successful response"
97             )
98         }
99     }
100
101     @Test
102     fun testImperativeBlueprintWorkflowExecutionService() {
103         runBlocking {
104             val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
105                 "1234",
106                 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
107             )
108
109             val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
110                 "execution-input/imperative-test-input.json",
111                 ExecutionServiceInput::class.java
112             )!!
113
114             val executionServiceOutput = bluePrintWorkflowExecutionService
115                 .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
116
117             assertNotNull(executionServiceOutput, "failed to get response")
118             assertEquals(
119                 BlueprintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
120                 "failed to get successful response"
121             )
122         }
123     }
124
125     @Test
126     fun `Blueprint fails on missing workflowName-parameters with a useful message`() {
127         assertFailsWith(exceptionClass = BlueprintProcessorException::class) {
128             runBlocking {
129                 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
130                     "1234",
131                     "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
132                 )
133                 // service input will have a mislabeled input params, we are expecting to get an error when that happens with a useful error message
134                 val executionServiceInput =
135                     JacksonUtils.readValueFromClassPathFile(
136                         "execution-input/resource-assignment-input-missing-resource_assignment_request.json",
137                         ExecutionServiceInput::class.java
138                     )!!
139
140                 val executionServiceOutput = bluePrintWorkflowExecutionService
141                     .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
142             }
143         }
144     }
145
146     @Test
147     fun `Should handle errors from resolve workflow output`() {
148         val imperativeWorkflowExecutionService: ImperativeWorkflowExecutionService = mockk()
149         val bluePrintWorkflowExecutionServiceImpl = BlueprintWorkflowExecutionServiceImpl(
150             mockk(), mockk(), imperativeWorkflowExecutionService
151         )
152         val bluePrintRuntimeService: BlueprintRuntimeService<MutableMap<String, JsonNode>> = mockk()
153         val bluePrintContext: BlueprintContext = mockk()
154         val executionServiceInput = ExecutionServiceInput().apply {
155             this.actionIdentifiers = ActionIdentifiers().apply { this.actionName = "config-assign" }
156             this.commonHeader = CommonHeader()
157             this.payload = """{"config-assign-request": {}}""".asJsonType() as ObjectNode
158         }
159         val workflow = Workflow().apply {
160             this.steps = mutableMapOf("one" to Step(), "two" to Step())
161         }
162         val blueprintError = BlueprintError()
163
164         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
165         every { bluePrintRuntimeService.assignWorkflowInputs(any(), any()) } returns Unit
166         every { bluePrintContext.workflowByName(any()) } returns workflow
167         every {
168             bluePrintRuntimeService.resolveWorkflowOutputs(any())
169         } throws RuntimeException("failed to resolve property...")
170         every {
171             runBlocking {
172                 imperativeWorkflowExecutionService.executeBlueprintWorkflow(any(), any(), any())
173             }
174         } returns ExecutionServiceOutput()
175         every { bluePrintRuntimeService.getBlueprintError() } returns blueprintError
176
177         runBlocking {
178             val output = bluePrintWorkflowExecutionServiceImpl.executeBlueprintWorkflow(
179                 bluePrintRuntimeService, executionServiceInput, mutableMapOf()
180             )
181             assertEquals("failed to resolve property...", blueprintError.errors[0])
182             assertEquals("""{"config-assign-response":{}}""".asJsonType(), output.payload)
183         }
184     }
185 }