Support retry in DCAE-SDK DMaaP-Client
[dcaegen2/services/sdk.git] / rest-services / http-client / src / main / java / org / onap / dcaegen2 / services / sdk / rest / services / adapters / http / test / DummyHttpServer.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 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.test;
22
23 import io.vavr.CheckedFunction0;
24 import io.vavr.Tuple3;
25 import io.vavr.control.Try;
26 import org.reactivestreams.Publisher;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import reactor.core.publisher.Mono;
30 import reactor.netty.DisposableServer;
31 import reactor.netty.http.server.HttpServer;
32 import reactor.netty.http.server.HttpServerResponse;
33 import reactor.netty.http.server.HttpServerRoutes;
34
35 import java.net.URL;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.time.Duration;
39 import java.util.concurrent.atomic.AtomicInteger;
40 import java.util.function.Consumer;
41
42 /**
43  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
44  * @since February 2019
45  */
46 public class DummyHttpServer {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(DummyHttpServer.class);
49     private final DisposableServer server;
50
51     private DummyHttpServer(DisposableServer server) {
52         this.server = server;
53     }
54
55     public static DummyHttpServer start(Consumer<HttpServerRoutes> routes) {
56         LOGGER.info("Starting dummy server");
57         final DisposableServer server = HttpServer.create()
58                 .host("127.0.0.1")
59                 .route(routes)
60                 .bind()
61                 .block();
62         LOGGER.info("Server started");
63         return new DummyHttpServer(server);
64     }
65
66     public static Publisher<Void> sendInOrder(AtomicInteger state, Publisher<Void>... responses) {
67         return responses[state.getAndIncrement()];
68     }
69
70     public static Publisher<Void> sendInOrderWithDelay(AtomicInteger counter, Tuple3<HttpServerResponse, Integer, Duration>... responses) {
71         Tuple3<HttpServerResponse, Integer, Duration> tuple = responses[counter.get()];
72         HttpServerResponse httpServerResponse = tuple._1;
73         Integer statusCode = tuple._2;
74         long timeout = tuple._3.toMillis();
75         Try.run(() -> Thread.sleep(timeout));
76         counter.incrementAndGet();
77         return sendString(httpServerResponse.status(statusCode), Mono.just("OK"));
78     }
79
80     public static Publisher<Void> sendWithDelay(HttpServerResponse response, int statusCode, Duration timeout) {
81         Try.run(() -> Thread.sleep(timeout.toMillis()));
82         return sendString(response.status(statusCode), Mono.just("OK"));
83     }
84
85     public static Publisher<Void> sendResource(HttpServerResponse httpServerResponse, String resourcePath) {
86         return sendString(httpServerResponse, Mono.fromCallable(() -> readResource(resourcePath)));
87     }
88
89     public static Publisher<Void> sendError(HttpServerResponse httpServerResponse, int statusCode, String message) {
90         return sendString(httpServerResponse.status(statusCode), Mono.just(message));
91     }
92
93     public static Publisher<Void> sendString(HttpServerResponse httpServerResponse, Publisher<String> content) {
94         return httpServerResponse.sendString(content);
95     }
96
97     public void close() {
98         server.disposeNow();
99     }
100
101     public DummyHttpServer closeAndGet() {
102         server.disposeNow();
103         return this;
104     }
105
106     public String host() {
107         return server.host();
108     }
109
110     public int port() {
111         return server.port();
112     }
113
114     private static String readResource(String resourcePath) {
115         try {
116             return CheckedFunction0.constant(resourcePath)
117                     .andThen(DummyHttpServer.class::getResource)
118                     .andThen(URL::toURI)
119                     .andThen(Paths::get)
120                     .andThen(Files::readAllBytes)
121                     .andThen(String::new)
122                     .apply();
123         } catch (Throwable throwable) {
124             throw new RuntimeException(throwable);
125         }
126     }
127 }