RateLimitProcessor implements Processor { public void setGatewayRateMger(IGatewayRateManager gtwRateMger) { if(log.isDebugEnabled()) { log.debug("GatewayRateManager set in RateLimit processor"); } this.rateManager = gtwRateMger; }  IGatewayRateManager getGtwRateMger(); void setGatewayRateMger(IGatewayRateManager gtwRateMger); @Override void process(Exchange exchange);  }
@Test @DirtiesContext public void testRateOK() throws Exception { AuthIdentity authIdentity = new AuthIdentity(); Auth auth = new Auth(); auth.setId("abc"); authIdentity.setAuth(auth); AuthProcessorMock authProcessor = new AuthProcessorMock(authIdentity); checkAuth.whenAnyExchangeReceived(authProcessor); RateLimitProcessor rateProcessor = new RateLimitProcessor(); rateProcessor.setGatewayRateMger(new IGatewayRateManager() { @Override public LimitCheckResult isAllowed(AuthIdentity authIdentity, boolean isTDREnabled) { LimitCheckResult result = new LimitCheckResult(); return result; } }); checkRate.whenAnyExchangeReceived(rateProcessor); testMessage.setExpectedMessageCount(1); producerTemplate.requestBody(String.class); testMessage.assertIsSatisfied(); }
@Test @DirtiesContext public void testRateKO() throws Exception { AuthIdentity authIdentity = new AuthIdentity(); Auth auth = new Auth(); auth.setId("ko"); authIdentity.setAuth(auth); AuthProcessorMock authProcessor = new AuthProcessorMock(authIdentity); checkAuth.whenAnyExchangeReceived(authProcessor); RateLimitProcessor rateProcessor = new RateLimitProcessor(); rateProcessor.setGatewayRateMger(new IGatewayRateManager() { @Override public LimitCheckResult isAllowed(AuthIdentity authIdentity, boolean isTDREnabled) { LimitCheckResult result = new LimitCheckResult(); result.setActionType(ActionType.REJECT); return result; } }); checkRate.whenAnyExchangeReceived(rateProcessor); testMessage.setExpectedMessageCount(0); testMessage.allMessages().body().isNull(); handleException.setExpectedMessageCount(1); handleException.message(0).property("CamelExceptionCaught").equals("com.alu.e3.rate.RatePolicyRuntimeException: Rate limit exceeded"); producerTemplate.requestBody(String.class); testMessage.assertIsSatisfied(); }
@Test @DirtiesContext public void testAuthIdentityRequired() throws Exception { RateLimitProcessor rateProcessor = new RateLimitProcessor(); rateProcessor.setGatewayRateMger(new NeverAllowed()); checkRate.whenAnyExchangeReceived(rateProcessor); testMessage.setExpectedMessageCount(0); testMessage.allMessages().body().isNull(); handleException.setExpectedMessageCount(1); handleException.message(0).property("CamelExceptionCaught").equals("The property " + ExchangeConstantKeys.E3_AUTH_IDENTITY.toString() + " is required to check the rate limits."); producerTemplate.requestBody(String.class); testMessage.assertIsSatisfied(); handleException.assertIsSatisfied(); }
@Test @DirtiesContext public void testRateLimitExeeded() throws Exception { RateLimitProcessor rateProcessor = new RateLimitProcessor(); rateProcessor.setGatewayRateMger(new NeverAllowed()); checkRate.whenAnyExchangeReceived(rateProcessor); testMessage.setExpectedMessageCount(0); testMessage.allMessages().body().isNull(); handleException.setExpectedMessageCount(1); handleException.message(0).property("CamelExceptionCaught").equals("com.alu.e3.rate.RatePolicyRuntimeException: Rate limit exceeded"); producerTemplate.requestBody(String.class); testMessage.assertIsSatisfied(); handleException.assertIsSatisfied(); }
@Test @DirtiesContext public void testRateLimitNotExeeded() throws Exception { AuthProcessorMock authProcessor = new AuthProcessorMock(new AuthIdentity()); checkAuth.whenAnyExchangeReceived(authProcessor); RateLimitProcessor rateProcessor = new RateLimitProcessor(); rateProcessor.setGatewayRateMger(new AlwaysAllowed()); checkRate.whenAnyExchangeReceived(rateProcessor); testMessage.setExpectedMessageCount(1); producerTemplate.requestBody(String.class); testMessage.assertIsSatisfied(); }