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
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.test;
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;
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;
43 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
44 * @since February 2019
46 public class DummyHttpServer {
48 private static final Logger LOGGER = LoggerFactory.getLogger(DummyHttpServer.class);
49 private final DisposableServer server;
51 private DummyHttpServer(DisposableServer server) {
55 public static DummyHttpServer start(Consumer<HttpServerRoutes> routes) {
56 LOGGER.info("Starting dummy server");
57 final DisposableServer server = HttpServer.create()
62 LOGGER.info("Server started");
63 return new DummyHttpServer(server);
66 public static Publisher<Void> sendInOrder(AtomicInteger state, Publisher<Void>... responses) {
67 return responses[state.getAndIncrement()];
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"));
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"));
85 public static Publisher<Void> sendResource(HttpServerResponse httpServerResponse, String resourcePath) {
86 return sendString(httpServerResponse, Mono.fromCallable(() -> readResource(resourcePath)));
89 public static Publisher<Void> sendError(HttpServerResponse httpServerResponse, int statusCode, String message) {
90 return sendString(httpServerResponse.status(statusCode), Mono.just(message));
93 public static Publisher<Void> sendString(HttpServerResponse httpServerResponse, Publisher<String> content) {
94 return httpServerResponse.sendString(content);
101 public DummyHttpServer closeAndGet() {
106 public String host() {
107 return server.host();
111 return server.port();
114 private static String readResource(String resourcePath) {
116 return CheckedFunction0.constant(resourcePath)
117 .andThen(DummyHttpServer.class::getResource)
120 .andThen(Files::readAllBytes)
121 .andThen(String::new)
123 } catch (Throwable throwable) {
124 throw new RuntimeException(throwable);