|
| 1 | +package org.openapitools.client; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 7 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.openapitools.client.model.TestInlineFreeformAdditionalPropertiesRequest; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +class JacksonTest { |
| 15 | + |
| 16 | + private ObjectMapper mapper; |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + void setUp() { |
| 20 | + mapper = JsonMapper.builder() |
| 21 | + // For determinist serialization results |
| 22 | + .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) |
| 23 | + .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) |
| 24 | + .build(); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testSerializeAdditionalProperties() throws JsonProcessingException { |
| 29 | + // Given |
| 30 | + TestInlineFreeformAdditionalPropertiesRequest model = new TestInlineFreeformAdditionalPropertiesRequest("value") |
| 31 | + .putAdditionalProperty("someString", "someValue") |
| 32 | + .putAdditionalProperty("someNumber", 1.23) |
| 33 | + .putAdditionalProperty("someBoolean", true); |
| 34 | + |
| 35 | + // When |
| 36 | + String string = mapper.writeValueAsString(model); |
| 37 | + |
| 38 | + // Then |
| 39 | + String expectedString = "{\"someProperty\":\"value\",\"someBoolean\":true,\"someNumber\":1.23,\"someString\":\"someValue\"}"; |
| 40 | + assertEquals(expectedString, string); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testDeserializeAdditionalProperties() throws JsonProcessingException { |
| 45 | + // Given |
| 46 | + String string = "{\"someProperty\":\"value\",\"someBoolean\":true,\"someNumber\":1.23,\"someString\":\"someValue\"}"; |
| 47 | + |
| 48 | + // When |
| 49 | + TestInlineFreeformAdditionalPropertiesRequest model = mapper.readValue( |
| 50 | + string, |
| 51 | + TestInlineFreeformAdditionalPropertiesRequest.class |
| 52 | + ); |
| 53 | + |
| 54 | + // Then |
| 55 | + TestInlineFreeformAdditionalPropertiesRequest expectedModel = new TestInlineFreeformAdditionalPropertiesRequest("value") |
| 56 | + .putAdditionalProperty("someString", "someValue") |
| 57 | + .putAdditionalProperty("someNumber", 1.23) |
| 58 | + .putAdditionalProperty("someBoolean", true); |
| 59 | + assertEquals(expectedModel, model); |
| 60 | + |
| 61 | + TestInlineFreeformAdditionalPropertiesRequest invalidModel = new TestInlineFreeformAdditionalPropertiesRequest("value"); |
| 62 | + assertNotEquals(invalidModel, model); |
| 63 | + } |
| 64 | +} |
0 commit comments