-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a702e17
commit 9116c42
Showing
11 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/faforever/api/avatar/AvatarAssignmentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/java/com/faforever/api/data/JsonApiAvatarIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/com/faforever/integration/factories/AvatarFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters