Skip to content

Commit

Permalink
Add JSON serialization scenario
Browse files Browse the repository at this point in the history
Part of test development to QUARKUS-1075.
Covers one of the advanced topics of the test plan.
https://github.com/quarkus-qe/quarkus-test-plans/blob/main/QUARKUS-1075.md
  • Loading branch information
jsmrcka authored and Sgitario committed Sep 6, 2021
1 parent 44b675e commit 1cb932d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ RESTEasy Reactive equivalent of `http/jaxrs`. Tests simple and multipart endpoin
Additional coverage:
- Execution model (blocking vs. non-blocking) of endpoints based on method signature.
- HTTP Caching features.
- Advanced JSON serialization.

### `http/rest-client`
Verifies Rest Client configuration using `quarkus-rest-client-jaxb` (XML support) and `quarkus-rest-client-jsonb` (JSON support).
Expand Down
2 changes: 1 addition & 1 deletion http/jaxrs-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.ts.jaxrs.reactive.json;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.fasterxml.jackson.annotation.JsonView;

import io.quarkus.resteasy.reactive.jackson.CustomSerialization;

@Path("/json")
public class JsonResource {
public static final Integer USER_ID = 1;
public static final String USER_NAME = "testUser";

@JsonView(Views.Public.class)
@GET
@Path("/public")
public User userPublic() {
return testUser();
}

@JsonView(Views.Private.class)
@GET
@Path("/private")
public User userPrivate() {
return testUser();
}

@CustomSerialization(UnquotedFields.class)
@GET
@Path("/custom")
public User userCustom() {
return testUser();
}

private User testUser() {
User user = new User();
user.id = USER_ID;
user.name = USER_NAME;
return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.ts.jaxrs.reactive.json;

import java.lang.reflect.Type;
import java.util.function.BiFunction;

import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class UnquotedFields implements BiFunction<ObjectMapper, Type, ObjectWriter> {
@Override
public ObjectWriter apply(ObjectMapper objectMapper, Type type) {
return objectMapper.writer().without(JsonWriteFeature.QUOTE_FIELD_NAMES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.ts.jaxrs.reactive.json;

import com.fasterxml.jackson.annotation.JsonView;

public class User {

@JsonView(Views.Private.class)
public Integer id;

@JsonView(Views.Public.class)
public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.ts.jaxrs.reactive.json;

public class Views {

public static class Public {
}

public static class Private extends Public {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.ts.jaxrs.reactive.json;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.scenarios.QuarkusScenario;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;

@Tag("QUARKUS-1075")
@QuarkusScenario
public class JsonResourceIT {
private static final String BASE_PATH = "/json";
private static final String CUSTOM_JSON = String.format("{id:%d,name:\"%s\"}", JsonResource.USER_ID,
JsonResource.USER_NAME);

@Test
public void shouldGetUserPublic() {
final User user = getUser("/public");
assertNull(user.id);
}

@Test
public void shouldGetUserPrivate() {
final User user = getUser("/private");
assertNotNull(user.id);
}

@Test
public void shouldGetCustomJson() {
final String stringBody = whenGet("/custom").extract().body().asString();
assertEquals(CUSTOM_JSON, stringBody);
}

private User getUser(String path) {
final User user = whenGet(path).extract().body().as(User.class);
assertNotNull(user.name);
return user;
}

private ValidatableResponse whenGet(String path) {
return given()
.get(BASE_PATH + path)
.then()
.statusCode(HttpStatus.SC_OK)
.contentType(ContentType.JSON);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.ts.jaxrs.reactive.json;

import io.quarkus.test.scenarios.OpenShiftScenario;

@OpenShiftScenario
public class OpenShiftJsonResourceIT extends JsonResourceIT {
}

0 comments on commit 1cb932d

Please sign in to comment.