Skip to content

Commit

Permalink
Implement basic tests for avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
IDragonfire committed May 9, 2017
1 parent a702e17 commit 9116c42
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faforever.api.avatar;

import com.faforever.api.data.domain.AvatarAssignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AvatarAssignmentRepository extends JpaRepository<AvatarAssignment, Integer> {
}
9 changes: 9 additions & 0 deletions src/main/java/com/faforever/api/avatar/AvatarRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.faforever.api.avatar;

import com.faforever.api.data.domain.Avatar;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AvatarRepository extends JpaRepository<Avatar, Integer> {
}
2 changes: 2 additions & 0 deletions src/main/java/com/faforever/api/data/domain/Avatar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.time.OffsetDateTime;
import java.util.List;

Expand Down Expand Up @@ -47,6 +48,7 @@ public OffsetDateTime getUpdateTime() {
}

@Column(name = "url")
@NotNull
public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.time.OffsetDateTime;

@Entity
Expand Down Expand Up @@ -39,12 +40,14 @@ public OffsetDateTime getExpiresAt() {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idAvatar")
@NotNull
public Avatar getAvatar() {
return avatar;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idUser")
@NotNull
public Player getPlayer() {
return player;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void createSecondClan() throws Exception {

@Test
public void createClanWithSameName() throws Exception {
Player otherLeader = PlayerFactory.createPlayer("Downloard", database);
Player otherLeader = PlayerFactory.builder().database(database).build();
Session session = SessionFactory.createUserAndGetAccessToken(
database, mvc);
String clanName = "My Cool ClanName";
Expand Down Expand Up @@ -193,7 +193,7 @@ public void createClanWithSameName() throws Exception {

@Test
public void createClanWithSameTag() throws Exception {
Player otherLeader = PlayerFactory.createPlayer("Downloard", database);
Player otherLeader = PlayerFactory.builder().database(database).build();
Session session = SessionFactory.createUserAndGetAccessToken(
database, mvc);
String clanName = "My Cool ClanName";
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/com/faforever/api/data/JsonApiAvatarIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.faforever.api.data;

import com.faforever.api.data.domain.Avatar;
import com.faforever.api.data.domain.Player;
import com.faforever.integration.TestDatabase;
import com.faforever.integration.factories.AvatarFactory;
import com.faforever.integration.factories.PlayerFactory;
import com.faforever.integration.utils.MockMvcHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;
import javax.servlet.Filter;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TestDatabase.class)
public class JsonApiAvatarIntegrationTest {

private MockMvc mvc;
private WebApplicationContext context;
private Filter springSecurityFilterChain;

private TestDatabase database;

@Inject
public void init(TestDatabase database, WebApplicationContext context, Filter springSecurityFilterChain) {
this.context = context;
this.springSecurityFilterChain = springSecurityFilterChain;
this.database = database;
}

@Before
public void setUp() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.addFilter(springSecurityFilterChain)
.build();
database.assertEmptyDatabase();
}

@After
public void tearDown() {
// TODO: This is needed, because Elide has some problems with @Transactional annotation #71
database.tearDown();
}


@Test
public void getSingleAvatar() throws Exception {
Avatar avatar = AvatarFactory.builder().database(database).build();

assertEquals(1, database.getAvatarRepository().count());

MockMvcHelper.of(this.mvc).perform(get("/data/avatar/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data", hasSize(1)))
.andExpect(jsonPath("$.data[0].id", is(String.valueOf(avatar.getId()))))
.andExpect(jsonPath("$.data[0].type", is("avatar")))
.andExpect(jsonPath("$.data[0].attributes.tooltip", is(avatar.getTooltip())))
.andExpect(jsonPath("$.data[0].attributes.url", is(avatar.getUrl())))
.andExpect(jsonPath("$.data[0].relationships.assignments.data", hasSize(0)));
assertEquals(1, database.getAvatarRepository().count());
}

@Test
public void getSingleAvatarWithPlayer() throws Exception {
Player player = PlayerFactory.builder().database(database).build();
Avatar avatar = AvatarFactory.builder().player(player).database(database).build();

assertEquals(1, database.getAvatarRepository().count());

MockMvcHelper.of(this.mvc).perform(get("/data/avatar/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data", hasSize(1)))
.andExpect(jsonPath("$.data[0].id", is(String.valueOf(avatar.getId()))))
.andExpect(jsonPath("$.data[0].type", is("avatar")))
.andExpect(jsonPath("$.data[0].attributes.tooltip", is(avatar.getTooltip())))
.andExpect(jsonPath("$.data[0].attributes.url", is(avatar.getUrl())))
.andExpect(jsonPath("$.data[0].relationships.assignments.data", hasSize(1)))
.andExpect(jsonPath("$.data[0].relationships.assignments.data[0].id",
is(String.valueOf(avatar.getAssignments().get(0).getId()))))
.andExpect(jsonPath("$.data[0].relationships.assignments.data[0].type", is("avatarAssignment")));

assertEquals(1, database.getAvatarRepository().count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void cannotKickLeaderFromClan() throws Exception {
public void cannotKickAsMember() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken(database, mvc);

Player bob = PlayerFactory.createPlayer("Bob", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();
Clan clan = new Clan().setLeader(bob).setTag("123").setName("abcClanName");
ClanMembership myMembership = new ClanMembership().setPlayer(session.getPlayer()).setClan(clan);
ClanMembership bobsMembership = new ClanMembership().setPlayer(bob).setClan(clan);
Expand All @@ -118,7 +118,7 @@ public void canKickMember() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken(database, mvc);
Player player = session.getPlayer();

Player bob = PlayerFactory.createPlayer("Bob", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();
Clan clan = new Clan().setLeader(player).setTag("123").setName("abcClanName");
ClanMembership myMembership = new ClanMembership().setPlayer(player).setClan(clan);
ClanMembership bobsMembership = new ClanMembership().setPlayer(bob).setClan(clan);
Expand All @@ -138,7 +138,7 @@ public void canKickMember() throws Exception {
public void canLeaveClan() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken(database, mvc);

Player bob = PlayerFactory.createPlayer("Bob", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();
Player player = session.getPlayer();

Clan clan = new Clan().setLeader(bob).setTag("123").setName("abcClanName");
Expand All @@ -158,7 +158,7 @@ public void canLeaveClan() throws Exception {
@Test
public void getFilteredPlayerForClanInvite() throws Exception {
String[] players = new String[]{"Dragonfire", "DRAGON", "Fire of Dragon", "d r a g o n", "firedragon"};
Arrays.stream(players).forEach(name -> noCatch(() -> PlayerFactory.createPlayer(name, database)));
Arrays.stream(players).forEach(name -> noCatch(() -> PlayerFactory.builder().login(name).database(database).build()));
assertEquals(players.length, database.getPlayerRepository().count());
ResultActions action = MockMvcHelper.of(this.mvc).perform(get("/data/player?filter=login==dragon*&sort=login"));

Expand Down Expand Up @@ -195,7 +195,7 @@ public void transferLeadership() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken("Leader", "foo", database, mvc);
Player player = session.getPlayer();

Player bob = PlayerFactory.createPlayer("Bob", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();
Clan clan = new Clan().setLeader(player).setTag("123").setName("abcClanName");
ClanMembership myMembership = new ClanMembership().setPlayer(player).setClan(clan);
ClanMembership bobsMembership = new ClanMembership().setPlayer(bob).setClan(clan);
Expand Down Expand Up @@ -247,7 +247,7 @@ public void transferLeadershipToNonClanMember() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken("Leader", "foo", database, mvc);
Player player = session.getPlayer();

Player bob = PlayerFactory.createPlayer("Bob", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();

Clan clan = new Clan().setLeader(player).setTag("123").setName("abcClanName");
ClanMembership myMembership = new ClanMembership().setPlayer(player).setClan(clan);
Expand Down Expand Up @@ -277,8 +277,8 @@ public void transferLeadershipAsNonLeader() throws Exception {
Session session = SessionFactory.createUserAndGetAccessToken("Leader", "foo", database, mvc);
Player player = session.getPlayer();

Player bob = PlayerFactory.createPlayer("Bob", database);
Player charlie = PlayerFactory.createPlayer("Charlie", database);
Player bob = PlayerFactory.builder().login("Bob").database(database).build();
Player charlie = PlayerFactory.builder().login("Charlie").database(database).build();

Clan clan = new Clan().setLeader(bob).setTag("123").setName("abcClanName");
ClanMembership myMembership = new ClanMembership().setPlayer(player).setClan(clan);
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/com/faforever/integration/TestDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.faforever.integration;

import com.faforever.api.avatar.AvatarAssignmentRepository;
import com.faforever.api.avatar.AvatarRepository;
import com.faforever.api.clan.ClanMembershipRepository;
import com.faforever.api.clan.ClanRepository;
import com.faforever.api.client.OAuthClientRepository;
Expand All @@ -20,22 +22,29 @@ public class TestDatabase {
private ClanMembershipRepository clanMembershipRepository;
private PlayerRepository playerRepository;
private OAuthClientRepository oAuthClientRepository;

private AvatarRepository avatarRepository;
private AvatarAssignmentRepository avatarAssignmentRepository;

@Inject
public void init(ClanRepository clanRepository,
UserRepository userRepository,
PlayerRepository playerRepository,
OAuthClientRepository oAuthClientRepository,
ClanMembershipRepository clanMembershipRepository) {
ClanMembershipRepository clanMembershipRepository,
AvatarRepository avatarRepository,
AvatarAssignmentRepository avatarAssignmentRepository) {
this.clanRepository = clanRepository;
this.userRepository = userRepository;
this.playerRepository = playerRepository;
this.oAuthClientRepository = oAuthClientRepository;
this.clanMembershipRepository = clanMembershipRepository;
this.avatarRepository = avatarRepository;
this.avatarAssignmentRepository = avatarAssignmentRepository;
}

public void assertEmptyDatabase() {
assertEquals(0, avatarAssignmentRepository.count());
assertEquals(0, avatarRepository.count());
assertEquals(0, clanRepository.count());
assertEquals(0, userRepository.count());
assertEquals(0, playerRepository.count());
Expand All @@ -44,6 +53,8 @@ public void assertEmptyDatabase() {
}

public void tearDown() {
avatarAssignmentRepository.deleteAll();
avatarRepository.deleteAll();
clanMembershipRepository.deleteAll();
clanRepository.deleteAll();
userRepository.deleteAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.faforever.integration.factories;

import com.faforever.api.data.domain.Avatar;
import com.faforever.api.data.domain.AvatarAssignment;
import com.faforever.api.data.domain.Player;
import com.faforever.integration.TestDatabase;
import lombok.Builder;
import org.springframework.util.Assert;

import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class AvatarFactory {
public static final String DEFAULT_TOOLTIP = "Cool Test Avatar Tooltip";
public static final String DEFAULT_URL = "www.faf-test.org/myavatar.png";

@Builder
public static Avatar createAvatar(String tooltip, String url,
Player player,
TestDatabase database) {
Assert.notNull(database, "'database' must not be null");
Avatar avatar = new Avatar()
.setTooltip(tooltip != null ? tooltip : DEFAULT_TOOLTIP)
.setUrl(url != null ? url : DEFAULT_URL);

long assignmentCount = database.getAvatarAssignmentRepository().count();
if (player != null) {
AvatarAssignment assignment = new AvatarAssignment()
.setAvatar(avatar)
.setPlayer(player);
avatar.setAssignments(Collections.singletonList(assignment));
assignmentCount++;
}

long count = database.getAvatarRepository().count();
database.getAvatarRepository().save(avatar);
assertEquals(count + 1, database.getAvatarRepository().count());
assertEquals(assignmentCount, database.getAvatarAssignmentRepository().count());
return avatar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
import com.faforever.api.data.domain.Player;
import com.faforever.api.data.domain.User;
import com.faforever.integration.TestDatabase;
import lombok.Builder;
import org.springframework.util.Assert;

import static org.junit.Assert.assertEquals;

public class PlayerFactory {
public static Player createPlayer(String login, TestDatabase database) throws Exception {
private static final String DEFAULT_USERNAME = "JUnitTestUser_PlayerFactory";
private static final String DEFAULT_PASSWORD = "testPassword";


@Builder
private static Player create(String login, String password, TestDatabase database) throws Exception {
Assert.notNull(database, "'database' must not be null");
User user = (User) new User()
.setPassword("foo")
.setLogin(login)
.setEmail(login + "@faforever.com");
.setPassword(password != null ? password : DEFAULT_PASSWORD)
.setLogin(login != null ? login : DEFAULT_USERNAME)
.setEmail(login + "@junit-test.org");
long count = database.getUserRepository().count();
database.getUserRepository().save(user);
assertEquals(count + 1, database.getUserRepository().count());
return database.getPlayerRepository().findOne(user.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SessionFactory {
@SneakyThrows
public static Session createUserAndGetAccessToken(TestDatabase database,
MockMvc mvc) {
return createUserAndGetAccessToken("JUnitOnDragonFire", "myCoolPassword",
return createUserAndGetAccessToken("JUnitTestUser_SessionFactory", "myCoolPassword",
database, mvc);
}

Expand Down

0 comments on commit 9116c42

Please sign in to comment.