DefaultRestFilterChain implements RestFilterChain { @Override public <R> DispatchRequest doFilter( RestAction<R> action, RestCallback<R> resultCallback, ExecuteCommand<RestAction<R>, RestCallback<R>> executeCommand) { if (filterIterator.hasNext()) { Entry<RestContext, RestFilter> filterEntry = filterIterator.next(); RestContext subjectContext = new RestContext.Builder(action).build(); if (filterEntry.getKey().equals(subjectContext)) { return filterEntry.getValue().filter(action, resultCallback, executeCommand, this); } else { return doFilter(action, resultCallback, executeCommand); } } else { return executeCommand.execute(action, resultCallback); } } @Inject protected  DefaultRestFilterChain(RestFilterRegistry filterRegistry); @Override DispatchRequest doFilter(
            RestAction<R> action,
            RestCallback<R> resultCallback,
            ExecuteCommand<RestAction<R>, RestCallback<R>> executeCommand);  }
@Test public void doFilter_noFilter_executesCommand() throws Exception { RestAction action = mock(RestAction.class); RestCallback callback = mock(RestCallback.class); ExecuteCommand command = mock(ExecuteCommand.class); Iterator iterator = mock(Iterator.class); given(filterRegistry.iterator()).willReturn(iterator); given(iterator.hasNext()).willReturn(false); DispatchRequest expected = mock(DispatchRequest.class); given(command.execute(same(action), same(callback))).willReturn(expected); DispatchRequest result = filterChainProvider.get().doFilter(action, callback, command); assertThat(result).isSameAs(expected); }
@Test public void doFilter_matchingFilter_delegatesToFilter() throws Exception { RestAction action = createRestAction(""); RestCallback callback = mock(RestCallback.class); ExecuteCommand command = mock(ExecuteCommand.class); DispatchRequest expected = mock(DispatchRequest.class); RestFilter filter = createFilter(createRestContext(action), expected); ArrayList<Entry<RestContext, RestFilter>> filters = Lists.newArrayList(new SimpleEntry<>(filter.getRestContext(), filter)); given(filterRegistry.iterator()).willReturn(filters.iterator()); DispatchRequest result = filterChainProvider.get().doFilter(action, callback, command); assertThat(result).isSameAs(expected); }
@Test public void doFilter_noMatchingFilter_executesCommand() throws Exception { RestAction action = createRestAction("/path1"); RestAction subjectAction = createRestAction("/path2"); RestCallback callback = mock(RestCallback.class); ExecuteCommand command = mock(ExecuteCommand.class); DispatchRequest expected = mock(DispatchRequest.class); RestFilter filter = createFilter(createRestContext(subjectAction), mock(DispatchRequest.class)); ArrayList<Entry<RestContext, RestFilter>> filters = Lists.newArrayList(new SimpleEntry<>(filter.getRestContext(), filter)); given(filterRegistry.iterator()).willReturn(filters.iterator()); given(command.execute(same(action), same(callback))).willReturn(expected); DispatchRequest result = filterChainProvider.get().doFilter(action, callback, command); assertThat(result).isSameAs(expected); verify(filter, never()).filter(any(RestAction.class), any(RestCallback.class), any(ExecuteCommand.class), any(RestFilterChain.class)); }