RateLimitingProcessor implements Processor { protected Map<String, ViolationMetaData> getCurrentlyBlockedIpsAndDateViolatedMap(String bucketName) { S3Object s3Object = null; try { s3Object = amazonS3.getObject(new GetObjectRequest(bucketName, SERIALIZED_DATA_FILE_NAME)); } catch (AmazonS3Exception e) { if (e.getErrorCode().equals("NoSuchKey")) { return new HashMap<>(); } } if (s3Object == null) { return new HashMap<>(); } try { TypeReference<HashMap<String,ViolationMetaData>> typeRef = new TypeReference<HashMap<String,ViolationMetaData>>() {}; return objectMapper.readValue(s3Object.getObjectContent(), typeRef); } catch (IOException e) { throw new RuntimeException("Failed to deserialize json data from previous runs", e); } } RateLimitingProcessor(ObjectMapper objectMapper, AWSWAFRegional awsWaf, AmazonS3 amazonS3); void setCidrLimitForIpSetOverride(int limitOverride); @Override void processLogEvents(List<ALBAccessLogEvent> events, LogProcessorLambdaConfig config, String bucketName);  }
@Test public void testThatGetCurrentlyBlockedIpsAndDateViolatedMapReturnsEmptyMapWhenS3HasNoObject() { when(amazonS3.getObject(isA(GetObjectRequest.class))).thenReturn(null); Map<String, ViolationMetaData> map = processor.getCurrentlyBlockedIpsAndDateViolatedMap(FAKE_BUCKET_NAME); assertTrue("The map should be empty", map.size() == 0); }
@Test public void testThatGetCurrentlyBlockedIpsAndDateViolatedMapCanProperlyDeserialize() { String json = "{\"192.168.0.1\":{\"date\":1476224112155,\"maxRate\":10}}"; S3Object object = new S3Object(); object.setObjectContent(new ByteArrayInputStream(json.getBytes())); when(amazonS3.getObject(isA(GetObjectRequest.class))).thenReturn(object); Map<String, ViolationMetaData> map = processor.getCurrentlyBlockedIpsAndDateViolatedMap(FAKE_BUCKET_NAME); assertTrue("The map should have one element", map.size() == 1); }