Get rid of arrow-effects usage
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-xnf-simulator / src / test / kotlin / org / onap / dcae / collectors / veshv / main / OngoingSimulationsTest.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.main
21
22 import org.assertj.core.api.Assertions.assertThat
23 import org.jetbrains.spek.api.Spek
24 import org.jetbrains.spek.api.dsl.describe
25 import org.jetbrains.spek.api.dsl.given
26 import org.jetbrains.spek.api.dsl.it
27 import org.jetbrains.spek.api.dsl.on
28 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
29 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusFailure
30 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusNotFound
31 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusOngoing
32 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusSuccess
33 import org.onap.dcae.collectors.veshv.tests.utils.waitUntilSucceeds
34 import reactor.core.publisher.Mono
35 import reactor.core.scheduler.Schedulers
36 import java.util.*
37
38 /**
39  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
40  * @since September 2018
41  */
42 internal class OngoingSimulationsTest : Spek({
43     val scheduler = Schedulers.single()
44     val cut = OngoingSimulations(scheduler)
45
46     describe("simulations repository") {
47         given("not existing task task id") {
48             val id = UUID.randomUUID()
49
50             on("asking for status") {
51                 val result = cut.status(id)
52
53                 it("should have 'not found' status") {
54                     assertThat(result).isEqualTo(StatusNotFound)
55                 }
56             }
57         }
58
59         given("no tasks") {
60             on("quering about any pending task") {
61                 it("should return false") {
62                     assertThat(cut.isAnySimulationPending()).isFalse()
63                 }
64             }
65         }
66
67         given("never ending task") {
68             val task = neverendingTask()
69
70             on("startAsynchronousSimulation") {
71                 val result = cut.startAsynchronousSimulation(task)
72
73                 it("should have ongoing status") {
74                     assertThat(cut.status(result)).isEqualTo(StatusOngoing)
75                 }
76
77                 it("should return true when asked about any pending tasks") {
78                     assertThat(cut.isAnySimulationPending()).isTrue()
79                 }
80             }
81         }
82
83         given("failing task") {
84             val (cause, task) = failingTask()
85
86             on("startAsynchronousSimulation") {
87                 val taskID = cut.startAsynchronousSimulation(task)
88
89                 it("should have failing status") {
90                     waitUntilSucceeds {
91                         assertThat(cut.status(taskID)).isEqualTo(StatusFailure(cause))
92                     }
93                 }
94
95                 it("should return false when asked about any pending tasks") {
96                     waitUntilSucceeds {
97                         assertThat(cut.isAnySimulationPending()).isFalse()
98                     }
99                 }
100             }
101         }
102
103         given("successful task") {
104             val task = succesfulTask()
105
106             on("startAsynchronousSimulation") {
107                 val taskID = cut.startAsynchronousSimulation(task)
108
109                 it("should have successful status") {
110                     waitUntilSucceeds {
111                         assertThat(cut.status(taskID)).isEqualTo(StatusSuccess)
112                     }
113                 }
114
115                 it("should return false when asked about any pending tasks") {
116                     waitUntilSucceeds {
117                         assertThat(cut.isAnySimulationPending()).isFalse()
118                     }
119                 }
120             }
121         }
122
123         afterGroup {
124             scheduler.dispose()
125         }
126     }
127
128     afterEachTest { cut.clear() }
129 })
130
131 private fun neverendingTask() = Mono.never<Void>()
132
133 private fun succesfulTask(): Mono<Void> = Mono.empty<Void>()
134         .doOnSuccess {
135             println("great success")
136         }
137
138 private fun failingTask(): Pair<RuntimeException, Mono<Void>> {
139     val cause = RuntimeException("facepalm")
140     val task = Mono.error<Void>(cause)
141     return Pair(cause, task)
142 }