Search in sources :

Example 26 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class AchievementService method unlock.

UpdatedAchievementResponse unlock(int playerId, String achievementId) {
    Achievement achievement = achievementRepository.getOne(achievementId);
    if (achievement.getType() != AchievementType.STANDARD) {
        throw new ApiException(new Error(ACHIEVEMENT_NOT_STANDARD, achievementId));
    }
    PlayerAchievement playerAchievement = getOrCreatePlayerAchievement(playerId, achievement, AchievementState.REVEALED);
    boolean newlyUnlocked = playerAchievement.getState() != AchievementState.UNLOCKED;
    if (newlyUnlocked) {
        playerAchievement.setState(AchievementState.UNLOCKED);
        playerAchievementRepository.save(playerAchievement);
    }
    return new UpdatedAchievementResponse(achievementId, newlyUnlocked, playerAchievement.getState());
}
Also used : Error(com.faforever.api.error.Error) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) Achievement(com.faforever.api.data.domain.Achievement) ApiException(com.faforever.api.error.ApiException)

Example 27 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteOnTooManyAnswers.

@Test
public void notSaveVoteOnTooManyAnswers() {
    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(1);
    Vote vote = new Vote();
    VotingAnswer votingAnswer = new VotingAnswer();
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    votingAnswer.setVotingChoice(votingChoice);
    VotingAnswer votingAnswer2 = new VotingAnswer();
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    votingAnswer2.setVotingChoice(votingChoice2);
    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));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.TOO_MANY_ANSWERS)));
    }
    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) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 28 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteIfAlternativeOrdinalWrong.

@Test
public void notSaveVoteIfAlternativeOrdinalWrong() {
    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(1);
    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));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS)));
    }
    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) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 29 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class AvatarService method checkImageDimensions.

private void checkImageDimensions(InputStream imageInputStream, String imageFileName) throws IOException {
    imageInputStream.mark(4096);
    final Dimension imageDimensions = readImageDimensions(imageInputStream, imageFileName);
    imageInputStream.reset();
    final int heightLimit = properties.getAvatar().getImageHeight();
    final int widthLimit = properties.getAvatar().getImageWidth();
    if (imageDimensions.width != widthLimit || imageDimensions.height != heightLimit) {
        throw new ApiException(new Error(ErrorCode.INVALID_AVATAR_DIMENSION, widthLimit, heightLimit, imageDimensions.width, imageDimensions.height));
    }
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Dimension(java.awt.Dimension) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Example 30 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class AvatarService method createAvatar.

@SneakyThrows
@Transactional
@Audit(messageTemplate = "Avatar [''{0}'' - ''{1}''] created.", expressions = { "${avatarMetadata.name}", "${originalFilename}" })
public void createAvatar(AvatarMetadata avatarMetadata, String originalFilename, InputStream imageDataInputStream, long avatarImageFileSize) {
    final Avatar avatarToCreate = new Avatar();
    final String normalizedAvatarFileName = FileNameUtil.normalizeFileName(originalFilename);
    String url = String.format(properties.getAvatar().getDownloadUrlFormat(), normalizedAvatarFileName);
    avatarRepository.findOneByUrl(url).ifPresent(existingAvatar -> {
        throw new ApiException(new Error(ErrorCode.AVATAR_NAME_CONFLICT, normalizedAvatarFileName));
    });
    avatarToCreate.setTooltip(avatarMetadata.getName()).setUrl(url);
    final InputStream markSupportedImageInputStream = getMarkSupportedInputStream(imageDataInputStream);
    validateImageFile(originalFilename, avatarImageFileSize);
    checkImageDimensions(markSupportedImageInputStream, normalizedAvatarFileName);
    final Path imageTargetPath = properties.getAvatar().getTargetDirectory().resolve(normalizedAvatarFileName);
    avatarRepository.save(avatarToCreate);
    copyAvatarFile(markSupportedImageInputStream, imageTargetPath, false);
}
Also used : Path(java.nio.file.Path) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Avatar(com.faforever.api.data.domain.Avatar) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException) Audit(com.faforever.api.security.Audit) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ApiException (com.faforever.api.error.ApiException)48 Error (com.faforever.api.error.Error)29 Player (com.faforever.api.data.domain.Player)18 Test (org.junit.Test)18 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Clan (com.faforever.api.data.domain.Clan)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ArrayList (java.util.ArrayList)6 ZipInputStream (java.util.zip.ZipInputStream)6 Map (com.faforever.api.config.FafApiProperties.Map)5 Vote (com.faforever.api.data.domain.Vote)5 VotingSubject (com.faforever.api.data.domain.VotingSubject)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 Jwt (org.springframework.security.jwt.Jwt)5 LuaValue (org.luaj.vm2.LuaValue)4