PreparationStepMarker implements StepMarker { @Override public Result mark(PreparationRepository repository, UUID marker) { if (repository.exist(Preparation.class, recentlyModified())) { LOGGER.info("Not running clean up (at least a preparation modified within last hour)."); logRecentlyModified(repository); return Result.INTERRUPTED; } final AtomicBoolean interrupted = new AtomicBoolean(false); repository .list(Preparation.class) .filter(p -> !interrupted.get()) .forEach(p -> { if (repository.exist(Preparation.class, recentlyModified())) { LOGGER.info("Interrupting clean up (preparation modified within last hour)."); logRecentlyModified(repository); interrupted.set(true); return; } final Collection<Identifiable> markedSteps = p .getSteps() .stream() .filter(s -> !Objects.equals(s, Step.ROOT_STEP)) .peek(s -> s.setMarker(marker.toString())) .collect(Collectors.toList()); repository.add(markedSteps); }); return interrupted.get() ? Result.INTERRUPTED : Result.COMPLETED; }  @Override Result mark(PreparationRepository repository, UUID marker);  }
@Test public void shouldMarkUnusedSteps() { final UUID stepMarker = UUID.randomUUID(); final StepMarker marker = new PreparationStepMarker(); final PreparationRepository repository = mock(PreparationRepository.class); final Preparation preparation = new Preparation(); final Step step = mock(Step.class); when(step.id()).thenReturn("1234"); when(step.getParent()).thenReturn(Step.ROOT_STEP.id()); when(repository.get(eq("1234"), eq(Step.class))).thenReturn(step); preparation.setSteps(Arrays.asList(Step.ROOT_STEP, step)); when(repository.list(eq(Preparation.class))).thenReturn(Stream.of(preparation)); final StepMarker.Result result = marker.mark(repository, stepMarker); assertEquals(StepMarker.Result.COMPLETED, result); verify(repository, times(1)).add(eq(Collections.singletonList(step))); verify(step, times(1)).setMarker(eq(stepMarker.toString())); }
@Test public void shouldDisableCleanUpAtStart() { final UUID stepMarker = UUID.randomUUID(); final StepMarker marker = new PreparationStepMarker(); final PreparationRepository repository = mock(PreparationRepository.class); when(repository.exist(eq(Preparation.class), any())).thenReturn(true); final StepMarker.Result result = marker.mark(repository, stepMarker); assertEquals(StepMarker.Result.INTERRUPTED, result); }
@Test public void shouldDisableCleanUpDuringProcess() { final UUID stepMarker = UUID.randomUUID(); final StepMarker marker = new PreparationStepMarker(); final PreparationRepository repository = mock(PreparationRepository.class); when(repository.exist(eq(Preparation.class), any())).thenReturn(false, true); final Preparation preparation = new Preparation(); when(repository.list(eq(Preparation.class))).thenReturn(Stream.of(preparation)); final StepMarker.Result result = marker.mark(repository, stepMarker); assertEquals(StepMarker.Result.INTERRUPTED, result); verify(repository, never()).add(Matchers.<Collection<Step>> any()); }