Skip to content

Commit ee3d3ee

Browse files
Simplest test with Custom Class as id
1 parent 851ff87 commit ee3d3ee

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
17+
18+
import java.util.Optional;
19+
import java.util.stream.Stream;
20+
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.Arguments;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.test.context.ContextConfiguration;
28+
29+
import software.xdev.spring.data.eclipse.store.helper.TestData;
30+
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
31+
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
32+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.IdTestConfiguration;
33+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CompositeKey;
34+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKey;
35+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model.CustomerWithIdCompositeKeyRepository;
36+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
37+
38+
39+
@SuppressWarnings("OptionalGetWithoutIsPresent")
40+
@IsolatedTestAnnotations
41+
@ContextConfiguration(classes = {IdTestConfiguration.class})
42+
class CustomIdTest
43+
{
44+
public static Stream<Arguments> generateData()
45+
{
46+
return Stream.of(
47+
new SingleTestDataset<>(
48+
id -> new CustomerWithIdCompositeKey(id, TestData.FIRST_NAME),
49+
context -> context.getBean(CustomerWithIdCompositeKeyRepository.class),
50+
() -> new CompositeKey(1, 1),
51+
() -> new CompositeKey(2, 2)
52+
).toArguments()
53+
);
54+
}
55+
56+
private final IdTestConfiguration configuration;
57+
58+
@Autowired
59+
public CustomIdTest(final IdTestConfiguration configuration)
60+
{
61+
this.configuration = configuration;
62+
}
63+
64+
@ParameterizedTest
65+
@MethodSource("generateData")
66+
<T, ID> void createSingleWithAutoIdInteger(
67+
final SingleTestDataset<T, ID> data, @Autowired final ApplicationContext context)
68+
{
69+
final EclipseStoreRepository<T, ID> repository = data.repositoryGenerator().apply(context);
70+
71+
final T customer = data.enitityGenerator().apply(data.firstIdSupplier().get());
72+
repository.save(customer);
73+
74+
TestUtil.doBeforeAndAfterRestartOfDatastore(
75+
this.configuration,
76+
() -> {
77+
final Optional<T> loadedCustomer = repository.findById(data.firstIdSupplier().get());
78+
Assertions.assertTrue(loadedCustomer.isPresent());
79+
Assertions.assertEquals(customer, loadedCustomer.get());
80+
}
81+
);
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
17+
18+
import java.util.function.Function;
19+
import java.util.function.Supplier;
20+
21+
import org.junit.jupiter.params.provider.Arguments;
22+
import org.springframework.context.ApplicationContext;
23+
24+
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository;
25+
26+
27+
public record SingleTestDataset<T, ID>(
28+
Function<ID, T> enitityGenerator,
29+
Function<ApplicationContext, EclipseStoreRepository<T, ID>> repositoryGenerator,
30+
Supplier<ID> firstIdSupplier,
31+
Supplier<ID> secondIdSupplier
32+
)
33+
{
34+
public Arguments toArguments()
35+
{
36+
return Arguments.of(this);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
2+
3+
import java.util.Objects;
4+
5+
6+
public class CompositeKey
7+
{
8+
private final int idPart1;
9+
private final int idPart2;
10+
11+
public CompositeKey(final int idPart1, final int idPart2)
12+
{
13+
this.idPart1 = idPart1;
14+
this.idPart2 = idPart2;
15+
}
16+
17+
@Override
18+
public boolean equals(final Object o)
19+
{
20+
if(this == o)
21+
{
22+
return true;
23+
}
24+
if(o == null || this.getClass() != o.getClass())
25+
{
26+
return false;
27+
}
28+
final CompositeKey that = (CompositeKey)o;
29+
return this.idPart1 == that.idPart1 && this.idPart2 == that.idPart2;
30+
}
31+
32+
@Override
33+
public int hashCode()
34+
{
35+
return Objects.hash(this.idPart1, this.idPart2);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.custom.model;
2+
3+
public record CompositeKeyAsRecord(int keyPart1, int keyPart2)
4+
{
5+
}
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 CustomerWithIdCompositeKey
24+
{
25+
@Id
26+
private CompositeKey id;
27+
28+
private final String firstName;
29+
30+
public CustomerWithIdCompositeKey(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 CustomerWithIdCompositeKey customer = (CustomerWithIdCompositeKey)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 CustomerWithIdCompositeKeyRepository
22+
extends EclipseStoreRepository<CustomerWithIdCompositeKey, CompositeKey>
23+
{
24+
}

0 commit comments

Comments
 (0)