aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/handler/LogHandlerTest.java
blob: 5a3b62be287160429a4ea133415d170ca22b45bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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\"}");
            }
        }
    }
}