70d8ba8339cbed5062ee65cc78ac89bc89901ad0
[dcaegen2/collectors/hv-ves.git] /
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.time.Duration
36 import java.util.*
37 import java.util.concurrent.Executors
38
39 /**
40  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41  * @since September 2018
42  */
43 internal class OngoingSimulationsTest : Spek({
44     val executor = Executors.newSingleThreadExecutor()
45     val cut = OngoingSimulations(executor)
46
47     describe("simulations repository") {
48         given("not existing task task id") {
49             val id = UUID.randomUUID()
50
51             on("status") {
52                 val result = cut.status(id)
53
54                 it("should have 'not found' status") {
55                     assertThat(result).isEqualTo(StatusNotFound)
56                 }
57             }
58         }
59
60         given("never ending task") {
61             val task = IO.async<Unit> { }
62
63             on("startAsynchronousSimulation") {
64                 val result = cut.startAsynchronousSimulation(task)
65
66                 it("should have ongoing status") {
67                     assertThat(cut.status(result)).isEqualTo(StatusOngoing)
68                 }
69             }
70         }
71
72         given("failing task") {
73             val cause = RuntimeException("facepalm")
74             val task = IO.raiseError<Unit>(cause)
75
76             on("startAsynchronousSimulation") {
77                 val result = cut.startAsynchronousSimulation(task)
78
79                 it("should have failing status") {
80                     waitUntilSucceeds {
81                         assertThat(cut.status(result)).isEqualTo(StatusFailure(cause))
82                     }
83                 }
84             }
85         }
86
87         given("successful task") {
88             val task = IO { println("great success!") }
89
90             on("startAsynchronousSimulation") {
91                 val result = cut.startAsynchronousSimulation(task)
92
93                 it("should have successful status") {
94                     waitUntilSucceeds {
95                         assertThat(cut.status(result)).isEqualTo(StatusSuccess)
96                     }
97                 }
98             }
99         }
100
101         afterGroup {
102             executor.shutdown()
103         }
104     }
105
106     afterEachTest { cut.clear() }
107 })