MutationOrderValidator implements Validator<Mutation<?>> { @Override public void validate(@NonNull final Mutation<?> mutation) { final long mutationId = mutation.getMetadata().getId(); log.debug("Validating order for mutation with id {}.", mutationId); if (lastSeenId.get() > mutationId) { log.warn( "Mutation with id {} is out of order and should precede {}.", mutationId, lastSeenId); handler.accept(mutation); } lastSeenId.set(mutationId); }  @Override void validate(@NonNull final Mutation<?> mutation); @Override void reset();  }
@Test public void testMutationInOrder() throws Exception { List<Mutation> unorderedMutations = Lists.newArrayList(); when(firstMetadata.getId()).thenReturn(1L); when(secondMetadata.getId()).thenReturn(2L); MutationOrderValidator validator = new MutationOrderValidator(unorderedMutations::add); validator.validate(firstMutation); validator.validate(secondMutation); assertTrue(unorderedMutations.isEmpty()); }
@Test public void testMutationOutOfOrder() throws Exception { List<Mutation> unorderedMutations = Lists.newArrayList(); when(firstMetadata.getId()).thenReturn(2L); when(secondMetadata.getId()).thenReturn(1L); MutationOrderValidator validator = new MutationOrderValidator(unorderedMutations::add); validator.validate(firstMutation); validator.validate(secondMutation); assertEquals(Arrays.asList(secondMutation), unorderedMutations); }