From f8f23a26e48123c8dc7e2ae290c273e11afc505f Mon Sep 17 00:00:00 2001 From: jcarranzan Date: Wed, 17 Jul 2024 13:02:11 +0200 Subject: [PATCH] Add some tests --- .../http/advanced/reactive/FootballTeam.java | 34 ++++---- .../http/advanced/reactive/JsonPayloadIT.java | 78 +++++++++++++++++++ 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/http/http-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeam.java b/http/http-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeam.java index 30324b906..bb5f0e807 100644 --- a/http/http-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeam.java +++ b/http/http-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/FootballTeam.java @@ -4,9 +4,26 @@ import java.util.List; public class FootballTeam { + private String name; private String colorTShirt; private int euroCups; + private Date foundationDate; + private String stadium; + private List keyPlayers; + + public FootballTeam() { + } + + public FootballTeam(String name, String colorTShirt, int euroCups, Date foundationDate, String stadium, + List keyPlayers) { + this.name = name; + this.colorTShirt = colorTShirt; + this.euroCups = euroCups; + this.foundationDate = foundationDate; + this.stadium = stadium; + this.keyPlayers = keyPlayers; + } public Date getFoundationDate() { return foundationDate; @@ -32,13 +49,6 @@ public void setKeyPlayers(List keyPlayers) { this.keyPlayers = keyPlayers; } - private Date foundationDate; - private String stadium; - private List keyPlayers; - - public FootballTeam() { - } - public String getName() { return name; } @@ -59,16 +69,6 @@ public int getEuroCups() { return euroCups; } - public FootballTeam(String name, String colorTShirt, int euroCups, Date foundationDate, String stadium, - List keyPlayers) { - this.name = name; - this.colorTShirt = colorTShirt; - this.euroCups = euroCups; - this.foundationDate = foundationDate; - this.stadium = stadium; - this.keyPlayers = keyPlayers; - } - public void setEuroCups(int euroCups) { this.euroCups = euroCups; } diff --git a/http/http-advanced-reactive/src/test/java/io/quarkus/ts/http/advanced/reactive/JsonPayloadIT.java b/http/http-advanced-reactive/src/test/java/io/quarkus/ts/http/advanced/reactive/JsonPayloadIT.java index b891a2e97..e3de5a730 100644 --- a/http/http-advanced-reactive/src/test/java/io/quarkus/ts/http/advanced/reactive/JsonPayloadIT.java +++ b/http/http-advanced-reactive/src/test/java/io/quarkus/ts/http/advanced/reactive/JsonPayloadIT.java @@ -8,6 +8,8 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Date; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.MethodOrderer; @@ -15,6 +17,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import io.quarkus.test.bootstrap.RestService; import io.quarkus.test.scenarios.QuarkusScenario; import io.quarkus.test.services.QuarkusApplication; @@ -103,4 +108,77 @@ public void sendInvalidJSONPayload() throws IOException { assertThat(response.body().asString(), containsString( "Invalid JSON - JsonProcessingException : Unexpected character ('{' (code 123)): was expecting comma to separate Array entries")); } + + @Test + public void sendASingleJSONObject() throws JsonProcessingException { + FootballTeam teamX = new FootballTeam( + "Single Team", + "Pink", + 3, + new Date(1900, 1, 1), + "A", + Arrays.asList("You", "You")); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonPayload = objectMapper.writeValueAsString(teamX); + + Response response = given() + .contentType(ContentType.JSON) + .body(jsonPayload) + .when() + .post("/resource/upload-football-json") + .then() + .statusCode(400) + .extract().response(); + assertThat(response.body().asString(), containsString( + "JsonMappingException : Cannot deserialize value of type `java.util.HashSet`")); + } + + @Test + public void sendIncorrectContentType() throws IOException { + String jsonPayload = FileUtils.readFileToString( + Paths.get("src", "test", "resources", "football-teams.json").toFile(), + StandardCharsets.UTF_8); + + given() + .contentType(ContentType.TEXT) + .body(jsonPayload) + .when() + .post("/resource/upload-football-json") + .then() + .statusCode(415); + + } + + @Test + public void sendSpecialCharactersJSONPayload() throws Exception { + FootballTeam team1 = new FootballTeam( + "Føøbåll™ Tëâm", + "Rëd&Blüé", + 3, + new Date(1900, 1, 1), + "Städiümß©", + Arrays.asList("Pläÿér Ône", "Pläÿér Twø´ñ")); + + FootballTeam team2 = new FootballTeam( + "Tëâm 2", + "Grëèn&Yëllöw", + 2, + new Date(2010, 1, 15), + "New Städiüm©", + Arrays.asList("Pläÿér Thrëéñ", "Pläÿér Fòur")); + + ObjectMapper objectMapper = new ObjectMapper(); + String jsonPayload = objectMapper.writeValueAsString(Arrays.asList(team1, team2)); + + Response response = given() + .contentType(ContentType.JSON) + .body(jsonPayload) + .when() + .post("/resource/upload-football-json") + .then() + .statusCode(201) + .extract().response(); + + assertThat(response.body().asString(), containsString("Teams added successfully")); + } }