summaryrefslogtreecommitdiffstats
path: root/container-core
diff options
context:
space:
mode:
authorOla Aunrønning <ola.aunroe@gmail.com>2018-09-20 14:34:44 +0200
committerOla Aunrønning <ola.aunroe@gmail.com>2018-09-20 14:56:56 +0200
commit2fa4d31e2ef1bf749efce2df9ed5a7d4dcede8a2 (patch)
tree74607406579e0b68375ee8739aea6968d3251553 /container-core
parentc1d8a9f6bf3941f1388fb06fea2edd5eeeddbe6e (diff)
Added test for LogHandler
Diffstat (limited to 'container-core')
-rw-r--r--container-core/src/main/java/com/yahoo/container/handler/LogHandler.java9
-rw-r--r--container-core/src/main/java/com/yahoo/container/handler/LogReader.java5
-rw-r--r--container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java54
-rw-r--r--container-core/src/test/java/com/yahoo/container/handler/LogReaderTest.java8
4 files changed, 66 insertions, 10 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java b/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java
index c6a50c65dd4..d1f84aefeaa 100644
--- a/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java
+++ b/container-core/src/main/java/com/yahoo/container/handler/LogHandler.java
@@ -17,10 +17,16 @@ import java.util.concurrent.Executor;
public class LogHandler extends ThreadedHttpRequestHandler {
private static final String LOG_DIRECTORY = "/home/y/logs/vespa/logarchive/";
+ private final LogReader logReader;
@Inject
public LogHandler(Executor executor) {
+ this(executor, new LogReader());
+ }
+
+ protected LogHandler(Executor executor, LogReader logReader) {
super(executor);
+ this.logReader = logReader;
}
@Override
@@ -30,9 +36,8 @@ public class LogHandler extends ThreadedHttpRequestHandler {
HashMap<String, String> apiParams = getParameters(request);
long earliestLogThreshold = getEarliestThreshold(apiParams);
long latestLogThreshold = getLatestThreshold(apiParams);
- LogReader logReader= new LogReader(earliestLogThreshold, latestLogThreshold);
try {
- JSONObject logJson = logReader.readLogs(LOG_DIRECTORY);
+ JSONObject logJson = logReader.readLogs(LOG_DIRECTORY, earliestLogThreshold, latestLogThreshold);
responseJSON.put("logs", logJson);
} catch (IOException | JSONException e) {
return new HttpResponse(404) {
diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
index 5d78e37ec9e..ae43d850258 100644
--- a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
+++ b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
@@ -14,12 +14,9 @@ public class LogReader {
long earliestLogThreshold;
long latestLogThreshold;
- public LogReader(long earliestLogThreshold, long latestLogThreshold) {
+ protected JSONObject readLogs(String logDirectory, long earliestLogThreshold, long latestLogThreshold) throws IOException, JSONException {
this.earliestLogThreshold = earliestLogThreshold;
this.latestLogThreshold = latestLogThreshold;
- }
-
- protected JSONObject readLogs(String logDirectory) throws IOException, JSONException {
JSONObject json = new JSONObject();
File root = new File(logDirectory);
traverse_folder(root, json, "");
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
new file mode 100644
index 00000000000..5a3b62be287
--- /dev/null
+++ b/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java
@@ -0,0 +1,54 @@
+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.util.concurrent.Executor;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+public class LogHandlerTest {
+
+
+ @Test
+ public void handleCorrectlyParsesQueryParameters() throws IOException {
+ MockLogReader mockLogReader = new MockLogReader();
+ LogHandler logHandler = new LogHandler(mock(Executor.class), mockLogReader);
+
+ {
+ String uri = "http://myhost.com:1111/logs?from=1000&to=2000";
+ 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\"}}";
+ assertEquals(expectedResponse, bos.toString());
+ }
+
+ {
+ String uri = "http://myhost.com:1111/logs?from=0&to=1000";
+ 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\"}}";
+ assertEquals(expectedResponse, bos.toString());
+ }
+
+ }
+
+ class MockLogReader extends LogReader {
+ @Override
+ protected JSONObject readLogs(String logDirectory, long earliestLogThreshold, long latestLogThreshold) throws JSONException {
+ if(latestLogThreshold > 1000) {
+ return new JSONObject("{\"one\":\"newer_log\"}");
+ } else {
+ return new JSONObject("{\"two\":\"older_log\"}");
+ }
+ }
+ }
+} \ No newline at end of file
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 534026f89ac..ad779f6b7b4 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
@@ -20,8 +20,8 @@ public class LogReaderTest {
@Test
public void testThatFilesAreWrittenCorrectlyToOutputStream() throws Exception{
String logDirectory = "src/test/resources/logfolder/";
- LogReader logReader = new LogReader(21, Long.MAX_VALUE);
- JSONObject json = logReader.readLogs(logDirectory);
+ LogReader logReader = new LogReader();
+ JSONObject json = logReader.readLogs(logDirectory, 21, Long.MAX_VALUE);
String expected = "{\"subfolder-log2.log\":\"VGhpcyBpcyBhbm90aGVyIGxvZyBmaWxl\",\"log1.log\":\"VGhpcyBpcyBvbmUgbG9nIGZpbGU=\"}";
String actual = json.toString();
assertEquals(expected, actual);
@@ -30,8 +30,8 @@ public class LogReaderTest {
@Test
public void testThatLogsOutsideRangeAreExcluded() throws Exception {
String logDirectory = "src/test/resources/logfolder/";
- LogReader logReader = new LogReader(Long.MAX_VALUE, Long.MIN_VALUE);
- JSONObject json = logReader.readLogs(logDirectory);
+ LogReader logReader = new LogReader();
+ JSONObject json = logReader.readLogs(logDirectory, Long.MAX_VALUE, Long.MIN_VALUE);
String expected = "{}";
String actual = json.toString();
assertEquals(expected, actual);