[Added] Tests and some more functionality to the Services

This commit is contained in:
Ninjaws
2024-10-30 21:47:45 +01:00
parent bf66869ebf
commit 2505fce920
21 changed files with 535 additions and 121 deletions
@@ -0,0 +1,17 @@
package com.ninjaws.quiz.models;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Answers {
private final String sessionId;
private List<String> answers;
public Answers(String sessionId) {
this.sessionId = sessionId;
}
}
@@ -0,0 +1,21 @@
package com.ninjaws.quiz.models;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
/**
* The response sent by the external API
*/
@Getter
@Setter
public class ApiResponse {
@JsonProperty("response_code")
private int responseCode;
@JsonProperty("results")
private List<Question> results;
}
@@ -0,0 +1,53 @@
package com.ninjaws.quiz.models;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
@Getter
public class Question {
private String type;
private String difficulty;
private String category;
private String question;
//* Hidden from the user */
private String correctAnswer;
private List<String> incorrectAnswers;
@JsonProperty("answers")
public List<String> getAnswers() {
List<String> possibleAnswers = new ArrayList<>(incorrectAnswers);
possibleAnswers.add(correctAnswer);
Collections.shuffle(possibleAnswers);
return possibleAnswers;
}
@JsonProperty("correct_answer")
public void setCorrectAnswer(String correctAnswer) {
this.correctAnswer = correctAnswer;
}
@JsonProperty("incorrect_answers")
public void setIncorrectAnswer(List<String> incorrectAnswers) {
this.incorrectAnswers = incorrectAnswers;
}
@JsonProperty("correct_answer")
@JsonIgnore
public String getCorrectAnswer() {
return correctAnswer;
}
@JsonProperty("incorrect_answers")
@JsonIgnore
public List<String> getIncorrectAnswers() {
return incorrectAnswers;
}
}
@@ -0,0 +1,86 @@
package com.ninjaws.quiz.models;
import com.ninjaws.quiz.models.QuizSettings.Difficulty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class QuizSettings {
public enum Type {
ANY, BOOLEAN, MULTIPLE;
public String getApiValue() {
return this.name().toLowerCase();
}
}
public enum Difficulty {
ANY, EASY, MEDIUM, HARD;
public String getApiValue() {
return this.name().toLowerCase();
}
}
public enum Category {
ANY, GEN_KNOWLEDGE, ENT_BOOKS, ENT_FILM, ENT_MUSIC, ENT_MUSICAL_THEATRE, ENT_TELEVISION,
ENT_VIDEOGAMES, ENT_BOARDGAMES, SCIENCE_NATURE, SCIENCE_COMPUTERS, SCIENCE_MATH, MYTHOLOGY, SPORTS, GEOGRAPHY, HISTORY, POLITICS, ART, CELEBRITIES,
ANIMALS, VEHICLES, ENT_COMICS, SCIENCE_GADGETS, ENT_ANIME_MANGA, ENT_CARTOON_ANIMATIONS;
/**
* For some reason the external API starts with a value of 9 for the General Knowledge category, so we have to adjust
* ANY also gets adjusted, but it doesn't matter since that one doesn't get used as parameter anyway
* @return The corrected value
*/
public int getApiValue() {
return 8 + this.ordinal();
}
}
@Min(1)
@Max(50)
private int amount = 10;
private Type type = Type.ANY;
private Difficulty difficulty = Difficulty.ANY;
private Category category = Category.ANY;
/**
* Ensures the value is between 1 and 50
* @Valid doesn't seem to be enforcing it, so this ensures the value is legal
*/
public void setAmount(@Valid int amount) {
this.amount = Math.max(1, Math.min(amount, 50));
}
/**
* Converts the class to a format that can be provided as arguments for the api request
* @return The resulting request arguments as String
*/
public String toApiQuery() {
StringBuilder query = new StringBuilder("?amount=" + amount);
if (category != null && category != Category.ANY) {
query.append("&category=").append(category.getApiValue());
}
if (difficulty != null && difficulty != Difficulty.ANY) {
query.append("&difficulty=").append(difficulty.getApiValue());
}
if (type != null && type != Type.ANY) {
query.append("&type=").append(type.getApiValue());
}
return query.toString();
}
}
@@ -0,0 +1,14 @@
package com.ninjaws.quiz.models;
import lombok.Getter;
@Getter
public class Request {
private final String sessionId;
private final QuizSettings quizSettings;
public Request(String sessionId, QuizSettings quizSettings) {
this.sessionId = sessionId;
this.quizSettings = quizSettings;
}
}
@@ -0,0 +1,18 @@
package com.ninjaws.quiz.models;
import java.util.List;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Session {
private final String id;
private List<Question> questions;
public Session() {
this.id = UUID.randomUUID().toString();
}
}