-
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
Showing
10 changed files
with
160 additions
and
12 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/inttest/java/com/faforever/api/event/EventControllerTest.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,112 @@ | ||
package com.faforever.api.event; | ||
|
||
import com.faforever.api.AbstractIntegrationTest; | ||
import com.faforever.api.data.DataController; | ||
import com.faforever.api.security.OAuthScope; | ||
import com.google.common.collect.Lists; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultUser.sql") | ||
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepEventsData.sql") | ||
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:sql/cleanEventsData.sql") | ||
public class EventControllerTest extends AbstractIntegrationTest { | ||
|
||
@Test | ||
public void singleExistingPlayerEventCanBeUpdated() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(1, "15b6c19a-6084-4e82-ada9-6c30e282191f", 10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.data", hasSize(1))) | ||
.andExpect(jsonPath("$.data[0].attributes.currentCount", Matchers.is(31))) | ||
.andExpect(jsonPath("$.data[0].attributes.eventId", Matchers.is("15b6c19a-6084-4e82-ada9-6c30e282191f"))); | ||
} | ||
|
||
@Test | ||
public void singleNonExistingPlayerEventCanBeCreated() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(1, "cc791f00-343c-48d4-b5b3-8900b83209c0", 10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.data", hasSize(1))) | ||
.andExpect(jsonPath("$.data[0].attributes.currentCount", Matchers.is(10))) | ||
.andExpect(jsonPath("$.data[0].attributes.eventId", Matchers.is("cc791f00-343c-48d4-b5b3-8900b83209c0"))); | ||
} | ||
|
||
@Test | ||
public void multipleExistingPlayerEventsCanBeUpdated() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList( | ||
new EventUpdateRequest(1, "15b6c19a-6084-4e82-ada9-6c30e282191f", 10), | ||
new EventUpdateRequest(1, "225e9b2e-ae09-4ae1-a198-eca8780b0fcd", 10) | ||
); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.data", hasSize(2))) | ||
.andExpect(jsonPath("$.data[*].attributes.currentCount", Matchers.containsInAnyOrder(31, 20))) | ||
.andExpect(jsonPath("$.data[*].attributes.eventId", Matchers.containsInAnyOrder("15b6c19a-6084-4e82-ada9-6c30e282191f", "225e9b2e-ae09-4ae1-a198-eca8780b0fcd"))); | ||
} | ||
|
||
@Test | ||
public void authWithoutCorrectScopeShouldFail() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(1, "15b6c19a-6084-4e82-ada9-6c30e282191f", 10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents))) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
public void nonExistingEventShouldFail() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(1, "non-existing-event-id", 10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isUnprocessableEntity()); | ||
} | ||
|
||
@Test | ||
public void nonExistingPlayerShouldFail() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(-1, "15b6c19a-6084-4e82-ada9-6c30e282191f", 10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isUnprocessableEntity()); | ||
} | ||
|
||
@Test | ||
public void negativeCountShouldFail() throws Exception { | ||
List<EventUpdateRequest> updatedEvents = Lists.newArrayList(new EventUpdateRequest(1, "15b6c19a-6084-4e82-ada9-6c30e282191f", -10)); | ||
mockMvc.perform( | ||
patch("/events/update") | ||
.header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE) | ||
.content(objectMapper.writeValueAsString(updatedEvents)) | ||
.with(getOAuthToken(OAuthScope._WRITE_EVENTS))) | ||
.andExpect(status().isUnprocessableEntity()); | ||
} | ||
} |
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,4 @@ | ||
DELETE | ||
FROM player_events; | ||
DELETE | ||
FROM event_definitions; |
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,14 @@ | ||
DELETE | ||
FROM player_events; | ||
DELETE | ||
FROM event_definitions; | ||
INSERT INTO event_definitions (id, name_key, image_url, type) | ||
VALUES ('15b6c19a-6084-4e82-ada9-6c30e282191f', 'event.seraphimWins', null, 'NUMERIC'); | ||
INSERT INTO event_definitions (id, name_key, image_url, type) | ||
VALUES ('225e9b2e-ae09-4ae1-a198-eca8780b0fcd', 'event.lostAirUnits', null, 'NUMERIC'); | ||
INSERT INTO event_definitions (id, name_key, image_url, type) | ||
VALUES ('cc791f00-343c-48d4-b5b3-8900b83209c0', 'event.secondsPlayed', null, 'TIME'); | ||
INSERT INTO player_events (id, player_id, event_id, count, create_time, update_time) | ||
VALUES (1, 1, '15b6c19a-6084-4e82-ada9-6c30e282191f', 21, '2019-01-06 10:48:18', '2019-01-06 10:48:18'); | ||
INSERT INTO player_events (id, player_id, event_id, count, create_time, update_time) | ||
VALUES (2, 1, '225e9b2e-ae09-4ae1-a198-eca8780b0fcd', 10, '2019-01-06 13:36:54', '2019-01-06 13:36:54'); |
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
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
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