class JsonLayout extends JsonLayoutBase<IAccessEvent> {

    public JsonLayout() {
        super();
        this.includeRemoteAddr = true;
        this.includeRemoteUser = true;
        this.includeRequestTime = true;
        this.includeRequestURI = true;
        this.includeStatusCode = true;
        this.includeMethod = true;
        this.includeProtocol = true;
        this.includeRequestURL = false;
        this.includeContentLength = false;
        this.includeRemoteHost = true;
        this.includeServerName = true;
        this.includeRequestHeader = true;
        this.includeRequestParameter = true;
        this.includeLocalPort = false;
        this.includeRequestContent = false;
        this.includeResponseContent = false;
    }

}

class JsonLayoutTest {

    private AccessContext context;

    @Test
    public void jsonLayout() throws Exception {

        configure(TEST_DIR_PREFIX + "input/json/jsonLayout.xml");
        ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("STR_LIST");
        IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
        listAppender.doAppend(event);

        assertEquals(1, listAppender.list.size());
        IAccessEvent iAccessEvent = listAppender.list.get(0);

        JsonLayout jsonLayout = new JsonLayout();
        jsonLayout.setContext(context);
        String log = jsonLayout.doLayout(iAccessEvent);

        assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEHOST_ATTR_NAME, event.getRemoteHost())));
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEUSER_ATTR_NAME, event.getRemoteUser())));
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.REMOTEADDR_ATTR_NAME, event.getRemoteAddr())));
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.METHOD_ATTR_NAME, event.getMethod())));
        if (event.getRequestHeaderMap().size() == 2) {
            Iterator<Map.Entry<String, String>> iterator = event.getRequestHeaderMap().entrySet().iterator();
            Map.Entry<String, String> firstInMap = iterator.next();
            Map.Entry<String, String> secondInMap = iterator.next();
            assertThat(log, containsString(String.format("%s={%s=%s, %s=%s}", JsonLayout.REQUESTHEADER_ATTR_NAME, firstInMap.getKey(), firstInMap.getValue(), secondInMap.getKey(), secondInMap.getValue())));
        }
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.PROTOCOL_ATTR_NAME, event.getProtocol())));
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.SERVERNAME_ATTR_NAME, event.getServerName())));
        assertThat(log, containsString(String.format("%s=%s", JsonLayout.STATUSCODE_ATTR_NAME, event.getStatusCode())));
    }

}