22
33import ch .qos .logback .classic .spi .ILoggingEvent ;
44import ch .qos .logback .core .read .ListAppender ;
5- import com .uid2 .admin .AdminConst ;
65import com .uid2 .admin .salt .helper .SaltBuilder ;
76import com .uid2 .admin .salt .helper .SaltSnapshotBuilder ;
87import com .uid2 .shared .model .SaltEntry ;
98import com .uid2 .shared .secret .IKeyGenerator ;
10- import io .vertx .core .json .JsonObject ;
119import org .junit .jupiter .api .AfterEach ;
1210import org .junit .jupiter .api .BeforeEach ;
1311import org .junit .jupiter .api .Test ;
@@ -45,8 +43,7 @@ void setup() {
4543 appender .start ();
4644 ((Logger ) LoggerFactory .getLogger (SaltRotation .class )).addAppender (appender );
4745
48- JsonObject config = new JsonObject ();
49- saltRotation = new SaltRotation (config , keyGenerator );
46+ saltRotation = new SaltRotation (keyGenerator );
5047 }
5148
5249 @ AfterEach
@@ -81,7 +78,7 @@ void testRotateSaltsAllSaltsUpToDate() throws Exception {
8178 };
8279
8380 var lastSnapshot = SaltSnapshotBuilder .start ()
84- .entries (10 , targetDate ())
81+ .entries (10 , targetDate (), targetDate () )
8582 .build ();
8683
8784 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.2 , targetDate ());
@@ -97,16 +94,16 @@ void testRotateSaltsAllSaltsOld() throws Exception {
9794 };
9895
9996 var lastSnapshot = SaltSnapshotBuilder .start ()
100- .entries (10 , daysEarlier (10 ))
97+ .entries (10 , daysEarlier (10 ), targetDate () )
10198 .build ();
10299
103100 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.2 , targetDate ());
104101 assertTrue (result .hasSnapshot ());
105- assertEquals (2 , countEntriesWithLastUpdated (result .getSnapshot ().getAllRotatingSalts (), result .getSnapshot ().getEffective ()));
106- assertEquals (8 , countEntriesWithLastUpdated (result .getSnapshot ().getAllRotatingSalts (), daysEarlier (10 )));
102+ assertEquals (1 , countEntriesWithLastUpdated (result .getSnapshot ().getAllRotatingSalts (), result .getSnapshot ().getEffective ()));
103+ assertEquals (9 , countEntriesWithLastUpdated (result .getSnapshot ().getAllRotatingSalts (), daysEarlier (10 )));
107104 assertEquals (targetDate ().asInstant (), result .getSnapshot ().getEffective ());
108105 assertEquals (daysLater (7 ).asInstant (), result .getSnapshot ().getExpires ());
109- verify (keyGenerator , times (2 )).generateRandomKeyString (anyInt ());
106+ verify (keyGenerator , times (1 )).generateRandomKeyString (anyInt ());
110107 }
111108
112109 @ Test
@@ -117,17 +114,17 @@ void testRotateSaltsRotateSaltsFromOldestBucketOnly() throws Exception {
117114 };
118115
119116 var lastSnapshot = SaltSnapshotBuilder .start ()
120- .entries (3 , daysEarlier (6 ))
121- .entries (5 , daysEarlier (5 ))
122- .entries (2 , daysEarlier (4 ))
117+ .entries (3 , daysEarlier (6 ), targetDate () )
118+ .entries (5 , daysEarlier (5 ), targetDate () )
119+ .entries (2 , daysEarlier (4 ), targetDate () )
123120 .build ();
124121
125122 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.2 , targetDate ());
126123 assertTrue (result .hasSnapshot ());
127124 var salts = result .getSnapshot ().getAllRotatingSalts ();
128125 assertEquals (2 , countEntriesWithLastUpdated (salts , result .getSnapshot ().getEffective ()));
129- assertEquals (1 , countEntriesWithLastUpdated (salts , daysEarlier (6 )));
130- assertEquals (5 , countEntriesWithLastUpdated (salts , daysEarlier (5 )));
126+ assertEquals (2 , countEntriesWithLastUpdated (salts , daysEarlier (6 )));
127+ assertEquals (4 , countEntriesWithLastUpdated (salts , daysEarlier (5 )));
131128 assertEquals (2 , countEntriesWithLastUpdated (salts , daysEarlier (4 )));
132129 assertEquals (targetDate ().asInstant (), result .getSnapshot ().getEffective ());
133130 assertEquals (daysLater (7 ).asInstant (), result .getSnapshot ().getExpires ());
@@ -142,19 +139,19 @@ void testRotateSaltsRotateSaltsFromNewerBucketOnly() throws Exception {
142139 };
143140
144141 var lastSnapshot = SaltSnapshotBuilder .start ()
145- .entries (3 , daysEarlier (4 ))
146- .entries (7 , daysEarlier (3 ))
142+ .entries (3 , daysEarlier (4 ), targetDate () )
143+ .entries (7 , daysEarlier (3 ), targetDate () )
147144 .build ();
148145
149146 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.2 , targetDate ());
150147 assertTrue (result .hasSnapshot ());
151148 var salts = result .getSnapshot ().getAllRotatingSalts ();
152- assertEquals (2 , countEntriesWithLastUpdated (salts , result .getSnapshot ().getEffective ()));
153- assertEquals (1 , countEntriesWithLastUpdated (salts , daysEarlier (4 )));
149+ assertEquals (1 , countEntriesWithLastUpdated (salts , result .getSnapshot ().getEffective ()));
150+ assertEquals (2 , countEntriesWithLastUpdated (salts , daysEarlier (4 )));
154151 assertEquals (7 , countEntriesWithLastUpdated (salts , daysEarlier (3 )));
155152 assertEquals (targetDate ().asInstant (), result .getSnapshot ().getEffective ());
156153 assertEquals (daysLater (7 ).asInstant (), result .getSnapshot ().getExpires ());
157- verify (keyGenerator , times (2 )).generateRandomKeyString (anyInt ());
154+ verify (keyGenerator , times (1 )).generateRandomKeyString (anyInt ());
158155 }
159156
160157 @ Test
@@ -165,9 +162,9 @@ void testRotateSaltsRotateSaltsFromMultipleBuckets() throws Exception {
165162 };
166163
167164 var lastSnapshot = SaltSnapshotBuilder .start ()
168- .entries (3 , daysEarlier (6 ))
169- .entries (5 , daysEarlier (5 ))
170- .entries (2 , daysEarlier (4 ))
165+ .entries (3 , daysEarlier (6 ), targetDate () )
166+ .entries (5 , daysEarlier (5 ), targetDate () )
167+ .entries (2 , daysEarlier (4 ), targetDate () )
171168 .build ();
172169
173170 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.45 , targetDate ());
@@ -190,9 +187,9 @@ void testRotateSaltsRotateSaltsInsufficientOutdatedSalts() throws Exception {
190187 };
191188
192189 var lastSnapshot = SaltSnapshotBuilder .start ()
193- .entries (1 , daysEarlier (5 ))
194- .entries (2 , daysEarlier (4 ))
195- .entries (7 , daysEarlier (2 ))
190+ .entries (1 , daysEarlier (5 ), targetDate () )
191+ .entries (2 , daysEarlier (4 ), targetDate () )
192+ .entries (7 , daysEarlier (2 ), targetDate () )
196193 .build ();
197194
198195 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.45 , targetDate ());
@@ -218,9 +215,9 @@ void testRotateSaltsRotateSaltsInsufficientOutdatedSalts() throws Exception {
218215 })
219216 void testRefreshFromCalculation (int lastRotationDaysAgo , int lastRotationMsOffset , int refreshFromDaysFromRotation ) throws Exception {
220217 var lastRotation = daysEarlier (lastRotationDaysAgo );
221- SaltBuilder saltBuilder = SaltBuilder .start ().lastUpdated (lastRotation .asInstant ().plusMillis (lastRotationMsOffset ));
218+ SaltBuilder saltBuilder = SaltBuilder .start ().lastUpdated (lastRotation .asInstant ().plusMillis (lastRotationMsOffset )). refreshFrom ( targetDate ()) ;
222219 var lastSnapshot = SaltSnapshotBuilder .start ()
223- .entries (saltBuilder )
220+ .entries (saltBuilder , saltBuilder , saltBuilder , saltBuilder )
224221 .build ();
225222
226223 var result = saltRotation .rotateSalts (lastSnapshot , new Duration []{Duration .ofDays (1 )}, 0.45 , targetDate ());
@@ -244,9 +241,9 @@ void testRotateSaltsPopulatePreviousSaltsOnRotation() throws Exception {
244241 var over90Days = daysEarlier (120 );
245242 var lastSnapshot = SaltSnapshotBuilder .start ()
246243 .entries (
247- SaltBuilder .start ().lastUpdated (lessThan90Days ).currentSalt ("salt1" ),
248- SaltBuilder .start ().lastUpdated (exactly90Days ).currentSalt ("salt2" ),
249- SaltBuilder .start ().lastUpdated (over90Days ).currentSalt ("salt3" )
244+ SaltBuilder .start ().lastUpdated (lessThan90Days ).refreshFrom ( targetDate ()). currentSalt ("salt1" ),
245+ SaltBuilder .start ().lastUpdated (exactly90Days ).refreshFrom ( targetDate ()). currentSalt ("salt2" ),
246+ SaltBuilder .start ().lastUpdated (over90Days ).refreshFrom ( targetDate ()). currentSalt ("salt3" )
250247 )
251248 .build ();
252249
@@ -270,10 +267,10 @@ void testRotateSaltsPreservePreviousSaltsLessThan90DaysOld() throws Exception {
270267 var validForRotation = daysEarlier (70 );
271268 var lastSnapshot = SaltSnapshotBuilder .start ()
272269 .entries (
273- SaltBuilder .start ().lastUpdated (notValidForRotation1 ).currentSalt ("salt1" ).previousSalt ("previousSalt1" ),
274- SaltBuilder .start ().lastUpdated (notValidForRotation2 ).currentSalt ("salt2" )
270+ SaltBuilder .start ().lastUpdated (notValidForRotation1 ).refreshFrom ( targetDate ()). currentSalt ("salt1" ).previousSalt ("previousSalt1" ),
271+ SaltBuilder .start ().lastUpdated (notValidForRotation2 ).refreshFrom ( targetDate ()). currentSalt ("salt2" )
275272 )
276- .entries (1 , validForRotation )
273+ .entries (1 , validForRotation , targetDate () )
277274 .build ();
278275
279276 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 1 , targetDate ());
@@ -295,10 +292,10 @@ void testRotateSaltsRemovePreviousSaltsOver90DaysOld() throws Exception {
295292 var validForRotation = daysEarlier (120 );
296293 var lastSnapshot = SaltSnapshotBuilder .start ()
297294 .entries (
298- SaltBuilder .start ().lastUpdated (exactly90Days ).previousSalt ("90DaysOld" ),
299- SaltBuilder .start ().lastUpdated (over90Days ).previousSalt ("over90DaysOld" )
295+ SaltBuilder .start ().lastUpdated (exactly90Days ).refreshFrom ( targetDate ()). previousSalt ("90DaysOld" ),
296+ SaltBuilder .start ().lastUpdated (over90Days ).refreshFrom ( targetDate ()). previousSalt ("over90DaysOld" )
300297 )
301- .entries (1 , validForRotation )
298+ .entries (1 , validForRotation , targetDate () )
302299 .build ();
303300
304301 var result = saltRotation .rotateSalts (lastSnapshot , minAges , 0.5 , targetDate ());
@@ -311,8 +308,7 @@ void testRotateSaltsRemovePreviousSaltsOver90DaysOld() throws Exception {
311308
312309 @ Test
313310 void testRotateSaltsRotateWhenRefreshFromIsTargetDate () throws Exception {
314- JsonObject config = new JsonObject ();
315- saltRotation = new SaltRotation (config , keyGenerator );
311+ saltRotation = new SaltRotation (keyGenerator );
316312
317313 final Duration [] minAges = {
318314 Duration .ofDays (90 ),
@@ -350,8 +346,7 @@ void testRotateSaltsRotateWhenRefreshFromIsTargetDate() throws Exception {
350346
351347 @ Test
352348 void testLogFewSaltAgesOnRotation () throws Exception {
353- JsonObject config = new JsonObject ();
354- saltRotation = new SaltRotation (config , keyGenerator );
349+ saltRotation = new SaltRotation (keyGenerator );
355350
356351 // 7 salts total, 5 refreshable, 3 will rotate (6 * 0.4 rounded up), up to 2 will rotate per age (3 * 0.8)
357352 var lastSnapshot = SaltSnapshotBuilder .start ()
@@ -394,8 +389,7 @@ void testLogFewSaltAgesOnRotation() throws Exception {
394389
395390 @ Test
396391 void testLogManySaltAgesOnRotation () throws Exception {
397- JsonObject config = new JsonObject ();
398- saltRotation = new SaltRotation (config , keyGenerator );
392+ saltRotation = new SaltRotation (keyGenerator );
399393
400394 // 50 salts total, 16 refreshable, 10 will rotate (18 * 0.2 rounded up), up to 8 will rotate per age (10 * 0.8)
401395 var lastSnapshot = SaltSnapshotBuilder .start ()
0 commit comments