From 37fef8811a10ab40b0456a5559e8f29fad825e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Brilhante?= Date: Sat, 30 Nov 2024 17:22:52 +0100 Subject: [PATCH] [java][resttemplate] Add test to ensure additional properties work --- bin/utils/test_file_list.yaml | 3 + .../org/openapitools/client/JacksonTest.java | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java diff --git a/bin/utils/test_file_list.yaml b/bin/utils/test_file_list.yaml index bdb057850e46..64614d68054f 100644 --- a/bin/utils/test_file_list.yaml +++ b/bin/utils/test_file_list.yaml @@ -15,3 +15,6 @@ sha256: 8b1b8f2a2ad00ccb090873a94a5f73e328b98317d2ec715f53bd7a1accb2a023 - filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java" sha256: a1f8a70bf7b0c382a8def5bacf7b1fb189e687fabb40235aa799001e0597f545 +# java resttemplate test files +- filename: "samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java" + sha256: 346af62abe32357474f0da870f75fbc1b765b0efe64eee40ddfc0f0d6fd43a97 diff --git a/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java b/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java new file mode 100644 index 000000000000..fccc9f23f716 --- /dev/null +++ b/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java @@ -0,0 +1,64 @@ +package org.openapitools.client; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openapitools.client.model.TestInlineFreeformAdditionalPropertiesRequest; + +import static org.junit.jupiter.api.Assertions.*; + +class JacksonTest { + + private ObjectMapper mapper; + + @BeforeEach + void setUp() { + mapper = JsonMapper.builder() + // For determinist serialization results + .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) + .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) + .build(); + } + + @Test + void testSerializeAdditionalProperties() throws JsonProcessingException { + // Given + TestInlineFreeformAdditionalPropertiesRequest model = new TestInlineFreeformAdditionalPropertiesRequest("value") + .putAdditionalProperty("someString", "someValue") + .putAdditionalProperty("someNumber", 1.23) + .putAdditionalProperty("someBoolean", true); + + // When + String string = mapper.writeValueAsString(model); + + // Then + String expectedString = "{\"someProperty\":\"value\",\"someBoolean\":true,\"someNumber\":1.23,\"someString\":\"someValue\"}"; + assertEquals(expectedString, string); + } + + @Test + void testDeserializeAdditionalProperties() throws JsonProcessingException { + // Given + String string = "{\"someProperty\":\"value\",\"someBoolean\":true,\"someNumber\":1.23,\"someString\":\"someValue\"}"; + + // When + TestInlineFreeformAdditionalPropertiesRequest model = mapper.readValue( + string, + TestInlineFreeformAdditionalPropertiesRequest.class + ); + + // Then + TestInlineFreeformAdditionalPropertiesRequest expectedModel = new TestInlineFreeformAdditionalPropertiesRequest("value") + .putAdditionalProperty("someString", "someValue") + .putAdditionalProperty("someNumber", 1.23) + .putAdditionalProperty("someBoolean", true); + assertEquals(expectedModel, model); + + TestInlineFreeformAdditionalPropertiesRequest invalidModel = new TestInlineFreeformAdditionalPropertiesRequest("value"); + assertNotEquals(invalidModel, model); + } +}