@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()); }
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; } }); }); }
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; } }); }); } }
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; } }); }); }  }
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); }
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);  }