Skip to content

Commit

Permalink
[java][resttemplate] Add test to ensure additional properties work
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoBrlt committed Nov 30, 2024
1 parent 620b368 commit 37fef88
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bin/utils/test_file_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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);
}
}

0 comments on commit 37fef88

Please sign in to comment.