aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/servlet/ServletAccessLoggingTest.java
blob: a533a447f6ab9df2db4e38249fbdce9e3fd4b8e2 (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
55
56
57
58
59
60
61
62
63
64
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty.servlet;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.container.logging.RequestLog;
import com.yahoo.container.logging.RequestLogEntry;
import com.yahoo.jdisc.http.server.jetty.TestDriver;
import com.yahoo.jdisc.http.server.jetty.TestDrivers;
import org.junit.Test;
import org.mockito.verification.VerificationMode;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

/**
 * @author bakksjo
 * @author bjorncs
 */
public class ServletAccessLoggingTest extends ServletTestBase {
    private static final long MAX_LOG_WAIT_TIME_MILLIS = TimeUnit.SECONDS.toMillis(60);

    @Test
    public void accessLogIsInvokedForNonJDiscServlet() throws Exception {
        final AccessLog accessLog = mock(AccessLog.class);
        final TestDriver testDriver = newTestDriver(accessLog);
        httpGet(testDriver, TestServlet.PATH).execute();
        verifyCallsLog(accessLog, timeout(MAX_LOG_WAIT_TIME_MILLIS).times(1));
    }

    @Test
    public void accessLogIsInvokedForJDiscServlet() throws Exception {
        final AccessLog accessLog = mock(AccessLog.class);
        final TestDriver testDriver = newTestDriver(accessLog);
        testDriver.client().newGet("/status.html").execute();
        verifyCallsLog(accessLog, timeout(MAX_LOG_WAIT_TIME_MILLIS).times(1));
    }

    private void verifyCallsLog(RequestLog requestLog, final VerificationMode verificationMode) {
        verify(requestLog, verificationMode).log(any(RequestLogEntry.class));
    }

    private TestDriver newTestDriver(RequestLog requestLog) throws IOException {
        return TestDrivers.newInstance(dummyRequestHandler, bindings(requestLog));
    }

    private Module bindings(RequestLog requestLog) {
        return Modules.combine(
                new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(RequestLog.class).toInstance(requestLog);
                    }
                },
                guiceModule());
    }
}