### Question:
RateLimitingPolicy extends AbstractMappedPolicy<RateLimitingConfig> { @Override protected void doApply(final ApiRequest request, final IPolicyContext context, final RateLimitingConfig config, final IPolicyChain<ApiRequest> chain) { String bucketId = createBucketId(request, config); final RateBucketPeriod period = getPeriod(config); if (bucketId == NO_USER_AVAILABLE) { IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class); PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_USER_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoUser")); chain.doFailure(failure); return; } if (bucketId == NO_CLIENT_AVAILABLE) { IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class); PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_APP_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoApp")); chain.doFailure(failure); return; } IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class); rateLimiter.accept(bucketId, period, config.getLimit(), 1, new IAsyncResultHandler<RateLimitResponse>() { @Override public void handle(IAsyncResult<RateLimitResponse> result) { if (result.isError()) { chain.throwError(result.getError()); } else { RateLimitResponse rtr = result.getResult(); Map<String, String> responseHeaders = responseHeaders(config, rtr, defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader()); if (rtr.isAccepted()) { context.setAttribute("rate-limit-response-headers", responseHeaders); chain.doApply(request); } else { IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class); PolicyFailure failure = limitExceededFailure(failureFactory); failure.getHeaders().putAll(responseHeaders); chain.doFailure(failure); } } } }); } RateLimitingPolicy();   }### Answer:
@Test public void testApply() { RateLimitingPolicy policy = new RateLimitingPolicy(); String json = "{\r\n" + " \"limit\" : 10,\r\n" + " \"granularity\" : \"User\",\r\n" + " \"period\" : \"Minute\",\r\n" + " \"userHeader\" : \"X-Identity\"\r\n" + "}"; Object config = policy.parseConfiguration(json); ApiRequest request = new ApiRequest(); request.setContract(createTestContract()); request.setType("GET"); request.setApiKey("12345"); request.setRemoteAddr("1.2.3.4"); request.setDestination("/"); request.getHeaders().put("X-Identity", "sclause"); IPolicyContext context = Mockito.mock(IPolicyContext.class); 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; } }); Mockito.when(context.getComponent(IRateLimiterComponent.class)).thenReturn(new InMemoryRateLimiterComponent()); IPolicyChain<ApiRequest> chain; for (int count = 0; count < 10; count++) { chain = Mockito.mock(IPolicyChain.class); policy.apply(request, context, config, chain); Mockito.verify(chain).doApply(request); } chain = Mockito.mock(IPolicyChain.class); policy.apply(request, context, config, chain); Mockito.verify(chain).doFailure(failure); }