Commit ec2d6197 authored by Ananas Orange's avatar Ananas Orange
Browse files

Added future and past check for pivotDate

parent be3e76c4
......@@ -23,4 +23,8 @@ public class DecodedVisit {
public String getStringLocationTemporaryPublicId() {
return this.getLocationTemporaryPublicId().toString();
}
public boolean isForward() {
return !this.isBackward();
}
}
......@@ -50,10 +50,11 @@ public class ReportService implements IReportService {
@Override
public List<DecodedVisit> report(ReportRequest reportRequestVo) {
long validatedPivotDate = this.validatePivotDate(reportRequestVo.getPivotDateAsNtpTimestamp());
List<DecodedVisit> verified = reportRequestVo.getVisits().stream()
.filter(visit -> !this.isOutdated(visit))
.filter(visit -> !this.isFuture(visit))
.map(it -> this.decode(it, reportRequestVo.getPivotDateAsNtpTimestamp()))
.map(it -> this.decode(it, validatedPivotDate))
.filter(Objects::nonNull)
.collect(Collectors.toList());
List<DecodedVisit> pruned = this.pruneDuplicates(verified);
......@@ -115,4 +116,17 @@ public class ReportService implements IReportService {
});
return cleaned;
}
private long validatePivotDate(long pivotDate) {
Instant pivotDateAsInstant = TimeUtils.instantFromTimestamp(pivotDate);
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
Instant retentionDateLimit = now.minus(retentionDurationInDays, ChronoUnit.DAYS);
if (pivotDateAsInstant.isAfter(now) || pivotDateAsInstant.isBefore(retentionDateLimit)) {
long retentionDateLimitAsNtp = TimeUtils.ntpTimestampFromInstant(retentionDateLimit);
log.warn("pivotDate: {} not between retentionLimitDate: {} and now: {}", pivotDateAsInstant, retentionDateLimit, now);
return retentionDateLimitAsNtp;
} else {
return pivotDate;
}
}
}
......@@ -54,7 +54,8 @@ class ReportServiceTest {
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(2, ChronoUnit.DAYS))), // pass
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS))), // pass
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now)) /* pass */);
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now)) /* pass */
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, 0L));
......@@ -73,7 +74,8 @@ class ReportServiceTest {
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS))), // pass
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now)), // pass
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now.plus(1, ChronoUnit.DAYS))) /* don't pass */);
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now.plus(1, ChronoUnit.DAYS))) /* don't pass */
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, 0L));
......@@ -95,7 +97,8 @@ class ReportServiceTest {
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(15, ChronoUnit.DAYS))), // don't pass
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.minus(14, ChronoUnit.DAYS))), // pass
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now.minus(2, ChronoUnit.DAYS))), // pass
newVisit(uuid4, TimeUtils.ntpTimestampFromInstant(now)) /* pass */);
newVisit(uuid4, TimeUtils.ntpTimestampFromInstant(now)) /* pass */
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, 0L));
......@@ -114,7 +117,8 @@ class ReportServiceTest {
UUID uuid2 = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now)), // pass
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.plus(2, ChronoUnit.SECONDS))) /* don't pass */);
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.plus(2, ChronoUnit.SECONDS))) /* don't pass */
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, 0L));
......@@ -135,7 +139,8 @@ class ReportServiceTest {
newVisit(uuidB, TimeUtils.ntpTimestampFromInstant(now.minus(3, ChronoUnit.HOURS))), // pass
newVisit(uuidB, TimeUtils.ntpTimestampFromInstant(now)), // don't pass
newVisit(uuidC, TimeUtils.ntpTimestampFromInstant(now)), // pass
newVisit(uuidC, TimeUtils.ntpTimestampFromInstant(now)) /* don't pass */);
newVisit(uuidC, TimeUtils.ntpTimestampFromInstant(now)) /* don't pass */
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, 0L));
......@@ -145,6 +150,48 @@ class ReportServiceTest {
assertThat(processed.stream().filter(it -> it.getLocationTemporaryPublicId().equals(uuidC)).count()).isEqualTo(1);
}
@Test
@DisplayName("if pivot date is in future, set it to retentionDate and check that all visits are forward")
void testWithPivotDateInFuture() throws CleaEncodingException {
long pivotDateInFutureAsNtp = TimeUtils.ntpTimestampFromInstant(now.plus(1, ChronoUnit.MINUTES));
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
UUID uuid3 = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(2, ChronoUnit.DAYS))),
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS))),
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now))
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, pivotDateInFutureAsNtp));
assertThat(processed.size()).isEqualTo(3);
assertThat(processed.stream().filter(DecodedVisit::isBackward).count()).isZero();
assertThat(processed.stream().filter(DecodedVisit::isForward).count()).isEqualTo(3L);
}
@Test
@DisplayName("if pivot date is before retentionDate, set it to retentionDate and check that all visits are forward")
void testWithPivotDateTooOld() throws CleaEncodingException {
long pivotDateTooOldAsNtp = TimeUtils.ntpTimestampFromInstant(now.minus(15, ChronoUnit.DAYS));
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
UUID uuid3 = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(2, ChronoUnit.DAYS))),
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS))),
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now))
);
List<DecodedVisit> processed = reportService.report(new ReportRequest(visits, pivotDateTooOldAsNtp));
assertThat(processed.size()).isEqualTo(3);
assertThat(processed.stream().filter(DecodedVisit::isBackward).count()).isZero();
assertThat(processed.stream().filter(DecodedVisit::isForward).count()).isEqualTo(3L);
}
private EncryptedLocationSpecificPart createEncryptedLocationSpecificPart(UUID locationTemporaryPublicId) {
return EncryptedLocationSpecificPart.builder()
.locationTemporaryPublicId(locationTemporaryPublicId)
......
Markdown is supported
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