From 1dda1f425c8b583e2eada6df062b6be3b5e6cfe0 Mon Sep 17 00:00:00 2001 From: uo287545 Date: Tue, 9 Apr 2024 00:27:24 +0200 Subject: [PATCH 1/2] Removed unused catch clauses and personalized exceptions --- .../components/MultipleQuestionGenerator.java | 3 ++- .../generators/AbstractQuestionGenerator.java | 17 +++-------------- .../generators/QuestionGenerator.java | 3 ++- .../services/InsertSampleDataService.java | 5 +++-- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java b/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java index 6d525d3b..e5001403 100644 --- a/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/MultipleQuestionGenerator.java @@ -3,6 +3,7 @@ import com.uniovi.components.generators.QuestionGenerator; import com.uniovi.entities.Question; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,7 +14,7 @@ public MultipleQuestionGenerator(QuestionGenerator... generators) { this.generators = generators; } - public List getQuestions() throws InterruptedException { + public List getQuestions() throws InterruptedException, IOException { List questions = new ArrayList<>(); for (QuestionGenerator generator : generators) { questions.addAll(generator.getQuestions()); diff --git a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java index 3d00dc8c..9754c2c5 100644 --- a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java @@ -6,6 +6,8 @@ import com.uniovi.entities.Category; import com.uniovi.entities.Question; import com.uniovi.services.CategoryService; + +import java.io.IOException; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; @@ -46,10 +48,8 @@ public void questionGenerator(String statement, List options, String cor questions.add(question); } - public List getQuestions() throws InterruptedException { + public List getQuestions() throws InterruptedException, IOException { HttpClient client = HttpClient.newHttpClient(); - try { - String endpointUrl = "https://query.wikidata.org/sparql?query=" + URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8) + "&format=json"; @@ -76,12 +76,6 @@ public List getQuestions() throws InterruptedException { questionGenerator(questionStatement, options, correctAnswer, this.getCategory()); } - } catch (InterruptedException e) { - throw e; - } catch (Exception e) { - throw new QuestionGeneratorException("An error occurred while generating questions." + e.getMessage()); - } - return questions; } @@ -90,9 +84,4 @@ public List getQuestions() throws InterruptedException { protected abstract String getQuestionSubject(JsonNode result); - private static class QuestionGeneratorException extends RuntimeException { - public QuestionGeneratorException(String message) { - super(message); - } - } } diff --git a/src/main/java/com/uniovi/components/generators/QuestionGenerator.java b/src/main/java/com/uniovi/components/generators/QuestionGenerator.java index c6db2c9b..fd9356fa 100644 --- a/src/main/java/com/uniovi/components/generators/QuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/QuestionGenerator.java @@ -4,13 +4,14 @@ import com.uniovi.entities.Question; import org.springframework.stereotype.Component; +import java.io.IOException; import java.util.List; @Component public interface QuestionGenerator { String getQuery(); - List getQuestions() throws InterruptedException; + List getQuestions() throws InterruptedException, IOException; Category getCategory(); diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java index 3386e26f..f51a0ed8 100644 --- a/src/main/java/com/uniovi/services/InsertSampleDataService.java +++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java @@ -22,6 +22,7 @@ import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; +import java.io.IOException; import java.time.LocalDateTime; import java.util.*; @@ -49,7 +50,7 @@ public InsertSampleDataService(PlayerService playerService, QuestionService ques @Transactional @EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup - public void insertSampleQuestions() throws InterruptedException { + public void insertSampleQuestions() throws InterruptedException, IOException { if (!playerService.getUserByEmail("test@test.com").isPresent()) { PlayerDto player = new PlayerDto(); player.setEmail("test@test.com"); @@ -74,7 +75,7 @@ public void generateTestQuestions() { } @Transactional - public void generateSampleData() throws InterruptedException { + public void generateSampleData() throws InterruptedException, IOException { questionRepository.deleteAll(); From b13c7464a82f3faf877e131606978a9768ee9d6a Mon Sep 17 00:00:00 2001 From: uo287545 Date: Tue, 9 Apr 2024 00:32:02 +0200 Subject: [PATCH 2/2] Fixed unit tests --- src/test/java/com/uniovi/Wiq_UnitTests.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index a8ece280..c28558c4 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -104,7 +104,7 @@ public void testPlayerService() { } @Test @Order(2) - public void testQuestions() throws InterruptedException { + public void testQuestions() throws InterruptedException, IOException { sampleDataService.insertSampleQuestions(); sampleDataService.generateSampleData(); List questions = questionService.getAllQuestions(); @@ -113,7 +113,7 @@ public void testQuestions() throws InterruptedException { } @Test @Order(2) - public void testRandomQuestions() throws InterruptedException { + public void testRandomQuestions() throws InterruptedException, IOException { sampleDataService.insertSampleQuestions(); sampleDataService.generateSampleData(); List questions = questionService.getRandomQuestions(5); @@ -122,7 +122,7 @@ public void testRandomQuestions() throws InterruptedException { @Test @Order(3) - public void testBorderQuestionsGenerator() throws InterruptedException { + public void testBorderQuestionsGenerator() throws InterruptedException, IOException { BorderQuestionGenerator borderQuestionGenerator=new BorderQuestionGenerator(categoryService,Question.SPANISH); List questions = borderQuestionGenerator.getQuestions(); Assertions.assertFalse(questions.isEmpty()); @@ -136,7 +136,7 @@ public void testBorderQuestionsGenerator() throws InterruptedException { @Test @Order(4) - public void testCapitalQuestionsGenerator() throws InterruptedException { + public void testCapitalQuestionsGenerator() throws InterruptedException, IOException { CapitalQuestionGenerator capitalQuestionGenerator=new CapitalQuestionGenerator(categoryService,Question.SPANISH); List questions = capitalQuestionGenerator.getQuestions(); Assertions.assertFalse(questions.isEmpty()); @@ -150,7 +150,7 @@ public void testCapitalQuestionsGenerator() throws InterruptedException { @Test @Order(5) - public void testContinentQuestionsGenerator() throws InterruptedException { + public void testContinentQuestionsGenerator() throws InterruptedException, IOException { ContinentQuestionGeneration continentQuestionGenerator=new ContinentQuestionGeneration(categoryService,Question.SPANISH); List questions = continentQuestionGenerator.getQuestions(); Assertions.assertFalse(questions.isEmpty()); @@ -1619,7 +1619,7 @@ private JSONObject parseJsonResponse(HttpResponse response) throws JSONE /** * Inserts some sample questions into the database */ - private void insertSomeQuestions() throws InterruptedException { + private void insertSomeQuestions() throws InterruptedException, IOException { List qs = new ContinentQuestionGeneration(categoryService, Question.SPANISH).getQuestions(); qs.forEach(questionService::addNewQuestion); }