### Question:
ConsentScheduleTask { @Scheduled(cron = "${xs2a.cms.consent.cron.expression}") @Transactional public void checkConsentStatus() { log.info("Consent schedule task is run!"); List<ConsentEntity> availableConsents = Optional.ofNullable(consentJpaRepository.findByConsentStatusIn(EnumSet.of(RECEIVED, VALID))) .orElse(Collections.emptyList()); consentJpaRepository.saveAll(updateConsent(availableConsents)); }  @Scheduled(cron = "${xs2a.cms.consent.cron.expression}") @Transactional void checkConsentStatus();  }### Answer:
@Test void checkConsentStatus_allConsentsExpired() { List<ConsentEntity> availableConsents = new ArrayList<>(); availableConsents.add(createConsent(RECEIVED)); availableConsents.add(createConsent(VALID)); when(consentJpaRepository.findByConsentStatusIn(EnumSet.of(RECEIVED, VALID))).thenReturn(availableConsents); when(consentJpaRepository.saveAll(consentCaptor.capture())).thenReturn(Collections.emptyList()); scheduleTask.checkConsentStatus(); assertEquals(2, consentCaptor.getValue().size()); consentCaptor.getValue().forEach(c -> assertEquals(EXPIRED, c.getConsentStatus())); verify(consentJpaRepository, times(1)).findByConsentStatusIn(EnumSet.of(RECEIVED, VALID)); verify(consentJpaRepository, times(1)).saveAll(anyList()); }
@Test void checkConsentStatus_notAllConsentsExpired() { List<ConsentEntity> availableConsents = new ArrayList<>(); availableConsents.add(createConsent(RECEIVED)); ConsentEntity consent = createConsent(VALID); consent.setValidUntil(LocalDate.now().plusDays(1)); availableConsents.add(consent); when(consentJpaRepository.findByConsentStatusIn(EnumSet.of(RECEIVED, VALID))).thenReturn(availableConsents); when(consentJpaRepository.saveAll(consentCaptor.capture())).thenReturn(Collections.emptyList()); scheduleTask.checkConsentStatus(); assertEquals(1, consentCaptor.getValue().size()); assertEquals("RECEIVED", consentCaptor.getValue().get(0).getExternalId()); consentCaptor.getValue().forEach(c -> assertEquals(EXPIRED, c.getConsentStatus())); verify(consentJpaRepository, times(1)).findByConsentStatusIn(EnumSet.of(RECEIVED, VALID)); verify(consentJpaRepository, times(1)).saveAll(anyList()); }
@Test void checkConsentStatus_nullValue() { when(consentJpaRepository.findByConsentStatusIn(EnumSet.of(RECEIVED, VALID))).thenReturn(null); when(consentJpaRepository.saveAll(Collections.emptyList())).thenReturn(Collections.emptyList()); scheduleTask.checkConsentStatus(); verify(consentJpaRepository, times(1)).findByConsentStatusIn(EnumSet.of(RECEIVED, VALID)); verify(consentJpaRepository, times(1)).saveAll(Collections.emptyList()); }