Add all required and reasonable MDCs
[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 org.onap.dcae.collectors.veshv.impl.adapters.HttpAdapter.Companion.INVOCATION_ID_HEADER
27 import reactor.core.publisher.Mono
28 import reactor.netty.http.client.HttpClient
29 import reactor.netty.http.server.HttpServer
30 import reactor.test.StepVerifier
31 import reactor.test.test
32 import java.util.*
33
34 /**
35  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
36  * @since May 2018
37  */
38 internal object HttpAdapterTest : Spek({
39     describe("HttpAdapter") {
40
41         val httpServer = HttpServer.create()
42                 .host("127.0.0.1")
43                 .route { routes ->
44                     routes.get("/url") { req, resp ->
45                         resp.sendString(Mono.just(req.uri()))
46                     }
47                     routes.get("/inv-id") { req, resp ->
48                         resp.sendString(Mono.just(req.requestHeaders()[INVOCATION_ID_HEADER]))
49                     }
50                 }
51                 .bindNow()
52         val baseUrl = "http://${httpServer.host()}:${httpServer.port()}"
53         val httpAdapter = HttpAdapter(HttpClient.create().baseUrl(baseUrl))
54
55         afterGroup {
56             httpServer.disposeNow()
57         }
58
59         given("url without query params") {
60             val url = "/url"
61             val invocationId = UUID.randomUUID()
62
63             it("should not append query string") {
64                 httpAdapter.get(url, invocationId).test()
65                         .expectNext(url)
66                         .verifyComplete()
67             }
68
69             it("should pass invocation id") {
70                 httpAdapter.get("/inv-id", invocationId).test()
71                         .expectNext(invocationId.toString())
72                         .verifyComplete()
73             }
74         }
75
76         given("url with query params") {
77             val queryParams = mapOf(Pair("p", "the-value"))
78             val url = "/url"
79             val invocationId = UUID.randomUUID()
80
81             it("should add them as query string to the url") {
82                 httpAdapter.get(url, invocationId, queryParams).test()
83                         .expectNext("/url?p=the-value")
84                         .verifyComplete()
85             }
86
87             it("should pass invocation id") {
88                 httpAdapter.get("/inv-id", invocationId, queryParams).test()
89                         .expectNext(invocationId.toString())
90                         .verifyComplete()
91             }
92         }
93
94         given("invalid url") {
95             val invalidUrl = "/wtf"
96             val invocationId = UUID.randomUUID()
97
98             it("should interrupt the flux") {
99                 StepVerifier
100                         .create(httpAdapter.get(invalidUrl, invocationId))
101                         .verifyError()
102             }
103         }
104     }
105
106 })