### Question:
IPWhitelistPolicy extends AbstractIPListPolicy<IPListConfig> { @Override protected void doApply(ApiRequest request, IPolicyContext context, IPListConfig config, IPolicyChain<ApiRequest> chain) { String remoteAddr = getRemoteAddr(request, config); if (isMatch(config, remoteAddr)) { super.doApply(request, context, config, chain); } else { IPolicyFailureFactoryComponent ffactory = context.getComponent(IPolicyFailureFactoryComponent.class); String msg = Messages.i18n.format("IPWhitelistPolicy.NotWhitelisted", remoteAddr); PolicyFailure failure = ffactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.IP_NOT_WHITELISTED, msg); failure.setResponseCode(config.getResponseCode()); if (config.getResponseCode() == 404) { failure.setType(PolicyFailureType.NotFound); } else if (config.getResponseCode() == 403) { failure.setType(PolicyFailureType.Authorization); } else if (config.getResponseCode() == 0) { failure.setResponseCode(500); } chain.doFailure(failure); } } IPWhitelistPolicy();   }### Answer:
@Test public void testApply() { IPWhitelistPolicy policy = new IPWhitelistPolicy(); String json = "{" + " \"ipList\" : [" + " \"1.2.3.4\"," + " \"3.4.5.6\"," + " \"10.0.0.11\"" + " ]" + "}"; Object config = policy.parseConfiguration(json); ApiRequest request = new ApiRequest(); request.setType("GET"); request.setApiKey("12345"); request.setRemoteAddr("1.2.3.4"); request.setDestination("/"); IPolicyContext context = Mockito.mock(IPolicyContext.class); IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class); policy.apply(request, context, config, chain); Mockito.verify(chain).doApply(request); final PolicyFailure failure = new PolicyFailure(); Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() { @Override public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) { return failure; } }); chain = Mockito.mock(IPolicyChain.class); request.setRemoteAddr("9.8.7.6"); policy.apply(request, context, config, chain); Mockito.verify(chain).doFailure(failure); }
@Test public void testApplyWithWildcards() { IPWhitelistPolicy policy = new IPWhitelistPolicy(); String json = "{" + " \"ipList\" : [" + " \"10.0.*.*\"" + " ]" + "}"; Object config = policy.parseConfiguration(json); ApiRequest request = new ApiRequest(); request.setType("GET"); request.setApiKey("12345"); request.setDestination("/"); IPolicyContext context = Mockito.mock(IPolicyContext.class); IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class); request.setRemoteAddr("10.0.87.33"); policy.apply(request, context, config, chain); Mockito.verify(chain).doApply(request); request.setRemoteAddr("10.0.12.19"); chain = Mockito.mock(IPolicyChain.class); policy.apply(request, context, config, chain); Mockito.verify(chain).doApply(request); final PolicyFailure failure = new PolicyFailure(); Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() { @Override public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) { return failure; } }); chain = Mockito.mock(IPolicyChain.class); request.setRemoteAddr("9.8.7.6"); policy.apply(request, context, config, chain); Mockito.verify(chain).doFailure(failure); }