becd228572f58c7f61fbdfd5adf8fbc3324c5533
[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.core.BluePrintTypes
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
31 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.serviceTemplate
32 import org.onap.ccsdk.cds.controllerblueprints.core.logger
33 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
35 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
38 import kotlin.test.Test
39 import kotlin.test.assertNotNull
40
41 class ImperativeWorkflowExecutionServiceTest {
42     val log = logger(ImperativeWorkflowExecutionServiceTest::class)
43
44     @Before
45     fun init() {
46         mockkObject(BluePrintDependencyService)
47         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
48     }
49
50     @After
51     fun afterTests() {
52         unmockkAll()
53     }
54
55     fun mockServiceTemplate(): ServiceTemplate {
56         return serviceTemplate("imperative-test", "1.0.0",
57                 "brindasanth@onap.com", "tosca") {
58
59             topologyTemplate {
60                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("resolve-config",
61                         "cba.wt.imperative.test.ResolveConfig"))
62                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-config",
63                         "cba.wt.imperative.test.ActivateConfig"))
64                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-config-rollback",
65                         "cba.wt.imperative.test.ActivateConfigRollback"))
66                 nodeTemplate(mockNodeTemplateComponentScriptExecutor("activate-licence",
67                         "cba.wt.imperative.test.ActivateLicence"))
68
69                 workflow("imperative-test-wf", "Test Imperative flow") {
70                     step("resolve-config", "resolve-config", "") {
71                         success("activate-config")
72                     }
73                     step("activate-config", "activate-config", "") {
74                         success("activate-licence")
75                         failure("activate-config-rollback")
76                     }
77                     step("activate-config-rollback", "activate-config-rollback", "")
78                     step("activate-licence", "activate-licence", "")
79                 }
80             }
81             nodeType(BluePrintTypes.nodeTypeComponentScriptExecutor())
82         }
83     }
84
85     @Test
86     fun testImperativeExecutionService() {
87         runBlocking {
88             val serviceTemplate = mockServiceTemplate()
89             val bluePrintContext = BluePrintContext(serviceTemplate)
90             bluePrintContext.rootPath = normalizedPathName(".")
91             bluePrintContext.entryDefinition = "cba.imperative.test.ImperativeTestDefinitions.kt"
92             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("12345", bluePrintContext)
93
94             val executionServiceInput = JacksonUtils
95                     .readValueFromClassPathFile("execution-input/imperative-test-input.json",
96                             ExecutionServiceInput::class.java)!!
97
98             val bluePrintWorkFlowService = ImperativeBluePrintWorkflowService(NodeTemplateExecutionService())
99             val imperativeWorkflowExecutionService = ImperativeWorkflowExecutionService(bluePrintWorkFlowService)
100             val output = imperativeWorkflowExecutionService
101                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
102             assertNotNull(output)
103         }
104     }
105 }