Skip to content

Commit

Permalink
Merge pull request #285 from Arquisoft/develop
Browse files Browse the repository at this point in the history
sonar fixes
  • Loading branch information
Toto-hitori authored Apr 24, 2024
2 parents d396440 + be45647 commit faceb13
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 285 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/lab/en2b/quizapi/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.List;

import static lab.en2b.quizapi.game.GameMode.*;

@SuppressWarnings("java:S1068")
@Entity
@Table(name = "games")
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import lab.en2b.quizapi.game.dtos.*;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lab.en2b.quizapi.questions.question.dtos.QuestionCategoryDto;
import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto;
import lombok.RequiredArgsConstructor;
Expand Down
2 changes: 0 additions & 2 deletions api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lab.en2b.quizapi.commons.utils.GameModeUtils;
import lab.en2b.quizapi.game.dtos.*;
import lab.en2b.quizapi.game.mappers.GameResponseDtoMapper;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lab.en2b.quizapi.questions.question.QuestionService;
import lab.en2b.quizapi.questions.question.dtos.QuestionCategoryDto;
import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto;
Expand All @@ -16,7 +15,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lab.en2b.quizapi.game.dtos;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.List;

@SuppressWarnings("java:S1068")
@Entity
@Table(name = "answers")
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.List;

@SuppressWarnings("java:S1068")
@Entity
@Table(name = "questions")
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@
@Component
public class QuestionHelper {

private static int MAX_DISTRACTORS = 3;
private QuestionHelper(){} // To hide the implicit public constructor as this is static only

public static List<Answer> getDistractors(AnswerRepository answerRepository, Question question){
List<Answer> distractors = new ArrayList<>();
AnswerCategory cat = question.getAnswerCategory();

switch (cat){ // Write the case only for the exceptions
case COUNTRY:
// Implement more cases
break;
default:
distractors = answerRepository.findDistractors(question.getAnswerCategory().toString(), question.getLanguage(), question.getCorrectAnswer().getText(), MAX_DISTRACTORS);
}
private static final int MAX_DISTRACTORS = 3;

return distractors;
public static List<Answer> getDistractors(AnswerRepository answerRepository, Question question){
return answerRepository.findDistractors(question.getAnswerCategory().toString(), question.getLanguage(), question.getCorrectAnswer().getText(), MAX_DISTRACTORS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ public List<QuestionResponseDto> getQuestionsWithPage(Long page){

private List<QuestionResponseDto> getPage(List<QuestionResponseDto> result, Long page) {
try{
int QUESTION_PAGE_SIZE = 100;
int startIndex = Math.toIntExact((page-1)* QUESTION_PAGE_SIZE);
int questionPageSize = 100;
int startIndex = Math.toIntExact((page-1)* questionPageSize);
if(startIndex > result.size())
throw new IllegalArgumentException("Invalid page number, maximum page is "+(result.size()/ QUESTION_PAGE_SIZE +1) + " and you requested page "+page);
if (result.size() < page* QUESTION_PAGE_SIZE)
throw new IllegalArgumentException("Invalid page number, maximum page is "+(result.size()/ questionPageSize +1) + " and you requested page "+page);
if (result.size() < page* questionPageSize)
return result.subList(startIndex,result.size());
return result.subList(startIndex, Math.toIntExact(page* QUESTION_PAGE_SIZE));
return result.subList(startIndex, Math.toIntExact(page* questionPageSize));
} catch (ArithmeticException e) {
throw new IllegalArgumentException("Invalid page number");
}
Expand All @@ -101,7 +101,6 @@ private List<QuestionResponseDto> getPage(List<QuestionResponseDto> result, Long
* Load the answers for a question (The distractors and the correct one)
* @param question The question to load the answers for
*/
//TODO: CHAPUZAS, FIXEAR ESTO
private void loadAnswers(Question question) {
// Create the new answers list with the distractors
if(question.getAnswers().size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;

import java.util.Arrays;
import java.util.List;

import static lab.en2b.quizapi.commons.utils.TestUtils.asJsonString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ void getQuestionsWithPageInvalidPage() {
@Test
void getQuestionsWithPageGreaterThanSize() {
when(questionRepository.findAll()).thenReturn(List.of(defaultQuestion));

assertThrows(IllegalArgumentException.class,() -> questionService.getQuestionsWithPage(2L));
}

Expand All @@ -200,4 +199,11 @@ void getQuestionsWithPageNoQuestions() {
Assertions.assertEquals(questionService.getQuestionsWithPage(1L), List.of());
}

@Test
void getQuestionsWithPageTooBig() {
when(questionRepository.findAll()).thenReturn(List.of(defaultQuestion));
Assertions.assertThrows(IllegalArgumentException.class, () -> questionService.getQuestionsWithPage(100000000L));

}

}
Loading

0 comments on commit faceb13

Please sign in to comment.