### Question:
SchedulerApiary { @Transactional public void scheduleBeekeeperEvent() { Optional<BeekeeperEvent> housekeepingEntitiesToBeScheduled = beekeeperEventReader.read(); if (housekeepingEntitiesToBeScheduled.isEmpty()) { return; } BeekeeperEvent beekeeperEvent = housekeepingEntitiesToBeScheduled.get(); List<HousekeepingEntity> housekeepingEntities = beekeeperEvent.getHousekeepingEntities(); for (HousekeepingEntity entity : housekeepingEntities) { try { LifecycleEventType eventType = LifecycleEventType.valueOf(entity.getLifecycleType()); SchedulerService scheduler = schedulerServiceMap.get(eventType); scheduler.scheduleForHousekeeping(entity); } catch (Exception e) { throw new BeekeeperException(format( "Unable to schedule %s deletion for entity, this message will go back on the queue", entity.getLifecycleType()), e); } } beekeeperEventReader.delete(beekeeperEvent); } @Autowired  SchedulerApiary(
      BeekeeperEventReader beekeeperEventReader,
      EnumMap<LifecycleEventType, SchedulerService> schedulerServiceMap
  ); @Transactional void scheduleBeekeeperEvent(); void close();  }### Answer:
@Test public void typicalPathSchedule() { Optional<BeekeeperEvent> event = Optional.of(newHousekeepingEvent(path, UNREFERENCED)); when(beekeeperEventReader.read()).thenReturn(event); scheduler.scheduleBeekeeperEvent(); verify(pathSchedulerService).scheduleForHousekeeping(path); verifyNoInteractions(tableSchedulerService); verify(beekeeperEventReader).delete(event.get()); }
@Test public void typicalTableSchedule() { Optional<BeekeeperEvent> event = Optional.of(newHousekeepingEvent(table, EXPIRED)); when(beekeeperEventReader.read()).thenReturn(event); scheduler.scheduleBeekeeperEvent(); verify(tableSchedulerService).scheduleForHousekeeping(table); verifyNoInteractions(pathSchedulerService); verify(beekeeperEventReader).delete(event.get()); }
@Test public void typicalNoSchedule() { when(beekeeperEventReader.read()).thenReturn(Optional.empty()); scheduler.scheduleBeekeeperEvent(); verifyNoInteractions(pathSchedulerService); verifyNoInteractions(tableSchedulerService); verify(beekeeperEventReader, times(0)).delete(any()); }
@Test public void housekeepingPathRepositoryThrowsException() { Optional<BeekeeperEvent> event = Optional.of(newHousekeepingEvent(path, UNREFERENCED)); when(beekeeperEventReader.read()).thenReturn(event); doThrow(new BeekeeperException("exception")).when(pathSchedulerService).scheduleForHousekeeping(path); try { scheduler.scheduleBeekeeperEvent(); fail("Should have thrown exception"); } catch (Exception e) { verify(pathSchedulerService).scheduleForHousekeeping(path); verify(beekeeperEventReader, times(0)).delete(any()); verifyNoInteractions(tableSchedulerService); assertThat(e).isInstanceOf(BeekeeperException.class); assertThat(e.getMessage()).isEqualTo( format("Unable to schedule %s deletion for entity, this message will go back on the queue", path.getLifecycleType())); } }
@Test public void housekeepingTableRepositoryThrowsException() { Optional<BeekeeperEvent> event = Optional.of(newHousekeepingEvent(table, EXPIRED)); when(beekeeperEventReader.read()).thenReturn(event); doThrow(new BeekeeperException("exception")).when(tableSchedulerService).scheduleForHousekeeping(table); try { scheduler.scheduleBeekeeperEvent(); fail("Should have thrown exception"); } catch (Exception e) { verify(tableSchedulerService).scheduleForHousekeeping(table); verify(beekeeperEventReader, times(0)).delete(any()); verifyNoInteractions(pathSchedulerService); assertThat(e).isInstanceOf(BeekeeperException.class); assertThat(e.getMessage()).isEqualTo( format("Unable to schedule %s deletion for entity, this message will go back on the queue", table.getLifecycleType())); } }