@Test public void testSchedule() { AmazonDynamoDB dynamoDB = mock(AmazonDynamoDB.class); doThrow(new ConditionalCheckFailedException("hello")).when(dynamoDB).putItem(any(PutItemRequest.class)); Scheduler scheduler = new Scheduler(dynamoDB); assertFalse(scheduler.schedule(new Fault2())); dynamoDB = mock(AmazonDynamoDB.class); doAnswer(i -> { PutItemRequest request = i.getArgument(0); Map<String, AttributeValue> item = request.getItem(); assertThat(item.size(), is(5)); assertThat(item.get("id").getS(), is("fault-id-2")); assertThat(item.get("name").getS(), is("Fault2")); assertThat(item.get("status").getS(), is("ACTIVE")); assertThat(item.get("params").getS(), is("{\"property1\":\"property2\",\"property2\":2}")); assertThat(request.getConditionExpression(), is("attribute_not_exists(id)")); return null; }).when(dynamoDB).putItem(any(PutItemRequest.class)); scheduler = new Scheduler(dynamoDB); Fault2 fault2 = new Fault2(); fault2.id("fault-id-2"); fault2.setProperty1("property2"); fault2.setProperty2(2); assertTrue(scheduler.schedule(fault2)); dynamoDB = mock(AmazonDynamoDB.class); doAnswer(i -> { PutItemRequest request = i.getArgument(0); Map<String, AttributeValue> item = request.getItem(); assertThat(item.size(), is(5)); assertThat(item.get("id").getS(), is("fault-id-4")); assertThat(item.get("name").getS(), is("fault-4")); assertThat(item.get("status").getS(), is("ACTIVE")); assertThat(item.get("params").getS(), is("{\"property1\":\"property4\",\"property2\":4}")); assertThat(request.getConditionExpression(), is("attribute_not_exists(id)")); return null; }).when(dynamoDB).putItem(any(PutItemRequest.class)); scheduler = new Scheduler(dynamoDB); Fault4 fault4 = new Fault4(); fault4.id("fault-id-4"); fault4.setProperty1("property4"); fault4.setProperty2(4); assertTrue(scheduler.schedule(fault4)); }
public boolean schedule(Fault fault) { Map<String, AttributeValue> item = new HashMap<>(); item.put("id", new AttributeValue().withS(fault.id())); Class<? extends Fault> clazz = fault.getClass(); Named named = clazz.getAnnotation(Named.class); item.put("name", new AttributeValue().withS((named != null) ? named.value() : clazz.getSimpleName())); item.put("status", new AttributeValue().withS("ACTIVE")); ObjectMapper mapper = new ObjectMapper(); try { item.put("params", new AttributeValue().withS(mapper.writeValueAsString(fault))); } catch (JsonProcessingException e) { throw new UncheckedException(e); } item.put("ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond() + EXPIRE))); PutItemRequest request = new PutItemRequest().withTableName(Prodigy.configuration().scheduler()).withItem(item) .withConditionExpression("attribute_not_exists(id)"); try { dynamoDB.putItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } }
Scheduler { public boolean schedule(Fault fault) { Map<String, AttributeValue> item = new HashMap<>(); item.put("id", new AttributeValue().withS(fault.id())); Class<? extends Fault> clazz = fault.getClass(); Named named = clazz.getAnnotation(Named.class); item.put("name", new AttributeValue().withS((named != null) ? named.value() : clazz.getSimpleName())); item.put("status", new AttributeValue().withS("ACTIVE")); ObjectMapper mapper = new ObjectMapper(); try { item.put("params", new AttributeValue().withS(mapper.writeValueAsString(fault))); } catch (JsonProcessingException e) { throw new UncheckedException(e); } item.put("ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond() + EXPIRE))); PutItemRequest request = new PutItemRequest().withTableName(Prodigy.configuration().scheduler()).withItem(item) .withConditionExpression("attribute_not_exists(id)"); try { dynamoDB.putItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } }
Scheduler { public boolean schedule(Fault fault) { Map<String, AttributeValue> item = new HashMap<>(); item.put("id", new AttributeValue().withS(fault.id())); Class<? extends Fault> clazz = fault.getClass(); Named named = clazz.getAnnotation(Named.class); item.put("name", new AttributeValue().withS((named != null) ? named.value() : clazz.getSimpleName())); item.put("status", new AttributeValue().withS("ACTIVE")); ObjectMapper mapper = new ObjectMapper(); try { item.put("params", new AttributeValue().withS(mapper.writeValueAsString(fault))); } catch (JsonProcessingException e) { throw new UncheckedException(e); } item.put("ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond() + EXPIRE))); PutItemRequest request = new PutItemRequest().withTableName(Prodigy.configuration().scheduler()).withItem(item) .withConditionExpression("attribute_not_exists(id)"); try { dynamoDB.putItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); }
Scheduler { public boolean schedule(Fault fault) { Map<String, AttributeValue> item = new HashMap<>(); item.put("id", new AttributeValue().withS(fault.id())); Class<? extends Fault> clazz = fault.getClass(); Named named = clazz.getAnnotation(Named.class); item.put("name", new AttributeValue().withS((named != null) ? named.value() : clazz.getSimpleName())); item.put("status", new AttributeValue().withS("ACTIVE")); ObjectMapper mapper = new ObjectMapper(); try { item.put("params", new AttributeValue().withS(mapper.writeValueAsString(fault))); } catch (JsonProcessingException e) { throw new UncheckedException(e); } item.put("ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond() + EXPIRE))); PutItemRequest request = new PutItemRequest().withTableName(Prodigy.configuration().scheduler()).withItem(item) .withConditionExpression("attribute_not_exists(id)"); try { dynamoDB.putItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); boolean schedule(Fault fault); boolean unschedule(String id); Entry get(String id); List<Entry> list(); }
Scheduler { public boolean schedule(Fault fault) { Map<String, AttributeValue> item = new HashMap<>(); item.put("id", new AttributeValue().withS(fault.id())); Class<? extends Fault> clazz = fault.getClass(); Named named = clazz.getAnnotation(Named.class); item.put("name", new AttributeValue().withS((named != null) ? named.value() : clazz.getSimpleName())); item.put("status", new AttributeValue().withS("ACTIVE")); ObjectMapper mapper = new ObjectMapper(); try { item.put("params", new AttributeValue().withS(mapper.writeValueAsString(fault))); } catch (JsonProcessingException e) { throw new UncheckedException(e); } item.put("ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond() + EXPIRE))); PutItemRequest request = new PutItemRequest().withTableName(Prodigy.configuration().scheduler()).withItem(item) .withConditionExpression("attribute_not_exists(id)"); try { dynamoDB.putItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); boolean schedule(Fault fault); boolean unschedule(String id); Entry get(String id); List<Entry> list();  }