|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 4 | import it.aboutbits.springboot.toolbox._support.HttpTest; |
5 | 5 | 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; |
6 | 8 | import it.aboutbits.springboot.toolbox.autoconfiguration.persistence.impl.jpa.CustomTypeTestModel; |
7 | 9 | import lombok.NonNull; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
8 | 11 | import org.junit.jupiter.api.Nested; |
9 | 12 | import org.junit.jupiter.api.Test; |
10 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
|
14 | 17 |
|
15 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
16 | 19 |
|
| 20 | +@Slf4j |
17 | 21 | @HttpTest |
18 | 22 | public class EntityIdBindingsForControllerTest { |
19 | 23 | @Autowired |
@@ -67,6 +71,51 @@ void emailAddressAsBody() throws Exception { |
67 | 71 | } |
68 | 72 | } |
69 | 73 |
|
| 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 | + |
70 | 119 | private @NonNull String performGetAndReturnResult(@NonNull String url) throws Exception { |
71 | 120 | var requestBuilder = MockMvcRequestBuilders.get(url) |
72 | 121 | .contentType(MediaType.APPLICATION_JSON); |
|
0 commit comments