3030import org .springframework .context .ApplicationContext ;
3131import org .springframework .test .context .ContextConfiguration ;
3232
33+ import software .xdev .spring .data .eclipse .store .exceptions .InvalidVersionException ;
3334import software .xdev .spring .data .eclipse .store .helper .TestData ;
3435import software .xdev .spring .data .eclipse .store .helper .TestUtil ;
3536import software .xdev .spring .data .eclipse .store .integration .isolated .IsolatedTestAnnotations ;
@@ -42,49 +43,55 @@ class VersionTest
4243{
4344 public static Stream <Arguments > generateData ()
4445 {
45- return List .of (
46+ return Stream .of (
4647 new SingleTestDataset <>(
47- name -> new VersionedEntityWithInteger ( name ) ,
48+ VersionedEntityWithInteger :: new ,
4849 context -> context .getBean (VersionedEntityWithIntegerRepository .class ),
4950 1 ,
5051 2
5152 ).toArguments (),
5253 new SingleTestDataset <>(
53- name -> new VersionedEntityWithLong ( name ) ,
54+ VersionedEntityWithLong :: new ,
5455 context -> context .getBean (VersionedEntityWithLongRepository .class ),
5556 1L ,
5657 2L
5758 ).toArguments (),
5859 new SingleTestDataset <>(
59- name -> new VersionedEntityWithPrimitiveInteger ( name ) ,
60+ VersionedEntityWithPrimitiveInteger :: new ,
6061 context -> context .getBean (VersionedEntityWithPrimitiveIntegerRepository .class ),
6162 1 ,
6263 2
6364 ).toArguments (),
6465 new SingleTestDataset <>(
65- name -> new VersionedEntityWithPrimitiveLong ( name ) ,
66+ VersionedEntityWithPrimitiveLong :: new ,
6667 context -> context .getBean (VersionedEntityWithPrimitiveLongRepository .class ),
6768 1L ,
6869 2L
6970 ).toArguments (),
7071 new SingleTestDataset <>(
71- name -> new VersionedEntityWithString ( name ) ,
72+ VersionedEntityWithString :: new ,
7273 context -> context .getBean (VersionedEntityWithStringRepository .class ),
7374 null ,
7475 null
7576 ).toArguments (),
7677 new SingleTestDataset <>(
77- name -> new VersionedEntityWithUuid ( name ) ,
78+ VersionedEntityWithUuid :: new ,
7879 context -> context .getBean (VersionedEntityWithUuidRepository .class ),
7980 null ,
8081 null
82+ ).toArguments (),
83+ new SingleTestDataset <>(
84+ VersionedEntityWithId ::new ,
85+ context -> context .getBean (VersionedEntityWithIdRepository .class ),
86+ 1 ,
87+ 2
8188 ).toArguments ()
82- ). stream () ;
89+ );
8390 }
8491
85- private record SingleTestDataset <T extends VersionedEntity >(
92+ private record SingleTestDataset <T extends VersionedEntity <?> >(
8693 Function <String , T > enitityGenerator ,
87- Function <ApplicationContext , EclipseStoreRepository <T , Void >> repositoryGenerator ,
94+ Function <ApplicationContext , EclipseStoreRepository <T , ? >> repositoryGenerator ,
8895 Object firstVersion ,
8996 Object secondVersion
9097 )
@@ -106,10 +113,10 @@ public VersionTest(final VersionTestConfiguration configuration)
106113
107114 @ ParameterizedTest
108115 @ MethodSource ("generateData" )
109- <T extends VersionedEntity > void simpleSave (
116+ <T extends VersionedEntity <?> > void simpleSave (
110117 final SingleTestDataset <T > data , @ Autowired final ApplicationContext context )
111118 {
112- final EclipseStoreRepository <T , Void > repository = data .repositoryGenerator .apply (context );
119+ final EclipseStoreRepository <T , ? > repository = data .repositoryGenerator .apply (context );
113120 final T entity = data .enitityGenerator .apply (TestData .FIRST_NAME );
114121 repository .save (entity );
115122 TestUtil .doBeforeAndAfterRestartOfDatastore (
@@ -131,10 +138,10 @@ <T extends VersionedEntity> void simpleSave(
131138
132139 @ ParameterizedTest
133140 @ MethodSource ("generateData" )
134- <T extends VersionedEntity > void doubleSave (
141+ <T extends VersionedEntity <?> > void doubleSave (
135142 final SingleTestDataset <T > data , @ Autowired final ApplicationContext context )
136143 {
137- final EclipseStoreRepository <T , Void > repository = data .repositoryGenerator .apply (context );
144+ final EclipseStoreRepository <T , ? > repository = data .repositoryGenerator .apply (context );
138145 final T entity = data .enitityGenerator .apply (TestData .FIRST_NAME );
139146 repository .save (entity );
140147 repository .save (repository .findAll ().get (0 ));
@@ -157,10 +164,10 @@ <T extends VersionedEntity> void doubleSave(
157164
158165 @ ParameterizedTest
159166 @ MethodSource ("generateData" )
160- <T extends VersionedEntity > void saveButLocked (
167+ <T extends VersionedEntity <?> > void saveButLocked (
161168 final SingleTestDataset <T > data , @ Autowired final ApplicationContext context )
162169 {
163- final EclipseStoreRepository <T , Void > repository = data .repositoryGenerator .apply (context );
170+ final EclipseStoreRepository <T , ? > repository = data .repositoryGenerator .apply (context );
164171 final T entity = data .enitityGenerator .apply (TestData .FIRST_NAME );
165172 repository .save (entity );
166173
@@ -243,4 +250,74 @@ void saveButLockedWithSameChild(@Autowired final VersionedEntityWithIntegerAndVe
243250
244251 Assertions .assertThrows (OptimisticLockException .class , () -> repository .save (secondLoadedEntry ));
245252 }
253+
254+ @ Test
255+ void replaceWithIdWithNull (@ Autowired final VersionedEntityWithIdRepository repository )
256+ {
257+ final VersionedEntityWithId existingEntity = new VersionedEntityWithId (TestData .FIRST_NAME );
258+ repository .save (existingEntity );
259+
260+ final int existingId = repository .findAll ().get (0 ).getId ();
261+ final VersionedEntityWithId nextEntity = new VersionedEntityWithId (
262+ existingId ,
263+ TestData .FIRST_NAME_ALTERNATIVE );
264+
265+ Assertions .assertThrows (InvalidVersionException .class , () -> repository .save (nextEntity ));
266+ }
267+
268+ @ Test
269+ void replaceWithIdWithDifferentVersion (@ Autowired final VersionedEntityWithIdRepository repository )
270+ {
271+ final VersionedEntityWithId existingEntity = new VersionedEntityWithId (TestData .FIRST_NAME );
272+ repository .save (existingEntity );
273+
274+ final VersionedEntityWithId foundEntity = repository .findAll ().get (0 );
275+ final VersionedEntityWithId nextEntity = new VersionedEntityWithId (
276+ foundEntity .getId (),
277+ TestData .FIRST_NAME_ALTERNATIVE );
278+ nextEntity .setVersion (foundEntity .getVersion () + 1 );
279+
280+ Assertions .assertThrows (OptimisticLockException .class , () -> repository .save (nextEntity ));
281+ }
282+
283+ @ Test
284+ void replaceWithIdWithSameVersion (@ Autowired final VersionedEntityWithIdRepository repository )
285+ {
286+ final VersionedEntityWithId existingEntity = new VersionedEntityWithId (TestData .FIRST_NAME );
287+ repository .save (existingEntity );
288+
289+ final VersionedEntityWithId foundEntity = repository .findAll ().get (0 );
290+ final VersionedEntityWithId nextEntity = new VersionedEntityWithId (
291+ foundEntity .getId (),
292+ TestData .FIRST_NAME_ALTERNATIVE );
293+ nextEntity .setVersion (foundEntity .getVersion ());
294+ repository .save (nextEntity );
295+
296+ TestUtil .doBeforeAndAfterRestartOfDatastore (
297+ this .configuration ,
298+ () -> {
299+ final List <VersionedEntityWithId > allEntities = repository .findAll ();
300+ Assertions .assertEquals (1 , allEntities .size ());
301+ Assertions .assertEquals (2 , allEntities .get (0 ).getVersion ());
302+ }
303+ );
304+ }
305+
306+ @ Test
307+ void findById (@ Autowired final VersionedEntityWithIdRepository repository )
308+ {
309+ final VersionedEntityWithId existingEntity = new VersionedEntityWithId (TestData .FIRST_NAME );
310+ repository .save (existingEntity );
311+
312+ final int existingId = repository .findAll ().get (0 ).getId ();
313+
314+ TestUtil .doBeforeAndAfterRestartOfDatastore (
315+ this .configuration ,
316+ () -> {
317+ Assertions .assertTrue (repository .findById (existingId ).isPresent ());
318+ Assertions .assertEquals (1 , repository .findById (existingId ).get ().getVersion ());
319+ Assertions .assertEquals (existingId , repository .findById (existingId ).get ().getId ());
320+ }
321+ );
322+ }
246323}
0 commit comments