Search in sources :

Example 1 with Player

use of com.faforever.api.data.domain.Player in project faf-java-api by FAForever.

the class VotingServiceTest method saveVoteInvalidChoiceId.

@Test
public void saveVoteInvalidChoiceId() {
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(true);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    votingQuestion.setMaxAnswers(2);
    Vote vote = new Vote();
    VotingAnswer votingAnswer = new VotingAnswer();
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    votingAnswer.setVotingChoice(votingChoice);
    votingAnswer.setAlternativeOrdinal(0);
    vote.setVotingAnswers(new HashSet<>(Collections.singletonList(votingAnswer)));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty());
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST));
    instance.saveVote(vote, player);
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) VotingChoice(com.faforever.api.data.domain.VotingChoice) Test(org.junit.Test)

Example 2 with Player

use of com.faforever.api.data.domain.Player in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteIfUserVotedAlready.

@Test
public void notSaveVoteIfUserVotedAlready() {
    Vote vote = new Vote();
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(false);
    votingQuestion.setMaxAnswers(1);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.of(new Vote()));
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.VOTED_TWICE)));
    }
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 3 with Player

use of com.faforever.api.data.domain.Player in project faf-java-api by FAForever.

the class VotingServiceTest method saveVoteInvalidVoteId.

@Test
public void saveVoteInvalidVoteId() {
    Vote vote = new Vote();
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    vote.setVotingSubject(votingSubject);
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST));
    instance.saveVote(vote, new Player());
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingSubject(com.faforever.api.data.domain.VotingSubject) Test(org.junit.Test)

Example 4 with Player

use of com.faforever.api.data.domain.Player in project faf-java-api by FAForever.

the class VotingServiceTest method saveVoteIfAlternativeOrdinalCorrect.

@Test
public void saveVoteIfAlternativeOrdinalCorrect() {
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(true);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    votingQuestion.setMaxAnswers(2);
    Vote vote = new Vote();
    VotingAnswer votingAnswer = new VotingAnswer();
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    votingAnswer.setVotingChoice(votingChoice);
    votingAnswer.setAlternativeOrdinal(0);
    VotingAnswer votingAnswer2 = new VotingAnswer();
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    votingAnswer2.setVotingChoice(votingChoice2);
    votingAnswer2.setAlternativeOrdinal(1);
    vote.setVotingAnswers(new HashSet<>(Arrays.asList(votingAnswer, votingAnswer2)));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty());
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    when(votingChoiceRepository.findById(votingChoice.getId())).thenReturn(Optional.of(votingChoice));
    when(votingChoiceRepository.findById(votingChoice2.getId())).thenReturn(Optional.of(votingChoice2));
    instance.saveVote(vote, player);
    verify(voteRepository).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) VotingChoice(com.faforever.api.data.domain.VotingChoice) Test(org.junit.Test)

Example 5 with Player

use of com.faforever.api.data.domain.Player in project faf-java-api by FAForever.

the class ClanServiceTest method generatePlayerInvitationTokenFromNonLeader.

@Test
public void generatePlayerInvitationTokenFromNonLeader() throws IOException {
    Player requester = new Player();
    requester.setId(1);
    Player newMember = new Player();
    newMember.setId(2);
    Player leader = new Player();
    leader.setId(3);
    Clan clan = ClanFactory.builder().leader(leader).build();
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    try {
        instance.generatePlayerInvitationToken(requester, newMember.getId(), clan.getId());
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_NOT_LEADER));
    }
    verify(jwtService, Mockito.never()).sign(any());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Aggregations

Player (com.faforever.api.data.domain.Player)41 Test (org.junit.Test)33 ApiException (com.faforever.api.error.ApiException)18 Clan (com.faforever.api.data.domain.Clan)17 Vote (com.faforever.api.data.domain.Vote)10 VotingSubject (com.faforever.api.data.domain.VotingSubject)10 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)8 VotingChoice (com.faforever.api.data.domain.VotingChoice)7 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)6 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)6 ClanMembership (com.faforever.api.data.domain.ClanMembership)5 VotingAnswer (com.faforever.api.data.domain.VotingAnswer)5 Error (com.faforever.api.error.Error)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 Path (java.nio.file.Path)4 HttpHeaders (org.springframework.http.HttpHeaders)4 Jwt (org.springframework.security.jwt.Jwt)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 InvitationResult (com.faforever.api.clan.result.InvitationResult)3