diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-01-24 08:45:20 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-01-24 08:45:20 +0100 |
commit | eeab67c36c05249ec79aeb571fae48ff74583f57 (patch) | |
tree | bbdd9e2cc7730d2908d0f6622c045559db82d670 /container-core | |
parent | 12381612a7e5a0f30d76dfd56c92ac7ecd1e551e (diff) |
GC unused test utilities
Diffstat (limited to 'container-core')
5 files changed, 0 insertions, 376 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/test/MockService.java b/container-core/src/main/java/com/yahoo/container/handler/test/MockService.java deleted file mode 100644 index 805da3a0a0f..00000000000 --- a/container-core/src/main/java/com/yahoo/container/handler/test/MockService.java +++ /dev/null @@ -1,225 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.handler.test; - -import com.yahoo.api.annotations.Beta; -import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; -import com.yahoo.filedistribution.fileacquirer.FileAcquirer; -import com.yahoo.jdisc.Metric; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; - -/** - * This is a generic http handler that can be used to mock a service when testing your application on jDISC. - * Configuration and necessary files are given to the handle in its configuration. - * - * Example config: - * <pre> - * <handler id="MockService"> - * <config name="container.handler.test.mockservice"> - * <file>myresponses.txt</file> - * </config> - * <binding>http://*\/my/service/path1/*</binding> - * </handler> - * </pre> - * - * The file formats supported out of the box is text, see {@link com.yahoo.container.handler.test.MockService.TextFileHandler}. - * for descriptions of the format. - * - * @author Ulf Lilleengen - */ -@Beta -public class MockService extends ThreadedHttpRequestHandler { - - private final MockServiceHandler handler; - - /** - * Create a mock service that mocks an external service using data provided via file distribution. - * A custom handler can be created by subclassing and overriding the createHandler method. - * - * @param executor used to create threads - * @param fileAcquirer used to fetch file from config - * @param config the mock config for this service - * @throws InterruptedException if unable to get data file within timeout - * @throws IOException if unable to create handler due to some IO errors - */ - public MockService(Executor executor, FileAcquirer fileAcquirer, MockserviceConfig config, Metric metric) throws InterruptedException, IOException { - super(executor, metric); - File dataFile = fileAcquirer.waitFor(config.file(), config.fileAcquirerTimeout(), TimeUnit.SECONDS); - this.handler = createHandler(dataFile); - } - - /** - * Create a handler for a file. Override this method to handle a custom file syntax of your own. - * - * @param dataFile the file to read - * @return the handler used to handle requests - * @throws IOException if errors occurred when loading the file - */ - protected MockServiceHandler createHandler(File dataFile) throws IOException { - if (!dataFile.getName().endsWith(".txt")) { - throw new IllegalArgumentException("Default handler only support .txt files"); - } - return new TextFileHandler(dataFile); - } - - @Override - public final HttpResponse handle(HttpRequest request) { - try { - MockServiceHandler.Key key = handler.createKey(request); - MockServiceHandler.Value value = handler.get(key); - if (value == null) { - return new ErrorResponse(404, key + " was not found"); - } - return new RawResponse(value.returnCode, value.data, value.contentType); - } catch (Exception e) { - return new ExceptionResponse(500, e); - } - } - - /** - * A .txt file handler deals with the following format when reading data: - * method:url:responsecode:data - * - * For instance: - * GET:/my/path1:200:{\"foo\":\"bar\"} - * PUT:/my/path1:403:{\"error\":\"permission denied\"} - * TODO: Support binary files - */ - private static class TextFileHandler implements MockServiceHandler { - private final Map<MockServiceHandler.Key, Value> store = new HashMap<>(); - - public TextFileHandler(File dataFile) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(dataFile)); - readInputFile(reader); - } - - private void readInputFile(BufferedReader reader) throws IOException { - StringBuilder sb = new StringBuilder(); - int ch; - char prevChar = 0; - while ((ch = reader.read()) >= 0) { - char c = (char) ch; - if (prevChar == '\n') { - if (c == '\n') { - parseEntry(sb.toString()); - sb = new StringBuilder(); - prevChar = 0; - continue; - } else { - sb.append(prevChar); - } - } - if (c != '\n') { - sb.append(c); - } - prevChar = c; - } - parseEntry(sb.toString()); - } - - private void parseEntry(String entry) { - String [] components = entry.split(":", 4); - MockServiceHandler.Key key = new TextKey(com.yahoo.jdisc.http.HttpRequest.Method.valueOf(components[0]), components[1]); - Value value = new Value(Integer.parseInt(components[2]), components[3].getBytes(), "text/plain"); - store.put(key, value); - } - - @Override - public MockServiceHandler.Key createKey(HttpRequest request) { - StringBuilder sb = new StringBuilder(); - sb.append(request.getUri().getPath()); - String query = request.getUri().getQuery(); - if (query != null) { - sb.append("?").append(query); - } - return new TextKey(request.getMethod(), sb.toString()); - } - - @Override - public Value get(MockServiceHandler.Key key) { - return store.get(key); - } - } - - private static class RawResponse extends HttpResponse { - private final String contentType; - private final byte[] data; - RawResponse(int status, byte[] data, String contentType) { - super(status); - this.data = data; - this.contentType = contentType; - } - - @Override - public String getContentType() { - return contentType; - } - - @Override - public void render(OutputStream outputStream) throws IOException { - outputStream.write(data); - } - } - - private static class ErrorResponse extends RawResponse { - ErrorResponse(int status, String message) { - super(status, message.getBytes(), "text/plain"); - } - } - - private static final class TextKey implements MockServiceHandler.Key { - private final com.yahoo.jdisc.http.HttpRequest.Method method; - private final String path; - public TextKey(com.yahoo.jdisc.http.HttpRequest.Method method, String path) { - this.method = method; - this.path = path; - } - - @Override - public int hashCode() { - return path.hashCode() + method.hashCode(); - } - - @Override - public boolean equals(Object other) { - if (other.getClass() != TextKey.class) { - return false; - } - TextKey rhs = (TextKey) other; - return (this.method == rhs.method) && - path.equals(rhs.path); - } - - @Override - public String toString() { - return method.toString() + ":" + path; - } - } - - private static class ExceptionResponse extends HttpResponse { - private final Exception e; - public ExceptionResponse(int code, Exception e) { - super(code); - this.e =e; - } - - @Override - public void render(OutputStream outputStream) throws IOException { - try (PrintStream ps = new PrintStream(outputStream)) { - e.printStackTrace(ps); - } - } - } - -} diff --git a/container-core/src/main/java/com/yahoo/container/handler/test/MockServiceHandler.java b/container-core/src/main/java/com/yahoo/container/handler/test/MockServiceHandler.java deleted file mode 100644 index a06422a1bf4..00000000000 --- a/container-core/src/main/java/com/yahoo/container/handler/test/MockServiceHandler.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.handler.test; - -import com.yahoo.api.annotations.Beta; -import com.yahoo.container.jdisc.HttpRequest; - -/** - * A service handler that is able to map a request to a key and retrieve a value given a key. - * - * @author Ulf Lilleengen - */ -@Beta -public interface MockServiceHandler { - - /** - * Create a custom Key given a http request. This will be called for each request, and allows a handler - * to customize its key format. - * @param request The client http request. - * @return a {@link Key} used to query for the value. - */ - Key createKey(HttpRequest request); - - /** - * Lookup a {@link Value} for a {@link Key}. Returns null if the key is not found. - * - * @param key The {@link Key} to look up. - * @return A {@link Value} used as response. - */ - Value get(Key key); - - final class Value { - - public final int returnCode; - public final byte[] data; - public final String contentType; - - public Value(int returnCode, byte[] data, String contentType) { - this.returnCode = returnCode; - this.data = data; - this.contentType = contentType; - } - - } - - interface Key { - - int hashCode(); - boolean equals(Object other); - - } -} diff --git a/container-core/src/main/java/com/yahoo/container/handler/test/package-info.java b/container-core/src/main/java/com/yahoo/container/handler/test/package-info.java deleted file mode 100644 index 23338286943..00000000000 --- a/container-core/src/main/java/com/yahoo/container/handler/test/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** - * Contains the handler test utilities. - */ -@ExportPackage -package com.yahoo.container.handler.test; - -import com.yahoo.osgi.annotation.ExportPackage; diff --git a/container-core/src/test/java/com/yahoo/container/handler/test/MockServiceTest.java b/container-core/src/test/java/com/yahoo/container/handler/test/MockServiceTest.java deleted file mode 100644 index eefcc43fcb7..00000000000 --- a/container-core/src/test/java/com/yahoo/container/handler/test/MockServiceTest.java +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.handler.test; - -import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer; -import org.junit.jupiter.api.Test; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.concurrent.Executor; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -/** - * @author Ulf Lilleengen - */ -public class MockServiceTest { - - private final File testFile = new File("src/test/java/com/yahoo/container/handler/test/test.txt"); - - @Test - void testHandlerTextFormat() throws InterruptedException, IOException { - HttpResponse response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.GET, "/foo/bar"); - assertResponse(response, 200, "Hello\nThere!"); - - response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.GET, "http://my.host:8080/foo/bar?key1=foo&key2=bar"); - assertResponse(response, 200, "With params!"); - - response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.PUT, "/bar"); - assertResponse(response, 301, "My data is on a single line"); - } - - @Test - void testNoHandlerFound() throws InterruptedException, IOException { - HttpResponse response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.DELETE, "/foo/bar"); - assertEquals(404, response.getStatus()); - assertResponseContents(response, "DELETE:/foo/bar was not found"); - } - - @Test - void testUnknownFileType() throws InterruptedException, IOException { - assertThrows(IllegalArgumentException.class, () -> { - runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method.GET, "", new File("nonexistant")); - }); - } - - @Test - void testExceptionResponse() throws InterruptedException, IOException { - assertThrows(FileNotFoundException.class, () -> { - runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method.GET, "", new File("nonexistant.txt")); - }); - } - - private void assertResponse(HttpResponse response, int expectedCode, String expectedMessage) throws IOException { - assertEquals(expectedCode, response.getStatus()); - assertResponseContents(response, expectedMessage); - } - - private void assertResponseContents(HttpResponse response, String expected) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - response.render(baos); - assertEquals(expected, baos.toString()); - } - - private HttpResponse runHandler(com.yahoo.jdisc.http.HttpRequest.Method method, String path) throws InterruptedException, IOException { - return runHandlerWithFile(method, path, testFile); - } - - private HttpResponse runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method method, String path, File file) throws InterruptedException, IOException { - MockserviceConfig.Builder builder = new MockserviceConfig.Builder(); - builder.file(file.getPath()); - MockService handler = new MockService(new MockExecutor(), MockFileAcquirer.returnFile(file), new MockserviceConfig(builder), null); - return handler.handle(HttpRequest.createTestRequest(path, method)); - } - - private static class MockExecutor implements Executor { - @Override - public void execute(Runnable command) { - command.run(); - } - } -} diff --git a/container-core/src/test/java/com/yahoo/container/handler/test/test.txt b/container-core/src/test/java/com/yahoo/container/handler/test/test.txt deleted file mode 100644 index baca20fbbdc..00000000000 --- a/container-core/src/test/java/com/yahoo/container/handler/test/test.txt +++ /dev/null @@ -1,6 +0,0 @@ -GET:/foo/bar:200:Hello -There! - -PUT:/bar:301:My data is on a single line - -GET:/foo/bar?key1=foo&key2=bar:200:With params! |