5ae62c87aa6495cc3738e6fcc70a30903c478e24
[dcaegen2/services/sdk.git] /
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.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString;
24
25 import io.netty.handler.codec.http.HttpResponseStatus;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.time.Duration;
29 import org.junit.jupiter.api.AfterAll;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer;
34 import reactor.core.publisher.Mono;
35 import reactor.test.StepVerifier;
36
37 class RxHttpClientIT {
38
39     private static final Duration TIMEOUT = Duration.ofHours(5);
40     private final RxHttpClient cut = RxHttpClient.create();
41     private static DummyHttpServer httpServer;
42
43     @BeforeAll
44     static void setUpClass() {
45         httpServer = DummyHttpServer.start(routes ->
46                 routes.get("/sample-get", (req, resp) -> sendString(resp, Mono.just("OK")))
47                         .get("/sample-get-500", (req, resp) -> resp.status(HttpResponseStatus.INTERNAL_SERVER_ERROR).send())
48                         .post("/echo-post", (req, resp) -> resp.send(req.receive().retain()))
49         );
50     }
51
52     @AfterAll
53     static void tearDownClass() {
54         httpServer.close();
55     }
56
57     private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException {
58         return ImmutableHttpRequest.builder()
59                 .url(new URL("http", httpServer.host(), httpServer.port(), path).toString());
60     }
61
62     @Test
63     void simpleGet() throws Exception {
64         // given
65         final HttpRequest httpRequest = requestFor("/sample-get").method(HttpMethod.GET).build();
66
67         // when
68         final Mono<String> bodyAsString = cut.call(httpRequest)
69                 .doOnNext(HttpResponse::throwIfUnsuccessful)
70                 .map(HttpResponse::bodyAsString);
71
72         // then
73         StepVerifier.create(bodyAsString).expectNext("OK").expectComplete().verify(TIMEOUT);
74     }
75
76     @Test
77     void getWithError() throws Exception {
78         // given
79         final HttpRequest httpRequest = requestFor("/sample-get-500").method(HttpMethod.GET).build();
80
81         // when
82         final Mono<String> bodyAsString = cut.call(httpRequest)
83                 .doOnNext(HttpResponse::throwIfUnsuccessful)
84                 .map(HttpResponse::bodyAsString);
85
86         // then
87         StepVerifier.create(bodyAsString).expectError(HttpException.class).verify(TIMEOUT);
88     }
89
90     @Test
91     void simplePost() throws Exception {
92         // given
93         final String requestBody = "hello world";
94         final HttpRequest httpRequest = requestFor("/echo-post")
95                 .method(HttpMethod.POST)
96                 .body(RequestBody.fromString(requestBody))
97                 .build();
98
99         // when
100         final Mono<String> bodyAsString = cut.call(httpRequest)
101                 .doOnNext(HttpResponse::throwIfUnsuccessful)
102                 .map(HttpResponse::bodyAsString);
103
104         // then
105         StepVerifier.create(bodyAsString)
106                 .expectNext(requestBody)
107                 .expectComplete()
108                 .verify(TIMEOUT);
109     }
110 }