Bump checkstyle version
[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 arrow.effects.IO
23 import org.assertj.core.api.Assertions.assertThat
24 import org.jetbrains.spek.api.Spek
25 import org.jetbrains.spek.api.dsl.describe
26 import org.jetbrains.spek.api.dsl.given
27 import org.jetbrains.spek.api.dsl.it
28 import org.jetbrains.spek.api.dsl.on
29 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.OngoingSimulations
30 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusFailure
31 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusNotFound
32 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusOngoing
33 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.StatusSuccess
34 import org.onap.dcae.collectors.veshv.tests.utils.waitUntilSucceeds
35 import java.util.*
36 import java.util.concurrent.Executors
37
38 /**
39  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
40  * @since September 2018
41  */
42 internal class OngoingSimulationsTest : Spek({
43     val executor = Executors.newSingleThreadExecutor()
44     val cut = OngoingSimulations(executor)
45
46     describe("simulations repository") {
47         given("not existing task task id") {
48             val id = UUID.randomUUID()
49
50             on("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("never ending task") {
60             val task = IO.async<Unit> { }
61
62             on("startAsynchronousSimulation") {
63                 val result = cut.startAsynchronousSimulation(task)
64
65                 it("should have ongoing status") {
66                     assertThat(cut.status(result)).isEqualTo(StatusOngoing)
67                 }
68             }
69         }
70
71         given("failing task") {
72             val cause = RuntimeException("facepalm")
73             val task = IO.raiseError<Unit>(cause)
74
75             on("startAsynchronousSimulation") {
76                 val result = cut.startAsynchronousSimulation(task)
77
78                 it("should have failing status") {
79                     waitUntilSucceeds {
80                         assertThat(cut.status(result)).isEqualTo(StatusFailure(cause))
81                     }
82                 }
83             }
84         }
85
86         given("successful task") {
87             val task = IO { println("great success!") }
88
89             on("startAsynchronousSimulation") {
90                 val result = cut.startAsynchronousSimulation(task)
91
92                 it("should have successful status") {
93                     waitUntilSucceeds {
94                         assertThat(cut.status(result)).isEqualTo(StatusSuccess)
95                     }
96                 }
97             }
98         }
99
100         afterGroup {
101             executor.shutdown()
102         }
103     }
104
105     afterEachTest { cut.clear() }
106 })