Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jul 17, 2024
1 parent 5bcb033 commit f8f23a2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> keyPlayers;

public FootballTeam() {
}

public FootballTeam(String name, String colorTShirt, int euroCups, Date foundationDate, String stadium,
List<String> keyPlayers) {
this.name = name;
this.colorTShirt = colorTShirt;
this.euroCups = euroCups;
this.foundationDate = foundationDate;
this.stadium = stadium;
this.keyPlayers = keyPlayers;
}

public Date getFoundationDate() {
return foundationDate;
Expand All @@ -32,13 +49,6 @@ public void setKeyPlayers(List<String> keyPlayers) {
this.keyPlayers = keyPlayers;
}

private Date foundationDate;
private String stadium;
private List<String> keyPlayers;

public FootballTeam() {
}

public String getName() {
return name;
}
Expand All @@ -59,16 +69,6 @@ public int getEuroCups() {
return euroCups;
}

public FootballTeam(String name, String colorTShirt, int euroCups, Date foundationDate, String stadium,
List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
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;
import org.junit.jupiter.api.Order;
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;
Expand Down Expand Up @@ -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<io.quarkus.ts.http.advanced.reactive.FootballTeam>`"));
}

@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"));
}
}

0 comments on commit f8f23a2

Please sign in to comment.