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.data.EdgeLabel
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Graph
27 import org.onap.ccsdk.cds.controllerblueprints.core.toGraph
28 import kotlin.test.assertNotNull
30 class BluePrintWorkflowServiceTest {
32 fun testSimpleFlow() {
34 val graph = "[START>A/SUCCESS, A>B/SUCCESS, B>C/SUCCESS, C>D/SUCCESS, D>E/SUCCESS, E>END/SUCCESS]"
36 val simpleWorkflow = TestBluePrintWorkFlowService()
37 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
38 val deferredOutput = CompletableDeferred<String>()
40 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
41 val response = deferredOutput.await()
42 assertNotNull(response, "failed to get response")
47 fun testConditionalFlow() {
49 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
51 val simpleWorkflow = TestBluePrintWorkFlowService()
52 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
53 val deferredOutput = CompletableDeferred<String>()
55 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
56 val response = deferredOutput.await()
57 assertNotNull(response, "failed to get response")
62 fun testBothConditionalFlow() {
65 val failurePatGraph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
67 val failurePathWorkflow = TestBluePrintWorkFlowService()
68 failurePathWorkflow.simulatedState = prepareSimulation(arrayListOf("B", "C", "D", "E"),
70 val failurePathWorkflowDeferredOutput = CompletableDeferred<String>()
71 val failurePathWorkflowInput = "123456"
72 failurePathWorkflow.executeWorkflow(failurePatGraph, mockBluePrintRuntimeService(), failurePathWorkflowInput, failurePathWorkflowDeferredOutput)
73 val failurePathResponse = failurePathWorkflowDeferredOutput.await()
74 assertNotNull(failurePathResponse, "failed to get response")
79 fun testMultipleSkipFlow() {
81 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, C>D/SUCCESS, D>E/SUCCESS, B>E/SUCCESS, E>END/SUCCESS]"
83 val simpleWorkflow = TestBluePrintWorkFlowService()
84 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
85 val deferredOutput = CompletableDeferred<String>()
87 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
88 val response = deferredOutput.await()
89 assertNotNull(response, "failed to get response")
94 fun testParallelFlow() {
96 val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/SUCCESS, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
98 val simpleWorkflow = TestBluePrintWorkFlowService()
99 simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D"), null)
100 val deferredOutput = CompletableDeferred<String>()
102 simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
103 val response = deferredOutput.await()
104 assertNotNull(response, "failed to get response")
108 private fun mockBluePrintRuntimeService(): BluePrintRuntimeService<*> {
109 val bluePrintRuntimeService = mockk<BluePrintRuntimeService<*>>()
110 every { bluePrintRuntimeService.id() } returns "123456"
111 return bluePrintRuntimeService
114 private fun prepareSimulation(successes: List<String>?, failures: List<String>?): MutableMap<String, EdgeLabel> {
115 val simulatedState: MutableMap<String, EdgeLabel> = hashMapOf()
117 simulatedState[it] = EdgeLabel.SUCCESS
120 simulatedState[it] = EdgeLabel.FAILURE
122 return simulatedState
126 class TestBluePrintWorkFlowService
127 : AbstractBluePrintWorkFlowService<String, String>() {
129 lateinit var simulatedState: MutableMap<String, EdgeLabel>
131 override suspend fun initializeWorkflow(input: String): EdgeLabel {
132 return EdgeLabel.SUCCESS
135 override suspend fun prepareNodeExecutionMessage(node: Graph.Node)
136 : NodeExecuteMessage<String, String> {
137 val deferredNodeOutput = CompletableDeferred<String>()
138 val nodeExecuteMessage = NodeExecuteMessage(node, "$node Input", deferredNodeOutput)
139 return nodeExecuteMessage
142 override suspend fun executeNode(node: Graph.Node, nodeInput: String,
143 deferredNodeOutput: CompletableDeferred<String>,
144 deferredNodeStatus: CompletableDeferred<EdgeLabel>) {
145 // val random = (1..10).random() * 1000
146 // println("will reply in $random ms")
147 // kotlinx.coroutines.delay(random.toLong())
148 val status = simulatedState[node.id] ?: throw BluePrintException("failed to get status for the node($node)")
149 deferredNodeStatus.complete(status)
150 deferredNodeOutput.complete("$node, Output: $nodeInput output")
153 override suspend fun prepareNodeSkipMessage(node: Graph.Node)
154 : NodeSkipMessage<String, String> {
155 val deferredNodeOutput = CompletableDeferred<String>()
156 val nodeSkipMessage = NodeSkipMessage(node, "$node Skip Input", deferredNodeOutput)
157 return nodeSkipMessage
160 override suspend fun skipNode(node: Graph.Node, nodeInput: String,
161 deferredNodeOutput: CompletableDeferred<String>,
162 deferredNodeStatus: CompletableDeferred<EdgeLabel>) {
163 val status = simulatedState[node.id] ?: throw BluePrintException("failed to get status for the node($node)")
164 deferredNodeStatus.complete(status)
167 override suspend fun cancelNode(node: Graph.Node, nodeInput: String,
168 deferredNodeOutput: CompletableDeferred<String>,
169 deferredNodeStatus: CompletableDeferred<EdgeLabel>) {
170 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
173 override suspend fun restartNode(node: Graph.Node, nodeInput: String,
174 deferredNodeOutput: CompletableDeferred<String>,
175 deferredNodeStatus: CompletableDeferred<EdgeLabel>) {
176 TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
179 override suspend fun prepareWorkflowOutput(): String {
180 return "Final Response"