Skip to content

Commit 54745dd

Browse files
Tests with EmbeddedIds and CustomId-classes
1 parent ee3d3ee commit 54745dd

File tree

11 files changed

+539
-5
lines changed

11 files changed

+539
-5
lines changed

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/AnnotatedFieldFinder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ private AnnotatedFieldFinder()
3131
}
3232

3333
/**
34-
* Finds any field in a class with an ID-Annotation ({@link jakarta.persistence.Id} or
35-
* {@link org.springframework.data.annotation.Id}). Finds this field recursively in the Hierarchy-tree.
34+
* Finds any field in a class with an ID-Annotation ({@link jakarta.persistence.Id},
35+
* {@link org.springframework.data.annotation.Id} or {@link jakarta.persistence.EmbeddedId}). Finds this field
36+
* recursively in the Hierarchy-tree.
3637
*
3738
* @return field with ID-Annotation. Is {@link Optional#empty()} if no field was found.
3839
*/
3940
public static Optional<Field> findIdField(final Class<?> domainClass)
4041
{
4142
return findAnnotatedField(
4243
domainClass,
43-
List.of(jakarta.persistence.Id.class, org.springframework.data.annotation.Id.class)
44+
List.of(
45+
jakarta.persistence.Id.class,
46+
org.springframework.data.annotation.Id.class,
47+
jakarta.persistence.EmbeddedId.class)
4448
);
4549
}
4650

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/id/custom/CustomIdTest.java

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom;
1717

18+
import java.time.LocalDate;
19+
import java.util.List;
1820
import java.util.Optional;
1921
import java.util.stream.Stream;
2022

@@ -31,8 +33,15 @@
3133
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
3234
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.IdTestConfiguration;
3335
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CompositeKey;
36+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CompositeKeyAsRecord;
3437
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKey;
38+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyAsRecord;
39+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyAsRecordRepository;
40+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyEmbeddedId;
41+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyEmbeddedIdRepository;
3542
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyRepository;
43+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdLocalDate;
44+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdLocalDateRepository;
3645
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
3746

3847

@@ -49,6 +58,24 @@ public static Stream<Arguments> generateData()
4958
context -> context.getBean(CustomerWithIdCompositeKeyRepository.class),
5059
() -> new CompositeKey(1, 1),
5160
() -> new CompositeKey(2, 2)
61+
).toArguments(),
62+
new SingleTestDataset<>(
63+
id -> new CustomerWithIdCompositeKeyEmbeddedId(id, TestData.FIRST_NAME),
64+
context -> context.getBean(CustomerWithIdCompositeKeyEmbeddedIdRepository.class),
65+
() -> new CompositeKey(1, 1),
66+
() -> new CompositeKey(2, 2)
67+
).toArguments(),
68+
new SingleTestDataset<>(
69+
id -> new CustomerWithIdCompositeKeyAsRecord(id, TestData.FIRST_NAME),
70+
context -> context.getBean(CustomerWithIdCompositeKeyAsRecordRepository.class),
71+
() -> new CompositeKeyAsRecord(1, 1),
72+
() -> new CompositeKeyAsRecord(2, 2)
73+
).toArguments(),
74+
new SingleTestDataset<>(
75+
id -> new CustomerWithIdLocalDate(id, TestData.FIRST_NAME),
76+
context -> context.getBean(CustomerWithIdLocalDateRepository.class),
77+
() -> LocalDate.of(2024, 1, 1),
78+
() -> LocalDate.of(2024, 1, 2)
5279
).toArguments()
5380
);
5481
}
@@ -63,7 +90,7 @@ public CustomIdTest(final IdTestConfiguration configuration)
6390

