ConsumerAuditUtil implements InitializingBean { public boolean audit(HttpServletRequest request, long consumerId) { if ("GET".equalsIgnoreCase(request.getMethod())) { return true; } String uri = request.getRequestURI(); if (!Strings.isNullOrEmpty(request.getQueryString())) { uri += "?" + request.getQueryString(); } ConsumerAudit consumerAudit = new ConsumerAudit(); Date now = new Date(); consumerAudit.setConsumerId(consumerId); consumerAudit.setUri(uri); consumerAudit.setMethod(request.getMethod()); consumerAudit.setDataChangeCreatedTime(now); consumerAudit.setDataChangeLastModifiedTime(now); return this.audits.offer(consumerAudit); } ConsumerAuditUtil(final ConsumerService consumerService); boolean audit(HttpServletRequest request, long consumerId); @Override void afterPropertiesSet(); void stopAudit();  }
@Test public void audit() throws Exception { long someConsumerId = 1; String someUri = "someUri"; String someQuery = "someQuery"; String someMethod = "someMethod"; when(request.getRequestURI()).thenReturn(someUri); when(request.getQueryString()).thenReturn(someQuery); when(request.getMethod()).thenReturn(someMethod); SettableFuture<List<ConsumerAudit>> result = SettableFuture.create(); doAnswer((Answer<Void>) invocation -> { Object[] args = invocation.getArguments(); result.set((List<ConsumerAudit>) args[0]); return null; }).when(consumerService).createConsumerAudits(anyCollection()); consumerAuditUtil.audit(request, someConsumerId); List<ConsumerAudit> audits = result.get(batchTimeout * 5, batchTimeUnit); assertEquals(1, audits.size()); ConsumerAudit audit = audits.get(0); assertEquals(String.format("%s?%s", someUri, someQuery), audit.getUri()); assertEquals(someMethod, audit.getMethod()); assertEquals(someConsumerId, audit.getConsumerId()); }