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