90b7200c9a4f5b907cffa2b87d54949668c2dfaf
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / BluePrintWorkflowExecutionServiceImplTest.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.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BluePrintClusterService
30 import org.onap.ccsdk.cds.blueprintsprocessor.services.workflow.mock.MockComponentFunction
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
33 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.test.mock.mockito.MockBean
39 import org.springframework.test.context.ContextConfiguration
40 import org.springframework.test.context.junit4.SpringRunner
41 import kotlin.test.assertEquals
42 import kotlin.test.assertFailsWith
43 import kotlin.test.assertNotNull
44
45 @RunWith(SpringRunner::class)
46 @ContextConfiguration(classes = [WorkflowServiceConfiguration::class])
47 class BluePrintWorkflowExecutionServiceImplTest {
48
49     @Autowired
50     lateinit var bluePrintWorkflowExecutionService: BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>
51
52     @MockBean
53     lateinit var bluePrintClusterService: BluePrintClusterService
54
55     @Before
56     fun init() {
57         mockkObject(BluePrintDependencyService)
58         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns MockComponentFunction()
59     }
60
61     @After
62     fun afterTests() {
63         unmockkAll()
64     }
65
66     @Test
67     fun testBluePrintWorkflowExecutionService() {
68         runBlocking {
69             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
70                 "1234",
71                 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
72             )
73
74             val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
75                 "execution-input/resource-assignment-input.json",
76                 ExecutionServiceInput::class.java
77             )!!
78
79             val executionServiceOutput = bluePrintWorkflowExecutionService
80                 .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
81
82             assertNotNull(executionServiceOutput, "failed to get response")
83             assertEquals(
84                 BluePrintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
85                 "failed to get successful response"
86             )
87         }
88     }
89
90     @Test
91     fun testImperativeBluePrintWorkflowExecutionService() {
92         runBlocking {
93             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
94                 "1234",
95                 "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
96             )
97
98             val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
99                 "execution-input/imperative-test-input.json",
100                 ExecutionServiceInput::class.java
101             )!!
102
103             val executionServiceOutput = bluePrintWorkflowExecutionService
104                 .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
105
106             assertNotNull(executionServiceOutput, "failed to get response")
107             assertEquals(
108                 BluePrintConstants.STATUS_SUCCESS, executionServiceOutput.status.message,
109                 "failed to get successful response"
110             )
111         }
112     }
113
114     @Test
115     fun `Blueprint fails on missing workflowName-parameters with a useful message`() {
116         assertFailsWith(exceptionClass = BluePrintProcessorException::class) {
117             runBlocking {
118                 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
119                     "1234",
120                     "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
121                 )
122                 // service input will have a mislabeled input params, we are expecting to get an error when that happens with a useful error message
123                 val executionServiceInput =
124                     JacksonUtils.readValueFromClassPathFile(
125                         "execution-input/resource-assignment-input-missing-resource_assignment_request.json",
126                         ExecutionServiceInput::class.java
127                     )!!
128
129                 val executionServiceOutput = bluePrintWorkflowExecutionService
130                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, hashMapOf())
131             }
132         }
133     }
134 }