private void doTest( String json, HashSet<String> userRoles, String verb, String path, boolean shouldSucceed) { AuthorizationPolicy policy = new AuthorizationPolicy(); Object config = policy.parseConfiguration(json); ApiRequest request = new ApiRequest(); request.setType(verb); request.setDestination(path); IPolicyContext context = Mockito.mock(IPolicyContext.class); IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class); Mockito.when( context.getAttribute( AuthorizationPolicy.AUTHENTICATED_USER_ROLES, (HashSet<String>) null)) .thenReturn(userRoles); final PolicyFailure failure = new PolicyFailure(); Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)) .thenReturn( new IPolicyFailureFactoryComponent() { @Override public PolicyFailure createFailure( PolicyFailureType type, int failureCode, String message) { failure.setFailureCode(failureCode); failure.setType(type); failure.setMessage(message); return failure; } }); // Success policy.apply(request, context, config, chain); if (shouldSucceed) { Mockito.verify(chain).doApply(request); } else { Mockito.verify(chain).doFailure(failure); } }
/** * @see * io.apiman.test.policies.IPolicyTestBackEndApi#invoke(io.apiman.gateway.engine.beans.ApiRequest, * byte[]) */ @Override public PolicyTestBackEndApiResponse invoke(ApiRequest request, byte[] requestBody) { ApiResponse apiResponse = new ApiResponse(); apiResponse.setCode(200); apiResponse.setMessage("OK"); // $NON-NLS-1$ apiResponse.getHeaders().put("Date", new Date().toString()); // $NON-NLS-1$ apiResponse.getHeaders().put("Server", "apiman.policy-test"); // $NON-NLS-1$ //$NON-NLS-2$ apiResponse.getHeaders().put("Content-Type", "text/plain"); // $NON-NLS-1$ //$NON-NLS-2$ int payloadSize = 20; String payloadSizeHeader = request.getHeaders().get("X-Payload-Size"); if (payloadSizeHeader != null) { payloadSize = new Integer(payloadSizeHeader); } byte[] payloadData = new byte[payloadSize]; Arrays.fill(payloadData, (byte) 80); String payload = new String(payloadData); PolicyTestBackEndApiResponse response = new PolicyTestBackEndApiResponse(apiResponse, payload); return response; }