Commit f5f261e1 authored by Bergamote Orange's avatar Bergamote Orange
Browse files

unit tests on VisitInSameCounter

parent 163a5491
......@@ -37,21 +37,25 @@ public class ReportService implements IReportService {
private final IDecodedVisitProducerService processService;
private final long exposureTimeUnit;
@Autowired
public ReportService(
@Value("${clea.conf.retentionDurationInDays}") int retentionDuration,
@Value("${clea.conf.duplicateScanThresholdInSeconds}") long duplicateScanThreshold,
LocationSpecificPartDecoder decoder,
@Value("${clea.conf.exposureTimeUnitInSeconds}") long exposureTimeUnit,
LocationSpecificPartDecoder decoder,
IDecodedVisitProducerService processService) {
this.retentionDurationInDays = retentionDuration;
this.duplicateScanThresholdInSeconds = duplicateScanThreshold;
this.exposureTimeUnit = exposureTimeUnit;
this.decoder = decoder;
this.processService = processService;
}
@Override
public List<DecodedVisit> report(ReportRequest reportRequestVo) {
final VisitsInSameUnitCounter closeScanTimeVisits = new VisitsInSameUnitCounter();
final VisitsInSameUnitCounter closeScanTimeVisits = new VisitsInSameUnitCounter(exposureTimeUnit);
List<Visit> reportVisits = reportRequestVo.getVisits();
List<DecodedVisit> verified = reportVisits.stream()
......
......@@ -2,15 +2,14 @@ package fr.gouv.clea.ws.service.impl;
import fr.gouv.clea.ws.model.DecodedVisit;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import java.time.Duration;
import java.time.Instant;
@Data
public class VisitsInSameUnitCounter {
@Value("${clea.conf.exposureTimeUnitInSeconds}")
private long exposureTimeUnit;
private final long exposureTimeUnit;
private int count;
......@@ -24,30 +23,13 @@ public class VisitsInSameUnitCounter {
final Instant qrCodeScanTime = decodedVisit.getQrCodeScanTime();
if (this.getLastScanTime() == null) {
this.setLastScanTime(qrCodeScanTime);
} else {
if (decodedVisit.isBackward()) {
if (backwardsVisitIsScannedAfterLessThanExposureTime(qrCodeScanTime)) {
this.incrementScanInSameUnitCount();
} else {
this.setLastScanTime(qrCodeScanTime);
}
} else {
if (forwardVisitIsScannedAfterLessThanExposureTime(qrCodeScanTime)) {
this.incrementScanInSameUnitCount();
} else {
this.setLastScanTime(qrCodeScanTime);
}
}
} else if (visitIsScannedAfterLessThanExposureTime(qrCodeScanTime)) {
this.incrementScanInSameUnitCount();
}
return decodedVisit;
}
private boolean forwardVisitIsScannedAfterLessThanExposureTime(Instant qrCodeScanTime) {
return this.getLastScanTime().compareTo(qrCodeScanTime) >= exposureTimeUnit;
}
private boolean backwardsVisitIsScannedAfterLessThanExposureTime(Instant qrCodeScanTime) {
return this.getLastScanTime().compareTo(qrCodeScanTime) < exposureTimeUnit;
private boolean visitIsScannedAfterLessThanExposureTime(Instant qrCodeScanTime) {
return Duration.between(this.getLastScanTime(), qrCodeScanTime).getSeconds() < exposureTimeUnit;
}
}
......@@ -31,9 +31,10 @@ class ReportServiceTest {
private final int retentionDuration = 14;
private final long duplicateScanThresholdInSeconds = 10800L;
private final long exposureTimeUnit = 1800L;
private final LocationSpecificPartDecoder decoder = mock(LocationSpecificPartDecoder.class);
private final IDecodedVisitProducerService processService = mock(IDecodedVisitProducerService.class);
private final IReportService reportService = new ReportService(retentionDuration, duplicateScanThresholdInSeconds, decoder, processService);
private final IReportService reportService = new ReportService(retentionDuration, duplicateScanThresholdInSeconds, exposureTimeUnit, decoder, processService);
private Instant now;
@BeforeEach
......
package fr.gouv.clea.ws.service.impl;
import fr.gouv.clea.ws.model.DecodedVisit;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class VisitsInSameCounterTest {
@Mock
private DecodedVisit decodedVisit;
@Test
void incrementIfScannedInSameTimeUnitThanLastScanTime_increments_counter_when_difference_between_scan_time_is_lower_than_exposureTimeUnit() {
final long exposureTimeUnit = 1800L;
final VisitsInSameUnitCounter counter = new VisitsInSameUnitCounter(exposureTimeUnit);
final int initialCount = 0;
counter.setCount(initialCount);
final Instant lastScanTime = Instant.now().minusMillis(exposureTimeUnit - 1000);
// 1s after
final Instant currentVisitScanTime = Instant.now();
counter.setLastScanTime(lastScanTime);
when(decodedVisit.getQrCodeScanTime()).thenReturn(currentVisitScanTime);
counter.incrementIfScannedInSameTimeUnitThanLastScanTime(decodedVisit);
Assertions.assertThat(counter.getCount()).isEqualTo(initialCount+1);
}
@Test
void incrementIfScannedInSameTimeUnitThanLastScanTime_does_not_increment_counter_when_difference_between_scan_time_is_greater_than_exposureTimeUnit() {
final long exposureTimeUnit = 1800L;
final VisitsInSameUnitCounter counter = new VisitsInSameUnitCounter(exposureTimeUnit);
final int initialCount = 0;
counter.setCount(initialCount);
final Instant lastScanTime = Instant.now().minus(exposureTimeUnit + 1000, ChronoUnit.SECONDS);
// 1s after
final Instant currentVisitScanTime = Instant.now();
counter.setLastScanTime(lastScanTime);
when(decodedVisit.getQrCodeScanTime()).thenReturn(currentVisitScanTime);
counter.incrementIfScannedInSameTimeUnitThanLastScanTime(decodedVisit);
Assertions.assertThat(counter.getCount()).isEqualTo(initialCount);
}
}
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