Skip to content

Commit

Permalink
test: game tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Toto-hitori committed Apr 12, 2024
1 parent 203b12c commit 310f558
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 10 deletions.
7 changes: 4 additions & 3 deletions api/src/main/java/lab/en2b/quizapi/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public void setLanguage(String language){
public void setCustomGameMode(CustomGameDto gameDto){
setRounds(gameDto.getRounds());
setRoundDuration(gameDto.getRoundDuration());
setQuestionCategoriesForCustom(gameDto.getCategories());
this.gamemode = CUSTOM;
setQuestionCategoriesForCustom(gameDto.getCategories());
}
public void setGamemode(GameMode gamemode){
if(gamemode == null){
Expand Down Expand Up @@ -172,7 +172,8 @@ private void setGamemodeParams(GameMode gamemode){ //This could be moved to a Ga
setRoundDuration(30);
break;
default:
throw new IllegalStateException("Invalid gamemode!");
setRounds(9L);
setRoundDuration(30);
}
this.gamemode = gamemode;
}
Expand Down Expand Up @@ -204,6 +205,6 @@ private boolean isLanguageSupported(String language) {
}

public boolean shouldBeGameOver() {
return getActualRound() >= getRounds() && !isGameOver;
return getActualRound() >= getRounds() && !isGameOver && currentRoundIsOver();
}
}
17 changes: 17 additions & 0 deletions api/src/test/java/lab/en2b/quizapi/game/GameControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,21 @@ void getQuestionCategoriesShouldReturn403() throws Exception{
.andExpect(status().isForbidden());
}

@Test
void getGameModeshouldReturn200() throws Exception{
mockMvc.perform(get("/games/gamemodes")
.with(user("test").roles("user"))
.contentType("application/json")
.with(csrf()))
.andExpect(status().isOk());
}

@Test
void getGameModesShouldReturn403() throws Exception{
mockMvc.perform(get("/games/gamemodes")
.contentType("application/json")
.with(csrf()))
.andExpect(status().isForbidden());
}

}
83 changes: 76 additions & 7 deletions api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lab.en2b.quizapi.commons.user.UserResponseDto;
import lab.en2b.quizapi.commons.user.UserService;
import lab.en2b.quizapi.commons.user.mappers.UserResponseDtoMapper;
import lab.en2b.quizapi.game.dtos.CustomGameDto;
import lab.en2b.quizapi.game.dtos.GameAnswerDto;
import lab.en2b.quizapi.game.dtos.GameResponseDto;
import lab.en2b.quizapi.game.mappers.GameResponseDtoMapper;
Expand All @@ -14,6 +15,7 @@
import lab.en2b.quizapi.questions.question.*;
import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto;
import lab.en2b.quizapi.questions.question.mappers.QuestionResponseDtoMapper;
import lab.en2b.quizapi.statistics.Statistics;
import lab.en2b.quizapi.statistics.StatisticsRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -30,8 +32,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@ExtendWith({MockitoExtension.class, SpringExtension.class})
public class GameServiceTest {
Expand Down Expand Up @@ -151,16 +152,73 @@ void setUp() {
.build();
}

// NEW GAME TESTS
@Test
public void newGame(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
GameResponseDto gameDto = gameService.newGame(null,null,null,authentication);
assertEquals(defaultGameResponseDto, gameDto);
}

@Test
public void newGameActiveGame(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.findActiveGameForUser(1L)).thenReturn(Optional.of(defaultGame));
GameResponseDto gameDto = gameService.newGame(null,null,null,authentication);
defaultGameResponseDto.setId(1L);
assertEquals(defaultGameResponseDto, gameDto);
}

@Test
public void newGameWasMeantToBeOver(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.findActiveGameForUser(1L)).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
defaultGame.setActualRound(10L);
gameService.newGame(null,null,null,authentication);
verify(statisticsRepository, times(1)).save(any());
}

@Test
public void newGameWasMeantToBeOverExistingLeaderboard(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.findActiveGameForUser(1L)).thenReturn(Optional.of(defaultGame));
when(statisticsRepository.findByUserId(1L)).thenReturn(Optional.of(Statistics.builder().user(new User())
.correct(0L)
.wrong(0L)
.total(0L)
.build()));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
defaultGame.setActualRound(10L);
gameService.newGame(null,null,null,authentication);
verify(statisticsRepository, times(1)).save(any());
}

@Test
public void newGameCustomGame(){
Authentication authentication = mock(Authentication.class);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
GameResponseDto gameDto = gameService.newGame("es",GameMode.CUSTOM,
CustomGameDto.builder()
.roundDuration(30)
.categories(List.of(QuestionCategory.GEOGRAPHY))
.rounds(10L)
.build()
,authentication);
defaultGameResponseDto.setGamemode(GameMode.CUSTOM);
defaultGameResponseDto.setRounds(10L);
defaultGameResponseDto.setRoundDuration(30);

assertEquals(defaultGameResponseDto, gameDto);
}

// START ROUND TESTS
@Test
public void startRound(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
Expand All @@ -184,17 +242,15 @@ public void startRoundGameOver(){
assertThrows(IllegalStateException.class, () -> gameService.startRound(1L,authentication));
}

/**
@Test
public void startRoundWhenRoundNotFinished(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any(),any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
gameService.startRound(1L,authentication);
assertThrows(IllegalStateException.class, () -> gameService.startRound(1L,authentication));
}
**/

@Test
public void getCurrentQuestion() {
Expand All @@ -207,12 +263,12 @@ public void getCurrentQuestion() {
assertEquals(defaultQuestionResponseDto, questionDto);
}

/*@Test
@Test
public void getCurrentQuestionRoundNotStarted() {
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
assertThrows(IllegalStateException.class, () -> gameService.getCurrentQuestion(1L,authentication));
}*/
}

@Test
public void getCurrentQuestionRoundFinished() {
Expand Down Expand Up @@ -278,6 +334,19 @@ public void answerQuestionWhenGameHasFinished(){
assertThrows(IllegalStateException.class, () -> gameService.answerQuestion(1L, new GameAnswerDto(1L), authentication));
}

@Test
public void answerQuestionLastRound(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(questionService.findRandomQuestion(any(),any())).thenReturn(defaultQuestion);
gameService.newGame(null,null,null,authentication);
defaultGame.setActualRound(8L);
gameService.startRound(1L, authentication);
gameService.answerQuestion(1L, new GameAnswerDto(1L), authentication);
verify(statisticsRepository, times(1)).save(any());
}

@Test
public void answerQuestionWhenRoundHasFinished(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
Expand Down

0 comments on commit 310f558

Please sign in to comment.