Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / test / kotlin / org / onap / dcae / collectors / veshv / impl / adapters / HttpAdapterTest.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.impl.adapters
21
22 import org.jetbrains.spek.api.Spek
23 import org.jetbrains.spek.api.dsl.describe
24 import org.jetbrains.spek.api.dsl.given
25 import org.jetbrains.spek.api.dsl.it
26 import reactor.core.publisher.Mono
27 import reactor.netty.http.client.HttpClient
28 import reactor.netty.http.server.HttpServer
29 import reactor.test.StepVerifier
30 import reactor.test.test
31
32 /**
33  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
34  * @since May 2018
35  */
36 internal object HttpAdapterTest : Spek({
37     describe("HttpAdapter") {
38
39         val httpServer = HttpServer.create()
40                 .host("127.0.0.1")
41                 .route { routes ->
42                     routes.get("/url") { req, resp ->
43                         resp.sendString(Mono.just(req.uri()))
44                     }
45                 }
46                 .bindNow()
47         val baseUrl = "http://${httpServer.host()}:${httpServer.port()}"
48         val httpAdapter = HttpAdapter(HttpClient.create().baseUrl(baseUrl))
49
50         afterGroup {
51             httpServer.disposeNow()
52         }
53
54         given("url without query params") {
55             val url = "/url"
56
57             it("should not append query string") {
58                 httpAdapter.get(url).test()
59                         .expectNext(url)
60                         .verifyComplete()
61             }
62         }
63
64         given("url with query params") {
65             val queryParams = mapOf(Pair("p", "the-value"))
66             val url = "/url"
67
68             it("should add them as query string to the url") {
69                 httpAdapter.get(url, queryParams).test()
70                         .expectNext("/url?p=the-value")
71                         .verifyComplete()
72             }
73         }
74
75         given("invalid url") {
76             val invalidUrl = "/wtf"
77
78             it("should interrupt the flux") {
79                 StepVerifier
80                         .create(httpAdapter.get(invalidUrl))
81                         .verifyError()
82             }
83         }
84     }
85
86 })