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