Skip to content

Commit

Permalink
Fixes some issues
Browse files Browse the repository at this point in the history
* Use jsonPath for comparison
* Deprecated API usage and correct test formatting
  • Loading branch information
IDragonfire committed May 13, 2017
1 parent cd8e63c commit 144eedf
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 142 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/faforever/api/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ public enum ErrorCode {
this.title = title;
this.detail = detail;
}

public String codeAsString() {
return String.valueOf(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Optional;

Expand Down Expand Up @@ -59,11 +59,11 @@ public void setUp() throws Exception {
public void incrementFirstTime() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.empty());
.thenReturn(Optional.empty());

instance.increment(PLAYER_ID, "111", 3);

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(3));
assertThat(playerAchievement.getState(), is(REVEALED));
Expand All @@ -78,7 +78,7 @@ private void mockAchievement(String achievementId, AchievementType type, Integer
when(achievementRepository.getOne(achievementId)).thenReturn(achievement);
}

private PlayerAchievement catpureSaveEvent() {
private PlayerAchievement captureSaveEvent() {
ArgumentCaptor<PlayerAchievement> captor = ArgumentCaptor.forClass(PlayerAchievement.class);
verify(playerAchievementRepository).save(captor.capture());
return captor.getValue();
Expand All @@ -91,11 +91,11 @@ private PlayerAchievement catpureSaveEvent() {
public void incrementExisting() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.of(createPlayerAchievement(4, REVEALED)));
.thenReturn(Optional.of(createPlayerAchievement(4, REVEALED)));

instance.increment(PLAYER_ID, "111", 3);

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(7));
assertThat(playerAchievement.getState(), is(REVEALED));
Expand All @@ -122,11 +122,11 @@ public void incrementNonIncremental() throws Exception {
public void setStepsAtLeastFirstTime() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.empty());
.thenReturn(Optional.empty());

instance.setStepsAtLeast(PLAYER_ID, "111", 4);

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(4));
assertThat(playerAchievement.getState(), is(REVEALED));
Expand All @@ -139,11 +139,11 @@ public void setStepsAtLeastFirstTime() throws Exception {
public void setStepsAtLeastExistingLessSteps() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.of(createPlayerAchievement(5, REVEALED)));
.thenReturn(Optional.of(createPlayerAchievement(5, REVEALED)));

instance.setStepsAtLeast(PLAYER_ID, "111", 4);

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(5));
assertThat(playerAchievement.getState(), is(REVEALED));
Expand All @@ -156,11 +156,11 @@ public void setStepsAtLeastExistingLessSteps() throws Exception {
public void setStepsAtLeastExistingMoreSteps() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.of(createPlayerAchievement(5, REVEALED)));
.thenReturn(Optional.of(createPlayerAchievement(5, REVEALED)));

