2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.workflow
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ObjectNode
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
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
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(classes = [WorkflowServiceConfiguration::class])
58 class BlueprintWorkflowExecutionServiceImplTest {
61 lateinit var bluePrintWorkflowExecutionService: BlueprintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>
64 lateinit var bluePrintClusterService: BlueprintClusterService
68 mockkObject(BlueprintDependencyService)
69 every { BlueprintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
78 fun testBlueprintWorkflowExecutionService() {
80 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
82 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
85 val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
86 "execution-input/resource-assignment-input.json",
87 ExecutionServiceInput::class.java
90 val executionServiceOutput = bluePrintWorkflowExecutionService
91 .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
93 assertNotNull(executionServiceOutput, "failed to get response")
95 BlueprintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
96 "failed to get successful response"
102 fun testImperativeBlueprintWorkflowExecutionService() {
104 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
106 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
109 val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
110 "execution-input/imperative-test-input.json",
111 ExecutionServiceInput::class.java
114 val executionServiceOutput = bluePrintWorkflowExecutionService
115 .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
117 assertNotNull(executionServiceOutput, "failed to get response")
119 BlueprintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
120 "failed to get successful response"
126 fun `Blueprint fails on missing workflowName-parameters with a useful message`() {
127 assertFailsWith(exceptionClass = BlueprintProcessorException::class) {
129 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
131 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
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
140 val executionServiceOutput = bluePrintWorkflowExecutionService
141 .executeBlueprintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
147 fun `Should handle errors from resolve workflow output`() {
148 val imperativeWorkflowExecutionService: ImperativeWorkflowExecutionService = mockk()
149 val bluePrintWorkflowExecutionServiceImpl = BlueprintWorkflowExecutionServiceImpl(
150 mockk(), mockk(), imperativeWorkflowExecutionService
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
159 val workflow = Workflow().apply {
160 this.steps = mutableMapOf("one" to Step(), "two" to Step())
162 val blueprintError = BlueprintError()
164 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
165 every { bluePrintRuntimeService.assignWorkflowInputs(any(), any()) } returns Unit
166 every { bluePrintContext.workflowByName(any()) } returns workflow
168 bluePrintRuntimeService.resolveWorkflowOutputs(any())
169 } throws RuntimeException("failed to resolve property...")
172 imperativeWorkflowExecutionService.executeBlueprintWorkflow(any(), any(), any())
174 } returns ExecutionServiceOutput()
175 every { bluePrintRuntimeService.getBlueprintError() } returns blueprintError
178 val output = bluePrintWorkflowExecutionServiceImpl.executeBlueprintWorkflow(
179 bluePrintRuntimeService, executionServiceInput, mutableMapOf()
181 assertEquals("failed to resolve property...", blueprintError.errors[0])
182 assertEquals("""{"config-assign-response":{}}""".asJsonType(), output.payload)