Revert "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 / 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
47     val log = logger(ImperativeWorkflowExecutionServiceTest::class)
48
49     @Before
50     fun init() {
51         mockkObject(BluePrintDependencyService)
52         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
53     }
54
55     @After
56     fun afterTests() {
57         unmockkAll()
58     }
59
60     fun mockServiceTemplate(): ServiceTemplate {
61         return serviceTemplate(
62             "imperative-test", "1.0.0",
63             "brindasanth@onap.com", "tosca"
64         ) {
65
66             topologyTemplate {
67                 nodeTemplate(
68                     mockNodeTemplateComponentScriptExecutor(
69                         "resolve-config",
70                         "cba.wt.imperative.test.ResolveConfig"
71                     )
72                 )
73                 nodeTemplate(
74                     mockNodeTemplateComponentScriptExecutor(
75                         "activate-config",
76                         "cba.wt.imperative.test.ActivateConfig"
77                     )
78                 )
79                 nodeTemplate(
80                     mockNodeTemplateComponentScriptExecutor(
81                         "activate-config-rollback",
82                         "cba.wt.imperative.test.ActivateConfigRollback"
83                     )
84                 )
85                 nodeTemplate(
86                     mockNodeTemplateComponentScriptExecutor(
87                         "activate-licence",
88                         "cba.wt.imperative.test.ActivateLicence"
89                     )
90                 )
91
92                 workflow("imperative-test-wf", "Test Imperative flow") {
93                     step("resolve-config", "resolve-config", "") {
94                         success("activate-config")
95                     }
96                     step("activate-config", "activate-config", "") {
97                         success("activate-licence")
98                         failure("activate-config-rollback")
99                     }
100                     step("activate-config-rollback", "activate-config-rollback", "")
101                     step("activate-licence", "activate-licence", "")
102                 }
103             }
104             nodeType(BluePrintTypes.nodeTypeComponentScriptExecutor())
105         }
106     }
107
108     @Test
109     fun testImperativeExecutionService() {
110         runBlocking {
111             val serviceTemplate = mockServiceTemplate()
112             val bluePrintContext = BluePrintContext(serviceTemplate)
113             bluePrintContext.rootPath = normalizedPathName(".")
114             bluePrintContext.entryDefinition = "cba.imperative.test.ImperativeTestDefinitions.kt"
115             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("12345", bluePrintContext)
116
117             val executionServiceInput = JacksonUtils
118                 .readValueFromClassPathFile(
119                     "execution-input/imperative-test-input.json",
120                     ExecutionServiceInput::class.java
121                 )!!
122
123             val imperativeWorkflowExecutionService = ImperativeWorkflowExecutionService(NodeTemplateExecutionService(mockk()))
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 }