Skip to content

Commit 335bde5

Browse files
committed
add missing enum tests
1 parent 5b77da0 commit 335bde5

8 files changed

Lines changed: 168 additions & 0 deletions

File tree

src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import it.aboutbits.springboot.toolbox._support.HttpTest;
55
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId;
6+
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId;
7+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
68
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
79
import lombok.NonNull;
10+
import lombok.extern.slf4j.Slf4j;
811
import org.junit.jupiter.api.Nested;
912
import org.junit.jupiter.api.Test;
1013
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,6 +17,7 @@
1417

1518
import static org.assertj.core.api.Assertions.assertThat;
1619

20+
@Slf4j
1721
@HttpTest
1822
public class EntityIdBindingsForControllerTest {
1923
@Autowired
@@ -67,6 +71,51 @@ void emailAddressAsBody() throws Exception {
6771
}
6872
}
6973

74+
@Nested
75+
class EnumEntityId {
76+
@Test
77+
void emailAddressAsPathVariable() throws Exception {
78+
var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_FIRST);
79+
80+
var resultAsString = performGetAndReturnResult(
81+
String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-path-variable/%s", value)
82+
);
83+
84+
var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class);
85+
86+
assertThat(actual).isEqualTo(value);
87+
}
88+
89+
@Test
90+
void emailAddressAsRequestParameter() throws Exception {
91+
var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_OTHER);
92+
93+
var resultAsString = performGetAndReturnResult(
94+
String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-request-parameter?value=%s", value)
95+
);
96+
97+
var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class);
98+
99+
assertThat(actual).isEqualTo(value);
100+
}
101+
102+
@Test
103+
void emailAddressAsBody() throws Exception {
104+
var value = new BodyWithEnumEntityId(
105+
new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_LAST)
106+
);
107+
108+
var resultAsString = performPostAndReturnResult(
109+
"/test/entity-id/CustomTypeEnumTestModel.ID/as-body",
110+
value
111+
);
112+
113+
var actual = objectMapper.readValue(resultAsString, BodyWithEnumEntityId.class);
114+
115+
assertThat(actual).isEqualTo(value);
116+
}
117+
}
118+
70119
private @NonNull String performGetAndReturnResult(@NonNull String url) throws Exception {
71120
var requestBuilder = MockMvcRequestBuilders.get(url)
72121
.contentType(MediaType.APPLICATION_JSON);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body;
2+
3+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
4+
5+
public record BodyWithEnumEntityId(
6+
CustomTypeEnumTestModel.ID enumEntityId
7+
) {
8+
}

src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/controller/EntityIdTestController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.controller;
22

33
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId;
4+
import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId;
5+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
46
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
57
import org.springframework.web.bind.annotation.GetMapping;
68
import org.springframework.web.bind.annotation.PathVariable;
@@ -27,4 +29,19 @@ public CustomTypeTestModel.ID customTypeTestModelIdAsRequestParameter(@RequestPa
2729
public BodyWithEntityId customTypeTestModelIdAsBody(@RequestBody BodyWithEntityId value) {
2830
return value;
2931
}
32+
33+
@GetMapping("/CustomTypeEnumTestModel.ID/as-path-variable/{value}")
34+
public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsPathVariable(@PathVariable CustomTypeEnumTestModel.ID value) {
35+
return value;
36+
}
37+
38+
@GetMapping("/CustomTypeEnumTestModel.ID/as-request-parameter")
39+
public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsRequestParameter(@RequestParam CustomTypeEnumTestModel.ID value) {
40+
return value;
41+
}
42+
43+
@PostMapping("/CustomTypeEnumTestModel.ID/as-body")
44+
public BodyWithEnumEntityId customTypeEnumTestModelIdAsBody(@RequestBody BodyWithEnumEntityId value) {
45+
return value;
46+
}
3047
}

src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/EntityIdJpaTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import it.aboutbits.springboot.toolbox._support.ApplicationTest;
44
import it.aboutbits.springboot.toolbox._support.WithPersistence;
5+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel;
6+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModelRepository;
57
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel;
68
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModelRepository;
79
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.ReferencedTestModel;
810
import org.junit.jupiter.api.Nested;
911
import org.junit.jupiter.api.Test;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113

14+
import java.util.Random;
15+
1216
import static org.assertj.core.api.Assertions.assertThat;
1317

1418
@ApplicationTest
@@ -17,6 +21,9 @@ public class EntityIdJpaTest {
1721
@Autowired
1822
CustomTypeTestModelRepository repository;
1923

24+
@Autowired
25+
CustomTypeEnumTestModelRepository repositoryEnum;
26+
2027
@Nested
2128
class OwnId {
2229
@Test
@@ -51,4 +58,26 @@ void inAndOut_shouldSucceed() {
5158
.isEqualTo(savedItem);
5259
}
5360
}
61+
62+
@Nested
63+
class EnumId {
64+
@Test
65+
void inAndOut_shouldSucceed() {
66+
var values = CustomTypeEnumTestModel.CustomTypeEnum.values();
67+
68+
var item = new CustomTypeEnumTestModel();
69+
item.setId(new CustomTypeEnumTestModel.ID(
70+
values[new Random().nextInt(values.length)]
71+
));
72+
73+
var savedItem = repositoryEnum.save(item);
74+
75+
var retrievedItem = repositoryEnum.findById(savedItem.getId());
76+
77+
assertThat(retrievedItem).isPresent()
78+
.get()
79+
.usingRecursiveComparison()
80+
.isEqualTo(savedItem);
81+
}
82+
}
5483
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa;
2+
3+
import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.AutoRegisteredJavaType;
4+
import it.aboutbits.springboot.toolbox.persistence.javatype.base.WrappedEnumJavaType;
5+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
6+
import it.aboutbits.springboot.toolbox.type.identity.Identified;
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Table;
10+
import lombok.Getter;
11+
import lombok.Setter;
12+
import org.hibernate.annotations.JavaType;
13+
14+
@Entity
15+
@Getter
16+
@Setter
17+
@Table(name = "custom_type_enum_test_model")
18+
public class CustomTypeEnumTestModel implements Identified<CustomTypeEnumTestModel.ID> {
19+
@Id
20+
@JavaType(CustomTypeEnumTestModel.ID.JavaType.class)
21+
private ID id;
22+
23+
public enum CustomTypeEnum {
24+
ENUM_FIRST, ENUM_OTHER, ENUM_LAST
25+
}
26+
27+
public record ID(
28+
CustomTypeEnum value
29+
) implements EntityId<CustomTypeEnum> {
30+
31+
@Override
32+
public String toString() {
33+
return value().name();
34+
}
35+
36+
public static class JavaType extends WrappedEnumJavaType<ID> implements AutoRegisteredJavaType<ID> {
37+
public JavaType() {
38+
super(ID.class);
39+
}
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface CustomTypeEnumTestModelRepository extends JpaRepository<CustomTypeEnumTestModel, CustomTypeEnumTestModel.ID> {
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
databaseChangeLog:
2+
- changeSet:
3+
author: Thomas Sapelza
4+
id: 2025-08-29-create-custom-type-enum-testing-table
5+
changes:
6+
- createTable:
7+
tableName: custom_type_enum_test_model
8+
columns:
9+
- column:
10+
name: id
11+
type: text
12+
constraints:
13+
nullable: false
14+
primaryKey: true

src/test/resources/db/changelog/master.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ databaseChangeLog:
88
- include:
99
file: 2024-09-26-create-query-transformer-testing-table.yml
1010
relativeToChangelogFile: true
11+
- include:
12+
file: 2025-08-29-create-custom-type-enum-testing-table.yml
13+
relativeToChangelogFile: true

0 commit comments

Comments
 (0)