From 335bde55c68a40aca18ffc962a2fe88aebee2b77 Mon Sep 17 00:00:00 2001 From: Thomas Sapelza Date: Fri, 29 Aug 2025 14:51:32 +0200 Subject: [PATCH 1/2] add missing enum tests --- .../EntityIdBindingsForControllerTest.java | 49 +++++++++++++++++++ .../mvc/body/BodyWithEnumEntityId.java | 8 +++ .../controller/EntityIdTestController.java | 17 +++++++ .../persistence/EntityIdJpaTest.java | 29 +++++++++++ .../impl/jpa/CustomTypeEnumTestModel.java | 42 ++++++++++++++++ .../CustomTypeEnumTestModelRepository.java | 6 +++ ...-create-custom-type-enum-testing-table.yml | 14 ++++++ src/test/resources/db/changelog/master.yml | 3 ++ 8 files changed, 168 insertions(+) create mode 100644 src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/body/BodyWithEnumEntityId.java create mode 100644 src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModel.java create mode 100644 src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModelRepository.java create mode 100644 src/test/resources/db/changelog/2025-08-29-create-custom-type-enum-testing-table.yml diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java index 2040a41..11c9885 100644 --- a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java @@ -3,8 +3,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import it.aboutbits.springboot.toolbox._support.HttpTest; import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId; +import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId; +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -14,6 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; +@Slf4j @HttpTest public class EntityIdBindingsForControllerTest { @Autowired @@ -67,6 +71,51 @@ void emailAddressAsBody() throws Exception { } } + @Nested + class EnumEntityId { + @Test + void emailAddressAsPathVariable() throws Exception { + var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_FIRST); + + var resultAsString = performGetAndReturnResult( + String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-path-variable/%s", value) + ); + + var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class); + + assertThat(actual).isEqualTo(value); + } + + @Test + void emailAddressAsRequestParameter() throws Exception { + var value = new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_OTHER); + + var resultAsString = performGetAndReturnResult( + String.format("/test/entity-id/CustomTypeEnumTestModel.ID/as-request-parameter?value=%s", value) + ); + + var actual = objectMapper.readValue(resultAsString, CustomTypeEnumTestModel.ID.class); + + assertThat(actual).isEqualTo(value); + } + + @Test + void emailAddressAsBody() throws Exception { + var value = new BodyWithEnumEntityId( + new CustomTypeEnumTestModel.ID(CustomTypeEnumTestModel.CustomTypeEnum.ENUM_LAST) + ); + + var resultAsString = performPostAndReturnResult( + "/test/entity-id/CustomTypeEnumTestModel.ID/as-body", + value + ); + + var actual = objectMapper.readValue(resultAsString, BodyWithEnumEntityId.class); + + assertThat(actual).isEqualTo(value); + } + } + private @NonNull String performGetAndReturnResult(@NonNull String url) throws Exception { var requestBuilder = MockMvcRequestBuilders.get(url) .contentType(MediaType.APPLICATION_JSON); diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/body/BodyWithEnumEntityId.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/body/BodyWithEnumEntityId.java new file mode 100644 index 0000000..8ffba20 --- /dev/null +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/body/BodyWithEnumEntityId.java @@ -0,0 +1,8 @@ +package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body; + +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel; + +public record BodyWithEnumEntityId( + CustomTypeEnumTestModel.ID enumEntityId +) { +} diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/controller/EntityIdTestController.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/controller/EntityIdTestController.java index abd6ccc..0cdc542 100644 --- a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/controller/EntityIdTestController.java +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/controller/EntityIdTestController.java @@ -1,6 +1,8 @@ package it.aboutbits.springboot.toolbox.autoconfiguration.mvc.controller; import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEntityId; +import it.aboutbits.springboot.toolbox.autoconfiguration.mvc.body.BodyWithEnumEntityId; +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -27,4 +29,19 @@ public CustomTypeTestModel.ID customTypeTestModelIdAsRequestParameter(@RequestPa public BodyWithEntityId customTypeTestModelIdAsBody(@RequestBody BodyWithEntityId value) { return value; } + + @GetMapping("/CustomTypeEnumTestModel.ID/as-path-variable/{value}") + public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsPathVariable(@PathVariable CustomTypeEnumTestModel.ID value) { + return value; + } + + @GetMapping("/CustomTypeEnumTestModel.ID/as-request-parameter") + public CustomTypeEnumTestModel.ID customTypeEnumTestModelIdAsRequestParameter(@RequestParam CustomTypeEnumTestModel.ID value) { + return value; + } + + @PostMapping("/CustomTypeEnumTestModel.ID/as-body") + public BodyWithEnumEntityId customTypeEnumTestModelIdAsBody(@RequestBody BodyWithEnumEntityId value) { + return value; + } } diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/EntityIdJpaTest.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/EntityIdJpaTest.java index 72f5ff9..fb214cb 100644 --- a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/EntityIdJpaTest.java +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/EntityIdJpaTest.java @@ -2,6 +2,8 @@ import it.aboutbits.springboot.toolbox._support.ApplicationTest; import it.aboutbits.springboot.toolbox._support.WithPersistence; +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel; +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModelRepository; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModelRepository; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.ReferencedTestModel; @@ -9,6 +11,8 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Random; + import static org.assertj.core.api.Assertions.assertThat; @ApplicationTest @@ -17,6 +21,9 @@ public class EntityIdJpaTest { @Autowired CustomTypeTestModelRepository repository; + @Autowired + CustomTypeEnumTestModelRepository repositoryEnum; + @Nested class OwnId { @Test @@ -51,4 +58,26 @@ void inAndOut_shouldSucceed() { .isEqualTo(savedItem); } } + + @Nested + class EnumId { + @Test + void inAndOut_shouldSucceed() { + var values = CustomTypeEnumTestModel.CustomTypeEnum.values(); + + var item = new CustomTypeEnumTestModel(); + item.setId(new CustomTypeEnumTestModel.ID( + values[new Random().nextInt(values.length)] + )); + + var savedItem = repositoryEnum.save(item); + + var retrievedItem = repositoryEnum.findById(savedItem.getId()); + + assertThat(retrievedItem).isPresent() + .get() + .usingRecursiveComparison() + .isEqualTo(savedItem); + } + } } diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModel.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModel.java new file mode 100644 index 0000000..3ddf7ee --- /dev/null +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModel.java @@ -0,0 +1,42 @@ +package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa; + +import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.AutoRegisteredJavaType; +import it.aboutbits.springboot.toolbox.persistence.javatype.base.WrappedEnumJavaType; +import it.aboutbits.springboot.toolbox.type.identity.EntityId; +import it.aboutbits.springboot.toolbox.type.identity.Identified; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.JavaType; + +@Entity +@Getter +@Setter +@Table(name = "custom_type_enum_test_model") +public class CustomTypeEnumTestModel implements Identified { + @Id + @JavaType(CustomTypeEnumTestModel.ID.JavaType.class) + private ID id; + + public enum CustomTypeEnum { + ENUM_FIRST, ENUM_OTHER, ENUM_LAST + } + + public record ID( + CustomTypeEnum value + ) implements EntityId { + + @Override + public String toString() { + return value().name(); + } + + public static class JavaType extends WrappedEnumJavaType implements AutoRegisteredJavaType { + public JavaType() { + super(ID.class); + } + } + } +} diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModelRepository.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModelRepository.java new file mode 100644 index 0000000..eb2fb33 --- /dev/null +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/persistence/impl/jpa/CustomTypeEnumTestModelRepository.java @@ -0,0 +1,6 @@ +package it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CustomTypeEnumTestModelRepository extends JpaRepository { +} diff --git a/src/test/resources/db/changelog/2025-08-29-create-custom-type-enum-testing-table.yml b/src/test/resources/db/changelog/2025-08-29-create-custom-type-enum-testing-table.yml new file mode 100644 index 0000000..287e54e --- /dev/null +++ b/src/test/resources/db/changelog/2025-08-29-create-custom-type-enum-testing-table.yml @@ -0,0 +1,14 @@ +databaseChangeLog: + - changeSet: + author: Thomas Sapelza + id: 2025-08-29-create-custom-type-enum-testing-table + changes: + - createTable: + tableName: custom_type_enum_test_model + columns: + - column: + name: id + type: text + constraints: + nullable: false + primaryKey: true diff --git a/src/test/resources/db/changelog/master.yml b/src/test/resources/db/changelog/master.yml index 151d81c..cdfe892 100644 --- a/src/test/resources/db/changelog/master.yml +++ b/src/test/resources/db/changelog/master.yml @@ -8,3 +8,6 @@ databaseChangeLog: - include: file: 2024-09-26-create-query-transformer-testing-table.yml relativeToChangelogFile: true + - include: + file: 2025-08-29-create-custom-type-enum-testing-table.yml + relativeToChangelogFile: true From 7337a9c58751b12c1a7277ad0c5ba1c9e92c7860 Mon Sep 17 00:00:00 2001 From: Thomas Sapelza Date: Fri, 29 Aug 2025 14:53:22 +0200 Subject: [PATCH 2/2] remove unused Slf4j logger --- .../toolbox/autoconfiguration/web/CustomTypeConfiguration.java | 2 -- .../CustomTypePropertyCustomizerForCodeGenerator.java | 2 -- .../mvc/EntityIdBindingsForControllerTest.java | 2 -- 3 files changed, 6 deletions(-) diff --git a/src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java b/src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java index 516de30..309d363 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java @@ -4,7 +4,6 @@ import it.aboutbits.springboot.toolbox.jackson.CustomTypeSerializer; import it.aboutbits.springboot.toolbox.type.CustomType; import it.aboutbits.springboot.toolbox.web.CustomTypePropertyEditor; -import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -14,7 +13,6 @@ import java.util.Set; -@Slf4j @Configuration public class CustomTypeConfiguration { @ControllerAdvice diff --git a/src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypePropertyCustomizerForCodeGenerator.java b/src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypePropertyCustomizerForCodeGenerator.java index 180f850..283d7c3 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypePropertyCustomizerForCodeGenerator.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypePropertyCustomizerForCodeGenerator.java @@ -4,10 +4,8 @@ import io.swagger.v3.core.converter.AnnotatedType; import io.swagger.v3.oas.models.media.Schema; import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil; -import lombok.extern.slf4j.Slf4j; import org.springdoc.core.customizers.PropertyCustomizer; -@Slf4j public class CustomTypePropertyCustomizerForCodeGenerator implements PropertyCustomizer { @Override public Schema customize(Schema property, AnnotatedType annotatedType) { diff --git a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java index 11c9885..6d243b9 100644 --- a/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java +++ b/src/test/java/it/aboutbits/springboot/toolbox/autoconfiguration/mvc/EntityIdBindingsForControllerTest.java @@ -7,7 +7,6 @@ import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeEnumTestModel; import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel; import lombok.NonNull; -import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -17,7 +16,6 @@ import static org.assertj.core.api.Assertions.assertThat; -@Slf4j @HttpTest public class EntityIdBindingsForControllerTest { @Autowired