|
16 | 16 | import org.mockito.junit.jupiter.MockitoExtension; |
17 | 17 |
|
18 | 18 | import java.time.OffsetDateTime; |
19 | | -import java.util.Arrays; |
| 19 | +import java.time.temporal.ChronoUnit; |
20 | 20 | import java.util.Optional; |
21 | 21 | import java.util.Set; |
22 | 22 |
|
23 | 23 | import static com.faforever.api.error.ApiExceptionMatcher.hasErrorCode; |
24 | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
25 | 25 | import static org.junit.jupiter.api.Assertions.assertThrows; |
26 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
27 | 26 | import static org.mockito.ArgumentMatchers.anyInt; |
28 | 27 | import static org.mockito.Mockito.never; |
29 | 28 | import static org.mockito.Mockito.verify; |
@@ -102,7 +101,7 @@ public void notSaveVoteIfUserVotedAlready() { |
102 | 101 | when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
103 | 102 |
|
104 | 103 | ApiException result = assertThrows(ApiException.class, () -> instance.saveVote(vote, player)); |
105 | | - assertTrue(Arrays.stream(result.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.VOTED_TWICE))); |
| 104 | + assertThat(result, hasErrorCode(ErrorCode.VOTED_TWICE)); |
106 | 105 |
|
107 | 106 | verify(voteRepository, never()).save(vote); |
108 | 107 | } |
@@ -176,7 +175,6 @@ public void saveVoteInvalidChoiceId() { |
176 | 175 | when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty()); |
177 | 176 | when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
178 | 177 |
|
179 | | - |
180 | 178 | ApiException result = assertThrows(ApiException.class, () -> instance.saveVote(vote, player)); |
181 | 179 | assertThat(result, hasErrorCode(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST)); |
182 | 180 |
|
@@ -219,11 +217,9 @@ public void notSaveVoteIfAlternativeOrdinalWrong() { |
219 | 217 | when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
220 | 218 | when(votingChoiceRepository.findById(anyInt())).thenReturn(Optional.of(votingChoice)).thenReturn(Optional.of(votingChoice2)); |
221 | 219 |
|
222 | | - try { |
223 | | - instance.saveVote(vote, player); |
224 | | - } catch (ApiException e) { |
225 | | - assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS))); |
226 | | - } |
| 220 | + ApiException result = assertThrows(ApiException.class, () -> instance.saveVote(vote, player)); |
| 221 | + assertThat(result, hasErrorCode(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS)); |
| 222 | + |
227 | 223 | verify(voteRepository, never()).save(vote); |
228 | 224 | } |
229 | 225 |
|
@@ -261,11 +257,59 @@ public void notSaveVoteOnTooManyAnswers() { |
261 | 257 | when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
262 | 258 | when(votingChoiceRepository.findById(anyInt())).thenReturn(Optional.of(votingChoice)).thenReturn(Optional.of(votingChoice2)); |
263 | 259 |
|
264 | | - try { |
265 | | - instance.saveVote(vote, player); |
266 | | - } catch (ApiException e) { |
267 | | - assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.TOO_MANY_ANSWERS))); |
268 | | - } |
| 260 | + ApiException result = assertThrows(ApiException.class, () -> instance.saveVote(vote, player)); |
| 261 | + assertThat(result, hasErrorCode(ErrorCode.TOO_MANY_ANSWERS)); |
| 262 | + |
| 263 | + verify(voteRepository, never()).save(vote); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void notSaveVoteOnNotEnoughGames() { |
| 268 | + Vote vote = new Vote(); |
| 269 | + VotingSubject votingSubject = new VotingSubject(); |
| 270 | + votingSubject.setId(1); |
| 271 | + votingSubject.setBeginOfVoteTime(OffsetDateTime.now()); |
| 272 | + votingSubject.setEndOfVoteTime(OffsetDateTime.MAX); |
| 273 | + VotingQuestion votingQuestion = new VotingQuestion(); |
| 274 | + votingQuestion.setAlternativeQuestion(false); |
| 275 | + votingQuestion.setMaxAnswers(1); |
| 276 | + votingSubject.setVotingQuestions(Set.of(votingQuestion)); |
| 277 | + votingSubject.setMinGamesToVote(100); |
| 278 | + |
| 279 | + vote.setVotingSubject(votingSubject); |
| 280 | + Player player = new Player(); |
| 281 | + |
| 282 | + when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty()); |
| 283 | + when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
| 284 | + |
| 285 | + ApiException result = assertThrows(ApiException.class, () -> instance.saveVote(vote, player)); |
| 286 | + assertThat(result, hasErrorCode(ErrorCode.NOT_ENOUGH_GAMES)); |
| 287 | + |
269 | 288 | verify(voteRepository, never()).save(vote); |
270 | 289 | } |
| 290 | + |
| 291 | + @Test |
| 292 | + public void saveVoteOnNotEnoughGamesButSteamLinkAndAccountAge() { |
| 293 | + Vote vote = new Vote(); |
| 294 | + VotingSubject votingSubject = new VotingSubject(); |
| 295 | + votingSubject.setId(1); |
| 296 | + votingSubject.setBeginOfVoteTime(OffsetDateTime.now()); |
| 297 | + votingSubject.setEndOfVoteTime(OffsetDateTime.MAX); |
| 298 | + VotingQuestion votingQuestion = new VotingQuestion(); |
| 299 | + votingQuestion.setAlternativeQuestion(false); |
| 300 | + votingQuestion.setMaxAnswers(1); |
| 301 | + votingSubject.setVotingQuestions(Set.of(votingQuestion)); |
| 302 | + votingSubject.setMinGamesToVote(100); |
| 303 | + |
| 304 | + vote.setVotingSubject(votingSubject); |
| 305 | + Player player = new Player(); |
| 306 | + player.setSteamId("someSteamId"); |
| 307 | + player.setCreateTime(OffsetDateTime.now().minus(5, ChronoUnit.YEARS)); |
| 308 | + |
| 309 | + when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty()); |
| 310 | + when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject)); |
| 311 | + |
| 312 | + instance.saveVote(vote, player); |
| 313 | + verify(voteRepository).save(vote); |
| 314 | + } |
271 | 315 | } |
0 commit comments