AuditInfoExtractor { static Map<String, String> extract(Message message) { Map<String, String> auditInfo = new HashMap<String, String>(); try { handleInMessage(auditInfo, message.getExchange().getInMessage()); handleOutSuccessMessage(auditInfo, message.getExchange().getOutMessage()); handleOutFaultMessage(auditInfo, message.getExchange().getOutFaultMessage()); handleTime(auditInfo, message.getExchange().get(AuditInfo.START_TIME.getKey())); } catch (Exception exception) { LogUtils.error(LOGGER, exception, "Error obtaining auditing information."); } return auditInfo; } private  AuditInfoExtractor();   }
@Test public void testSuccessExecution() { Message message = Mockito.mock(Message.class); Exchange exchange = Mockito.mock(Exchange.class); Message inMessage = getInMessage(); Mockito.when(message.getExchange()).thenReturn(exchange); Mockito.when(exchange.getInMessage()).thenReturn(inMessage); Message outSuccessMessage = getOutSuccessMessage(); Mockito.when(exchange.getOutMessage()).thenReturn(outSuccessMessage); Mockito.when(exchange.get(AuditInfo.START_TIME.getKey())).thenReturn(1000l); Map<String, String> auditInfo = AuditInfoExtractor.extract(message); assertEquals(auditInfo.size(), 18); assertEquals(auditInfo.get(AuditInfo.HOST.getKey()), "localhost"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_CONTENT_TYPE.getKey()), "application/octet-stream"); assertEquals(auditInfo.get(AuditInfo.HTTP_METHOD.getKey()), "GET"); assertEquals(auditInfo.get(AuditInfo.BODY_AS_STRING.getKey()), "body-content"); assertEquals(auditInfo.get(AuditInfo.USER_ROLE.getKey()), "ADMIN"); assertEquals(auditInfo.get(AuditInfo.REMOTE_HOST.getKey()), "127.0.0.1"); assertEquals(auditInfo.get(AuditInfo.START_TIME.getKey()), "1000"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_LENGTH.getKey()), "150"); assertEquals(auditInfo.get(AuditInfo.BASE_PATH.getKey()), "users"); assertEquals(auditInfo.get(AuditInfo.QUERY_STRING.getKey()), ""); assertEquals(auditInfo.get(AuditInfo.USER_GROUPS.getKey()), "everyone"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_STATUS_CODE.getKey()), "200"); assertEquals(auditInfo.get(AuditInfo.PATH.getKey()), "users/user/15"); assertEquals(auditInfo.get(AuditInfo.USER_NAME.getKey()), "admin"); assertEquals(auditInfo.get(AuditInfo.REMOTE_ADDR.getKey()), "127.0.0.1"); assertEquals(auditInfo.get(AuditInfo.REMOTE_USER.getKey()), "User[id=2, name=admin, group=[UserGroup[id=1, groupName=everyone]], role=ADMIN]"); assertNotNull(auditInfo.get(AuditInfo.END_TIME.getKey())); assertEquals(Long.parseLong(auditInfo.get(AuditInfo.TOTAL_TIME.getKey())), Long.parseLong(auditInfo.get(AuditInfo.END_TIME.getKey())) - 1000); }
@Test public void testFaultExecution() { Message message = Mockito.mock(Message.class); Exchange exchange = Mockito.mock(Exchange.class); Message inMessage = getInMessage(); Mockito.when(message.getExchange()).thenReturn(exchange); Mockito.when(exchange.getInMessage()).thenReturn(inMessage); Message outFaultMessage = getOutFaultMessage(); Mockito.when(exchange.getOutFaultMessage()).thenReturn(outFaultMessage); Mockito.when(exchange.get(AuditInfo.START_TIME.getKey())).thenReturn(1000l); Map<String, String> auditInfo = AuditInfoExtractor.extract(message); assertEquals(auditInfo.size(), 20); assertEquals(auditInfo.get(AuditInfo.HOST.getKey()), "localhost"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_CONTENT_TYPE.getKey()), "application/octet-stream"); assertEquals(auditInfo.get(AuditInfo.HTTP_METHOD.getKey()), "GET"); assertEquals(auditInfo.get(AuditInfo.BODY_AS_STRING.getKey()), "body-content"); assertEquals(auditInfo.get(AuditInfo.USER_ROLE.getKey()), "ADMIN"); assertEquals(auditInfo.get(AuditInfo.REMOTE_HOST.getKey()), "127.0.0.1"); assertEquals(auditInfo.get(AuditInfo.START_TIME.getKey()), "1000"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_LENGTH.getKey()), "100"); assertEquals(auditInfo.get(AuditInfo.BASE_PATH.getKey()), "users"); assertEquals(auditInfo.get(AuditInfo.QUERY_STRING.getKey()), ""); assertEquals(auditInfo.get(AuditInfo.USER_GROUPS.getKey()), "everyone"); assertEquals(auditInfo.get(AuditInfo.RESPONSE_STATUS_CODE.getKey()), "500"); assertEquals(auditInfo.get(AuditInfo.PATH.getKey()), "users/user/15"); assertEquals(auditInfo.get(AuditInfo.USER_NAME.getKey()), "admin"); assertEquals(auditInfo.get(AuditInfo.REMOTE_ADDR.getKey()), "127.0.0.1"); assertEquals(auditInfo.get(AuditInfo.REMOTE_USER.getKey()), "User[id=2, name=admin, group=[UserGroup[id=1, groupName=everyone]], role=ADMIN]"); assertEquals(auditInfo.get(AuditInfo.ERROR_MESSAGE.getKey()), "exception-message"); assertEquals(auditInfo.get(AuditInfo.FAILED.getKey()), "true"); assertNotNull(auditInfo.get(AuditInfo.END_TIME.getKey())); assertEquals(Long.parseLong(auditInfo.get(AuditInfo.TOTAL_TIME.getKey())), Long.parseLong(auditInfo.get(AuditInfo.END_TIME.getKey())) - 1000); }