6491
@ParameterizedTest
6592
@MethodSource("generateData")
66-
<T, ID> void createSingleWithAutoIdInteger(
93+
<T, ID> void createSingleWithCustomKey(
6794
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
6895
{
6996
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
@@ -80,4 +107,94 @@ <T, ID> void createSingleWithAutoIdInteger(
80107
}
81108
);
82109
}
110+
111+
@ParameterizedTest
112+
@MethodSource("generateData")
113+
<T, ID> void createDoubleWithCustomKey(
114+
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
115+
{
116+
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
117+
118+
final T customer1 = data.enitityGenerator().apply(data.firstIdSupplier().get());
119+
repository.save(customer1);
120+
121+
final T customer2 = data.enitityGenerator().apply(data.secondIdSupplier().get());
122+
repository.save(customer2);
123+
124+
TestUtil.doBeforeAndAfterRestartOfDatastore(
125+
this.configuration,
126+
() -> {
127+
Assertions.assertEquals(2, repository.findAll().size());
128+
Assertions.assertEquals(
129+
2,
130+
repository.findAllById(List.of(data.firstIdSupplier().get(), data.secondIdSupplier().get()))
131+
.size());
132+
133+
final Optional<T> loadedCustomer1 = repository.findById(data.firstIdSupplier().get());
134+
Assertions.assertTrue(loadedCustomer1.isPresent());
135+
Assertions.assertEquals(customer1, loadedCustomer1.get());
136+
137+
final Optional<T> loadedCustomer2 = repository.findById(data.secondIdSupplier().get());
138+
Assertions.assertTrue(loadedCustomer2.isPresent());
139+
Assertions.assertEquals(customer2, loadedCustomer2.get());
140+
}
141+
);
142+
}
143+
144+
@ParameterizedTest
145+
@MethodSource("generateData")
146+
<T, ID> void createNullWithCustomKey(
147+
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
148+
{
149+
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
150+
151+
final T customer = data.enitityGenerator().apply(null);
152+
Assertions.assertThrows(IllegalArgumentException.class, () -> repository.save(customer));
153+
}
154+
155+
@ParameterizedTest
156+
@MethodSource("generateData")
157+
<T, ID> void deleteBeforeRestartWithCustomKey(
158+
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
159+
{
160+
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
161+
162+
final T customer = data.enitityGenerator().apply(data.firstIdSupplier().get());
163+
repository.save(customer);
164+
165+
repository.deleteById(data.firstIdSupplier().get());
166+
167+
TestUtil.doBeforeAndAfterRestartOfDatastore(
168+
this.configuration,
169+
() -> {
170+
Assertions.assertTrue(repository.findAll().isEmpty());
171+
final Optional<T> loadedCustomer = repository.findById(data.firstIdSupplier().get());
172+
Assertions.assertFalse(loadedCustomer.isPresent());
173+
}
174+
);
175+
}
176+
177+
@ParameterizedTest
178+
@MethodSource("generateData")
179+
<T, ID> void deleteAfterRestartWithCustomKey(
180+
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
181+
{
182+
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
183+
184+
final T customer = data.enitityGenerator().apply(data.firstIdSupplier().get());
185+
repository.save(customer);
186+
187+
TestUtil.restartDatastore(this.configuration);
188+
189+
repository.deleteById(data.firstIdSupplier().get());
190+
191+
TestUtil.doBeforeAndAfterRestartOfDatastore(
192+
this.configuration,
193+
() -> {
194+
Assertions.assertTrue(repository.findAll().isEmpty());
195+
final Optional<T> loadedCustomer = repository.findById(data.firstIdSupplier().get());
196+
Assertions.assertFalse(loadedCustomer.isPresent());
197+
}
198+
);
199+
}
83200
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/id/custom/model/CompositeKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
22

3+
import java.io.Serializable;
34
import java.util.Objects;
45

56

6-
public class CompositeKey
7+
public class CompositeKey implements Serializable
78
{
89
private final int idPart1;
910
private final int idPart2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
17+
18+
import java.util.Objects;
19+
20+
import jakarta.persistence.Id;
21+
22+
23+
public class CustomerWithIdCompositeKeyAsRecord
24+
{
25+
@Id
26+
private CompositeKeyAsRecord id;
27+
28+
private final String firstName;
29+
30+
public CustomerWithIdCompositeKeyAsRecord(final CompositeKeyAsRecord id, final String firstName)
31+
{
32+
this.id = id;
33+
this.firstName = firstName;
34+
}
35+
36+
public String getFirstName()
37+
{
38+
return this.firstName;
39+
}
40+
41+
@Override
42+
public String toString()
43+
{
44+
return String.format(
45+
"Customer[firstName='%s']",
46+
this.firstName);
47+
}
48+
49+
public CompositeKeyAsRecord getId()
50+
{
51+
return this.id;
52+
}
53+
54+
@Override
55+
public boolean equals(final Object o)
56+
{
57+
if(this == o)
58+
{
59+
return true;
60+
}
61+
if(o == null || this.getClass() != o.getClass())
62+
{
63+
return false;
64+
}
65+
final CustomerWithIdCompositeKeyAsRecord customer = (CustomerWithIdCompositeKeyAsRecord)o;
66+
return Objects.equals(this.firstName, customer.firstName);
67+
}
68+
69+
@Override
70+
public int hashCode()
71+
{
72+
return Objects.hash(this.firstName);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
17+
18+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
19+
20+
21+
public interface CustomerWithIdCompositeKeyAsRecordRepository
22+
extends EclipseStoreRepository<CustomerWithIdCompositeKeyAsRecord, CompositeKeyAsRecord>
23+
{
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
17+
18+
import java.util.Objects;
19+
20+
import jakarta.persistence.EmbeddedId;
21+
22+
23+
public class CustomerWithIdCompositeKeyEmbeddedId
24+
{
25+
@EmbeddedId
26+
private final CompositeKey id;
27+
28+
private final String firstName;
29+
30+
public CustomerWithIdCompositeKeyEmbeddedId(final CompositeKey id, final String firstName)
31+
{
32+
this.id = id;
33+
this.firstName = firstName;
34+
}
35+
36+
public String getFirstName()
37+
{
38+
return this.firstName;
39+
}
40+
41+
@Override
42+
public String toString()
43+
{
44+
return String.format(
45+
"Customer[firstName='%s']",
46+
this.firstName);
47+
}
48+
49+
public CompositeKey getId()
50+
{
51+
return this.id;
52+
}
53+
54+
@Override
55+
public boolean equals(final Object o)
56+
{
57+
if(this == o)
58+
{
59+
return true;
60+
}
61+
if(o == null || this.getClass() != o.getClass())
62+
{
63+
return false;
64+
}
65+
final CustomerWithIdCompositeKeyEmbeddedId customer = (CustomerWithIdCompositeKeyEmbeddedId)o;
66+
return Objects.equals(this.firstName, customer.firstName);
67+
}
68+
69+
@Override
70+
public int hashCode()
71+
{
72+
return Objects.hash(this.firstName);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
17+
18+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
19+
20+
21+
public interface CustomerWithIdCompositeKeyEmbeddedIdRepository
22+
extends EclipseStoreRepository<CustomerWithIdCompositeKeyEmbeddedId, CompositeKey>
23+
{
24+
}

0 commit comments

Comments
 (0)