Skip to content

Commit

Permalink
Merge pull request #187 from Arquisoft/last-fixes-before-3rd-deliver
Browse files Browse the repository at this point in the history
Fixed unused catch clauses and personalized exceptions
  • Loading branch information
Pelayori authored Apr 8, 2024
2 parents 48379ef + b13c746 commit af04f3e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,7 +14,7 @@ public MultipleQuestionGenerator(QuestionGenerator... generators) {
this.generators = generators;
}

public List<Question> getQuestions() throws InterruptedException {
public List<Question> getQuestions() throws InterruptedException, IOException {
List<Question> questions = new ArrayList<>();
for (QuestionGenerator generator : generators) {
questions.addAll(generator.getQuestions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,10 +48,8 @@ public void questionGenerator(String statement, List<String> options, String cor
questions.add(question);
}

public List<Question> getQuestions() throws InterruptedException {
public List<Question> 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";
Expand All @@ -76,12 +76,6 @@ public List<Question> 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;
}

Expand All @@ -90,9 +84,4 @@ public List<Question> getQuestions() throws InterruptedException {

protected abstract String getQuestionSubject(JsonNode result);

private static class QuestionGeneratorException extends RuntimeException {
public QuestionGeneratorException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question> getQuestions() throws InterruptedException;
List<Question> getQuestions() throws InterruptedException, IOException;

Category getCategory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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("[email protected]").isPresent()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
Expand All @@ -74,7 +75,7 @@ public void generateTestQuestions() {
}

@Transactional
public void generateSampleData() throws InterruptedException {
public void generateSampleData() throws InterruptedException, IOException {

questionRepository.deleteAll();

Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question> questions = questionService.getAllQuestions();
Expand All @@ -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<Question> questions = questionService.getRandomQuestions(5);
Expand All @@ -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<Question> questions = borderQuestionGenerator.getQuestions();
Assertions.assertFalse(questions.isEmpty());
Expand All @@ -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<Question> questions = capitalQuestionGenerator.getQuestions();
Assertions.assertFalse(questions.isEmpty());
Expand All @@ -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<Question> questions = continentQuestionGenerator.getQuestions();
Assertions.assertFalse(questions.isEmpty());
Expand Down Expand Up @@ -1619,7 +1619,7 @@ private JSONObject parseJsonResponse(HttpResponse<String> response) throws JSONE
/**
* Inserts some sample questions into the database
*/
private void insertSomeQuestions() throws InterruptedException {
private void insertSomeQuestions() throws InterruptedException, IOException {
List<Question> qs = new ContinentQuestionGeneration(categoryService, Question.SPANISH).getQuestions();
qs.forEach(questionService::addNewQuestion);
}
Expand Down

0 comments on commit af04f3e

Please sign in to comment.