Fix missing status event type workflow response.
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / ImperativeWorkflowExecutionServiceTest.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 io.mockk.every
20 import io.mockk.mockkObject
21 import io.mockk.unmockkAll
22 import kotlinx.coroutines.runBlocking
23 import org.junit.After
24 import org.junit.Before
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.nodeTypeComponentScriptExecutor
27 import org.onap.ccsdk.cds.blueprintsprocessor.services.workflow.mock.MockComponentFunction
28 import org.onap.ccsdk.cds.blueprintsprocessor.services.workflow.mock.mockNodeTemplateComponentScriptExecutor
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
33 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.serviceTemplate
34 import org.onap.ccsdk.cds.controllerblueprints.core.logger
35 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import kotlin.test.Test
41 import kotlin.test.assertEquals
42 import kotlin.test.assertNotNull
43
44 class ImperativeWorkflowExecutionServiceTest {
45     val log = logger(ImperativeWorkflowExecutionServiceTest::class)
46
47     @Before
48     fun init() {
49         mockkObject(BluePrintDependencyService)
50         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
51     }
52
53     @After
54     fun afterTests() {
55         unmockkAll()
56     }
57
58     fun mockServiceTemplate(): ServiceTemplate {
59         return serviceTemplate("imperative-test", "1.0.0",
60                 "brindasanth@onap.com", "tosca") {
61
62             topologyTemplate {
63                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("resolve-config",
64                         "cba.wt.imperative.test.ResolveConfig"))
65                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-config",
66                         "cba.wt.imperative.test.ActivateConfig"))
67                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-config-rollback",
68                         "cba.wt.imperative.test.ActivateConfigRollback"))
69                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-licence",
70                         "cba.wt.imperative.test.ActivateLicence"))
71
72                 workflow("imperative-test-wf", "Test Imperative flow") {
73                     step("resolve-config", "resolve-config", "") {
74                         success("activate-config")
75                     }
76                     step("activate-config", "activate-config", "") {
77                         success("activate-licence")
78                         failure("activate-config-rollback")
79                     }
80                     step("activate-config-rollback", "activate-config-rollback", "")
81                     step("activate-licence", "activate-licence", "")
82                 }
83             }
84             nodeType(BluePrintTypes.nodeTypeComponentScriptExecutor())
85         }
86     }
87
88     @Test
89     fun testImperativeExecutionService() {
90         runBlocking {
91             val serviceTemplate = mockServiceTemplate()
92             val bluePrintContext = BluePrintContext(serviceTemplate)
93             bluePrintContext.rootPath = normalizedPathName(".")
94             bluePrintContext.entryDefinition = "cba.imperative.test.ImperativeTestDefinitions.kt"
95             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("12345", bluePrintContext)
96
97             val executionServiceInput = JacksonUtils
98                     .readValueFromClassPathFile("execution-input/imperative-test-input.json",
99                             ExecutionServiceInput::class.java)!!
100
101             val bluePrintWorkFlowService = ImperativeBluePrintWorkflowService(NodeTemplateExecutionService())
102             val imperativeWorkflowExecutionService = ImperativeWorkflowExecutionService(bluePrintWorkFlowService)
103             val output = imperativeWorkflowExecutionService
104                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
105             assertNotNull(output, "failed to get imperative workflow output")
106             assertNotNull(output.status, "failed to get imperative workflow output status")
107             assertEquals(output.status.message, BluePrintConstants.STATUS_SUCCESS)
108             assertEquals(output.status.eventType, EventType.EVENT_COMPONENT_EXECUTED.name)
109         }
110     }
111 }