### Question:
UserSyncPoller { @VisibleForTesting void syncAllFreeIpaStacks() { try { Optional<String> requestId = Optional.of(UUID.randomUUID().toString()); MDCBuilder.addRequestId(requestId.get()); LOGGER.debug("Setting request id = {} for this poll", requestId); ThreadBasedUserCrnProvider.doAs(INTERNAL_ACTOR_CRN, () -> { LOGGER.debug("Attempting to sync users to FreeIPA stacks"); List<Stack> stackList = stackService.findAllWithStatuses(Status.AVAILABLE_STATUSES); LOGGER.debug("Found {} active stacks", stackList.size()); stackList.stream() .collect(Collectors.groupingBy(Stack::getAccountId)) .entrySet().stream() .forEach(stringListEntry -> { String accountId = stringListEntry.getKey(); if (userSyncPollerEntitlementChecker.isAccountEntitled(accountId)) { LOGGER.debug("Automatic usersync polling is entitled in account {}", accountId); syncFreeIpaStacksInAccount(requestId, accountId, stringListEntry.getValue()); } else { LOGGER.debug("Automatic usersync polling is not entitled in account {}.", accountId); } }); }); } finally { MDCBuilder.cleanupMdc(); } }  @Scheduled(fixedDelayString = "${freeipa.usersync.poller.fixed-delay-millis}", initialDelayString = "${freeipa.usersync.poller.initial-delay-millis}") void automaticUserSyncTask();  }### Answer:
@Test void testSyncStackWhenNotInSync() { UserSyncStatus userSyncStatus = setupMocks(); when(eventGenerationIdsChecker.isInSync(eq(userSyncStatus), any())).thenReturn(false); when(cooldownChecker.isCooldownExpired(eq(userSyncStatus), any())).thenReturn(true); underTest.syncAllFreeIpaStacks(); verify(userSyncService).synchronizeUsers(UserSyncTestUtils.ACCOUNT_ID, INTERNAL_ACTOR_CRN, Set.of(UserSyncTestUtils.ENVIRONMENT_CRN), Set.of(), Set.of()); }
@Test void testDontSyncStackWhenInSync() { UserSyncStatus userSyncStatus = setupMocks(); when(eventGenerationIdsChecker.isInSync(eq(userSyncStatus), any())).thenReturn(true); underTest.syncAllFreeIpaStacks(); verify(userSyncService, times(0)) .synchronizeUsers(any(), any(), any(), any(), any()); }
@Test void testSyncStackWhenCool() { UserSyncStatus userSyncStatus = setupMocks(); when(eventGenerationIdsChecker.isInSync(eq(userSyncStatus), any())).thenReturn(false); when(cooldownChecker.isCooldownExpired(eq(userSyncStatus), any())).thenReturn(true); underTest.syncAllFreeIpaStacks(); verify(userSyncService).synchronizeUsers(UserSyncTestUtils.ACCOUNT_ID, INTERNAL_ACTOR_CRN, Set.of(UserSyncTestUtils.ENVIRONMENT_CRN), Set.of(), Set.of()); }
@Test void testDontSyncStackWhenNotCool() { UserSyncStatus userSyncStatus = setupMocks(); when(eventGenerationIdsChecker.isInSync(eq(userSyncStatus), any())).thenReturn(false); when(cooldownChecker.isCooldownExpired(eq(userSyncStatus), any())).thenReturn(false); underTest.syncAllFreeIpaStacks(); verify(userSyncService, times(0)) .synchronizeUsers(any(), any(), any(), any(), any()); }
@Test void testDontSyncStackWhenNotEntitled() { setupMockStackService(UserSyncTestUtils.createStack()); when(userSyncPollerEntitlementChecker.isAccountEntitled(anyString())).thenReturn(false); underTest.syncAllFreeIpaStacks(); verify(userSyncService, times(0)) .synchronizeUsers(any(), any(), any(), any(), any()); }