Skip to content

Commit 64263ac

Browse files
committed
Replace Spring Data getById
It is deprecated in Spring Data JPA 3.1
1 parent 9cf2799 commit 64263ac

File tree

8 files changed

+53
-54
lines changed

8 files changed

+53
-54
lines changed

src/inttest/java/com/faforever/api/clan/ClanControllerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ClanControllerTest extends AbstractIntegrationTest {
4545

4646
@Test
4747
public void meDataWithoutClan() throws Exception {
48-
Player player = playerRepository.getById(USERID_USER);
48+
Player player = playerRepository.findById(USERID_USER).orElseThrow();
4949

5050
mockMvc.perform(get("/clans/me")
5151
.with(getOAuthTokenForUserId(USERID_USER)))
@@ -57,8 +57,8 @@ public void meDataWithoutClan() throws Exception {
5757

5858
@Test
5959
public void meDataWithClan() throws Exception {
60-
Player player = playerRepository.getById(USERID_CLAN_MEMBER);
61-
Clan clan = clanRepository.getById(1);
60+
Player player = playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow();
61+
Clan clan = clanRepository.findById(1).orElseThrow();
6262

6363
mockMvc.perform(
6464
get("/clans/me")
@@ -73,7 +73,7 @@ public void meDataWithClan() throws Exception {
7373

7474
@Test
7575
public void createClanWithSuccess() throws Exception {
76-
Player player = playerRepository.getById(USERID_USER);
76+
Player player = playerRepository.findById(USERID_USER).orElseThrow();
7777

7878
assertNull(player.getClan());
7979
assertFalse(clanRepository.findOneByName(NEW_CLAN_NAME).isPresent());
@@ -111,7 +111,7 @@ public void createClanWithoutAuth() throws Exception {
111111

112112
@Test
113113
public void createClanWithExistingName() throws Exception {
114-
Player player = playerRepository.getById(USERID_USER);
114+
Player player = playerRepository.findById(USERID_USER).orElseThrow();
115115

116116
assertNull(player.getClan());
117117
assertTrue(clanRepository.findOneByName(EXISTING_CLAN).isPresent());
@@ -133,7 +133,7 @@ public void createClanWithExistingName() throws Exception {
133133

134134
@Test
135135
public void createClanWithExistingTag() throws Exception {
136-
Player player = playerRepository.getById(USERID_USER);
136+
Player player = playerRepository.findById(USERID_USER).orElseThrow();
137137

138138
assertNull(player.getClan());
139139
assertFalse(clanRepository.findOneByName(NEW_CLAN_NAME).isPresent());
@@ -155,7 +155,7 @@ public void createClanWithExistingTag() throws Exception {
155155

156156
@Test
157157
public void createSecondClan() throws Exception {
158-
Player player = playerRepository.getById(USERID_CLAN_MEMBER);
158+
Player player = playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow();
159159

160160
assertNotNull(player.getClan());
161161
assertFalse(clanRepository.findOneByName(NEW_CLAN_NAME).isPresent());

src/inttest/java/com/faforever/api/data/ClanElideTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public class ClanElideTest extends AbstractIntegrationTest {
4848

4949
@Test
5050
public void canDeleteMemberOfOwnClan() throws Exception {
51-
assertNotNull(playerRepository.getById(USERID_CLAN_MEMBER).getClan());
51+
assertNotNull(playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow().getClan());
5252

5353
mockMvc.perform(
5454
delete("/data/clanMembership/2") // magic value from prepClanData.sql
5555
.with(getOAuthTokenForUserId(USERID_CLAN_LEADER)))
5656
.andExpect(status().isNoContent());
57-
assertNull(playerRepository.getById(USERID_CLAN_MEMBER).getClan());
57+
assertNull(playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow().getClan());
5858
}
5959

6060
@Test
@@ -77,14 +77,14 @@ public void cannotDeleteLeaderFromClan() throws Exception {
7777

7878
@Test
7979
public void canLeaveClan() throws Exception {
80-
assertNotNull(playerRepository.getById(USERID_CLAN_MEMBER).getClan());
80+
assertNotNull(playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow().getClan());
8181

8282
mockMvc.perform(
8383
delete("/data/clanMembership/2") // magic value from prepClanData.sql
8484
.with(getOAuthTokenForUserId(USERID_CLAN_MEMBER)))
8585
.andExpect(status().isNoContent());
8686

87-
assertNull(playerRepository.getById(USERID_CLAN_MEMBER).getClan());
87+
assertNull(playerRepository.findById(USERID_CLAN_MEMBER).orElseThrow().getClan());
8888
}
8989

9090
@Test
@@ -108,7 +108,7 @@ public void getFilteredPlayerForClanInvite() throws Exception {
108108

109109
@Test
110110
public void canTransferLeadershipAsLeader() throws Exception {
111-
assertThat(clanRepository.getById(1).getLeader().getLogin(), is(AUTH_CLAN_LEADER));
111+
assertThat(clanRepository.findById(1).orElseThrow().getLeader().getLogin(), is(AUTH_CLAN_LEADER));
112112

113113
mockMvc.perform(
114114
patch("/data/clan/1")
@@ -117,12 +117,12 @@ public void canTransferLeadershipAsLeader() throws Exception {
117117
.content(generateTransferLeadershipContent(1, USERID_CLAN_MEMBER))) // magic value from prepClanData.sql
118118
.andExpect(status().isNoContent());
119119

120-
assertThat(clanRepository.getById(1).getLeader().getLogin(), is(AUTH_CLAN_MEMBER));
120+
assertThat(clanRepository.findById(1).orElseThrow().getLeader().getLogin(), is(AUTH_CLAN_MEMBER));
121121
}
122122

123123
@Test
124124
public void cannotTransferLeadershipAsMember() throws Exception {
125-
assertThat(clanRepository.getById(1).getLeader().getLogin(), is(AUTH_CLAN_LEADER));
125+
assertThat(clanRepository.findById(1).orElseThrow().getLeader().getLogin(), is(AUTH_CLAN_LEADER));
126126

127127
mockMvc.perform(
128128
patch("/data/clan/1")
@@ -132,7 +132,7 @@ public void cannotTransferLeadershipAsMember() throws Exception {
132132
.andExpect(status().isForbidden())
133133
.andExpect(jsonPath("$.errors[0].detail", is("UpdatePermission Denied")));
134134

135-
assertThat(clanRepository.getById(1).getLeader().getLogin(), is(AUTH_CLAN_LEADER));
135+
assertThat(clanRepository.findById(1).orElseThrow().getLeader().getLogin(), is(AUTH_CLAN_LEADER));
136136
}
137137

138138
@Test

src/inttest/java/com/faforever/api/data/MapElideTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void canUpdateMapRecommendationToTrueWithScopeAndRole() throws Exception
6868
.content(MAP_RECOMMENDED_TRUE_ID_1))
6969
.andExpect(status().isNoContent());
7070

71-
assertThat(mapRepository.getById(1).getRecommended(), is(true));
71+
assertThat(mapRepository.findById(1).orElseThrow().getRecommended(), is(true));
7272
}
7373

7474
@Test
@@ -81,7 +81,7 @@ public void cannotUpdateMapRecommendationToTrueWithoutScope() throws Exception {
8181
.content(MAP_RECOMMENDED_TRUE_ID_1))
8282
.andExpect(status().isForbidden());
8383

84-
assertThat(mapRepository.getById(1).getRecommended(), is(false));
84+
assertThat(mapRepository.findById(1).orElseThrow().getRecommended(), is(false));
8585
}
8686

8787
@Test
@@ -94,7 +94,7 @@ public void cannotUpdateMapRecommendationToTrueWithoutRole() throws Exception {
9494
.content(MAP_RECOMMENDED_TRUE_ID_1))
9595
.andExpect(status().isForbidden());
9696

97-
assertThat(mapRepository.getById(1).getRecommended(), is(false));
97+
assertThat(mapRepository.findById(1).orElseThrow().getRecommended(), is(false));
9898
}
9999

100100
@Test
@@ -107,7 +107,7 @@ public void canUpdateMapRecommendationToFalseWithScopeAndRole() throws Exception
107107
.content(MAP_RECOMMENDED_FALSE_ID_1))
108108
.andExpect(status().isNoContent());
109109

110-
assertThat(mapRepository.getById(1).getRecommended(), is(false));
110+
assertThat(mapRepository.findById(1).orElseThrow().getRecommended(), is(false));
111111
}
112112

113113
@Test

src/inttest/java/com/faforever/api/data/ModElideTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void canUpdateModRecommendationToTrueWithScopeAndRole() throws Exception
6868
.content(MOD_RECOMMENDED_TRUE_ID_1))
6969
.andExpect(status().isNoContent());
7070

71-
assertThat(modRepository.getById(1).getRecommended(), is(true));
71+
assertThat(modRepository.findById(1).orElseThrow().getRecommended(), is(true));
7272
}
7373

7474
@Test
@@ -81,7 +81,7 @@ public void cannotUpdateModRecommendationToTrueWithoutScope() throws Exception {
8181
.content(MOD_RECOMMENDED_TRUE_ID_1))
8282
.andExpect(status().isForbidden());
8383

84-
assertThat(modRepository.getById(1).getRecommended(), is(false));
84+
assertThat(modRepository.findById(1).orElseThrow().getRecommended(), is(false));
8585
}
8686

8787
@Test
@@ -94,7 +94,7 @@ public void cannotUpdateModRecommendationToTrueWithoutRole() throws Exception {
9494
.content(MOD_RECOMMENDED_TRUE_ID_1))
9595
.andExpect(status().isForbidden());
9696

97-
assertThat(modRepository.getById(1).getRecommended(), is(false));
97+
assertThat(modRepository.findById(1).orElseThrow().getRecommended(), is(false));
9898
}
9999

100100
@Test
@@ -107,7 +107,7 @@ public void canUpdateModRecommendationToFalseWithScopeAndRole() throws Exception
107107
.content(MOD_RECOMMENDED_FALSE_ID_1))
108108
.andExpect(status().isNoContent());
109109

110-
assertThat(modRepository.getById(1).getRecommended(), is(false));
110+
assertThat(modRepository.findById(1).orElseThrow().getRecommended(), is(false));
111111
}
112112

113113
@Test

src/inttest/java/com/faforever/api/data/UserNoteTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ public void cannotCreateUserNoteWithoutRole() throws Exception {
117117

118118
@Test
119119
public void canCreateUserNoteWithScopeAndRole() throws Exception {
120-
assertThat(playerRepository.getById(3).getUserNotes().size(), is(0));
120+
assertThat(playerRepository.findById(3).orElseThrow().getUserNotes().size(), is(0));
121121

122122
mockMvc.perform(post("/data/userNote")
123123
.with(getOAuthTokenWithActiveUser(OAuthScope._READ_SENSIBLE_USERDATA, GroupPermission.ROLE_ADMIN_ACCOUNT_NOTE))
124124
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
125125
.content(testPost))
126126
.andExpect(status().isCreated());
127127

128-
assertThat(playerRepository.getById(3).getUserNotes().size(), is(1));
128+
assertThat(playerRepository.findById(3).orElseThrow().getUserNotes().size(), is(1));
129129
}
130130
}

src/inttest/java/com/faforever/api/data/VotingElideTest.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package com.faforever.api.data;
22

3+
import com.faforever.api.AbstractIntegrationTest;
4+
import com.faforever.api.data.domain.GroupPermission;
5+
import com.faforever.api.data.domain.VotingChoice;
6+
import com.faforever.api.data.domain.VotingQuestion;
7+
import com.faforever.api.security.OAuthScope;
8+
import com.faforever.api.voting.VotingQuestionRepository;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.HttpHeaders;
12+
import org.springframework.http.MediaType;
13+
import org.springframework.test.context.jdbc.Sql;
14+
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
15+
16+
import java.time.OffsetDateTime;
17+
import java.time.format.DateTimeFormatter;
18+
import java.util.List;
19+
320
import static com.faforever.api.data.JsonApiMediaType.JSON_API_MEDIA_TYPE;
421
import static org.hamcrest.MatcherAssert.assertThat;
522
import static org.hamcrest.Matchers.hasSize;
@@ -10,24 +27,6 @@
1027
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
1128
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1229

13-
import java.time.OffsetDateTime;
14-
import java.time.format.DateTimeFormatter;
15-
import java.util.List;
16-
17-
import org.junit.jupiter.api.Test;
18-
import org.springframework.beans.factory.annotation.Autowired;
19-
import org.springframework.http.HttpHeaders;
20-
import org.springframework.http.MediaType;
21-
import org.springframework.test.context.jdbc.Sql;
22-
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
23-
24-
import com.faforever.api.AbstractIntegrationTest;
25-
import com.faforever.api.data.domain.GroupPermission;
26-
import com.faforever.api.data.domain.VotingChoice;
27-
import com.faforever.api.data.domain.VotingQuestion;
28-
import com.faforever.api.security.OAuthScope;
29-
import com.faforever.api.voting.VotingQuestionRepository;
30-
3130
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql")
3231
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql")
3332
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepVotingData.sql")
@@ -199,7 +198,7 @@ public void canRevealWinnerOnEndedSubjectWorksWithScopeAndRole() throws Exceptio
199198
.header(HttpHeaders.CONTENT_TYPE, JSON_API_MEDIA_TYPE)
200199
.content(PATCH_VOTING_SUBJECT_REVEAL_ID_2))
201200
.andExpect(status().isNoContent());
202-
VotingQuestion question = votingQuestionRepository.getById(2);
201+
VotingQuestion question = votingQuestionRepository.findById(2).orElseThrow();
203202
List<VotingChoice> winners = question.getWinners();
204203
assertThat(winners, hasSize(1));
205204
assertThat(winners.get(0).getId(), is(3));

src/inttest/java/com/faforever/api/user/UsersControllerTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void changePasswordWithSuccess() throws Exception {
119119
.params(params))
120120
.andExpect(status().isOk());
121121

122-
User user = userRepository.getById(USERID_USER);
122+
User user = userRepository.findById(USERID_USER).orElseThrow();
123123
assertEquals(user.getPassword(), "5c29a959abce4eda5f0e7a4e7ea53dce4fa0f0abbe8eaa63717e2fed5f193d31");
124124
}
125125

@@ -367,7 +367,7 @@ public void changeUsernameWithWrongScope() throws Exception {
367367

368368
@Test
369369
public void changeUsernameSuccess() throws Exception {
370-
assertThat(userRepository.getById(1).getLogin(), is(AUTH_USER));
370+
assertThat(userRepository.findById(1).orElseThrow().getLogin(), is(AUTH_USER));
371371

372372
MultiValueMap<String, String> params = new HttpHeaders();
373373
params.add("newUsername", NEW_USER);
@@ -378,12 +378,12 @@ public void changeUsernameSuccess() throws Exception {
378378
.params(params))
379379
.andExpect(status().isOk());
380380

381-
assertThat(userRepository.getById(1).getLogin(), is(NEW_USER));
381+
assertThat(userRepository.findById(1).orElseThrow().getLogin(), is(NEW_USER));
382382
}
383383

384384
@Test
385385
public void changeUsernameForcedByUser() throws Exception {
386-
assertThat(userRepository.getById(1).getLogin(), is(AUTH_USER));
386+
assertThat(userRepository.findById(1).orElseThrow().getLogin(), is(AUTH_USER));
387387

388388
MultiValueMap<String, String> params = new HttpHeaders();
389389
params.add("newUsername", NEW_USER);
@@ -395,7 +395,7 @@ public void changeUsernameForcedByUser() throws Exception {
395395
.andExpect(status().is4xxClientError())
396396
.andReturn();
397397

398-
assertThat(userRepository.getById(1).getLogin(), is(not(NEW_USER)));
398+
assertThat(userRepository.findById(1).orElseThrow().getLogin(), is(not(NEW_USER)));
399399
}
400400

401401
@Test
@@ -409,7 +409,7 @@ public void changeUsernameForcedByModerator() throws Exception {
409409
.params(params))
410410
.andExpect(status().isOk());
411411

412-
assertThat(userRepository.getById(2).getLogin(), is(NEW_USER));
412+
assertThat(userRepository.findById(2).orElseThrow().getLogin(), is(NEW_USER));
413413
}
414414

415415
@Test
@@ -438,7 +438,7 @@ public void changeUsernameForcedByModeratorWithoutRole() throws Exception {
438438

439439
@Test
440440
public void changeUsernameTooEarly() throws Exception {
441-
assertThat(userRepository.getById(2).getLogin(), is(AUTH_MODERATOR));
441+
assertThat(userRepository.findById(2).orElseThrow().getLogin(), is(AUTH_MODERATOR));
442442

443443
MultiValueMap<String, String> params = new HttpHeaders();
444444
params.add("newUsername", NEW_USER);
@@ -452,7 +452,7 @@ public void changeUsernameTooEarly() throws Exception {
452452

453453
assertApiError(result, ErrorCode.USERNAME_CHANGE_TOO_EARLY);
454454

455-
assertThat(userRepository.getById(2).getLogin(), is(AUTH_MODERATOR));
455+
assertThat(userRepository.findById(2).orElseThrow().getLogin(), is(AUTH_MODERATOR));
456456
}
457457

458458
@Test
@@ -467,7 +467,7 @@ public void changeUsernameTooEarlyButForced() throws Exception {
467467
.andExpect(status().isOk())
468468
.andReturn();
469469

470-
assertThat(userRepository.getById(2).getLogin(), is(NEW_USER));
470+
assertThat(userRepository.findById(2).orElseThrow().getLogin(), is(NEW_USER));
471471
}
472472

473473
@Test

src/main/java/com/faforever/api/user/MeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public JsonApiDocument me() {
4242
.filter(o -> FafUserAuthenticationToken.class.isAssignableFrom(o.getClass()))
4343
.map(FafUserAuthenticationToken.class::cast)
4444
.map(authentication -> {
45-
Player player = playerService.getById(authentication.getUserId());
45+
Player player = playerService.getById(authentication.getUserId());
4646
Set<String> grantedAuthorities = authentication.getRoles().stream()
4747
//.map(FafRole::role)
4848
// TEMPORARY WORKAROUND: we stripped away the ROLE_ prefix, but clients need to adapt. Until then, we add both

0 commit comments

Comments
 (0)