2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.service
21 import kotlinx.coroutines.CompletableDeferred
22 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.EdgeLabel
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.Graph
28 import org.onap.ccsdk.cds.controllerblueprints.core.toGraph
29 import kotlin.test.assertNotNull
31 class BluePrintWorkflowServiceTest {
33 fun testSimpleFlow() {
35 val graph = "[START>A/SUCCESS, A>B/SUCCESS, B>C/SUCCESS, C>D/SUCCESS, D>E/SUCCESS, E>END/SUCCESS]"
37 val simpleWorkflow = TestBluePrintWorkFlowService()
38 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
39 val deferredOutput = CompletableDeferred<String>()
41 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
42 val response = deferredOutput.await()
43 assertNotNull(response, "failed to get response")
48 fun testConditionalFlow() {
50 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
52 val simpleWorkflow = TestBluePrintWorkFlowService()
53 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
54 val deferredOutput = CompletableDeferred<String>()
56 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
57 val response = deferredOutput.await()
58 assertNotNull(response, "failed to get response")
63 fun testBothConditionalFlow() {
66 val failurePatGraph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
68 val failurePathWorkflow = TestBluePrintWorkFlowService()
69 failurePathWorkflow.simulatedState = prepareSimulation(arrayListOf("B", "C", "D", "E"),
71 val failurePathWorkflowDeferredOutput = CompletableDeferred<String>()
72 val failurePathWorkflowInput = "123456"
73 failurePathWorkflow.executeWorkflow(failurePatGraph, mockBluePrintRuntimeService(), failurePathWorkflowInput, failurePathWorkflowDeferredOutput)
74 val failurePathResponse = failurePathWorkflowDeferredOutput.await()
75 assertNotNull(failurePathResponse, "failed to get response")
80 fun testMultipleSkipFlow() {
82 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, C>D/SUCCESS, D>E/SUCCESS, B>E/SUCCESS, E>END/SUCCESS]"
84 val simpleWorkflow = TestBluePrintWorkFlowService()
85 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
86 val deferredOutput = CompletableDeferred<String>()
88 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
89 val response = deferredOutput.await()
90 assertNotNull(response, "failed to get response")
95 fun testParallelFlow() {
97 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/SUCCESS, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
99 val simpleWorkflow = TestBluePrintWorkFlowService()
100 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D"), null)
101 val deferredOutput = CompletableDeferred<String>()
103 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
104 val response = deferredOutput.await()
105 assertNotNull(response, "failed to get response")
109 private fun mockBluePrintRuntimeService(): BluePrintRuntimeService<*> {
110 val bluePrintRuntimeService = mockk<BluePrintRuntimeService<*>>()
111 every { bluePrintRuntimeService.id() } returns "123456"
112 return bluePrintRuntimeService
115 private fun prepareSimulation(successes: List<String>?, failures: List<String>?): MutableMap<String, EdgeLabel> {
116 val simulatedState: MutableMap<String, EdgeLabel> = hashMapOf()
118 simulatedState[it] = EdgeLabel.SUCCESS
121 simulatedState[it] = EdgeLabel.FAILURE
123 return simulatedState
127 class TestBluePrintWorkFlowService
128 : AbstractBluePrintWorkFlowService<String, String>() {
130 lateinit var simulatedState: MutableMap<String, EdgeLabel>
132 override suspend fun initializeWorkflow(input: String): EdgeLabel {
133 return EdgeLabel.SUCCESS
136 override suspend fun prepareNodeExecutionMessage(node: Graph.Node)
137 : NodeExecuteMessage<String, String> {
138 return NodeExecuteMessage(node, "$node Input", "")
141 override suspend fun executeNode(node: Graph.Node, nodeInput: String,
142 nodeOutput: String): EdgeLabel {
143 // val random = (1..10).random() * 1000
144 // println("will reply in $random ms")
145 // kotlinx.coroutines.delay(random.toLong())
146 val status = simulatedState[node.id] ?: throw BluePrintException("failed to get status for the node($node)")
150 override suspend fun prepareNodeSkipMessage(node: Graph.Node): NodeSkipMessage<String, String> {
152 val nodeSkipMessage = NodeSkipMessage(node, "$node Skip Input", nodeOutput)
153 return nodeSkipMessage
156 override suspend fun skipNode(node: Graph.Node, nodeInput: String,
157 nodeOutput: String): EdgeLabel {
158 val status = simulatedState[node.id] ?: throw BluePrintException("failed to get status for the node($node)")
162 override suspend fun cancelNode(node: Graph.Node, nodeInput: String,
163 nodeOutput: String): EdgeLabel {
164 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
167 override suspend fun restartNode(node: Graph.Node, nodeInput: String,
168 nodeOutput: String): EdgeLabel {
169 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
172 override suspend fun prepareWorkflowOutput(exception: BluePrintProcessorException?): String {
173 return "Final Response"