Skip to content

Commit

Permalink
Merge pull request #284 from Arquisoft/fix/sonarcloud-analysis
Browse files Browse the repository at this point in the history
Fix/sonarcloud analysis
  • Loading branch information
Toto-hitori authored Apr 24, 2024
2 parents 89f7626 + afac713 commit be45647
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 272 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 @@ -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 be45647

Please sign in to comment.