Attention une mise à jour du service Gitlab va être effectuée le mardi 18 janvier (et non lundi 17 comme annoncé précédemment) entre 18h00 et 18h30. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes.

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

unit tests on logging line

parent 8cd174c7
......@@ -55,12 +55,13 @@ public class ReportService implements IReportService {
@Override
public List<DecodedVisit> report(ReportRequest reportRequestVo) {
final Instant now = Instant.now();
final VisitsInSameUnitCounter closeScanTimeVisits = new VisitsInSameUnitCounter(exposureTimeUnit);
long validatedPivotDate = this.validatePivotDate(reportRequestVo.getPivotDateAsNtpTimestamp());
long validatedPivotDate = this.validatePivotDate(reportRequestVo.getPivotDateAsNtpTimestamp(), now);
List<Visit> reportVisits = reportRequestVo.getVisits();
List<DecodedVisit> verified = reportVisits.stream()
.filter(visit -> !isOutdated(visit))
.filter(visit -> !isFuture(visit))
.filter(visit -> !isOutdated(visit, now))
.filter(visit -> !isFuture(visit, now))
.map(it -> this.decode(it, validatedPivotDate))
.filter(Objects::nonNull)
.collect(Collectors.toList());
......@@ -73,7 +74,7 @@ public class ReportService implements IReportService {
log.info("BATCH_REPORT {}#{}#{}#{}#{}", reportVisits.size(), reportVisits.size() - pruned.size(),
pruned.stream().filter(DecodedVisit::isBackward).count(),
pruned.stream().filter(visit -> !visit.isBackward()).count(),
pruned.stream().filter(DecodedVisit::isForward).count(),
closeScanTimeVisits.getCount());
return pruned;
}
......@@ -90,16 +91,16 @@ public class ReportService implements IReportService {
}
}
private boolean isOutdated(Visit visit) {
boolean outdated = ChronoUnit.DAYS.between(TimeUtils.instantFromTimestamp(visit.getQrCodeScanTimeAsNtpTimestamp()), Instant.now()) > retentionDurationInDays;
private boolean isOutdated(Visit visit, Instant now) {
boolean outdated = ChronoUnit.DAYS.between(TimeUtils.instantFromTimestamp(visit.getQrCodeScanTimeAsNtpTimestamp()), now) > retentionDurationInDays;
if (outdated) {
log.warn("report: {} rejected: Outdated", MessageFormatter.truncateQrCode(visit.getQrCode()));
}
return outdated;
}
private boolean isFuture(Visit visit) {
boolean future = TimeUtils.instantFromTimestamp(visit.getQrCodeScanTimeAsNtpTimestamp()).isAfter(Instant.now());
private boolean isFuture(Visit visit, Instant now) {
boolean future = TimeUtils.instantFromTimestamp(visit.getQrCodeScanTimeAsNtpTimestamp()).isAfter(now);
if (future) {
log.warn("report: {} rejected: In future", MessageFormatter.truncateQrCode(visit.getQrCode()));
}
......@@ -133,9 +134,9 @@ public class ReportService implements IReportService {
return cleaned;
}
private long validatePivotDate(long pivotDate) {
private long validatePivotDate(long pivotDate, Instant now) {
Instant pivotDateAsInstant = TimeUtils.instantFromTimestamp(pivotDate);
Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
Instant nowWithoutMilis = now.truncatedTo(ChronoUnit.SECONDS);
Instant retentionDateLimit = now.minus(retentionDurationInDays, ChronoUnit.DAYS);
if (pivotDateAsInstant.isAfter(now) || pivotDateAsInstant.isBefore(retentionDateLimit)) {
long retentionDateLimitAsNtp = TimeUtils.ntpTimestampFromInstant(retentionDateLimit);
......
package fr.gouv.clea.ws.service.impl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.apache.tomcat.util.codec.binary.Base64;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import fr.gouv.clea.ws.model.DecodedVisit;
import fr.gouv.clea.ws.service.IDecodedVisitProducerService;
import fr.gouv.clea.ws.service.IReportService;
......@@ -28,6 +15,19 @@ import fr.inria.clea.lsp.LocationSpecificPartDecoder;
import fr.inria.clea.lsp.LocationSpecificPartEncoder;
import fr.inria.clea.lsp.exception.CleaEncodingException;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.junit.jupiter.api.*;
import org.slf4j.LoggerFactory;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.*;
class ReportServiceTest {
......@@ -139,7 +139,7 @@ class ReportServiceTest {
UUID uuidB2 = UUID.fromString("de4c7b16-d5a2-45fa-a4f4-50fbf1e3880b");
UUID uuidC = UUID.fromString("bdbf9725-c1ad-42e3-b725-e475272b7f54");
UUID uuidC2 = UUID.fromString("bdbf9725-c1ad-42e3-b725-e475272b7f54");
List<Visit> visits = List.of(
newVisit(uuidA, TimeUtils.ntpTimestampFromInstant(now.minus(4, ChronoUnit.HOURS))), // pass
newVisit(uuidA2, TimeUtils.ntpTimestampFromInstant(now)), // pass
......@@ -235,7 +235,6 @@ class ReportServiceTest {
assertThat(processed.get(0).isBackward()).isTrue();
}
private EncryptedLocationSpecificPart createEncryptedLocationSpecificPart(UUID locationTemporaryPublicId) {
return EncryptedLocationSpecificPart.builder()
.locationTemporaryPublicId(locationTemporaryPublicId)
......@@ -253,4 +252,104 @@ class ReportServiceTest {
return new Visit(qrCode, qrCodeScanTime);
}
@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class LoggingTest {
private ListAppender<ILoggingEvent> loggingEventListAppender;
@BeforeEach
void setUp() {
now = Instant.now();
this.loggingEventListAppender = new ListAppender<>();
this.loggingEventListAppender.start();
((Logger) LoggerFactory.getLogger(ReportService.class)).addAppender(loggingEventListAppender);
}
@Test
void test_that_duplicate_visits_increment_rejected_visits_count() throws CleaEncodingException {
UUID uuidA = UUID.randomUUID();
UUID uuidB = UUID.randomUUID();
UUID uuidC = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuidA, TimeUtils.ntpTimestampFromInstant(now.minus(4, ChronoUnit.HOURS))), // pass
newVisit(uuidA, TimeUtils.ntpTimestampFromInstant(now)), // pass
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 */
);
reportService.report(new ReportRequest(visits, 0L));
List<ILoggingEvent> logsList = loggingEventListAppender.list;
assertThat(logsList).extracting("formattedMessage").contains(String.format("BATCH_REPORT %s#%s#%s#%s#%s", visits.size(), 2, 0, 4, 0));
}
@Test
void test_that_backward_visit_increments_backward_visits_count() throws CleaEncodingException {
long pivotDate = TimeUtils.ntpTimestampFromInstant(now);
long qrScan = TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS));
UUID uuid = UUID.randomUUID();
List<Visit> visits = List.of(newVisit(uuid, qrScan));
reportService.report(new ReportRequest(visits, pivotDate));
List<ILoggingEvent> logsList = loggingEventListAppender.list;
assertThat(logsList).extracting("formattedMessage").contains(String.format("BATCH_REPORT %s#%s#%s#%s#%s", visits.size(), 0, 1, 0, 0));
}
@Test
void test_that_forward_visit_increments_forward_visits_count() throws CleaEncodingException {
long pivotDate = TimeUtils.ntpTimestampFromInstant(now.minus(1, ChronoUnit.DAYS));
long qrScan = TimeUtils.ntpTimestampFromInstant(now);
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuid1, qrScan),
newVisit(uuid2, pivotDate)
);
reportService.report(new ReportRequest(visits, pivotDate));
List<ILoggingEvent> logsList = loggingEventListAppender.list;
assertThat(logsList).extracting("formattedMessage").contains(String.format("BATCH_REPORT %s#%s#%s#%s#%s", visits.size(), 0, 0, 2, 0));
}
@Test
void test_that_outdated_visits_increments_rejected_visits_count() throws CleaEncodingException {
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
UUID uuid3 = UUID.randomUUID();
UUID uuid4 = UUID.randomUUID();
List<Visit> visits = List.of(
newVisit(uuid1, TimeUtils.ntpTimestampFromInstant(now.minus(15, ChronoUnit.DAYS))), // don't pass
newVisit(uuid2, TimeUtils.ntpTimestampFromInstant(now.minus(13, ChronoUnit.DAYS))), // pass
newVisit(uuid3, TimeUtils.ntpTimestampFromInstant(now.minus(2, ChronoUnit.DAYS))), // pass
newVisit(uuid4, TimeUtils.ntpTimestampFromInstant(now)) /* pass */
);
ReportRequest reportRequestVo = new ReportRequest(visits, 0L);
reportService.report(reportRequestVo);
List<ILoggingEvent> logsList = loggingEventListAppender.list;
assertThat(logsList).extracting("formattedMessage").contains(String.format("BATCH_REPORT %s#%s#%s#%s#%s", visits.size(), 1, 0, 3, 0));
}
@Test
void test_that_future_visits_increments_rejected_visits_count() throws CleaEncodingException {
UUID uuid1 = UUID.randomUUID();
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
);
reportService.report(new ReportRequest(visits, 0L));
List<ILoggingEvent> logsList = loggingEventListAppender.list;
assertThat(logsList).extracting("formattedMessage").contains(String.format("BATCH_REPORT %s#%s#%s#%s#%s", visits.size(), 1, 0, 1, 0));
}
}
}
\ No newline at end of file
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