diff --git a/api/src/main/java/lab/en2b/quizapi/game/Game.java b/api/src/main/java/lab/en2b/quizapi/game/Game.java index a12cb59d..c359fd79 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/Game.java +++ b/api/src/main/java/lab/en2b/quizapi/game/Game.java @@ -15,7 +15,7 @@ import java.util.List; import static lab.en2b.quizapi.game.GameMode.*; - +@SuppressWarnings("java:S1068") @Entity @Table(name = "games") @NoArgsConstructor diff --git a/api/src/main/java/lab/en2b/quizapi/game/GameController.java b/api/src/main/java/lab/en2b/quizapi/game/GameController.java index 99c1d872..8b21c4c9 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/GameController.java +++ b/api/src/main/java/lab/en2b/quizapi/game/GameController.java @@ -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; diff --git a/api/src/main/java/lab/en2b/quizapi/game/GameService.java b/api/src/main/java/lab/en2b/quizapi/game/GameService.java index 36b9d8a1..d78db2f4 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/GameService.java +++ b/api/src/main/java/lab/en2b/quizapi/game/GameService.java @@ -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; @@ -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; diff --git a/api/src/main/java/lab/en2b/quizapi/game/dtos/GameAnswerDto.java b/api/src/main/java/lab/en2b/quizapi/game/dtos/GameAnswerDto.java index 54cc8634..80c60996 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/dtos/GameAnswerDto.java +++ b/api/src/main/java/lab/en2b/quizapi/game/dtos/GameAnswerDto.java @@ -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.*; diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java index 1c8bd8ad..a9d2b77f 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/Answer.java @@ -6,6 +6,7 @@ import java.util.List; +@SuppressWarnings("java:S1068") @Entity @Table(name = "answers") @NoArgsConstructor diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java b/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java index 17234423..30662f27 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/Question.java @@ -9,6 +9,7 @@ import java.util.List; +@SuppressWarnings("java:S1068") @Entity @Table(name = "questions") @NoArgsConstructor diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java index 093a1368..667cfd8a 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionService.java @@ -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"); } @@ -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) { diff --git a/api/src/test/java/lab/en2b/quizapi/auth/AuthControllerTest.java b/api/src/test/java/lab/en2b/quizapi/auth/AuthControllerTest.java index 1871688f..bd336638 100644 --- a/api/src/test/java/lab/en2b/quizapi/auth/AuthControllerTest.java +++ b/api/src/test/java/lab/en2b/quizapi/auth/AuthControllerTest.java @@ -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; diff --git a/api/src/test/java/lab/en2b/quizapi/questions/QuestionServiceTest.java b/api/src/test/java/lab/en2b/quizapi/questions/QuestionServiceTest.java index a773740c..5d99584f 100644 --- a/api/src/test/java/lab/en2b/quizapi/questions/QuestionServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/questions/QuestionServiceTest.java @@ -190,7 +190,6 @@ void getQuestionsWithPageInvalidPage() { @Test void getQuestionsWithPageGreaterThanSize() { when(questionRepository.findAll()).thenReturn(List.of(defaultQuestion)); - assertThrows(IllegalArgumentException.class,() -> questionService.getQuestionsWithPage(2L)); } @@ -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)); + + } + } diff --git a/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java b/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java index 60349876..cdbfdd7d 100644 --- a/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/statistics/StatisticsServiceTest.java @@ -4,6 +4,7 @@ import lab.en2b.quizapi.commons.user.User; import lab.en2b.quizapi.commons.user.dtos.UserResponseDto; import lab.en2b.quizapi.commons.user.UserService; +import lab.en2b.quizapi.commons.user.mappers.UserResponseDtoMapper; import lab.en2b.quizapi.game.GameRepository; import lab.en2b.quizapi.statistics.dtos.StatisticsResponseDto; import lab.en2b.quizapi.statistics.mappers.StatisticsResponseDtoMapper; @@ -37,12 +38,6 @@ public class StatisticsServiceTest { @Mock private StatisticsRepository statisticsRepository; - @Mock - private Authentication authentication; - - @Mock - private StatisticsResponseDtoMapper statisticsResponseDtoMapper; - @Mock private GameRepository gameRepository; @@ -60,7 +55,7 @@ public class StatisticsServiceTest { @BeforeEach public void setUp(){ - this.statisticsService = new StatisticsService(statisticsRepository, userService, statisticsResponseDtoMapper, gameRepository); + this.statisticsService = new StatisticsService(statisticsRepository, userService, new StatisticsResponseDtoMapper(new UserResponseDtoMapper()), gameRepository); this.defaultUser = User.builder() .id(1L) .email("test@email.com") @@ -73,7 +68,7 @@ public void setUp(){ this.defaultUserResponseDto = UserResponseDto.builder() .id(1L) - .email("test") + .email("test@email.com") .username("test") .build(); @@ -121,16 +116,43 @@ public void getStatisticsForUserTest(){ Authentication authentication = mock(Authentication.class); when(userService.getUserByAuthentication(any())).thenReturn(defaultUser); when(statisticsRepository.findByUserId(any())).thenReturn(Optional.of(defaultStatistics1)); - when(statisticsResponseDtoMapper.apply(any())).thenReturn(defaultStatisticsResponseDto1); StatisticsResponseDto result = statisticsService.getStatisticsForUser(authentication); Assertions.assertEquals(defaultStatisticsResponseDto1, result); } + @Test + public void getStatisticsForUserTestEmpty(){ + Authentication authentication = mock(Authentication.class); + when(userService.getUserByAuthentication(any())).thenReturn(defaultUser); + when(statisticsRepository.findByUserId(any())).thenReturn(Optional.empty()); + when(statisticsRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0)); + StatisticsResponseDto result = statisticsService.getStatisticsForUser(authentication); + Assertions.assertEquals(StatisticsResponseDto.builder() + .id(null) + .right(0L) + .wrong(0L) + .total(0L) + .correctRate(0L) + .finishedGames(0L) + .user(defaultUserResponseDto).build() + , result); + } + + @Test + public void getCorrectRateTotalZero(){ + Statistics statistics = Statistics.builder() + .id(1L) + .user(defaultUser) + .correct(0L) + .wrong(0L) + .total(0L) + .finishedGames(1L) + .build(); + Assertions.assertEquals(0L, statistics.getCorrectRate()); + } @Test public void getTopTenStatisticsTestWhenThereAreNotTen(){ when(statisticsRepository.findAll()).thenReturn(List.of(defaultStatistics2, defaultStatistics1)); - when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); - when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2); List<StatisticsResponseDto> result = statisticsService.getTopTenStatistics(); Assertions.assertEquals(List.of(defaultStatisticsResponseDto2,defaultStatisticsResponseDto1), result); } @@ -155,255 +177,8 @@ public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){ .finishedGames(1L) .build(); when(statisticsRepository.findAll()).thenReturn(List.of(defaultStatistics1, defaultStatistics3)); - when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); - when(statisticsResponseDtoMapper.apply(defaultStatistics3)).thenReturn(defaultStatisticsResponseDto3); List<StatisticsResponseDto> result = statisticsService.getTopTenStatistics(); Assertions.assertEquals(List.of(defaultStatisticsResponseDto1,defaultStatisticsResponseDto3), result); } - @Test - public void getTopTenStatisticsWhenThereAreTen(){ - Statistics defaultStatistics3 = Statistics.builder() - .id(3L) - .user(defaultUser) - .correct(1L) - .wrong(9L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics4 = Statistics.builder() - .id(4L) - .user(defaultUser) - .correct(2L) - .wrong(8L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics5 = Statistics.builder() - .id(5L) - .user(defaultUser) - .correct(3L) - .wrong(7L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics6 = Statistics.builder() - .id(6L) - .user(defaultUser) - .correct(4L) - .wrong(6L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics7 = Statistics.builder() - .id(7L) - .user(defaultUser) - .correct(6L) - .wrong(4L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics8 = Statistics.builder() - .id(8L) - .user(defaultUser) - .correct(8L) - .wrong(2L) - .total(10L) - .finishedGames(1L) - .build(); - List<Statistics> statistics = List.of(defaultStatistics8, defaultStatistics2, defaultStatistics7, - defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, defaultStatistics3); - when(statisticsRepository.findAll()).thenReturn(statistics); - when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); - when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2); - when(statisticsResponseDtoMapper.apply(defaultStatistics3)).thenReturn(StatisticsResponseDto.builder() - .id(3L) - .right(1L) - .wrong(9L) - .total(10L) - .correctRate(10L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() - .id(4L) - .right(2L) - .wrong(8L) - .total(10L) - .correctRate(20L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() - .id(5L) - .right(3L) - .wrong(7L) - .total(10L) - .correctRate(30L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() - .id(6L) - .right(4L) - .wrong(6L) - .total(10L) - .correctRate(40L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() - .id(7L) - .right(6L) - .wrong(4L) - .total(10L) - .correctRate(60L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() - .id(8L) - .right(8L) - .wrong(2L) - .total(10L) - .correctRate(80L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - List<StatisticsResponseDto> result = statistics.stream().map(statisticsResponseDtoMapper::apply).toList(); - Assertions.assertEquals(statisticsService.getTopTenStatistics(), result); - } - - @Test - public void getTopTenWhenThereAreMoreThanTen(){ - Statistics defaultStatistics3 = Statistics.builder() - .id(3L) - .user(defaultUser) - .correct(1L) - .wrong(9L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics4 = Statistics.builder() - .id(4L) - .user(defaultUser) - .correct(2L) - .wrong(8L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics5 = Statistics.builder() - .id(5L) - .user(defaultUser) - .correct(3L) - .wrong(7L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics6 = Statistics.builder() - .id(6L) - .user(defaultUser) - .correct(4L) - .wrong(6L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics7 = Statistics.builder() - .id(7L) - .user(defaultUser) - .correct(6L) - .wrong(4L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics8 = Statistics.builder() - .id(8L) - .user(defaultUser) - .correct(8L) - .wrong(2L) - .total(10L) - .finishedGames(1L) - .build(); - Statistics defaultStatistics9 = Statistics.builder() - .id(9L) - .user(defaultUser) - .correct(9L) - .wrong(1L) - .total(10L) - .finishedGames(1L) - .build(); - List<Statistics> statistics = List.of(defaultStatistics9, defaultStatistics8, defaultStatistics2, - defaultStatistics7, defaultStatistics1, defaultStatistics6, defaultStatistics5, defaultStatistics4, - defaultStatistics3); - when(statisticsRepository.findAll()).thenReturn(statistics); - when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1); - when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2); - when(statisticsResponseDtoMapper.apply(defaultStatistics3)).thenReturn(StatisticsResponseDto.builder() - .id(3L) - .right(1L) - .wrong(9L) - .total(10L) - .correctRate(10L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics4)).thenReturn(StatisticsResponseDto.builder() - .id(4L) - .right(2L) - .wrong(8L) - .total(10L) - .correctRate(20L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics5)).thenReturn(StatisticsResponseDto.builder() - .id(5L) - .right(3L) - .wrong(7L) - .total(10L) - .correctRate(30L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics6)).thenReturn(StatisticsResponseDto.builder() - .id(6L) - .right(4L) - .wrong(6L) - .total(10L) - .correctRate(40L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics7)).thenReturn(StatisticsResponseDto.builder() - .id(7L) - .right(6L) - .wrong(4L) - .total(10L) - .correctRate(60L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics8)).thenReturn(StatisticsResponseDto.builder() - .id(8L) - .right(8L) - .wrong(2L) - .total(10L) - .correctRate(80L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - when(statisticsResponseDtoMapper.apply(defaultStatistics9)).thenReturn(StatisticsResponseDto.builder() - .id(9L) - .right(9L) - .wrong(1L) - .total(10L) - .correctRate(90L) - .finishedGames(1L) - .user(defaultUserResponseDto) - .build()); - List<StatisticsResponseDto> result = statistics.stream().limit(10). - map(statisticsResponseDtoMapper::apply).toList(); - Assertions.assertEquals(statisticsService.getTopTenStatistics(), result); - } - } diff --git a/api/src/test/java/lab/en2b/quizapi/user/UserServiceTest.java b/api/src/test/java/lab/en2b/quizapi/user/UserServiceTest.java index ed698280..79e76a43 100644 --- a/api/src/test/java/lab/en2b/quizapi/user/UserServiceTest.java +++ b/api/src/test/java/lab/en2b/quizapi/user/UserServiceTest.java @@ -13,7 +13,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.core.Authentication; import org.springframework.test.context.junit.jupiter.SpringExtension;