instance.setStepsAtLeast(PLAYER_ID, "111", 6);

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(6));
assertThat(playerAchievement.getState(), is(REVEALED));
Expand All @@ -173,11 +173,11 @@ public void setStepsAtLeastExistingMoreSteps() throws Exception {
public void unlockFirstTime() throws Exception {
mockAchievement("111", AchievementType.STANDARD, null);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.empty());
.thenReturn(Optional.empty());

instance.unlock(PLAYER_ID, "111");

PlayerAchievement playerAchievement = catpureSaveEvent();
PlayerAchievement playerAchievement = captureSaveEvent();

assertThat(playerAchievement.getCurrentSteps(), is(CoreMatchers.nullValue()));
assertThat(playerAchievement.getState(), is(UNLOCKED));
Expand All @@ -190,7 +190,7 @@ public void unlockFirstTime() throws Exception {
public void unlockSecondTime() throws Exception {
mockAchievement("111", AchievementType.STANDARD, null);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID))
.thenReturn(Optional.of(createPlayerAchievement(null, UNLOCKED)));
.thenReturn(Optional.of(createPlayerAchievement(null, UNLOCKED)));

instance.unlock(PLAYER_ID, "111");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
Expand All @@ -33,19 +33,19 @@ public void setUp() throws Exception {
@Test
public void update() throws Exception {
AchievementUpdateRequest[] updateRequests = new AchievementUpdateRequest[]{
new AchievementUpdateRequest(1, "111", Operation.INCREMENT, 7),
new AchievementUpdateRequest(1, "222", Operation.INCREMENT, 19),
new AchievementUpdateRequest(1, "333", Operation.SET_STEPS_AT_LEAST, 9),
new AchievementUpdateRequest(1, "444", Operation.SET_STEPS_AT_LEAST, 13),
new AchievementUpdateRequest(1, "555", Operation.UNLOCK, 11),
new AchievementUpdateRequest(1, "666", Operation.UNLOCK, 17),
new AchievementUpdateRequest(1, "111", Operation.INCREMENT, 7),
new AchievementUpdateRequest(1, "222", Operation.INCREMENT, 19),
new AchievementUpdateRequest(1, "333", Operation.SET_STEPS_AT_LEAST, 9),
new AchievementUpdateRequest(1, "444", Operation.SET_STEPS_AT_LEAST, 13),
new AchievementUpdateRequest(1, "555", Operation.UNLOCK, 11),
new AchievementUpdateRequest(1, "666", Operation.UNLOCK, 17),
};
when(achievementService.increment(anyInt(), any(), anyInt())).thenAnswer(invocation ->
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
when(achievementService.setStepsAtLeast(anyInt(), any(), anyInt())).thenAnswer(invocation ->
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED, invocation.getArgument(2)));
when(achievementService.unlock(anyInt(), any())).thenAnswer(invocation ->
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED));
new UpdatedAchievementResponse(invocation.getArgument(1), true, AchievementState.UNLOCKED));

JsonApiDocument result = instance.update(updateRequests);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.faforever.api.data.domain.Clan;
import com.faforever.api.data.domain.ClanMembership;
import com.faforever.api.data.domain.Player;
import com.faforever.api.error.ErrorCode;
import com.faforever.integration.TestDatabase;
import com.faforever.integration.factories.PlayerFactory;
import com.faforever.integration.factories.SessionFactory;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void meDataWithoutClan() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.player.id", is(player.getId())))
.andExpect(jsonPath("$.player.login", is(player.getLogin())))
.andExpect(jsonPath("$.clan", nullValue()));
.andExpect(jsonPath("$.clan", is(nullValue())));
}

@Test
Expand Down Expand Up @@ -120,7 +121,7 @@ public void createClan() throws Exception {

action.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(id)))
.andExpect(jsonPath("$.type", is(String.valueOf("clan"))));
.andExpect(jsonPath("$.type", is("clan")));
assertEquals(1, database.getPlayerRepository().count());
assertEquals(1, database.getClanRepository().count());
assertEquals(1, database.getClanMembershipRepository().count());
Expand Down Expand Up @@ -149,18 +150,18 @@ public void createSecondClan() throws Exception {

action.andExpect(status().isUnprocessableEntity())
.andExpect(jsonPath("$.errors", hasSize(1)))
.andExpect(jsonPath("$.errors[0].status", is(String.valueOf(HttpStatus.UNPROCESSABLE_ENTITY.value()))))
.andExpect(jsonPath("$.errors[0].status", is(HttpStatus.UNPROCESSABLE_ENTITY.toString())))
.andExpect(jsonPath("$.errors[0].title", is("You are already in a clan")))
.andExpect(jsonPath("$.errors[0].detail", is("Clan creator is already member of a clan")))
.andExpect(jsonPath("$.errors[0].code", is("149")));
.andExpect(jsonPath("$.errors[0].code", is(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN.codeAsString())));
assertEquals(1, database.getPlayerRepository().count());
assertEquals(1, database.getClanRepository().count());
assertEquals(1, database.getClanMembershipRepository().count());
}

