SecurityPolicyChainProvider extends AbstractPolicyChainProvider { @Override public StreamableProcessor<ExecutionContext, Buffer> provide(ExecutionContext context) { if (context.getAttribute("skip-security-chain") == null) { List<Policy> policies = policyResolver.resolve(StreamType.ON_REQUEST, context); if (policies == null) { return new DirectPolicyChain( PolicyResult.failure( PLAN_UNRESOLVABLE, HttpStatusCode.UNAUTHORIZED_401, "Unauthorized"), context); } return RequestPolicyChain.create(policies, context); } return new NoOpPolicyChain(context); } SecurityPolicyChainProvider(final PolicyResolver policyResolver); @Override StreamableProcessor<ExecutionContext, Buffer> provide(ExecutionContext context);  }
@Test public void shouldReturnRequestPolicyChain() { Mockito.when(policyResolver.resolve(StreamType.ON_REQUEST, executionContext)).thenReturn(Collections.singletonList(Mockito.mock(Policy.class))); StreamableProcessor<ExecutionContext, Buffer> processor = securityPolicyChainResolver.provide(executionContext); Assert.assertEquals(RequestPolicyChain.class, processor.getClass()); }
@Test public void shouldReturnUnauthorizedPolicyChain_onRequest() { Mockito.when(policyResolver.resolve(StreamType.ON_REQUEST, executionContext)).thenReturn(null); StreamableProcessor<ExecutionContext, Buffer> processor = securityPolicyChainResolver.provide(executionContext); Assert.assertEquals(DirectPolicyChain.class, processor.getClass()); Assert.assertNotNull(((DirectPolicyChain) processor).policyResult()); Assert.assertEquals(SecurityPolicyChainProvider.PLAN_UNRESOLVABLE, ((DirectPolicyChain) processor).policyResult().key()); }