@@ -30,6 +30,7 @@ import org.junit.Assert.assertTrue
3030import org.junit.Before
3131import org.junit.Rule
3232import org.junit.Test
33+ import org.mockito.kotlin.any
3334import org.mockito.kotlin.mock
3435import org.mockito.kotlin.whenever
3536import java.util.concurrent.TimeUnit
@@ -208,7 +209,7 @@ class RealEligibleScanJobProviderTest {
208209
209210 @Test
210211 fun whenGetAllEligibleScanJobsWithOptOutRequestedAndDueForConfirmThenReturnScanRecord () = runTest {
211- val expectedScanRecord = scanJobRecordNotExecuted
212+ val expectedScanRecord = scanJobRecordMatchFound
212213 whenever(mockPirRepository.getAllBrokerSchedulingConfigs()).thenReturn(listOf (brokerSchedulingConfig))
213214 whenever(mockPirSchedulingRepository.getAllValidOptOutJobRecords()).thenReturn(listOf (optOutJobRecordRequested))
214215 whenever(mockPirSchedulingRepository.getAllValidScanJobRecords()).thenReturn(emptyList())
@@ -404,9 +405,19 @@ class RealEligibleScanJobProviderTest {
404405 )
405406
406407 // Create corresponding scan records for opt-out records
407- val scanFromOptOut1 = scanJobRecordNotExecuted.copy(userProfileId = 1001L )
408- val scanFromOptOut2 = scanJobRecordNotExecuted.copy(userProfileId = 1002L )
409- val scanFromOptOut3 = scanJobRecordNotExecuted.copy(userProfileId = 1003L )
408+ val scanFromOptOut1 = scanJobRecordNotExecuted.copy(
409+ userProfileId = 1001L ,
410+ lastScanDateInMillis = currentTimeMillis - TimeUnit .HOURS .toMillis(25 ),
411+ )
412+ val scanFromOptOut2 = scanJobRecordNotExecuted.copy(
413+ userProfileId = 1002L ,
414+ lastScanDateInMillis = currentTimeMillis - TimeUnit .HOURS .toMillis(25 ),
415+ )
416+ val scanFromOptOut3 = scanJobRecordNotExecuted.copy(
417+ userProfileId = 1003L ,
418+
419+ lastScanDateInMillis = currentTimeMillis - TimeUnit .HOURS .toMillis(25 ),
420+ )
410421
411422 // Add a duplicate scan record that should be deduplicated
412423 val duplicateScanRecord = scanFromOptOut3.copy() // Same as scanRecord1
@@ -542,7 +553,7 @@ class RealEligibleScanJobProviderTest {
542553 // Deprecated flag only affects REMOVED opt-out records for maintenance scans,
543554 // REQUESTED opt-out records should still trigger confirmation scans
544555 val deprecatedOptOutRequested = optOutJobRecordRequested.copy(deprecated = true )
545- val expectedScanRecord = scanJobRecordNotExecuted
556+ val expectedScanRecord = scanJobRecordMatchFound
546557
547558 whenever(mockPirRepository.getAllBrokerSchedulingConfigs()).thenReturn(listOf (brokerSchedulingConfig))
548559 whenever(mockPirSchedulingRepository.getAllValidOptOutJobRecords()).thenReturn(listOf (deprecatedOptOutRequested))
@@ -602,8 +613,11 @@ class RealEligibleScanJobProviderTest {
602613 )
603614
604615 val scanForDeprecatedRemoved = scanJobRecordNotExecuted.copy(userProfileId = 2001L )
605- val scanForNonDeprecatedRemoved = scanJobRecordNotExecuted.copy(userProfileId = 2002L )
606- val scanForRequested = scanJobRecordNotExecuted.copy(userProfileId = 3001L )
616+ val scanForNonDeprecatedRemoved = scanJobRecordNotExecuted.copy(
617+ userProfileId = 2002L ,
618+ lastScanDateInMillis = currentTimeMillis - DAYS .toMillis(1 ),
619+ )
620+ val scanForRequested = scanJobRecordNotExecuted.copy(userProfileId = 3001L , lastScanDateInMillis = currentTimeMillis - DAYS .toMillis(1 ))
607621
608622 whenever(mockPirRepository.getAllBrokerSchedulingConfigs()).thenReturn(listOf (brokerSchedulingConfig))
609623 whenever(mockPirSchedulingRepository.getAllValidOptOutJobRecords()).thenReturn(
@@ -632,4 +646,54 @@ class RealEligibleScanJobProviderTest {
632646 assertTrue(result.none { it.userProfileId == 1001L }) // deprecated scan record filtered
633647 assertTrue(result.none { it.userProfileId == 2001L }) // deprecated removed opt-out filtered
634648 }
649+
650+ @Test
651+ fun whenOptOutJobRecordIsRequestedButAlreadyScannedThenRecordIsNotEligible () = runTest {
652+ // Confirmation scan should have happened 6 hours ago confirmation scan after 24 hours)
653+ val requestedJobRecord = optOutJobRecordRequested.copy(
654+ userProfileId = 125L ,
655+ optOutRequestedDateInMillis = currentTimeMillis - HOURS .toMillis(30 ),
656+ )
657+
658+ // Scan just happened an hour ago
659+ val scanJobRecord = scanJobRecordMatchFound.copy(
660+ lastScanDateInMillis = currentTimeMillis - HOURS .toMillis(1 ),
661+ )
662+ whenever(mockPirRepository.getAllBrokerSchedulingConfigs()).thenReturn(listOf (brokerSchedulingConfig))
663+ whenever(mockPirSchedulingRepository.getAllValidOptOutJobRecords()).thenReturn(listOf (requestedJobRecord))
664+ whenever(mockPirSchedulingRepository.getValidScanJobRecord(" test-broker" , 2001L )).thenReturn(scanJobRecord)
665+ whenever(mockPirSchedulingRepository.getAllValidScanJobRecords()).thenReturn(
666+ listOf (scanJobRecord),
667+ )
668+
669+ val result = testee.getAllEligibleScanJobs(currentTimeMillis)
670+
671+ // Opt-out not eligible as a scan already happened for the confirmation
672+ assertTrue(result.isEmpty())
673+ }
674+
675+ @Test
676+ fun whenOptOutJobRecordIsRemovedButAlreadyScannedThenRecordIsNotEligible () = runTest {
677+ // Maintenance scan should have happened 3 days ago. Maintenance scan after 7 days of removal)
678+ val requestedJobRecord = optOutJobRecordRemoved.copy(
679+ userProfileId = 125L ,
680+ optOutRemovedDateInMillis = currentTimeMillis - DAYS .toMillis(10 ),
681+ )
682+
683+ // Scan just happened an hour ago
684+ val scanJobRecord = scanJobRecordMatchFound.copy(
685+ lastScanDateInMillis = currentTimeMillis - HOURS .toMillis(1 ),
686+ )
687+ whenever(mockPirRepository.getAllBrokerSchedulingConfigs()).thenReturn(listOf (brokerSchedulingConfig))
688+ whenever(mockPirSchedulingRepository.getAllValidOptOutJobRecords()).thenReturn(listOf (requestedJobRecord))
689+ whenever(mockPirSchedulingRepository.getValidScanJobRecord(" test-broker" , 2001L )).thenReturn(scanJobRecord)
690+ whenever(mockPirSchedulingRepository.getAllValidScanJobRecords()).thenReturn(
691+ listOf (scanJobRecord),
692+ )
693+
694+ val result = testee.getAllEligibleScanJobs(currentTimeMillis)
695+
696+ // Opt-out not eligible as a scan already happened for the confirmation
697+ assertTrue(result.isEmpty())
698+ }
635699}
0 commit comments