19
19
*/
20
20
package io .wcm .caconfig .extensions .persistence .impl ;
21
21
22
+ import static com .day .cq .wcm .api .NameConstants .PN_LAST_MOD ;
22
23
import static io .wcm .caconfig .extensions .persistence .testcontext .PersistenceTestUtils .writeConfiguration ;
23
24
import static io .wcm .caconfig .extensions .persistence .testcontext .PersistenceTestUtils .writeConfigurationCollection ;
24
25
import static org .apache .sling .api .resource .ResourceResolver .PROPERTY_RESOURCE_TYPE ;
25
26
import static org .apache .sling .testing .mock .caconfig .ContextPlugins .CACONFIG ;
26
27
import static org .hamcrest .MatcherAssert .assertThat ;
27
28
import static org .junit .jupiter .api .Assertions .assertEquals ;
29
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
28
30
import static org .junit .jupiter .api .Assertions .assertNotNull ;
29
31
import static org .junit .jupiter .api .Assertions .assertNull ;
30
32
import static org .junit .jupiter .api .Assertions .assertTrue ;
31
33
34
+ import java .util .Calendar ;
32
35
import java .util .List ;
33
36
34
37
import org .apache .sling .caconfig .ConfigurationBuilder ;
@@ -162,6 +165,36 @@ void testListConfig() {
162
165
assertEquals (234 , config2 .intParam ());
163
166
}
164
167
168
+ @ Test
169
+ void testListConfig_updateLastModifiedIfPropertyRemoved () {
170
+ context .registerInjectActivateService (new PagePersistenceStrategy (), "enabled" , true );
171
+
172
+ // write config
173
+ writeConfigurationCollection (context , contentPage .getPath (), ListConfig .class .getName (), List .of (
174
+ ImmutableValueMap .of ("stringParam" , "value1" , "intParam" , 123 ),
175
+ ImmutableValueMap .of ("stringParam" , "value2" , "intParam" , 234 )
176
+ ));
177
+
178
+ // assert storage in page in /conf
179
+ Page parentPage = context .pageManager ().getPage ("/conf/test/site1/sling:configs/" + ListConfig .class .getName ());
180
+ assertNotNull (parentPage );
181
+
182
+ Page configPage1 = context .pageManager ().getPage ("/conf/test/site1/sling:configs/" + ListConfig .class .getName () + "/item0" );
183
+ assertThat (configPage1 .getContentResource (), ResourceMatchers .props ("stringParam" , "value1" , "intParam" , 123 ));
184
+
185
+ Page configPage2 = context .pageManager ().getPage ("/conf/test/site1/sling:configs/" + ListConfig .class .getName () + "/item1" );
186
+ assertThat (configPage2 .getContentResource (), ResourceMatchers .props ("stringParam" , "value2" , "intParam" , 234 ));
187
+
188
+ writeConfigurationCollection (context , contentPage .getPath (), ListConfig .class .getName (), List .of (
189
+ ImmutableValueMap .of ("stringParam" , "value1" , "intParam" , 123 ),
190
+ ImmutableValueMap .of ("stringParam" , "value2" )
191
+ ));
192
+ Calendar lastModifiedConfigPage2AfterUpdate = configPage2 .getContentResource ().getValueMap ().get (PN_LAST_MOD , Calendar .class );
193
+ System .out .println (lastModifiedConfigPage2AfterUpdate );
194
+ //ConfigPage2 last modified date should be updated because it is updated
195
+ assertNotNull (lastModifiedConfigPage2AfterUpdate );
196
+ }
197
+
165
198
@ Test
166
199
void testListConfig_Nested () {
167
200
context .registerInjectActivateService (new PagePersistenceStrategy (), "enabled" , true );
@@ -207,10 +240,14 @@ void testListConfig_Nested() {
207
240
assertEquals (1 , config2 .subListConfig ().length );
208
241
assertEquals ("value21" , config2 .subListConfig ()[0 ].stringParam ());
209
242
243
+
244
+ Calendar lastModifiedConfigPage1 = configPage1 .getContentResource ().getValueMap ().get (PN_LAST_MOD , Calendar .class );
245
+ Calendar lastModifiedConfigPage2 = configPage2 .getContentResource ().getValueMap ().get (PN_LAST_MOD , Calendar .class );
246
+
210
247
// update config collection items
211
248
writeConfigurationCollection (context , contentPage .getPath (), ListNestedConfig .class .getName (), List .of (
212
249
ImmutableValueMap .of ("stringParam" , "value1-new" , "intParam" , 123 ),
213
- ImmutableValueMap .of ("stringParam" , "value2-new " , "intParam" , 234 ),
250
+ ImmutableValueMap .of ("stringParam" , "value2" , "intParam" , 234 ),
214
251
ImmutableValueMap .of ("stringParam" , "value3-new" , "intParam" , 345 )));
215
252
216
253
// read config
@@ -224,18 +261,34 @@ void testListConfig_Nested() {
224
261
assertEquals (2 , config1 .subListConfig ().length );
225
262
assertEquals ("value11" , config1 .subListConfig ()[0 ].stringParam ());
226
263
assertEquals ("value12" , config1 .subListConfig ()[1 ].stringParam ());
264
+ //ConfigPage1 last modified date should be updated because it is updated
265
+ assertTrue (configLastModifiedUpdated (configPage1 , false , lastModifiedConfigPage1 ));
227
266
228
267
config2 = configs .get (1 );
229
- assertEquals ("value2-new " , config2 .stringParam ());
268
+ assertEquals ("value2" , config2 .stringParam ());
230
269
assertEquals (234 , config2 .intParam ());
231
270
assertEquals (1 , config2 .subListConfig ().length );
232
271
assertEquals ("value21" , config2 .subListConfig ()[0 ].stringParam ());
272
+ //ConfigPage2 last modified date should not be updated because it is not updated
273
+ assertFalse (configLastModifiedUpdated (configPage2 , false , lastModifiedConfigPage2 ));
233
274
234
275
ListNestedConfig config3 = configs .get (2 );
235
276
assertEquals ("value3-new" , config3 .stringParam ());
236
277
assertEquals (345 , config3 .intParam ());
237
278
assertEquals (0 , config3 .subListConfig ().length );
279
+ Page configPage3 = context .pageManager ().getPage ("/conf/test/site1/sling:configs/" + ListNestedConfig .class .getName () + "/item2" );
280
+ //ConfigPage3 last modified date should be added because it is newly created
281
+ assertTrue (configLastModifiedUpdated (configPage3 , true , null ));
282
+ }
238
283
284
+ private boolean configLastModifiedUpdated (Page configPage , boolean newlyCreatedConfig , Calendar lastModifiedBeforeUpdateOrCreate ) {
285
+ Calendar lastModifiedAfterUpdateOrCreate = configPage .getContentResource ().getValueMap ().get (PN_LAST_MOD , Calendar .class );
286
+ if (newlyCreatedConfig ) {
287
+ //If the config is newly created then last modified date should be updated
288
+ return lastModifiedAfterUpdateOrCreate != null ;
289
+ }
290
+ //If the config is updated then last modified date should be updated
291
+ return lastModifiedAfterUpdateOrCreate != null && lastModifiedAfterUpdateOrCreate .after (lastModifiedBeforeUpdateOrCreate );
239
292
}
240
293
241
294
@ Test
0 commit comments