LocalAccessControlInterceptor implements Interceptor { private void checkAccessControl(final String component, final JsonEnvelope jsonEnvelope) { final Optional<AccessControlViolation> accessControlViolation = accessControlService.checkAccessControl(component, jsonEnvelope); if (accessControlViolation.isPresent()) { final String errorMessage = accessControlFailureMessageGenerator.errorMessageFrom( jsonEnvelope, accessControlViolation.get()); throw new AccessControlViolationException(errorMessage); } }  @Override InterceptorContext process(final InterceptorContext interceptorContext, final InterceptorChain interceptorChain);  }
@Test public void shouldApplyAccessControlToInputIfLocalComponent() throws Exception { final InterceptorContext inputContext = interceptorContextWithInput(envelope); inputContext.setInputParameter("component", "command"); when(accessControlService.checkAccessControl("command", envelope)).thenReturn(Optional.empty()); interceptorChain.processNext(inputContext); verify(accessControlService).checkAccessControl("command", envelope); }
@Test public void shouldThrowAccessControlViolationExceptionIfAccessControlFailsForInput() throws Exception { final InterceptorContext inputContext = interceptorContextWithInput(envelope); inputContext.setInputParameter("component", "command"); final AccessControlViolation accessControlViolation = new AccessControlViolation("reason"); when(accessControlService.checkAccessControl("command", envelope)).thenReturn(Optional.of(accessControlViolation)); when(accessControlFailureMessageGenerator.errorMessageFrom(envelope, accessControlViolation)).thenReturn("Error message"); exception.expect(AccessControlViolationException.class); exception.expectMessage("Error message"); interceptorChain.processNext(inputContext); }