Custom detekt rule for logger usage check
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / test / kotlin / org / onap / dcae / collectors / veshv / utils / http / ResponsesTest.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.utils.http
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 java.util.*
29 import javax.json.JsonObject
30
31 /**
32  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
33  * @since September 2018
34  */
35 internal class ResponsesTest : Spek({
36     describe("response factory") {
37         describe("accepted response") {
38             given("uuid") {
39                 val uuid = UUID.randomUUID()
40
41                 on("calling acceptedResponse") {
42                     val result = Responses.acceptedResponse(uuid)
43
44                     it ("should have ACCEPTED status") {
45                         assertThat(result.status).isEqualTo(HttpStatus.ACCEPTED)
46                     }
47
48                     it ("should have text body") {
49                         assertThat(result.content.type).isEqualTo(ContentType.TEXT)
50                     }
51
52                     it ("should contain UUID text in the body") {
53                         val serialized = result.content.serializer.run { result.content.value.show() }
54                         assertThat(serialized).isEqualTo(uuid.toString())
55                     }
56                 }
57             }
58         }
59         describe("status response") {
60             given("all params are specified") {
61                 val status = "ok"
62                 val message = "good job"
63                 val httpStatus = HttpStatus.OK
64
65                 on("calling statusResponse") {
66                     val result = Responses.statusResponse(status, message, httpStatus)
67                     val json = result.content.value as JsonObject
68
69                     it ("should have OK status") {
70                         assertThat(result.status).isEqualTo(HttpStatus.OK)
71                     }
72
73                     it ("should have json body") {
74                         assertThat(result.content.type).isEqualTo(ContentType.JSON)
75                     }
76
77                     it ("should contain status as string") {
78                         assertThat(json.getString("status")).isEqualTo(status)
79                     }
80
81                     it ("should contain message") {
82                         assertThat(json.getString("message")).isEqualTo(message)
83                     }
84                 }
85             }
86
87             given("default params are omitted") {
88                 val status = "ok"
89                 val message = "good job"
90
91                 on("calling statusResponse") {
92                     val result = Responses.statusResponse(status, message)
93
94                     it ("should have OK status") {
95                         assertThat(result.status).isEqualTo(HttpStatus.OK)
96                     }
97                 }
98             }
99         }
100     }
101 })