### Question:
AuditFilter implements Filter { @Override public Promise<ResourceResponse, ResourceException> filterUpdate(Context context, UpdateRequest request, RequestHandler next) { CrestAuditor auditor = newAuditor(context, request); auditor.auditAccessAttempt(); return auditResponse(next.handleUpdate(context, request), auditor, request); } @Inject  AuditFilter(@Named("frRest") Debug debug, CrestAuditorFactory crestAuditorFactory); @Override Promise<ActionResponse, ResourceException> filterAction(Context context, ActionRequest request,
            RequestHandler next); @Override Promise<ResourceResponse, ResourceException> filterCreate(Context context, CreateRequest request,
            RequestHandler next); @Override Promise<ResourceResponse, ResourceException> filterDelete(Context context, DeleteRequest request,
            RequestHandler next); @Override Promise<ResourceResponse, ResourceException> filterPatch(Context context, PatchRequest request,
            RequestHandler next); @Override Promise<QueryResponse, ResourceException> filterQuery(Context context, QueryRequest request,
            QueryResourceHandler handler, RequestHandler next); @Override Promise<ResourceResponse, ResourceException> filterRead(Context context, ReadRequest request,
            RequestHandler next); @Override Promise<ResourceResponse, ResourceException> filterUpdate(Context context, UpdateRequest request,
            RequestHandler next); JsonValue getCreateSuccessDetail(CreateRequest request, ResourceResponse response); JsonValue getReadSuccessDetail(ReadRequest request, ResourceResponse response); JsonValue getUpdateSuccessDetail(UpdateRequest request, ResourceResponse response); JsonValue getDeleteSuccessDetail(DeleteRequest request, ResourceResponse response); JsonValue getPatchSuccessDetail(PatchRequest request, ResourceResponse response); JsonValue getActionSuccessDetail(ActionRequest request, ActionResponse response); JsonValue getQuerySuccessDetail(QueryRequest request, QueryResponse response);  }### Answer:
@Test public void shouldAuditSuccessForResults() throws Exception { ResourceResponse response = mock(ResourceResponse.class); Promise<ResourceResponse, ResourceException> myPromise = Promises.newResultPromise(response); given(filterChain.handleUpdate(any(Context.class), any(UpdateRequest.class))).willReturn(myPromise); given(auditorFactory.create(context, updateRequest)).willReturn(auditor); AuditFilter auditFilter = new AuditFilter(debug, auditorFactory); auditFilter.filterUpdate(context, updateRequest, filterChain); verify(auditor).auditAccessSuccess(null); }
@Test public void shouldAuditFailureForExceptions() throws Exception { Promise<ResourceResponse, ResourceException> myPromise = new NotFoundException("message").asPromise(); given(filterChain.handleUpdate(any(Context.class), any(UpdateRequest.class))).willReturn(myPromise); given(auditorFactory.create(context, updateRequest)).willReturn(auditor); AuditFilter auditFilter = new AuditFilter(debug, auditorFactory); auditFilter.filterUpdate(context, updateRequest, filterChain); verify(auditor).auditAccessFailure(404, "message"); }
@Test public void shouldAuditFailureForRuntimeExceptions() throws Exception { Promise<ResourceResponse, ResourceException> myPromise = Promises.newResultPromise(null); myPromise = myPromise.thenAsync(new AsyncFunction<ResourceResponse, ResourceResponse, ResourceException>() { @Override public Promise<? extends ResourceResponse, ? extends ResourceException> apply(ResourceResponse value) throws ResourceException { throw new NullPointerException("foo"); } }); given(filterChain.handleUpdate(any(Context.class), any(UpdateRequest.class))).willReturn(myPromise); given(auditorFactory.create(context, updateRequest)).willReturn(auditor); AuditFilter auditFilter = new AuditFilter(debug, auditorFactory); auditFilter.filterUpdate(context, updateRequest, filterChain); verify(auditor).auditAccessFailure(500, "foo"); }