Skip to content

Commit

Permalink
Add native test for verifying JsonSubTypes.Type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 3, 2024
1 parent a110cea commit d7c765c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.it.jackson;

import java.util.List;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.it.jackson.model.Elephant;
import io.quarkus.it.jackson.model.MammalFamily;
import io.quarkus.it.jackson.model.Whale;

@Path("jsonSubTypes")
public class JsonSubTypesResource {

private final ObjectMapper objectMapper;

public JsonSubTypesResource(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@GET
public String test() throws JsonProcessingException {
return objectMapper.writeValueAsString(new MammalFamily(List.of(
new Whale(30.0, "white"), new Elephant(10, "africa"))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.it.jackson.model;

public record Elephant(
int hornLength, String continent) implements Mammal {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.it.jackson.model;

import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY;
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = NAME, include = PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = Elephant.class, name = "Elephant"),
@JsonSubTypes.Type(value = Whale.class, name = "Whale"),
})
public interface Mammal {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.it.jackson.model;

import java.util.Collection;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public record MammalFamily(Collection<Mammal> mammals) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.it.jackson.model;

public record Whale(
double swimSpeed, String color) implements Mammal {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.jackson;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class JsonSubTypesResourceIT extends JsonSubTypesResourceTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.it.jackson;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

@QuarkusTest
class JsonSubTypesResourceTest {

@Test
void test() {
Response response = given()
.accept(ContentType.JSON)
.when()
.get("jsonSubTypes")
.then()
.statusCode(200)
.extract().response();
assertThat(response.jsonPath().getString("mammals[0].color")).isEqualTo("white");
assertThat(response.jsonPath().getString("mammals[1].continent")).isEqualTo("africa");
}
}

0 comments on commit d7c765c

Please sign in to comment.