Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
DiagnosisKey: Reject negative retention period for threshold calculat…
Browse files Browse the repository at this point in the history
…ion (#239)
  • Loading branch information
jonaskoepper authored May 22, 2020
1 parent 2713d27 commit a619999
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ public long getSubmissionTimestamp() {
*
* @param daysToRetain the number of days before a key is outdated
* @return true, if the rolling start number is in the time span between now, and the given days to retain
* @throws IllegalArgumentException if {@code daysToRetain} is negative.
*/
public boolean isYoungerThanRetentionThreshold(long daysToRetain) {
public boolean isYoungerThanRetentionThreshold(int daysToRetain) {
if (daysToRetain < 0) {
throw new IllegalArgumentException("Retention threshold must be greater or equal to 0.");
}
long threshold = LocalDateTime
.ofInstant(Instant.now(), UTC)
.minusDays(daysToRetain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@

import static java.time.ZoneOffset.UTC;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;


import java.nio.charset.Charset;
import java.time.Instant;
import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;


public class DiagnosisKeyTest {

Expand Down Expand Up @@ -66,8 +73,24 @@ public void testIsYoungerThanRetentionThreshold() {
DiagnosisKey diagnosisKeyFiveDays = new DiagnosisKey(expKeyData, fiveDaysAgo,
expRollingPeriod, expTransmissionRiskLevel, expSubmissionTimestamp);

assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(4L)).isFalse();
assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(5L)).isFalse();
assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(6L)).isTrue();
assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(4)).isFalse();
assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(5)).isFalse();
assertThat(diagnosisKeyFiveDays.isYoungerThanRetentionThreshold(6)).isTrue();
}

@DisplayName("Test retention threshold accepts positive value")
@ValueSource(ints = {0, 1, Integer.MAX_VALUE})
@ParameterizedTest
public void testRetentionThresholdAcceptsPositiveValue(int daysToRetain) {
assertThatCode(() -> diagnosisKey.isYoungerThanRetentionThreshold(daysToRetain))
.doesNotThrowAnyException();
}

@DisplayName("Test retention threshold rejects negative value")
@ValueSource(ints = {Integer.MIN_VALUE, -1})
@ParameterizedTest
public void testRetentionThresholdRejectsNegativeValue(int daysToRetain) {
assertThatIllegalArgumentException()
.isThrownBy(() -> diagnosisKey.isYoungerThanRetentionThreshold(daysToRetain));
}
}

0 comments on commit a619999

Please sign in to comment.