Skip to content

Commit

Permalink
Add JsonPayload coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jul 19, 2024
1 parent 567369d commit 9d1c50e
Show file tree
Hide file tree
Showing 14 changed files with 2,796 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.quarkus.ts.http.advanced.reactive;

import java.util.Date;
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;
}

public void setFoundationDate(Date foundationDate) {
this.foundationDate = foundationDate;
}

public String getStadium() {
return stadium;
}

public void setStadium(String stadium) {
this.stadium = stadium;
}

public List<String> getKeyPlayers() {
return keyPlayers;
}

public void setKeyPlayers(List<String> keyPlayers) {
this.keyPlayers = keyPlayers;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getColorTShirt() {
return colorTShirt;
}

public void setColorTShirt(String colorTShirt) {
this.colorTShirt = colorTShirt;
}

public int getEuroCups() {
return euroCups;
}

public void setEuroCups(int euroCups) {
this.euroCups = euroCups;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.ts.http.advanced.reactive;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.smallrye.mutiny.Uni;

@Path("/football")
public class FootballTeamResource {

private static final Logger LOG = Logger.getLogger(FootballTeamResource.class);

private Set<FootballTeam> footballTeamSet = Collections.synchronizedSet(new LinkedHashSet<>());

/**
*
* When a JSON extension is installed such as quarkus-rest-jackson or quarkus-rest-jsonb
* Quarkus will use the application/json media type by default for most return values
*/

@POST
@Path("/upload-football-json")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> uploadFootballJson(String data) {
try {
ObjectMapper objectMapper = new ObjectMapper();
Set<FootballTeam> teamSet = objectMapper.readValue(data, new TypeReference<Set<FootballTeam>>() {
});
for (FootballTeam footballTeam : teamSet) {
if (footballTeam == null || footballTeam.getName().isEmpty()) {
return Uni.createFrom().item(Response.status(400).entity("Some fields are empty or null").build());
} else {
footballTeamSet.add(footballTeam);
}
}
return Uni.createFrom()
.item(() -> {
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("teams", footballTeamSet);
responseMap.put("message", "Teams added successfully");
return Response.status(201).entity(responseMap).build();
});
} catch (JsonMappingException ex) {
LOG.error(ex.getMessage());
return Uni.createFrom()
.item(Response.status(400).entity("JsonMappingException : " + ex.getMessage()).build());
} catch (JsonProcessingException e) {
LOG.error(e.getMessage());
return Uni.createFrom().item(
Response.status(400).entity("Invalid JSON - JsonProcessingException : " + e.getOriginalMessage()).build());
}

}

@DELETE
@Path("/clear")
public Response clearTeams() {
footballTeamSet.clear();
return Response.ok().build();
}

}
Loading

0 comments on commit 9d1c50e

Please sign in to comment.