From 047bf0fcd4311549fed27f2c818b792ebde1786d Mon Sep 17 00:00:00 2001 From: uo287545 Date: Thu, 25 Apr 2024 22:19:08 +0200 Subject: [PATCH] Fixed unit tests --- .../generators/QuestionGeneratorV2.java | 3 - src/main/java/com/uniovi/dto/AnswerDto.java | 1 + src/main/java/com/uniovi/dto/CategoryDto.java | 1 + .../services/QuestionGeneratorService.java | 1 - src/test/java/com/uniovi/Wiq_UnitTests.java | 122 +++--------------- 5 files changed, 22 insertions(+), 106 deletions(-) diff --git a/src/main/java/com/uniovi/components/generators/QuestionGeneratorV2.java b/src/main/java/com/uniovi/components/generators/QuestionGeneratorV2.java index 239985dc..efb1d8f6 100644 --- a/src/main/java/com/uniovi/components/generators/QuestionGeneratorV2.java +++ b/src/main/java/com/uniovi/components/generators/QuestionGeneratorV2.java @@ -91,9 +91,6 @@ private List generateQuestion(JsonNode question, Category cat) throws // Generate the question Question q = new Question(questionStatement, options, correct, cat, language); - // Scramble the options - q.scrambleOptions(); - // Add the question to the list questions.add(q); } diff --git a/src/main/java/com/uniovi/dto/AnswerDto.java b/src/main/java/com/uniovi/dto/AnswerDto.java index 026eede6..304ee640 100644 --- a/src/main/java/com/uniovi/dto/AnswerDto.java +++ b/src/main/java/com/uniovi/dto/AnswerDto.java @@ -6,6 +6,7 @@ @Getter @Setter @NoArgsConstructor +@AllArgsConstructor @ToString public class AnswerDto { diff --git a/src/main/java/com/uniovi/dto/CategoryDto.java b/src/main/java/com/uniovi/dto/CategoryDto.java index fc87530e..57aef22e 100644 --- a/src/main/java/com/uniovi/dto/CategoryDto.java +++ b/src/main/java/com/uniovi/dto/CategoryDto.java @@ -11,6 +11,7 @@ @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class CategoryDto { @Schema(description = "The name of the category", example = "Geography") diff --git a/src/main/java/com/uniovi/services/QuestionGeneratorService.java b/src/main/java/com/uniovi/services/QuestionGeneratorService.java index e443c6f8..7c2d36c7 100644 --- a/src/main/java/com/uniovi/services/QuestionGeneratorService.java +++ b/src/main/java/com/uniovi/services/QuestionGeneratorService.java @@ -11,7 +11,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index 46ff6a25..2fde133f 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -1,44 +1,22 @@ package com.uniovi; -import com.uniovi.dto.*; -import com.uniovi.entities.*; -import com.uniovi.repositories.*; -import com.uniovi.services.*; -import com.uniovi.services.impl.*; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; -import com.uniovi.components.generators.geography.BorderQuestionGenerator; -import com.uniovi.components.generators.geography.CapitalQuestionGenerator; -import com.uniovi.components.generators.geography.ContinentQuestionGeneration; +import com.uniovi.dto.PlayerDto; +import com.uniovi.dto.RoleDto; import com.uniovi.entities.*; +import com.uniovi.repositories.*; import com.uniovi.services.*; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.junit.Assert; -import org.junit.jupiter.api.*; -import org.springframework.beans.factory.annotation.Autowired; -import com.uniovi.dto.*; -import com.uniovi.entities.*; -import com.uniovi.repositories.*; -import com.uniovi.services.*; -import com.uniovi.services.impl.*; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; -import java.io.IOException; -import java.awt.print.Pageable; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; import java.io.IOException; import java.net.URI; import java.net.URLEncoder; @@ -49,13 +27,6 @@ import java.time.LocalDateTime; import java.util.*; -import static org.junit.Assert.*; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - @SpringBootTest @Tag("unit") @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) @@ -96,9 +67,10 @@ public class Wiq_UnitTests { private final HttpClient httpClient = HttpClient.newHttpClient(); - private Player createPlayer(){ - return new Player("name","test@email.com","password"); + private Player createPlayer() { + return new Player("name", "test@email.com", "password"); } + @Test @Order(1) public void testPlayerService() { @@ -106,67 +78,11 @@ public void testPlayerService() { Assertions.assertEquals(1, players.size()); } - @Test - @Order(2) - public void testQuestions() throws InterruptedException, IOException { - sampleDataService.insertSampleQuestions(); - sampleDataService.generateSampleData(); @Order(3) public void testQuestionsGenerator() throws IOException { questionGeneratorService.generateTestQuestions(); List questions = questionService.getAllQuestions(); Assertions.assertFalse(questions.isEmpty()); - - } - @Test - @Order(2) - public void testRandomQuestions() throws InterruptedException, IOException { - sampleDataService.insertSampleQuestions(); - sampleDataService.generateSampleData(); - List questions = questionService.getRandomQuestions(5); - Assertions.assertEquals(5,questions.size()); - } - - @Test - @Order(3) - public void testBorderQuestionsGenerator() throws InterruptedException, IOException { - BorderQuestionGenerator borderQuestionGenerator=new BorderQuestionGenerator(categoryService,Question.SPANISH); - List questions = borderQuestionGenerator.getQuestions(); - Assertions.assertFalse(questions.isEmpty()); - - for (Question question : questions) { - Assertions.assertNotNull(question.getCorrectAnswer()); - Assertions.assertEquals(4, question.getOptions().size()); - Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); - } - } - - @Test - @Order(4) - public void testCapitalQuestionsGenerator() throws InterruptedException, IOException { - CapitalQuestionGenerator capitalQuestionGenerator=new CapitalQuestionGenerator(categoryService,Question.SPANISH); - List questions = capitalQuestionGenerator.getQuestions(); - Assertions.assertFalse(questions.isEmpty()); - - for (Question question : questions) { - Assertions.assertNotNull(question.getCorrectAnswer()); - Assertions.assertEquals(4, question.getOptions().size()); - Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); - } - } - - @Test - @Order(5) - public void testContinentQuestionsGenerator() throws InterruptedException, IOException { - ContinentQuestionGeneration continentQuestionGenerator=new ContinentQuestionGeneration(categoryService,Question.SPANISH); - List questions = continentQuestionGenerator.getQuestions(); - Assertions.assertFalse(questions.isEmpty()); - - for (Question question : questions) { - Assertions.assertNotNull(question.getCorrectAnswer()); - Assertions.assertEquals(4, question.getOptions().size()); - Assertions.assertTrue(question.getOptions().contains(question.getCorrectAnswer())); - } } @Test @@ -393,6 +309,7 @@ public void testGetDuration() { Assertions.assertEquals("00:05:00", gameSession.getDuration()); } + @Test @Order(22) public void testPlayerToJson() { @@ -460,8 +377,7 @@ public void testScrambleOptions() { question.addOption(option2); question.addOption(option3); - question.scrambleOptions(); - List scrambledOptions = question.getOptions(); + List scrambledOptions = question.returnScrambledOptions(); Assertions.assertTrue(scrambledOptions.contains(option1)); Assertions.assertTrue(scrambledOptions.contains(option2)); @@ -661,7 +577,7 @@ public void testCreatePlayerValid() throws IOException, InterruptedException, JS data.put("username", "newUser"); data.put("email", "newUser@email.com"); data.put("password", "password"); - data.put("roles", new String[] {"ROLE_USER"}); + data.put("roles", new String[]{"ROLE_USER"}); HttpResponse response = sendRequest("POST", "/api/players", Map.of("API-KEY", apiKey.getKeyToken()), data); @@ -884,7 +800,7 @@ public void testGetQuestionsByCategoryName() throws IOException, InterruptedExce ApiKey apiKey = player.getApiKey(); HttpResponse response = sendRequest("GET", "/api/questions", Map.of(), - Map.of("apiKey", apiKey.getKeyToken(), "category", "Geography")); + Map.of("apiKey", apiKey.getKeyToken(), "category", "Science")); Assertions.assertEquals(200, response.statusCode()); JSONObject json = parseJsonResponse(response); @@ -898,7 +814,7 @@ public void testGetQuestionsByCategoryId() throws IOException, InterruptedExcept insertSomeQuestions(); Player player = playerService.getUsersByRole("ROLE_USER").get(0); ApiKey apiKey = player.getApiKey(); - Category cat = categoryService.getCategoryByName("Geography"); + Category cat = categoryService.getCategoryByName("Science"); HttpResponse response = sendRequest("GET", "/api/questions", Map.of(), Map.of("apiKey", apiKey.getKeyToken(), "category", cat.getId())); @@ -1564,17 +1480,18 @@ public void testDeleteQuestion() throws IOException, InterruptedException, JSONE /** * Sends an HTTP request to the API - * @param method HTTP method - * @param uri URI to send the request to + * + * @param method HTTP method + * @param uri URI to send the request to * @param headers Headers to include in the request - * @param data Data to send in the request + * @param data Data to send in the request * @return The response from the server * @throws IOException * @throws InterruptedException */ private HttpResponse sendRequest(String method, String uri, - Map headers, - Map data) throws IOException, InterruptedException { + Map headers, + Map data) throws IOException, InterruptedException { HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(); uri = Wiq_IntegrationTests.URL.substring(0, Wiq_IntegrationTests.URL.length() - 1) + uri; @@ -1603,6 +1520,7 @@ private HttpResponse sendRequest(String method, String uri, /** * Builds a query string from a map of data + * * @param data The data to include in the query string * @return The query string */ @@ -1615,6 +1533,7 @@ private String buildQueryString(Map data) { /** * Parses the JSON response from the server + * * @param response The response from the server * @return The JSON object * @throws JSONException @@ -1627,7 +1546,6 @@ private JSONObject parseJsonResponse(HttpResponse response) throws JSONE * Inserts some sample questions into the database */ private void insertSomeQuestions() throws InterruptedException, IOException { - List qs = new ContinentQuestionGeneration(categoryService, Question.SPANISH).getQuestions(); - qs.forEach(questionService::addNewQuestion); + questionGeneratorService.generateTestQuestions(); } }