diff options
Diffstat (limited to 'container-core/src/test')
-rw-r--r-- | container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java | 22 | ||||
-rw-r--r-- | container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java | 36 |
2 files changed, 26 insertions, 32 deletions
diff --git a/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java b/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java index d578a745c9e..f0885451431 100644 --- a/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java +++ b/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java @@ -3,12 +3,11 @@ package com.yahoo.container.handler; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; -import org.json.JSONException; -import org.json.JSONObject; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.time.Instant; import java.util.concurrent.Executor; @@ -17,7 +16,6 @@ import static org.mockito.Mockito.mock; public class LogHandlerTest { - @Test public void handleCorrectlyParsesQueryParameters() throws IOException { MockLogReader mockLogReader = new MockLogReader(); @@ -28,7 +26,7 @@ public class LogHandlerTest { HttpResponse response = logHandler.handle(HttpRequest.createTestRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.GET)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); response.render(bos); - String expectedResponse = "{\"logs\":{\"one\":\"newer_log\"}}"; + String expectedResponse = "newer log"; assertEquals(expectedResponse, bos.toString()); } @@ -37,7 +35,7 @@ public class LogHandlerTest { HttpResponse response = logHandler.handle(HttpRequest.createTestRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.GET)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); response.render(bos); - String expectedResponse = "{\"logs\":{\"two\":\"older_log\"}}"; + String expectedResponse = "older log"; assertEquals(expectedResponse, bos.toString()); } @@ -49,12 +47,14 @@ public class LogHandlerTest { } @Override - protected JSONObject readLogs(Instant earliestLogThreshold, Instant latestLogThreshold) throws JSONException { - if (latestLogThreshold.isAfter(Instant.ofEpochMilli(1000))) { - return new JSONObject("{\"one\":\"newer_log\"}"); - } else { - return new JSONObject("{\"two\":\"older_log\"}"); - } + protected void writeLogs(OutputStream outputStream, Instant earliestLogThreshold, Instant latestLogThreshold) { + try { + if (latestLogThreshold.isAfter(Instant.ofEpochMilli(1000))) { + outputStream.write("newer log".getBytes()); + } else { + outputStream.write("older log".getBytes()); + } + } catch (Exception e) {} } } } diff --git a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java index 464d6f772eb..27300e5f3f5 100644 --- a/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java +++ b/container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java @@ -2,7 +2,6 @@ package com.yahoo.container.handler; import com.yahoo.vespa.test.file.TestFileSystem; -import org.json.JSONObject; import org.junit.Before; import org.junit.Test; @@ -15,7 +14,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileTime; import java.time.Instant; -import java.util.Scanner; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -37,47 +35,37 @@ public class LogReaderTest { Files.setLastModifiedTime( Files.write(logDirectory.resolve("subfolder/log2.log"), "This is another log file\n".getBytes()), FileTime.from(Instant.ofEpochMilli(234))); - } - @Test - public void testThatFilesAreWrittenCorrectlyToOutputStream() throws Exception{ - LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); - JSONObject json = logReader.readLogs(Instant.ofEpochMilli(21), Instant.now()); - String expected = "{\"subfolder-log2.log\":\"VGhpcyBpcyBhbm90aGVyIGxvZyBmaWxlCg==\",\"log1.log.gz\":\"VGhpcyBpcyBvbmUgbG9nIGZpbGUK\"}"; - String actual = json.toString(); - assertEquals(expected, actual); } @Test public void testThatLogsOutsideRangeAreExcluded() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); - JSONObject json = logReader.readLogs(Instant.MAX, Instant.MIN); - String expected = "{}"; - String actual = json.toString(); + logReader.writeLogs(baos, Instant.ofEpochMilli(235), Instant.ofEpochMilli(300)); + String expected = ""; + String actual = decompress(baos.toByteArray()); assertEquals(expected, actual); } @Test public void testThatLogsNotMatchingRegexAreExcluded() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*2\\.log")); - JSONObject json = logReader.readLogs(Instant.ofEpochMilli(21), Instant.now()); - String expected = "{\"subfolder-log2.log\":\"VGhpcyBpcyBhbm90aGVyIGxvZyBmaWxlCg==\"}"; - String actual = json.toString(); + logReader.writeLogs(baos, Instant.ofEpochMilli(21), Instant.now()); + String expected = "This is another log file\n"; + String actual = decompress(baos.toByteArray()); assertEquals(expected, actual); } @Test public void testZippedStreaming() throws IOException { - ByteArrayOutputStream zippedBaos = new ByteArrayOutputStream(); LogReader logReader = new LogReader(logDirectory, Pattern.compile(".*")); logReader.writeLogs(zippedBaos, Instant.ofEpochMilli(21), Instant.now()); - GZIPInputStream unzippedIs = new GZIPInputStream(new ByteArrayInputStream(zippedBaos.toByteArray())); - - Scanner s = new Scanner(unzippedIs).useDelimiter("\\A"); - String actual = s.hasNext() ? s.next() : ""; String expected = "This is one log file\nThis is another log file\n"; + String actual = decompress(zippedBaos.toByteArray()); assertEquals(expected, actual); } @@ -89,4 +77,10 @@ public class LogReaderTest { return baos.toByteArray(); } + private String decompress(byte[] input) throws IOException { + if (input.length == 0) return ""; + byte[] decompressed = new GZIPInputStream(new ByteArrayInputStream(input)).readAllBytes(); + return new String(decompressed); + } + } |