Skip to content

Commit

Permalink
Add new MapPoolAssignment
Browse files Browse the repository at this point in the history
Resolve MapPool->MapVersion relation into new entity
  • Loading branch information
Sheikah45 authored and Brutus5000 committed Mar 16, 2021
1 parent 28128cc commit 04ca187
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build
on: [push]
env:
FLYWAY_VERSION: 7.5.4
FAF_DB_VERSION: v111
FAF_DB_VERSION: v112
jobs:
test:
runs-on: ubuntu-latest
Expand Down
49 changes: 35 additions & 14 deletions src/inttest/java/com/faforever/api/data/MapPoolElideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,36 @@
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql")
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepMapData.sql")
public class MapPoolElideTest extends AbstractIntegrationTest {
private static final String NEW_LADDER_MAP_BODY = "{\"data\":[{\"type\":\"mapVersion\",\"id\":\"2\"}]}";
private static final String NEW_LADDER_MAP_BODY = """
{
"data": {
"type": "mapPoolAssignment",
"attributes": {
"weight": 1,
"mapParams": "{\\"type\\": \\"none\\"}"
},
"relationships": {
"mapPool": {
"data": {
"type": "mapPool",
"id": "1"
}
},
"mapVersion": {
"data": {
"type": "mapVersion",
"id": "1"
}
}
}
}
}""";


@Test
public void cannotCreateMapPoolItemWithoutScope() throws Exception {
mockMvc.perform(
post("/data/mapPool/1/relationships/mapVersions")
post("/data/mapPoolAssignment")
.with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP))
.header(HttpHeaders.CONTENT_TYPE, JsonApiMediaType.JSON_API_MEDIA_TYPE)
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
Expand All @@ -31,7 +55,7 @@ public void cannotCreateMapPoolItemWithoutScope() throws Exception {
@Test
public void cannotCreateMapPoolItemWithoutRole() throws Exception {
mockMvc.perform(
post("/data/mapPool/1/relationships/mapVersions")
post("/data/mapPoolAssignment")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES))
.header(HttpHeaders.CONTENT_TYPE, JsonApiMediaType.JSON_API_MEDIA_TYPE)
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
Expand All @@ -41,37 +65,34 @@ public void cannotCreateMapPoolItemWithoutRole() throws Exception {
@Test
public void canCreateMapPoolItemWithScopeAndRole() throws Exception {
mockMvc.perform(
post("/data/mapPool/1/relationships/mapVersions")
post("/data/mapPoolAssignment")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP))
.header(HttpHeaders.CONTENT_TYPE, JsonApiMediaType.JSON_API_MEDIA_TYPE)
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
.andExpect(status().isNoContent());
.andExpect(status().isCreated());
}

@Test
public void canDeleteMapPoolItemWithScopeAndRole() throws Exception {
mockMvc.perform(
delete("/data/mapPool/1/relationships/mapVersions/1")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP))
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
delete("/data/mapPoolAssignment/1")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)))
.andExpect(status().isNoContent());
}

@Test
public void cannotDeleteMapPoolItemWithoutScope() throws Exception {
mockMvc.perform(
delete("/data/mapPool/1/relationships/mapVersions/1")
.with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP))
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
delete("/data/mapPoolAssignment/1")
.with(getOAuthTokenWithTestUser(NO_SCOPE, GroupPermission.ROLE_WRITE_MATCHMAKER_MAP)))
.andExpect(status().isForbidden());
}

@Test
public void cannotDeleteMapPoolItemWithoutRole() throws Exception {
mockMvc.perform(
delete("/data/mapPool/1")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES))
.content(NEW_LADDER_MAP_BODY)) // magic value from prepMapData.sql
delete("/data/mapPoolAssignment/1")
.with(getOAuthTokenWithTestUser(OAuthScope._ADMINISTRATIVE_ACTION, NO_AUTHORITIES)))
.andExpect(status().isForbidden());
}
}
7 changes: 4 additions & 3 deletions src/inttest/resources/sql/prepMapData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ INSERT INTO matchmaker_queue_map_pool (matchmaker_queue_id, map_pool_id, min_rat
(1, 4, 1300, 1800),
(1, 5, 1800, null);

INSERT INTO map_pool_map_version (map_pool_id, map_version_id) VALUES
(1,1),
(2,2);
INSERT INTO map_pool_map_version (id, map_pool_id, map_version_id, map_params, weight) VALUES
(1, 1, 1, null, 1),
(2, 1, null, '{"type": "neroxis", "size": 512, "spawns": 2, "version": "1.4.3"}', 1),
(3, 2, 2, null, 1);
18 changes: 6 additions & 12 deletions src/main/java/com/faforever/api/data/domain/MapPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
Expand All @@ -26,7 +25,7 @@
public class MapPool extends AbstractEntity {
private String name;
private MatchmakerQueueMapPool matchmakerQueueMapPool;
private Set<MapVersion> mapVersions;
private Set<MapPoolAssignment> mapPoolAssignments;

@NotNull
public String getName() {
Expand All @@ -38,13 +37,8 @@ public MatchmakerQueueMapPool getMatchmakerQueueMapPool() {
return matchmakerQueueMapPool;
}

@ManyToMany
@JoinTable(name = "map_pool_map_version",
joinColumns = @JoinColumn(name = "map_pool_id"),
inverseJoinColumns = @JoinColumn(name = "map_version_id")
)
@NotNull
public Set<MapVersion> getMapVersions() {
return mapVersions;
@OneToMany(mappedBy = "mapPool", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<MapPoolAssignment> getMapPoolAssignments() {
return mapPoolAssignments;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/faforever/api/data/domain/MapPoolAssignment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.faforever.api.data.domain;

import com.faforever.api.security.elide.permission.WriteMatchmakerMapCheck;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import javax.persistence.Column;
import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Setter
@Table(name = "map_pool_map_version")
@Include(rootLevel = true, type = "mapPoolAssignment")
@CreatePermission(expression = WriteMatchmakerMapCheck.EXPRESSION)
@UpdatePermission(expression = WriteMatchmakerMapCheck.EXPRESSION)
@DeletePermission(expression = WriteMatchmakerMapCheck.EXPRESSION)
public class MapPoolAssignment extends AbstractEntity {
private MapPool mapPool;
private MapVersion mapVersion;
private Integer weight;
private String mapParams;

@OneToOne
@JoinColumn(name = "map_pool_id")
@NotNull
public MapPool getMapPool() {
return mapPool;
}

@OneToOne
@JoinColumn(name = "map_version_id")
public MapVersion getMapVersion() {
return mapVersion;
}

@Column(name = "weight")
@NotNull
public Integer getWeight() {
return weight;
}

@Column(name = "map_params")
public String getMapParams() { return mapParams; }
}
2 changes: 1 addition & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ faf-api:
challonge:
key: ${CHALLONGE_KEY:}
database:
schema-version: ${DATABASE_SCHEMA_VERSION:111}
schema-version: ${DATABASE_SCHEMA_VERSION:112}
mautic:
base-url: ${MAUTIC_BASE_URL:false}
client-id: ${MAUTIC_CLIENT_ID:false}
Expand Down

0 comments on commit 04ca187

Please sign in to comment.