private IAccessEvent mockBasicILoggingEvent() { IAccessEvent event = mock(IAccessEvent.class); when(event.getContentLength()).thenReturn(123L); when(event.getMethod()).thenReturn("GET"); when(event.getProtocol()).thenReturn("HTTPS"); when(event.getRemoteHost()).thenReturn("123.123.123.123"); when(event.getRemoteUser()).thenReturn("remote-user"); when(event.getRequestURI()).thenReturn("/my/uri"); when(event.getRequestURL()).thenReturn("https://123.123.123.123/my/uri"); when(event.getStatusCode()).thenReturn(200); when(event.getTimeStamp()).thenReturn(System.currentTimeMillis()); return event; }
@Test public void basicsAreIncluded() throws Exception { final long timestamp = System.currentTimeMillis(); IAccessEvent event = mockBasicILoggingEvent(); when(event.getTimeStamp()).thenReturn(timestamp); encoder.doEncode(event); closeQuietly(outputStream); JsonNode node = MAPPER.readTree(outputStream.toByteArray()); assertThat( node.get("@timestamp").textValue(), is(FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").format(timestamp))); assertThat(node.get("@version").intValue(), is(1)); assertThat( node.get("@message").textValue(), is( String.format( "%s - %s [%s] \"%s\" %s %s", event.getRemoteHost(), event.getRemoteUser(), FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ") .format(event.getTimeStamp()), event.getRequestURL(), event.getStatusCode(), event.getContentLength()))); assertThat(node.get("@fields.method").textValue(), is(event.getMethod())); assertThat(node.get("@fields.protocol").textValue(), is(event.getProtocol())); assertThat(node.get("@fields.status_code").asInt(), is(event.getStatusCode())); assertThat(node.get("@fields.requested_url").textValue(), is(event.getRequestURL())); assertThat(node.get("@fields.requested_uri").textValue(), is(event.getRequestURI())); assertThat(node.get("@fields.remote_host").textValue(), is(event.getRemoteHost())); assertThat(node.get("@fields.HOSTNAME").textValue(), is(event.getRemoteHost())); assertThat(node.get("@fields.remote_user").textValue(), is(event.getRemoteUser())); assertThat(node.get("@fields.content_length").asLong(), is(event.getContentLength())); }