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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.adapters.http;
23 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString;
25 import io.netty.handler.codec.http.HttpResponseStatus;
26 import java.net.MalformedURLException;
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;
37 class RxHttpClientIT {
39 private static final Duration TIMEOUT = Duration.ofHours(5);
40 private final RxHttpClient cut = RxHttpClient.create();
41 private static DummyHttpServer httpServer;
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()))
53 static void tearDownClass() {
57 private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException {
58 return ImmutableHttpRequest.builder()
59 .url(new URL("http", httpServer.host(), httpServer.port(), path).toString());
63 void simpleGet() throws Exception {
65 final HttpRequest httpRequest = requestFor("/sample-get").method(HttpMethod.GET).build();
68 final Mono<String> bodyAsString = cut.call(httpRequest)
69 .doOnNext(HttpResponse::throwIfUnsuccessful)
70 .map(HttpResponse::bodyAsString);
73 StepVerifier.create(bodyAsString).expectNext("OK").expectComplete().verify(TIMEOUT);
77 void getWithError() throws Exception {
79 final HttpRequest httpRequest = requestFor("/sample-get-500").method(HttpMethod.GET).build();
82 final Mono<String> bodyAsString = cut.call(httpRequest)
83 .doOnNext(HttpResponse::throwIfUnsuccessful)
84 .map(HttpResponse::bodyAsString);
87 StepVerifier.create(bodyAsString).expectError(HttpException.class).verify(TIMEOUT);
91 void simplePost() throws Exception {
93 final String requestBody = "hello world";
94 final HttpRequest httpRequest = requestFor("/echo-post")
95 .method(HttpMethod.POST)
96 .body(RequestBody.fromString(requestBody))
100 final Mono<String> bodyAsString = cut.call(httpRequest)
101 .doOnNext(HttpResponse::throwIfUnsuccessful)
102 .map(HttpResponse::bodyAsString);
105 StepVerifier.create(bodyAsString)
106 .expectNext(requestBody)