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());
}
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);
}
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);
}
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));
}
}
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);
}
Aggregations