MongoDBAccessEventAppender extends MongoDBAppenderBase<IAccessEvent> { @Override protected BasicDBObject toMongoDocument(IAccessEvent event) { final BasicDBObject doc = new BasicDBObject(); doc.append("timeStamp", new Date(event.getTimeStamp())); if (serverName) doc.append("serverName", event.getServerName()); addRemote(doc, event); addRequest(doc, event); addResponse(doc, event); return doc; }  void setServerName(boolean serverName); void setRequestUri(boolean requestUri); void setRequestProtocol(boolean requestProtocol); void setRequestMethod(boolean requestMethod); void setRequestPostContent(boolean requestPostContent); void setRequestSessionId(boolean requestSessionId); void setRequestUserAgent(boolean requestUserAgent); void setRequestReferer(boolean requestReferer); void setRemoteHost(boolean remoteHost); void setRemoteUser(boolean remoteUser); void setRemoteAddr(boolean remoteAddr); void setResponseContentLength(boolean responseContentLength); void setResponseStatusCode(boolean responseStatusCode);  }
@Test public void testTimeStamp() { Mockito.when(event.getTimeStamp()).thenReturn(1000L); final BasicDBObject dbObject = appender.toMongoDocument(event); assertEquals(new Date(1000L), dbObject.get("timeStamp")); }
@Test public void testServerName() { Mockito.when(event.getServerName()).thenReturn("servername"); final BasicDBObject dbObject = appender.toMongoDocument(event); assertEquals("servername", dbObject.get("serverName")); }
@Test public void testRemote() { Mockito.when(event.getRemoteHost()).thenReturn("host"); Mockito.when(event.getRemoteUser()).thenReturn("user"); Mockito.when(event.getRemoteAddr()).thenReturn("addr"); final BasicDBObject dbObject = appender.toMongoDocument(event); BasicDBObject remoteDBObject = (BasicDBObject) dbObject.get("remote"); assertEquals("host", remoteDBObject.getString("host")); assertEquals("user", remoteDBObject.getString("user")); assertEquals("addr", remoteDBObject.getString("addr")); }
@Test public void testRequest() { Mockito.when(event.getRequestURI()).thenReturn("uri"); Mockito.when(event.getProtocol()).thenReturn("protocol"); Mockito.when(event.getMethod()).thenReturn("method"); Mockito.when(event.getRequestContent()).thenReturn("postContent"); Mockito.when(event.getCookie("JSESSIONID")).thenReturn("sessionId"); Mockito.when(event.getRequestHeader("User-Agent")).thenReturn("userAgent"); Mockito.when(event.getRequestHeader("Referer")).thenReturn("referer"); final BasicDBObject dbObject = appender.toMongoDocument(event); BasicDBObject requestDBObject = (BasicDBObject) dbObject.get("request"); assertEquals("uri", requestDBObject.getString("uri")); assertEquals("protocol", requestDBObject.getString("protocol")); assertEquals("method", requestDBObject.getString("method")); assertEquals("postContent", requestDBObject.getString("postContent")); assertEquals("sessionId", requestDBObject.getString("sessionId")); assertEquals("userAgent", requestDBObject.getString("userAgent")); assertEquals("referer", requestDBObject.getString("referer")); }