Commit 11def193 authored by calocedre TAC's avatar calocedre TAC
Browse files

Merge branch 'fix/slot-generation-tests' into 'develop'

Fix/slot generation tests

See merge request stemcovid19/tac-server/backend-server!225
parents b99be67b 43fb4414
......@@ -47,7 +47,7 @@ public class VisitExpositionAggregatorService implements IVisitExpositionAggrega
int exposureTime = this.getExposureTimeSlots(visit.getVenueType(), visit.getVenueCategory1(), visit.getVenueCategory2(), visit.isStaff());
int firstExposedSlot = Math.max(0, (int) scanTimeSlot - exposureTime);
int lastExposedSlot = Math.min(this.getPeriodMaxSlot(visit.getPeriodDuration()), (int) scanTimeSlot + exposureTime);
List<ExposedVisitEntity> exposedVisits = repository.findAllByLocationTemporaryPublicIdAndPeriodStart(visit.getLocationTemporaryPublicId(), this.periodStartFromCompressedPeriodStart(visit.getCompressedPeriodStartTime()));
List<ExposedVisitEntity> toUpdate = new ArrayList<>();
......@@ -87,7 +87,8 @@ public class VisitExpositionAggregatorService implements IVisitExpositionAggrega
if (periodDuration == 255) {
return Integer.MAX_VALUE;
}
return (int) Duration.of(periodDuration, ChronoUnit.HOURS).dividedBy(Duration.of(durationUnitInSeconds, ChronoUnit.SECONDS));
int nbSlotsInPeriod = (int) Duration.of(periodDuration, ChronoUnit.HOURS).dividedBy(Duration.of(durationUnitInSeconds, ChronoUnit.SECONDS));
return nbSlotsInPeriod - 1; // 0 based index
}
private long periodStartFromCompressedPeriodStart(long compressedPeriodStartTime) {
......
......@@ -174,12 +174,10 @@ class VisitExpositionAggregatorServiceTest {
}
@Test
@DisplayName("test how many slots are generated for a given visit")
@DisplayName("test how many slots are generated for a given visit with a period duration of 24 hour")
void testSlotGeneration() {
Instant todayAtMidnight = Instant.now().truncatedTo(ChronoUnit.DAYS);
long todayAtMidnightAsNtp = TimeUtils.ntpTimestampFromInstant(todayAtMidnight);
Instant todayAt8am = todayAtMidnight.plus(8, ChronoUnit.HOURS);
Visit visit = Visit.builder()
.version(0)
.type(0)
......@@ -191,13 +189,14 @@ class VisitExpositionAggregatorServiceTest {
.venueCategory1(0)
.venueCategory2(0)
.periodDuration(24)
.compressedPeriodStartTime((int) (todayAtMidnightAsNtp / 3600))
.compressedPeriodStartTime(getCompressedPeriodStartTime(todayAtMidnight))
.qrCodeValidityStartTime(todayAtMidnight)
.locationTemporarySecretKey(locationTemporarySecretKey)
.encryptedLocationContactMessage(encryptedLocationContactMessage)
.qrCodeScanTime(todayAt8am)
.isBackward(true)
.build();
service.updateExposureCount(visit);
/*
......@@ -210,15 +209,14 @@ class VisitExpositionAggregatorServiceTest {
*
* then:
* => scanTimeSlot = 8*2 = 16
* => slots to generate = 3 before & scanTimeSlot & 3 after = 7
* => slots to generate = 3 before + scanTimeSlot + 3 after = 7
* => firstExposedSlot = 16-3 = 13
* => lastExposedSlot = 16+3 = 19
*/
assertThat(repository.count()).isEqualTo(7L);
List<ExposedVisitEntity> entities = repository.findAll();
IntStream.rangeClosed(13, 19)
.forEach(step -> assertThat(entities.stream().filter(it -> it.getTimeSlot() == step).count()).isEqualTo(1));
.forEach(slot -> assertThat(entities.stream().filter(it -> it.getTimeSlot() == slot).count()).isEqualTo(1));
}
@Test
......@@ -226,8 +224,6 @@ class VisitExpositionAggregatorServiceTest {
void testWhenQrScanIsBeforePeriodStart() {
Instant todayAtMidnight = Instant.now().truncatedTo(ChronoUnit.DAYS);
Instant todayAt8am = todayAtMidnight.plus(8, ChronoUnit.HOURS);
long todayAt8amAsNtp = TimeUtils.ntpTimestampFromInstant(todayAt8am);
Visit visit = Visit.builder()
.version(0)
.type(0)
......@@ -239,15 +235,155 @@ class VisitExpositionAggregatorServiceTest {
.venueCategory1(0)
.venueCategory2(0)
.periodDuration(24)
.compressedPeriodStartTime((int) (todayAt8amAsNtp / 3600))
.qrCodeValidityStartTime(todayAtMidnight)
.compressedPeriodStartTime(getCompressedPeriodStartTime(todayAt8am))
.qrCodeValidityStartTime(todayAt8am)
.locationTemporarySecretKey(locationTemporarySecretKey)
.encryptedLocationContactMessage(encryptedLocationContactMessage)
.qrCodeScanTime(todayAtMidnight)
.isBackward(true)
.build();
service.updateExposureCount(visit);
assertThat(repository.count()).isZero();
}
@Test
@DisplayName("test how many slots are generated for a given visit with a period duration of 1 hour")
void testSlotGenerationWithPeriodDuration1() {
Instant todayAtMidnight = Instant.now().truncatedTo(ChronoUnit.DAYS);
Instant todayAt8am = todayAtMidnight.plus(8, ChronoUnit.HOURS);
Visit visit = Visit.builder()
.version(0)
.type(0)
.countryCode(33)
.staff(true)
.locationTemporaryPublicId(uuid)
.qrCodeRenewalIntervalExponentCompact(2)
.venueType(4)
.venueCategory1(0)
.venueCategory2(0)
.periodDuration(1)
.compressedPeriodStartTime(getCompressedPeriodStartTime(todayAt8am))
.qrCodeValidityStartTime(todayAt8am)
.locationTemporarySecretKey(locationTemporarySecretKey)
.encryptedLocationContactMessage(encryptedLocationContactMessage)
.qrCodeScanTime(todayAt8am)
.isBackward(true)
.build();
service.updateExposureCount(visit);
/*
* if:
* periodDuration = 1 hours
* periodStartTime = today at 08:00:00
* qrCodeScanTime = today at 08:00:00
* durationUnit = 1800 seconds
* exposureTime = 3
*
* then:
* => scanTimeSlot = 0
* => slots to generate = scanTimeSlot + 1 after = 2
* => firstExposedSlot = 0
* => lastExposedSlot = 0+1 = 1
*/
assertThat(repository.count()).isEqualTo(2L);
List<ExposedVisitEntity> entities = repository.findAll();
IntStream.rangeClosed(0, 1)
.forEach(step -> assertThat(entities.stream().filter(it -> it.getTimeSlot() == step).count()).isEqualTo(1));
}
@Test
@DisplayName("test how many slots are generated for a given visit with a period duration of 255 hour")
void testSlotGenerationWithPeriodDuration255() {
Instant todayAtMidnight = Instant.now().truncatedTo(ChronoUnit.DAYS);
Instant todayAt8am = todayAtMidnight.plus(8, ChronoUnit.HOURS);
Visit visit = Visit.builder()
.version(0)
.type(0)
.countryCode(33)
.staff(true)
.locationTemporaryPublicId(uuid)
.qrCodeRenewalIntervalExponentCompact(2)
.venueType(4)
.venueCategory1(0)
.venueCategory2(0)
.periodDuration(255)
.compressedPeriodStartTime(getCompressedPeriodStartTime(todayAt8am))
.qrCodeValidityStartTime(todayAt8am)
.locationTemporarySecretKey(locationTemporarySecretKey)
.encryptedLocationContactMessage(encryptedLocationContactMessage)
.qrCodeScanTime(todayAt8am)
.isBackward(true)
.build();
service.updateExposureCount(visit);
/*
* if:
* periodDuration = 255 hours
* periodStartTime = today at 08:00:00
* qrCodeScanTime = today at 08:00:00
* durationUnit = 1800 seconds
* exposureTime = 3
*
* then:
* => scanTimeSlot = 0
* => slots to generate = scanTimeSlot + 2 after = 3
* => firstExposedSlot = 0
* => lastExposedSlot = 0+3-1 = 2
*/
assertThat(repository.count()).isEqualTo(4L);
List<ExposedVisitEntity> entities = repository.findAll();
entities.forEach(it -> System.out.println(it.getTimeSlot()));
IntStream.rangeClosed(0, 2)
.forEach(step -> assertThat(entities.stream().filter(it -> it.getTimeSlot() == step).count()).isEqualTo(1));
}
@Test
@DisplayName("no slot should be generated when qrScanTime is after period validity")
void testSlotGenerationWithQrScanTimeFarFromPeriodDuration() {
Instant todayAtMidnight = Instant.now().truncatedTo(ChronoUnit.DAYS);
Instant todayAt8am = todayAtMidnight.plus(8, ChronoUnit.HOURS);
Visit visit = Visit.builder()
.version(0)
.type(0)
.countryCode(33)
.staff(true)
.locationTemporaryPublicId(uuid)
.qrCodeRenewalIntervalExponentCompact(2)
.venueType(4)
.venueCategory1(0)
.venueCategory2(0)
.periodDuration(1)
.compressedPeriodStartTime(getCompressedPeriodStartTime(todayAtMidnight))
.qrCodeValidityStartTime(todayAtMidnight)
.locationTemporarySecretKey(locationTemporarySecretKey)
.encryptedLocationContactMessage(encryptedLocationContactMessage)
.qrCodeScanTime(todayAt8am)
.isBackward(true)
.build();
service.updateExposureCount(visit);
/*
* if:
* periodDuration = 1 hours
* periodStartTime = today at 00:00:00
* qrCodeScanTime = today at 08:00:00
* durationUnit = 1800 seconds
* exposureTime = 3
*
* then:
* => scanTimeSlot = 0
* => slots to generate = 0
*/
assertThat(repository.count()).isZero();
}
protected int getCompressedPeriodStartTime(Instant instant) {
return (int) (TimeUtils.ntpTimestampFromInstant(instant) / 3600);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment