use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class ClanServiceTest method acceptPlayerInvitationTokenWrongPlayer.
@Test
public void acceptPlayerInvitationTokenWrongPlayer() throws IOException {
String stringToken = "1234";
Player newMember = new Player();
newMember.setId(2);
Clan clan = ClanFactory.builder().build();
Player otherPlayer = new Player();
otherPlayer.setId(3);
long expire = System.currentTimeMillis() + 1000 * 3;
Jwt jwtToken = Mockito.mock(Jwt.class);
when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"newMember\":{\"id\":%s},\"clan\":{\"id\":%s}}", expire, newMember.getId(), clan.getId()));
when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
when(playerRepository.findById(newMember.getId())).thenReturn(Optional.of(newMember));
when(playerService.getPlayer(any())).thenReturn(otherPlayer);
try {
instance.acceptPlayerInvitationToken(stringToken, null);
fail();
} catch (ApiException e) {
assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
}
verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class ClanServiceTest method acceptPlayerInvitationTokenInvalidClan.
@Test
public void acceptPlayerInvitationTokenInvalidClan() throws IOException {
String stringToken = "1234";
long expire = System.currentTimeMillis() + 1000 * 3;
Jwt jwtToken = Mockito.mock(Jwt.class);
when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"clan\":{\"id\":42}}", expire));
when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
try {
instance.acceptPlayerInvitationToken(stringToken, null);
fail();
} catch (ApiException e) {
assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_NOT_EXISTS));
}
verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class ClanServiceTest method createClanWithSameTag.
@Test
public void createClanWithSameTag() {
String clanName = "My cool Clan";
String tag = "123";
String description = "A cool clan for testing";
Player creator = new Player();
creator.setId(1);
when(clanRepository.findOneByName(clanName)).thenReturn(Optional.empty());
when(clanRepository.findOneByTag(tag)).thenReturn(Optional.of(new Clan()));
try {
instance.create(clanName, tag, description, creator);
fail();
} catch (ApiException e) {
assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_TAG_EXISTS));
}
ArgumentCaptor<Clan> clanCaptor = ArgumentCaptor.forClass(Clan.class);
verify(clanRepository, Mockito.times(0)).save(clanCaptor.capture());
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class VotingService method saveVote.
@Transactional
public void saveVote(Vote vote, Player player) {
vote.setPlayer(player);
Assert.notNull(vote.getVotingSubject(), "You must specify a subject");
List<Error> errors = ableToVote(player, vote.getVotingSubject().getId());
if (vote.getVotingAnswers() == null) {
vote.setVotingAnswers(Collections.emptySet());
}
VotingSubject subject = votingSubjectRepository.findById(vote.getVotingSubject().getId()).orElseThrow(() -> new IllegalArgumentException("Subject of vote not found"));
vote.getVotingAnswers().forEach(votingAnswer -> {
VotingChoice votingChoice = votingAnswer.getVotingChoice();
VotingChoice one = votingChoiceRepository.findById(votingChoice.getId()).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST, votingChoice.getId())));
votingAnswer.setVotingChoice(one);
votingAnswer.setVote(vote);
});
subject.getVotingQuestions().forEach(votingQuestion -> {
List<VotingAnswer> votingAnswers = vote.getVotingAnswers().stream().filter(votingAnswer -> votingAnswer.getVotingChoice().getVotingQuestion().equals(votingQuestion)).collect(Collectors.toList());
long countOfAnswers = votingAnswers.size();
int maxAnswers = votingQuestion.getMaxAnswers();
if (maxAnswers < countOfAnswers) {
errors.add(new Error(ErrorCode.TOO_MANY_ANSWERS, countOfAnswers, maxAnswers));
}
if (votingQuestion.isAlternativeQuestion()) {
for (int i = 0; i < countOfAnswers; i++) {
int finalI = i;
long answersWithOrdinal = votingAnswers.stream().filter(votingAnswer -> Objects.equals(votingAnswer.getAlternativeOrdinal(), finalI)).count();
if (answersWithOrdinal == 1) {
continue;
}
errors.add(new Error(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS));
}
}
});
if (!errors.isEmpty()) {
throw new ApiException(errors.toArray(new Error[0]));
}
voteRepository.save(vote);
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class MapService method postProcessZipFiles.
@SneakyThrows
private void postProcessZipFiles(MapUploadData mapUploadData) {
Optional<Path> mapFolder;
try (Stream<Path> mapFolderStream = Files.list(mapUploadData.getBaseDir())) {
mapFolder = mapFolderStream.filter(path -> Files.isDirectory(path)).findFirst();
}
if (!mapFolder.isPresent()) {
throw new ApiException(new Error(ErrorCode.MAP_MISSING_MAP_FOLDER_INSIDE_ZIP));
}
try (Stream<Path> mapFolderStream = Files.list(mapUploadData.getBaseDir())) {
if (mapFolderStream.count() != 2) {
throw new ApiException(new Error(ErrorCode.MAP_INVALID_ZIP));
}
}
mapUploadData.setOriginalMapFolder(mapFolder.get());
mapUploadData.setUploadFolderName(mapUploadData.getOriginalMapFolder().getFileName().toString());
List<Path> filePaths = new ArrayList<>();
try (Stream<Path> mapFileStream = Files.list(mapUploadData.getOriginalMapFolder())) {
mapFileStream.forEach(filePaths::add);
Arrays.stream(REQUIRED_FILES).forEach(filePattern -> {
if (filePaths.stream().noneMatch(filePath -> filePath.toString().endsWith(filePattern))) {
throw new ApiException(new Error(ErrorCode.MAP_FILE_INSIDE_ZIP_MISSING, filePattern));
}
});
}
}
Aggregations