b8d8cea3e3bf793b693425198da2b2d7511215a2
[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.controllerblueprints.core.service
18
19 import io.mockk.every
20 import io.mockk.mockk
21 import kotlinx.coroutines.CompletableDeferred
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Test
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
30
31 class BluePrintWorkflowServiceTest {
32     @Test
33     fun testSimpleFlow() {
34         runBlocking {
35             val graph = "[START>A/SUCCESS, A>B/SUCCESS, B>C/SUCCESS, C>D/SUCCESS, D>E/SUCCESS, E>END/SUCCESS]"
36                     .toGraph()
37             val simpleWorkflow = TestBluePrintWorkFlowService()
38             simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
39             val deferredOutput = CompletableDeferred<String>()
40             val input = "123456"
41             simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
42             val response = deferredOutput.await()
43             assertNotNull(response, "failed to get response")
44         }
45     }
46
47     @Test
48     fun testConditionalFlow() {
49         runBlocking {
50             val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
51                     .toGraph()
52             val simpleWorkflow = TestBluePrintWorkFlowService()
53             simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
54             val deferredOutput = CompletableDeferred<String>()
55             val input = "123456"
56             simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
57             val response = deferredOutput.await()
58             assertNotNull(response, "failed to get response")
59         }
60     }
61
62     @Test
63     fun testBothConditionalFlow() {
64         runBlocking {
65             // Failure Flow
66             val failurePatGraph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
67                     .toGraph()
68             val failurePathWorkflow = TestBluePrintWorkFlowService()
69             failurePathWorkflow.simulatedState = prepareSimulation(arrayListOf("B", "C", "D", "E"),
70                     arrayListOf("A"))
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")
76         }
77     }
78
79     @Test
80     fun testMultipleSkipFlow() {
81         runBlocking {
82             val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/FAILURE, C>D/SUCCESS, D>E/SUCCESS, B>E/SUCCESS, E>END/SUCCESS]"
83                     .toGraph()
84             val simpleWorkflow = TestBluePrintWorkFlowService()
85             simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D", "E"), null)
86             val deferredOutput = CompletableDeferred<String>()
87             val input = "123456"
88             simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
89             val response = deferredOutput.await()
90             assertNotNull(response, "failed to get response")
91         }
92     }
93
94     @Test
95     fun testParallelFlow() {
96         runBlocking {
97             val graph = "[START>A/SUCCESS, A>B/SUCCESS, A>C/SUCCESS, B>D/SUCCESS, C>D/SUCCESS, D>END/SUCCESS]"
98                     .toGraph()
99             val simpleWorkflow = TestBluePrintWorkFlowService()
100             simpleWorkflow.simulatedState = prepareSimulation(arrayListOf("A", "B", "C", "D"), null)
101             val deferredOutput = CompletableDeferred<String>()
102             val input = "123456"
103             simpleWorkflow.executeWorkflow(graph, mockBluePrintRuntimeService(), input, deferredOutput)
104             val response = deferredOutput.await()
105             assertNotNull(response, "failed to get response")
106         }
107     }
108
109     private fun mockBluePrintRuntimeService(): BluePrintRuntimeService<*> {
110         val bluePrintRuntimeService = mockk<BluePrintRuntimeService<*>>()
111         every { bluePrintRuntimeService.id() } returns "123456"
112         return bluePrintRuntimeService
113     }
114
115     private fun prepareSimulation(successes: List<String>?, failures: List<String>?): MutableMap<String, EdgeLabel> {
116         val simulatedState: MutableMap<String, EdgeLabel> = hashMapOf()
117         successes?.forEach {
118             simulatedState[it] = EdgeLabel.SUCCESS
119         }
120         failures?.forEach {
121             simulatedState[it] = EdgeLabel.FAILURE
122         }
123         return simulatedState
124     }
125 }
126
127 class TestBluePrintWorkFlowService
128     : AbstractBluePrintWorkFlowService<String, String>() {
129
130     lateinit var simulatedState: MutableMap<String, EdgeLabel>
131
132     override suspend fun initializeWorkflow(input: String): EdgeLabel {
133         return EdgeLabel.SUCCESS
134     }
135
136     override suspend fun prepareNodeExecutionMessage(node: Graph.Node)
137             : NodeExecuteMessage<String, String> {
138         return NodeExecuteMessage(node, "$node Input", "")
139     }
140
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)")
147         return status
148     }
149
150     override suspend fun prepareNodeSkipMessage(node: Graph.Node): NodeSkipMessage<String, String> {
151         val nodeOutput = ""
152         val nodeSkipMessage = NodeSkipMessage(node, "$node Skip Input", nodeOutput)
153         return nodeSkipMessage
154     }
155
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)")
159         return status
160     }
161
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.
165     }
166
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.
170     }
171
172     override suspend fun prepareWorkflowOutput(exception: BluePrintProcessorException?): String {
173         return "Final Response"
174     }
175 }