@Test
public void createClanWithSameName() throws Exception {
Player otherLeader = PlayerFactory.createPlayer("Downloard", database);
Player otherLeader = PlayerFactory.createPlayer("AnotherJunitUser", database);
Session session = SessionFactory.createUserAndGetAccessToken(
database, mvc);
String clanName = "My Cool ClanName";
Expand All @@ -184,7 +185,7 @@ public void createClanWithSameName() throws Exception {
.andExpect(jsonPath("$.errors[0].status", is(HttpStatus.UNPROCESSABLE_ENTITY.toString())))
.andExpect(jsonPath("$.errors[0].title", is("Clan Name already in use")))
.andExpect(jsonPath("$.errors[0].detail", is("The clan name 'My Cool ClanName' is already in use. Please choose a different clan name.")))
.andExpect(jsonPath("$.errors[0].code", is("156")))
.andExpect(jsonPath("$.errors[0].code", is(ErrorCode.CLAN_NAME_EXISTS.codeAsString())))
.andExpect(jsonPath("$.errors[0].meta.args[0]", is("My Cool ClanName")));
assertEquals(2, database.getPlayerRepository().count());
assertEquals(1, database.getClanRepository().count());
Expand All @@ -193,7 +194,7 @@ public void createClanWithSameName() throws Exception {

@Test
public void createClanWithSameTag() throws Exception {
Player otherLeader = PlayerFactory.createPlayer("Downloard", database);
Player otherLeader = PlayerFactory.createPlayer("AnotherJunitUser", database);
Session session = SessionFactory.createUserAndGetAccessToken(
database, mvc);
String clanName = "My Cool ClanName";
Expand All @@ -217,7 +218,7 @@ public void createClanWithSameTag() throws Exception {
.andExpect(jsonPath("$.errors[0].status", is(HttpStatus.UNPROCESSABLE_ENTITY.toString())))
.andExpect(jsonPath("$.errors[0].title", is("Clan Tag already in use")))
.andExpect(jsonPath("$.errors[0].detail", is("The clan tag 'My Cool ClanName' is already in use. Please choose a different clan tag.")))
.andExpect(jsonPath("$.errors[0].code", is("157")))
.andExpect(jsonPath("$.errors[0].code", is(ErrorCode.CLAN_TAG_EXISTS.codeAsString())))
.andExpect(jsonPath("$.errors[0].meta.args[0]", is("My Cool ClanName")));
assertEquals(2, database.getPlayerRepository().count());
assertEquals(1, database.getClanRepository().count());
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/com/faforever/api/clan/ClanServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.jwt.Jwt;

import java.io.IOException;
Expand Down Expand Up @@ -177,10 +177,10 @@ public void generatePlayerInvitationTokenFromNonLeader() throws IOException {
leader.setId(3);

Clan clan = new Clan()
.setId(1)
.setTag("123")
.setName("abc")
.setLeader(leader);
.setId(1)
.setTag("123")
.setName("abc")
.setLeader(leader);

when(clanRepository.findOne(clan.getId())).thenReturn(clan);

Expand All @@ -199,10 +199,10 @@ public void generatePlayerInvitationTokenInvalidPlayer() throws IOException {
requester.setId(1);

Clan clan = new Clan()
.setId(1)
.setTag("123")
.setName("abc")
.setLeader(requester);
.setId(1)
.setTag("123")
.setName("abc")
.setLeader(requester);

when(clanRepository.findOne(clan.getId())).thenReturn(clan);

Expand All @@ -224,9 +224,9 @@ public void generatePlayerInvitationToken() throws IOException {
newMember.setId(2);

Clan clan = new Clan()
.setId(1)
.setTag("123")
.setName("abc");
.setId(1)
.setTag("123")
.setName("abc");
clan.setLeader(requester);

FafApiProperties props = new FafApiProperties();
Expand Down Expand Up @@ -255,7 +255,7 @@ public void acceptPlayerInvitationTokenExpire() throws IOException {
Jwt jwtToken = Mockito.mock(Jwt.class);

when(jwtToken.getClaims()).thenReturn(
String.format("{\"expire\":%s}", expire));
String.format("{\"expire\":%s}", expire));
when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);

try {
Expand All @@ -275,7 +275,7 @@ public void acceptPlayerInvitationTokenInvalidClan() throws IOException {
Jwt jwtToken = Mockito.mock(Jwt.class);

when(jwtToken.getClaims()).thenReturn(
String.format("{\"expire\":%s,\"clan\":{\"id\":42}}", expire));
String.format("{\"expire\":%s,\"clan\":{\"id\":42}}", expire));
when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);

try {
Expand Down Expand Up @@ -352,7 +352,7 @@ public void acceptPlayerInvitationTokenPlayerIAlreadyInAClan() throws IOExceptio
Player newMember = new Player();
newMember.setId(2);
newMember.setClanMemberships(
Collections.singletonList(new ClanMembership().setClan(clan).setPlayer(newMember)));
Collections.singletonList(new ClanMembership().setClan(clan).setPlayer(newMember)));

long expire = System.currentTimeMillis() + 1000 * 3;
Jwt jwtToken = Mockito.mock(Jwt.class);
Expand Down
Loading

0 comments on commit 144eedf

Please sign in to comment.