Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/game #173

Merged
merged 6 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GameResponseDto newGame(Authentication authentication) {

public GameResponseDto startRound(Long id, Authentication authentication) {
Game game = gameRepository.findByIdForUser(id, userService.getUserByAuthentication(authentication).getId()).orElseThrow();
game.newRound(questionRepository.findRandomQuestion(game.getLanguage()));
game.newRound(questionService.findRandomQuestion(game.getLanguage()));
if (game.isGameOver()){
Statistics statistics = Statistics.builder()
.user(game.getUser())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public QuestionResponseDto getRandomQuestion(String lang) {

return questionResponseDtoMapper.apply(q);
}

public Question findRandomQuestion(String lang){
if (lang==null || lang.isBlank()) {
lang = "en";
}
Question q = questionRepository.findRandomQuestion(lang);
loadAnswers(q);
return q;
}

public QuestionResponseDto getQuestionById(Long id) {
return questionResponseDtoMapper.apply(questionRepository.findById(id).orElseThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -25,7 +26,7 @@ public StatisticsResponseDto getStatisticsForUser(Authentication authentication)
}

public List<StatisticsResponseDto> getTopTenStatistics(){
List<Statistics> all = statisticsRepository.findAll();
List<Statistics> all = new ArrayList<>(statisticsRepository.findAll());
all.sort(Comparator.comparing(Statistics::getCorrectRate).reversed());
List<Statistics> topTen = all.stream().limit(10).collect(Collectors.toList());
return topTen.stream().map(statisticsResponseDtoMapper).collect(Collectors.toList());
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
JWT_EXPIRATION_MS=86400000
REFRESH_TOKEN_DURATION_MS=86400000
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto=validate
spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
Expand Down
22 changes: 11 additions & 11 deletions api/src/test/java/lab/en2b/quizapi/game/GameServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void newGame(){
public void startRound(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
GameResponseDto gameDto = gameService.startRound(1L, authentication);
GameResponseDto result = defaultGameResponseDto;
Expand All @@ -177,7 +177,7 @@ public void startRound(){
@Test
public void startRoundGameOver(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
defaultGame.setActualRound(10);
assertThrows(IllegalStateException.class, () -> gameService.startRound(1L,authentication));
Expand All @@ -187,7 +187,7 @@ public void startRoundGameOver(){
public void startRoundWhenRoundNotFinished(){
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
gameService.startRound(1L,authentication);
assertThrows(IllegalStateException.class, () -> gameService.startRound(1L,authentication));
Expand All @@ -197,7 +197,7 @@ public void startRoundWhenRoundNotFinished(){
public void getCurrentQuestion() {
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
gameService.startRound(1L,authentication);
QuestionResponseDto questionDto = gameService.getCurrentQuestion(1L,authentication);
Expand All @@ -215,7 +215,7 @@ public void getCurrentQuestionRoundNotStarted() {
public void getCurrentQuestionRoundFinished() {
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
gameService.startRound(1L,authentication);
defaultGame.setRoundStartTime(LocalDateTime.now().minusSeconds(100));
Expand All @@ -227,7 +227,7 @@ public void getCurrentQuestionGameFinished() {
when(gameRepository.findByIdForUser(any(), any())).thenReturn(Optional.of(defaultGame));
when(userService.getUserByAuthentication(authentication)).thenReturn(defaultUser);
when(gameRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.startRound(1L,authentication);
defaultGame.setActualRound(10);
assertThrows(IllegalStateException.class, () -> gameService.getCurrentQuestion(1L,authentication));
Expand All @@ -238,7 +238,7 @@ public void answerQuestionCorrectly(){
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(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.newGame(authentication);
gameService.startRound(1L, authentication);
gameService.answerQuestion(1L, new GameAnswerDto(1L), authentication);
Expand All @@ -252,7 +252,7 @@ public void answerQuestionIncorrectly(){
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(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.newGame(authentication);
gameService.startRound(1L, authentication);
gameService.answerQuestion(1L, new GameAnswerDto(2L), authentication);
Expand All @@ -266,7 +266,7 @@ public void answerQuestionWhenGameHasFinished(){
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(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.newGame(authentication);
gameService.startRound(1L, authentication);
defaultGame.setActualRound(30);
Expand All @@ -278,7 +278,7 @@ public void answerQuestionWhenRoundHasFinished(){
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(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.newGame(authentication);
gameService.startRound(1L, authentication);
defaultGame.setRoundStartTime(LocalDateTime.now().minusSeconds(100));
Expand All @@ -290,7 +290,7 @@ public void answerQuestionInvalidId(){
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(questionRepository.findRandomQuestion(any())).thenReturn(defaultQuestion);
when(questionService.findRandomQuestion(any())).thenReturn(defaultQuestion);
gameService.newGame(authentication);
gameService.startRound(1L, authentication);
assertThrows(IllegalArgumentException.class, () -> gameService.answerQuestion(1L, new GameAnswerDto(3L), authentication));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,37 @@ public void getStatisticsForUserTest(){
Assertions.assertEquals(defaultStatisticsResponseDto1, result);
}

/*@Test
@Test
public void getTopTenStatisticsTestWhenThereAreNotTen(){
when(statisticsRepository.findAll()).thenReturn(List.of(defaultStatistics2, defaultStatistics1));
when(statisticsResponseDtoMapper.apply(any())).thenReturn(defaultStatisticsResponseDto1);
when(statisticsResponseDtoMapper.apply(any())).thenReturn(defaultStatisticsResponseDto2);
when(statisticsResponseDtoMapper.apply(any())).thenReturn(defaultStatisticsResponseDto1);
when(statisticsResponseDtoMapper.apply(defaultStatistics1)).thenReturn(defaultStatisticsResponseDto1);
when(statisticsResponseDtoMapper.apply(defaultStatistics2)).thenReturn(defaultStatisticsResponseDto2);
List<StatisticsResponseDto> result = statisticsService.getTopTenStatistics();
Assertions.assertEquals(List.of(defaultStatisticsResponseDto2,defaultStatisticsResponseDto1), result);
}*/
}

@Test
public void getTopTenStatisticsTestWhenThereAreNotTenAndAreEqual(){
Statistics defaultStatistics3 = Statistics.builder()
.id(2L)
.user(defaultUser)
.correct(5L)
.wrong(5L)
.total(10L)
.build();
StatisticsResponseDto defaultStatisticsResponseDto3 = StatisticsResponseDto.builder()
.id(2L)
.right(5L)
.wrong(5L)
.total(10L)
.correctRate(50L)
.user(defaultUserResponseDto)
.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);
}

}