Skip to content

Commit 69ec09e

Browse files
committed
Added unit tests for refreshFrom day truncation
1 parent b4d6e80 commit 69ec09e

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/main/java/com/uid2/admin/salt/TargetDate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Objects;
88

99
public class TargetDate {
10-
private final static long DAY_IN_MS = Duration.ofDays(1).toMillis();
10+
private static final long DAY_IN_MS = Duration.ofDays(1).toMillis();
1111

1212
private final LocalDate date;
1313
private final long epochMs;

src/test/java/com/uid2/admin/salt/SaltRotationTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import ch.qos.logback.classic.Logger;
3030
import org.slf4j.LoggerFactory;
3131

32-
public class SaltRotationTest {
32+
class SaltRotationTest {
3333
@Mock
3434
private IKeyGenerator keyGenerator;
3535
private SaltRotation saltRotation;
@@ -50,7 +50,7 @@ void setup() {
5050
}
5151

5252
@AfterEach
53-
void tearDown() throws Exception {
53+
void teardown() throws Exception {
5454
appender.stop();
5555
mocks.close();
5656
}
@@ -209,13 +209,16 @@ void rotateSaltsRotateSaltsInsufficientOutdatedSalts() throws Exception {
209209

210210
@ParameterizedTest
211211
@CsvSource({
212-
"5, 30", // Soon after rotation, use 30 days post rotation
213-
"40, 60", // >30 days after rotation use the next increment of 30 days
214-
"60, 90", // Exactly at multiple of 30 days post rotation, use next increment of 30 days
212+
"5, 0, 30", // Soon after rotation, use 30 days post rotation
213+
"5, 100, 30", // Soon after rotation, use 30 days post rotation with some offset
214+
"40, 0, 60", // >30 days after rotation use the next increment of 30 days
215+
"40, 100, 60", // >30 days after rotation use the next increment of 30 days with some offset
216+
"60, 0, 90", // Exactly at multiple of 30 days post rotation, use next increment of 30 days
217+
"60, 100, 90" // Exactly at multiple of 30 days post rotation, use next increment of 30 days with some offset
215218
})
216-
void testRefreshFromCalculation(int lastRotationDaysAgo, int refreshFromDaysFromRotation) throws Exception {
219+
void testRefreshFromCalculation(int lastRotationDaysAgo, int lastRotationMsOffset, int refreshFromDaysFromRotation) throws Exception {
217220
var lastRotation = daysEarlier(lastRotationDaysAgo);
218-
SaltBuilder saltBuilder = SaltBuilder.start().lastUpdated(lastRotation);
221+
SaltBuilder saltBuilder = SaltBuilder.start().lastUpdated(lastRotation.asInstant().plusMillis(lastRotationMsOffset));
219222
var lastSnapshot = SaltSnapshotBuilder.start()
220223
.entries(saltBuilder)
221224
.build();

src/test/java/com/uid2/admin/salt/helper/SaltBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ public SaltBuilder lastUpdated(TargetDate lastUpdated) {
3232
return this;
3333
}
3434

35+
public SaltBuilder lastUpdated(Instant lastUpdated) {
36+
this.lastUpdated = lastUpdated;
37+
return this;
38+
}
39+
3540
public SaltBuilder refreshFrom(TargetDate refreshFrom) {
3641
this.refreshFrom = refreshFrom.asInstant();
3742
return this;
3843
}
3944

45+
public SaltBuilder refreshFrom(Instant refreshFrom) {
46+
this.refreshFrom = refreshFrom;
47+
return this;
48+
}
49+
4050
public SaltBuilder currentSalt(String currentSalt) {
4151
this.currentSalt = currentSalt;
4252
return this;

src/test/java/com/uid2/admin/salt/helper/TargetDateUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.uid2.admin.salt.TargetDate;
44

5-
public class TargetDateUtil {
5+
public final class TargetDateUtil {
66
private static final TargetDate TARGET_DATE = TargetDate.of(2025, 1, 1);
77

8+
private TargetDateUtil() {
9+
}
10+
811
public static TargetDate daysEarlier(int days) {
912
return TARGET_DATE.minusDays(days);
1013
}

0 commit comments

Comments
 (0)