diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-04-22 17:04:00 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-04-23 11:07:45 +0200 |
commit | 768b36dfc0974bc82c5b4b4516b92bf866c065ac (patch) | |
tree | 7681ea803d29719e412fbc0b55396d57314c6d5a /container-core/src | |
parent | 8149a140186e19093477007d4079f739716515c0 (diff) |
Add test driver for RestApi
Diffstat (limited to 'container-core/src')
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/RestApiTestDriver.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiTestDriver.java b/container-core/src/main/java/com/yahoo/restapi/RestApiTestDriver.java new file mode 100644 index 00000000000..7dc5b710bbe --- /dev/null +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiTestDriver.java @@ -0,0 +1,96 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.restapi; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.yahoo.container.jdisc.HttpRequest; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.container.jdisc.LoggingRequestHandler; +import com.yahoo.jdisc.http.server.jetty.testutils.TestDriver; +import com.yahoo.jdisc.test.MockMetric; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.OptionalInt; +import java.util.concurrent.Executors; + +import static com.yahoo.yolean.Exceptions.uncheck; + +/** + * Test driver for {@link RestApi} + * + * @author bjorncs + */ +public class RestApiTestDriver implements AutoCloseable { + + private final RestApiRequestHandler<?> handler; + private final TestDriver testDriver; + + private RestApiTestDriver(Builder builder) { + this.handler = builder.handler; + this.testDriver = builder.jdiscHttpServer ? TestDriver.newBuilder().withRequestHandler(builder.handler).build() : null; + } + + public static Builder newBuilder(RestApiRequestHandler<?> handler) { return new Builder(handler); } + + @FunctionalInterface public interface RestApiRequestHandlerFactory { RestApiRequestHandler<?> create(LoggingRequestHandler.Context context); } + public static Builder newBuilder(RestApiRequestHandlerFactory factory) { return new Builder(factory); } + + public static LoggingRequestHandler.Context createHandlerTestContext() { + return new LoggingRequestHandler.Context(Executors.newSingleThreadExecutor(), new MockMetric()); + } + + public OptionalInt listenPort() { + return testDriver != null ? OptionalInt.of(testDriver.server().getListenPort()) : OptionalInt.empty(); + } + + public RestApiRequestHandler<?> handler() { return handler; } + public RestApi restApi() { return handler.restApi(); } + public ObjectMapper jacksonJsonMapper() { return handler.restApi().jacksonJsonMapper(); } + + public HttpResponse executeRequest(HttpRequest request) { return handler.handle(request); } + + public InputStream requestContentOf(Object jacksonEntity) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + uncheck(() -> handler.restApi().jacksonJsonMapper().writeValue(out, jacksonEntity)); + return new ByteArrayInputStream(out.toByteArray()); + } + + public <T> T parseJacksonResponseContent(HttpResponse response, TypeReference<T> type) { + return uncheck(() -> handler.restApi().jacksonJsonMapper().readValue(responseData(response), type)); + } + + public <T> T parseJacksonResponseContent(HttpResponse response, Class<T> type) { + return uncheck(() -> handler.restApi().jacksonJsonMapper().readValue(responseData(response), type)); + } + + private static byte[] responseData(HttpResponse response) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + uncheck(() -> response.render(out)); + return out.toByteArray(); + } + + @Override + public void close() throws Exception { + if (testDriver != null) { + testDriver.close(); + } + } + + public static class Builder { + private final RestApiRequestHandler<?> handler; + private boolean jdiscHttpServer = false; + + private Builder(RestApiRequestHandler<?> handler) { + this.handler = handler; + } + + private Builder(RestApiRequestHandlerFactory factory) { this(factory.create(createHandlerTestContext())); } + + public Builder withJdiscHttpServer() { this.jdiscHttpServer = true; return this; } + + public RestApiTestDriver build() { return new RestApiTestDriver(this); } + } + +} |