RateLimitingProcessor implements Processor { protected Map<String, ViolationMetaData> getCurrentViolators(Map<String, Integer> reqIdCountMap, LogProcessorLambdaConfig params) { Date now = new Date(); Map<String, ViolationMetaData> violators = Maps.newHashMap(); reqIdCountMap.entrySet().stream() .filter(entry -> entry.getValue() > params.getRequestPerIntervalLimit()) .forEach(entry -> { String ip = entry.getKey(); if (violators.containsKey(ip)) { ViolationMetaData metaData = violators.get(ip); if (metaData.getMaxRate() < entry.getValue()) { metaData.setMaxRate(entry.getValue()); } } else { violators.put(ip, new ViolationMetaData(now, entry.getValue())); } }); return violators; } RateLimitingProcessor(ObjectMapper objectMapper, AWSWAFRegional awsWaf, AmazonS3 amazonS3); void setCidrLimitForIpSetOverride(int limitOverride); @Override void processLogEvents(List<ALBAccessLogEvent> events, LogProcessorLambdaConfig config, String bucketName);  }
@Test public void testThatGetCurrentViolatorsReturnsAMapOfIpAddressToMetaData() { when(config.getRequestPerIntervalLimit()).thenReturn(2); Map<String, Integer> map = Maps.newHashMap(); map.put("108.171.135.164", 10); map.put("108.171.135.160", 1); Map<String, ViolationMetaData> violators = processor.getCurrentViolators(map, config); assertTrue("The map should have one violator", violators.size() == 1); assertTrue("The map should should contain 108.171.135.164", violators.containsKey("108.171.135.164")); assertTrue(violators.get("108.171.135.164").getMaxRate() == 10); }
@Test public void testThatGetCurrentViolatorsReturnsAMapOfIpAddressToMetaDataAndContainsTheHighestRate() { when(config.getRequestPerIntervalLimit()).thenReturn(2); Map<String, Integer> map = Maps.newHashMap(); map.put("109.171.135.160", 10); map.put("109.171.135.160", 20); Map<String, ViolationMetaData> violators = processor.getCurrentViolators(map, config); assertTrue("The map should have one violator", violators.size() == 1); assertTrue("The map should should contain 109.171.135.164", violators.containsKey("109.171.135.160")); assertTrue(violators.get("109.171.135.160").getMaxRate() == 20); }