415f11d58f6cc289e0abd4273461a2486b60e939
[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(
60             "imperative-test", "1.0.0",
61             "brindasanth@onap.com", "tosca"
62         ) {
63
64             topologyTemplate {
65                 nodeTemplate(
66                     mockNodeTemplateComponentScriptExecutor(
67                         "resolve-config",
68                         "cba.wt.imperative.test.ResolveConfig"
69                     )
70                 )
71                 nodeTemplate(
72                     mockNodeTemplateComponentScriptExecutor(
73                         "activate-config",
74                         "cba.wt.imperative.test.ActivateConfig"
75                     )
76                 )
77                 nodeTemplate(
78                     mockNodeTemplateComponentScriptExecutor(
79                         "activate-config-rollback",
80                         "cba.wt.imperative.test.ActivateConfigRollback"
81                     )
82                 )
83                 nodeTemplate(
84                     mockNodeTemplateComponentScriptExecutor(
85                         "activate-licence",
86                         "cba.wt.imperative.test.ActivateLicence"
87                     )
88                 )
89
90                 workflow("imperative-test-wf", "Test Imperative flow") {
91                     step("resolve-config", "resolve-config", "") {
92                         success("activate-config")
93                     }
94                     step("activate-config", "activate-config", "") {
95                         success("activate-licence")
96                         failure("activate-config-rollback")
97                     }
98                     step("activate-config-rollback", "activate-config-rollback", "")
99                     step("activate-licence", "activate-licence", "")
100                 }
101             }
102             nodeType(BluePrintTypes.nodeTypeComponentScriptExecutor())
103         }
104     }
105
106     @Test
107     fun testImperativeExecutionService() {
108         runBlocking {
109             val serviceTemplate = mockServiceTemplate()
110             val bluePrintContext = BluePrintContext(serviceTemplate)
111             bluePrintContext.rootPath = normalizedPathName(".")
112             bluePrintContext.entryDefinition = "cba.imperative.test.ImperativeTestDefinitions.kt"
113             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("12345", bluePrintContext)
114
115             val executionServiceInput = JacksonUtils
116                 .readValueFromClassPathFile(
117                     "execution-input/imperative-test-input.json",
118                     ExecutionServiceInput::class.java
119                 )!!
120
121             val bluePrintWorkFlowService = ImperativeBluePrintWorkflowService(NodeTemplateExecutionService())
122             val imperativeWorkflowExecutionService = ImperativeWorkflowExecutionService(bluePrintWorkFlowService)
123             val output = imperativeWorkflowExecutionService
124                 .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
125             assertNotNull(output, "failed to get imperative workflow output")
126             assertNotNull(output.status, "failed to get imperative workflow output status")
127             assertEquals(output.status.message, BluePrintConstants.STATUS_SUCCESS)
128             assertEquals(output.status.eventType, EventType.EVENT_COMPONENT_EXECUTED.name)
129         }
130     }
131 }