|
| 1 | +/* |
| 2 | + * Copyright 2019 The original author or authors |
| 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 io.soabase.recordbuilder.test; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; |
| 21 | +import io.soabase.recordbuilder.test.JacksonAnnotated.JacksonAnnotatedRecord; |
| 22 | +import io.soabase.recordbuilder.test.JacksonAnnotated.JacksonAnnotatedRecordCustomSetterPrefix; |
| 23 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 34 | + |
| 35 | +class TestJacksonAnnotations { |
| 36 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 37 | + |
| 38 | + @ParameterizedTest |
| 39 | + @MethodSource("recordBuilders") |
| 40 | + void addsJsonPOJOBuilderAnnotation(Class<? extends JacksonAnnotated> type, String expectedPrefix) { |
| 41 | + final var annotations = Arrays.stream(type.getAnnotations()).toList(); |
| 42 | + assertThat(annotations).filteredOn(annotation -> annotation.annotationType().equals(JsonPOJOBuilder.class)) |
| 43 | + .hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(JsonPOJOBuilder.class)) |
| 44 | + .satisfies(annotation -> { |
| 45 | + assertThat(annotation.withPrefix()).isEqualTo(expectedPrefix); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + static Stream<Arguments> recordBuilders() { |
| 50 | + return Stream.of(arguments(JacksonAnnotatedRecordBuilder.class, ""), |
| 51 | + arguments(JacksonAnnotatedRecordCustomSetterPrefixBuilder.class, "set")); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @ValueSource(classes = { JacksonAnnotatedRecord.class, JacksonAnnotatedRecordCustomSetterPrefix.class }) |
| 56 | + void deserializingModelInvokesBuilder(Class<? extends JacksonAnnotated> type) throws JsonProcessingException { |
| 57 | + final var json = """ |
| 58 | + { |
| 59 | + "name" : "test" |
| 60 | + } |
| 61 | + """; |
| 62 | + |
| 63 | + final var model = objectMapper.readValue(json, type); |
| 64 | + assertThat(model.name()).isEqualTo("test"); |
| 65 | + assertThat(model.type()).isEqualTo("dummy"); // default value |
| 66 | + assertThat(model.properties()).isNotNull().isEmpty(); // non-null initialized immutable collection |
| 67 | + } |
| 68 | +} |
0 commit comments