### Question:
PunishmentChecker { public Optional<ActivePunishmentRecord> checkPunishment(UUID uuid, ActivePunishmentType activePunishmentType) { return punishmentDataRepository.getPunishmentData(uuid) .flatMap(punishmentData -> { Optional<ActivePunishmentRecord> record = punishmentData.getPunishment(activePunishmentType); return record.flatMap(activePunishmentRecord -> { if (activePunishmentRecord.isExpired()) { punishmentData.removePunishment(activePunishmentType); taskFactory .newAsyncTask(() -> punishmentDataRepository.update(punishmentData)) .execute(); return Optional.empty(); } else { return record; } }); }); }  Optional<ActivePunishmentRecord> checkPunishment(UUID uuid, ActivePunishmentType activePunishmentType);  }### Answer:
@Test public void whenPunishmentExists() { PunishmentDataRepository repository = mock(PunishmentDataRepository.class); TaskFactory taskFactory = mock(TaskFactory.class); UUID uuid = UUID.randomUUID(); PunishmentData data = mock(PunishmentData.class); when(data.getPunishment(eq(ActivePunishmentType.BAN))) .thenReturn(Optional.of(mock(ActivePunishmentRecord.class))); when(repository.getPunishmentData(eq(uuid))).thenReturn( Optional.of(data) ); PunishmentChecker checker = new PunishmentChecker(repository, taskFactory); Optional<ActivePunishmentRecord> optionalResult = checker.checkPunishment(uuid, ActivePunishmentType.BAN); assertTrue(optionalResult.isPresent()); }
@Test public void whenPunishmentDoesNotExist() { PunishmentDataRepository repository = mock(PunishmentDataRepository.class); TaskFactory taskFactory = mock(TaskFactory.class); UUID uuid = UUID.randomUUID(); PunishmentData data = mock(PunishmentData.class); when(data.getPunishment(any())) .thenReturn(Optional.empty()); PunishmentChecker punishmentChecker = new PunishmentChecker(repository, taskFactory); Optional<ActivePunishmentRecord> optionalResult = punishmentChecker.checkPunishment(uuid, ActivePunishmentType.MUTE); assertFalse(optionalResult.isPresent()); }
@Test public void whenPunishmentExpired() { PunishmentDataRepository repository = mock(PunishmentDataRepository.class); TaskFactory taskFactory = mock(TaskFactory.class); UUID uuid = UUID.randomUUID(); PunishmentData data = mock(PunishmentData.class); ActivePunishmentRecord expiredRecord = mock(ActivePunishmentRecord.class); when(expiredRecord.isExpired()).thenReturn(true); when(data.getPunishment(eq(ActivePunishmentType.MUTE))) .thenReturn(Optional.of(expiredRecord)); when(taskFactory.newAsyncTask(any())).thenReturn(mock(Task.class)); when(repository.getPunishmentData(eq(uuid))).thenReturn( Optional.of(data) ); PunishmentChecker checker = new PunishmentChecker(repository, taskFactory); assertFalse(checker.checkPunishment(uuid, ActivePunishmentType.MUTE).isPresent()); verify(data).removePunishment(eq(ActivePunishmentType.MUTE)); }