Skip to content

Commit

Permalink
Fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori committed Apr 26, 2024
1 parent 8d6c06e commit 88ccb95
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Component
public interface QuestionGenerator {
List<Question> getQuestions(String language) throws IOException;
List<Question> getQuestions(String language) throws IOException, InterruptedException;

List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException;
List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public QuestionGeneratorV2(JsonNode jsonNode) {
}

@Override
public List<Question> getQuestions(String language) throws IOException {
public List<Question> getQuestions(String language) throws IOException, InterruptedException {
this.language = language;
List<Question> questions = new ArrayList<>();
JsonNode categories = jsonNode.findValue("categories");
Expand All @@ -55,12 +55,12 @@ public List<Question> getQuestions(String language) throws IOException {
}

@Override
public List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException {
public List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException {
this.language = language;
return this.generateQuestion(question, cat);
}

private List<Question> generateQuestion(JsonNode question, Category cat) throws IOException {
private List<Question> generateQuestion(JsonNode question, Category cat) throws IOException, InterruptedException {
// Get the SPARQL query from the JSON
String query = question.get("sparqlQuery").textValue();

Expand Down Expand Up @@ -89,14 +89,16 @@ private List<Question> generateQuestion(JsonNode question, Category cat) throws
List<Answer> options = this.generateOptions(results, correctAnswer, answerLabel);
options.add(correct);

// Generate the question statement
String questionStatement = statement.replace(question_placeholder, result.path(questionLabel).path("value").asText());
if (statement != null) {
// Generate the question statement
String questionStatement = statement.replace(question_placeholder, result.path(questionLabel).path("value").asText());

// Generate the question
Question q = new Question(questionStatement, options, correct, cat, language);
// Generate the question
Question q = new Question(questionStatement, options, correct, cat, language);

// Add the question to the list
questions.add(q);
// Add the question to the list
questions.add(q);
}
}
return questions;
}
Expand Down Expand Up @@ -134,41 +136,28 @@ private String prepareStatement(JsonNode question) {
return null;
}

private JsonNode getQueryResult(String query) throws IOException {
private JsonNode getQueryResult(String query) throws IOException, InterruptedException {

System.out.println("Query: " + query);
HttpClient client = HttpClient.newHttpClient();
JsonNode resultsNode;
try {

String endpointUrl = "https://query.wikidata.org/sparql?query=" +
URLEncoder.encode(query, StandardCharsets.UTF_8) +
"&format=json";
String endpointUrl = "https://query.wikidata.org/sparql?query=" +
URLEncoder.encode(query, StandardCharsets.UTF_8) +
"&format=json";

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpointUrl))
.header("Accept", "application/json")
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpointUrl))
.header("Accept", "application/json")
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

// Process the JSON response using Jackson ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(response.body());
// Process the JSON response using Jackson ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(response.body());

// Access the data from the JSON response
resultsNode = jsonResponse.path("results").path("bindings");

} catch (InterruptedException e) {
throw new QuestionGeneratorException("Generation of questions was interrupted");
}
// Access the data from the JSON response
resultsNode = jsonResponse.path("results").path("bindings");
return resultsNode;

}

private static class QuestionGeneratorException extends RuntimeException {
public QuestionGeneratorException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void generateAllQuestions(){

@Scheduled(fixedRate = 150000)
@Transactional
public void generateQuestions() throws IOException {
public void generateQuestions() throws IOException, InterruptedException {
if (types.isEmpty()) {
return;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public void generateQuestions() throws IOException {
}

@Transactional
public void generateTestQuestions() throws IOException {
public void generateTestQuestions() throws IOException, InterruptedException {
QuestionGenerator qgen = new QuestionGeneratorV2(json);
QuestionType type = types.pop();
List<QuestionDto> questions;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testPlayerService() {
}

@Order(3)
public void testQuestionsGenerator() throws IOException {
public void testQuestionsGenerator() throws IOException, InterruptedException {
questionGeneratorService.generateTestQuestions();
List<Question> questions = questionService.getAllQuestions();
Assertions.assertFalse(questions.isEmpty());
Expand Down Expand Up @@ -1547,7 +1547,7 @@ private JSONObject parseJsonResponse(HttpResponse<String> response) throws JSONE
/**
* Inserts some sample questions into the database
*/
private void insertSomeQuestions() throws IOException {
private void insertSomeQuestions() throws IOException, InterruptedException {
questionGeneratorService.generateTestQuestions();
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/uniovi/steps/GameStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GameStep extends Wiq_IntegrationTests {
private Logger log = LoggerFactory.getLogger(GameStep.class);

@When("I press Play")
public void iPressPlay() throws IOException {
public void iPressPlay() throws IOException, InterruptedException {
questionGeneratorService.generateTestQuestions();
List<WebElement> elems = SeleniumUtils.waitLoadElementsBy(driver, "free", "//*[@href=\"/game\"]", 5);
elems.get(0).click();
Expand Down

0 comments on commit 88ccb95

Please sign in to comment.