-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[java][resttemplate] Add test to ensure additional properties work
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
.../client/petstore/java/resttemplate/src/test/java/org/openapitools/client/JacksonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |