e032f00ee251dfe51927b201ae5d8a7713de7d66
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-main / src / test / kotlin / org / onap / dcae / collectors / veshv / main / MainTest.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 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 com.nhaarman.mockitokotlin2.any
24 import com.nhaarman.mockitokotlin2.inOrder
25 import com.nhaarman.mockitokotlin2.mock
26 import com.nhaarman.mockitokotlin2.verify
27 import com.nhaarman.mockitokotlin2.whenever
28 import org.assertj.core.api.Assertions.assertThat
29 import org.jetbrains.spek.api.Spek
30 import org.jetbrains.spek.api.dsl.describe
31 import org.jetbrains.spek.api.dsl.given
32 import org.jetbrains.spek.api.dsl.it
33 import org.jetbrains.spek.api.dsl.on
34 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthDescription
35 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthState
36 import org.onap.dcae.collectors.veshv.utils.ServerHandle
37
38 /**
39  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
40  * @since January 2019
41  */
42 internal object MainTest : Spek({
43     describe("closeServer shutdown hook") {
44         given("server handles and health state") {
45             val handle: ServerHandle = mock()
46             var closed = false
47             val handleClose = IO {
48                 closed = true
49             }
50             whenever(handle.close()).thenReturn(handleClose)
51             val healthState: HealthState = mock()
52
53             on("closeServers") {
54                 closeServers(handle, healthState = healthState).unsafeRunSync()
55
56                 it("should close all handles") {
57                     assertThat(closed).isTrue()
58                 }
59
60                 it("should change state to SHUTTING_DOWN") {
61                     verify(healthState).changeState(HealthDescription.SHUTTING_DOWN)
62                 }
63
64                 it("should first change state and then close servers") {
65                     inOrder(handle, healthState) {
66                         verify(healthState).changeState(any())
67                         verify(handle).close()
68                     }
69                 }
70             }
71         }
72     }
73 })