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