cdddaeff1f074e16f3a0e29fcc93236239898581
[dcaegen2/services/sdk.git] / rest-services / http-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / adapters / http / RxHttpClientIT.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
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
21 package org.onap.dcaegen2.services.sdk.rest.services.adapters.http;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString;
25
26 import io.netty.handler.codec.http.HttpResponseStatus;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.time.Duration;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer;
35 import reactor.core.publisher.Mono;
36 import reactor.test.StepVerifier;
37
38 class RxHttpClientIT {
39
40     private static final Duration TIMEOUT = Duration.ofHours(5);
41     private final RxHttpClient cut = RxHttpClientFactory.create();
42     private static DummyHttpServer httpServer;
43
44     @BeforeAll
45     static void setUpClass() {
46         httpServer = DummyHttpServer.start(routes ->
47                 routes.get("/sample-get", (req, resp) -> sendString(resp, Mono.just("OK")))
48                         .get("/sample-get-500", (req, resp) -> resp.status(HttpResponseStatus.INTERNAL_SERVER_ERROR).send())
49                         .post("/headers-post", (req, resp) -> resp
50                                 .sendString(Mono.just(req.requestHeaders().toString())))
51                         .post("/echo-post", (req, resp) -> resp.send(req.receive().retain()))
52         );
53     }
54
55     @AfterAll
56     static void tearDownClass() {
57         httpServer.close();
58     }
59
60     private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException {
61         return ImmutableHttpRequest.builder()
62                 .url(new URL("http", httpServer.host(), httpServer.port(), path).toString());
63     }
64
65     @Test
66     void simpleGet() throws Exception {
67         // given
68         final HttpRequest httpRequest = requestFor("/sample-get").method(HttpMethod.GET).build();
69
70         // when
71         final Mono<String> bodyAsString = cut.call(httpRequest)
72                 .doOnNext(HttpResponse::throwIfUnsuccessful)
73                 .map(HttpResponse::bodyAsString);
74
75         // then
76         StepVerifier.create(bodyAsString).expectNext("OK").expectComplete().verify(TIMEOUT);
77     }
78
79     @Test
80     void getWithError() throws Exception {
81         // given
82         final HttpRequest httpRequest = requestFor("/sample-get-500").method(HttpMethod.GET).build();
83
84         // when
85         final Mono<String> bodyAsString = cut.call(httpRequest)
86                 .doOnNext(HttpResponse::throwIfUnsuccessful)
87                 .map(HttpResponse::bodyAsString);
88
89         // then
90         StepVerifier.create(bodyAsString).expectError(HttpException.class).verify(TIMEOUT);
91     }
92
93     @Test
94     void simplePost() throws Exception {
95         // given
96         final String requestBody = "hello world";
97         final HttpRequest httpRequest = requestFor("/echo-post")
98                 .method(HttpMethod.POST)
99                 .body(RequestBody.fromString(requestBody))
100                 .build();
101
102         // when
103         final Mono<String> bodyAsString = cut.call(httpRequest)
104                 .doOnNext(HttpResponse::throwIfUnsuccessful)
105                 .map(HttpResponse::bodyAsString);
106
107         // then
108         StepVerifier.create(bodyAsString)
109                 .expectNext(requestBody)
110                 .expectComplete()
111                 .verify(TIMEOUT);
112     }
113
114     @Test
115     void testChunkedEncoding() throws Exception {
116         // given
117         final String requestBody = "hello world";
118         final HttpRequest httpRequest = requestFor("/headers-post")
119                 .method(HttpMethod.POST)
120                 .body(RequestBody.chunkedFromString(Mono.just(requestBody)))
121                 .build();
122
123         // when
124         final Mono<String> bodyAsString = cut.call(httpRequest)
125                 .doOnNext(HttpResponse::throwIfUnsuccessful)
126                 .map(HttpResponse::bodyAsString);
127
128         // then
129         StepVerifier.create(bodyAsString.map(String::toLowerCase))
130                 .consumeNextWith(responseBody -> {
131                     assertThat(responseBody).contains("transfer-encoding: chunked");
132                     assertThat(responseBody).doesNotContain("content-length");
133                 })
134                 .expectComplete()
135                 .verify(TIMEOUT);
136     }
137
138     @Test
139     void testUnchunkedEncoding() throws Exception {
140         // given
141         final String requestBody = "hello world";
142         final HttpRequest httpRequest = requestFor("/headers-post")
143                 .method(HttpMethod.POST)
144                 .body(RequestBody.fromString(requestBody))
145                 .build();
146
147         // when
148         final Mono<String> bodyAsString = cut.call(httpRequest)
149                 .doOnNext(HttpResponse::throwIfUnsuccessful)
150                 .map(HttpResponse::bodyAsString);
151
152         // then
153         StepVerifier.create(bodyAsString.map(String::toLowerCase))
154                 .consumeNextWith(responseBody -> {
155                     assertThat(responseBody).doesNotContain("transfer-encoding");
156                     assertThat(responseBody).contains("content-length");
157                 })
158                 .expectComplete()
159                 .verify(TIMEOUT);
160     }
161 }