Skip to content

Commit fc106b4

Browse files
committed
💚 checkstyle + pmd
1 parent cf2f1dc commit fc106b4

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/ChecklistService.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44

55
import de.muenchen.dbs.personalization.checklist.domain.Checklist;
66
import de.muenchen.dbs.personalization.common.NotFoundException;
7-
import de.muenchen.dbs.personalization.security.Authorities;
87
import java.time.ZonedDateTime;
98
import java.util.List;
109
import java.util.UUID;
1110
import lombok.RequiredArgsConstructor;
1211
import lombok.extern.slf4j.Slf4j;
1312
import org.apache.commons.lang3.StringUtils;
14-
import org.apache.hc.client5.http.HttpResponseException;
1513
import org.springframework.http.HttpStatus;
16-
import org.springframework.security.access.prepost.PreAuthorize;
1714
import org.springframework.security.core.Authentication;
1815
import org.springframework.security.core.context.SecurityContextHolder;
1916
import org.springframework.security.oauth2.jwt.Jwt;
20-
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
2117
import org.springframework.stereotype.Service;
2218
import org.springframework.web.server.ResponseStatusException;
2319

@@ -29,30 +25,30 @@ public class ChecklistService {
2925
private final ChecklistRepository checklistRepository;
3026

3127
public Checklist createChecklist(final Checklist checklist) {
32-
String userMail = getUserMailFromAuthenticationOrThrow();
28+
final String userMail = getUserMailFromAuthenticationOrThrow();
3329
log.debug("Create Checklist {} for {}", checklist, userMail);
3430
checklist.setEmail(userMail);
3531
return checklistRepository.save(checklist);
3632
}
3733

3834
public List<Checklist> getChecklists() {
39-
String userMail = getUserMailFromAuthenticationOrThrow();
35+
final String userMail = getUserMailFromAuthenticationOrThrow();
4036
log.debug("Get all checklists of {}", userMail);
4137
return checklistRepository.findChecklistByEmail(userMail);
4238
}
4339

4440
public Checklist getChecklist(final UUID checklistId) {
45-
String userMail = getUserMailFromAuthenticationOrThrow();
41+
final String userMail = getUserMailFromAuthenticationOrThrow();
4642
log.debug("Get checklist with ID {} for {}", checklistId, userMail);
47-
Checklist checklistOrThrowException = getChecklistOrThrowException(checklistId);
43+
final Checklist checklistOrThrowException = getChecklistOrThrowException(checklistId);
4844

4945
isChecklistOwnerOrThrow(checklistOrThrowException, userMail);
5046

5147
return checklistOrThrowException;
5248
}
5349

5450
public Checklist updateChecklist(final Checklist checklist, final UUID checklistId) {
55-
String userMail = getUserMailFromAuthenticationOrThrow();
51+
final String userMail = getUserMailFromAuthenticationOrThrow();
5652
log.debug("Update checklist with ID {} for {}", checklistId, userMail);
5753
final Checklist foundChecklist = getChecklistOrThrowException(checklistId);
5854

@@ -65,7 +61,7 @@ public Checklist updateChecklist(final Checklist checklist, final UUID checklist
6561
}
6662

6763
public void deleteChecklist(final UUID checklistId) {
68-
String userMail = getUserMailFromAuthenticationOrThrow();
64+
final String userMail = getUserMailFromAuthenticationOrThrow();
6965
log.debug("Delete Checklist with ID {} for {}", checklistId, userMail);
7066

7167
final Checklist foundChecklist = getChecklistOrThrowException(checklistId);
@@ -81,18 +77,18 @@ private Checklist getChecklistOrThrowException(final UUID checklistId) {
8177
}
8278

8379
private String getUserMailFromAuthenticationOrThrow() {
84-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
85-
if(authentication.getPrincipal() instanceof Jwt jwt) {
86-
String email = jwt.getClaims().get("email").toString();
87-
if(!StringUtils.isBlank(email)) {
80+
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
81+
if (authentication.getPrincipal() instanceof Jwt jwt) {
82+
final String email = jwt.getClaims().get("email").toString();
83+
if (!StringUtils.isBlank(email)) {
8884
return email;
8985
}
9086
}
9187
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
9288
}
9389

94-
private void isChecklistOwnerOrThrow(Checklist checklist, String userMail) {
95-
if(!checklist.getEmail().equals(userMail)) {
90+
private void isChecklistOwnerOrThrow(final Checklist checklist, final String userMail) {
91+
if (!checklist.getEmail().equals(userMail)) {
9692
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not own the checklist");
9793
}
9894
}

personalization-service/src/test/java/de/muenchen/dbs/personalization/IntegrationTestBase.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import java.time.Instant;
88
import java.util.Map;
9-
import org.hibernate.annotations.common.reflection.XPackage;
109
import org.springframework.beans.factory.annotation.Autowired;
1110
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1211
import org.springframework.boot.test.context.SpringBootTest;
@@ -22,21 +21,20 @@
2221
)
2322
@ActiveProfiles(profiles = { SPRING_TEST_PROFILE, SPRING_NO_SECURITY_PROFILE })
2423
@AutoConfigureMockMvc
25-
public abstract class IntegrationTestBase {
24+
public class IntegrationTestBase {
2625

2726
@Autowired
2827
protected MockMvc mockMvc;
2928

3029
@Autowired
3130
protected ObjectMapper objectMapper;
3231

33-
protected final String TOKEN_USER_MAIL = "[email protected]";
34-
protected final Jwt DEFAULT_JWT = new Jwt(
32+
protected static final String TOKEN_USER_MAIL = "[email protected]";
33+
protected static final Jwt DEFAULT_JWT = new Jwt(
3534
"tokenvalue",
3635
Instant.now(),
3736
Instant.now().plusSeconds(3600),
3837
Map.of("alg", "HS256",
3938
"typ", "JWT"),
40-
Map.of("email", TOKEN_USER_MAIL)
41-
);
39+
Map.of("email", TOKEN_USER_MAIL));
4240
}

personalization-service/src/test/java/de/muenchen/dbs/personalization/checklist/ChecklistIntegrationTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class ChecklistIntegrationTest extends IntegrationTestBase {
4343
private static final PostgreSQLContainer<?> POSTGRE_SQL_CONTAINER = new PostgreSQLContainer<>(
4444
DockerImageName.parse(TestConstants.TESTCONTAINERS_POSTGRES_IMAGE));
4545

46-
4746
private UUID testChecklistId;
4847

4948
@Autowired
@@ -65,8 +64,8 @@ class GetChecklist {
6564
@Test
6665
void givenChecklistId_thenReturnChecklist() throws Exception {
6766
mockMvc.perform(get("/checklist/{checklistID}", testChecklistId)
68-
.contentType(MediaType.APPLICATION_JSON)
69-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
67+
.contentType(MediaType.APPLICATION_JSON)
68+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
7069
.andExpect(status().isOk())
7170
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
7271
.andExpect(jsonPath("$.id", is(testChecklistId.toString())))
@@ -79,8 +78,8 @@ class GetChecklists {
7978
@Test
8079
void givenEmail_thenReturnChecklists() throws Exception {
8180
mockMvc.perform(get("/checklist")
82-
.contentType(MediaType.APPLICATION_JSON)
83-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
81+
.contentType(MediaType.APPLICATION_JSON)
82+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
8483
.andExpect(status().isOk())
8584
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
8685
.andExpect(jsonPath("$", hasSize(2)));
@@ -102,9 +101,9 @@ void givenChecklist_thenChecklistIsSaved() throws Exception {
102101
final String requestBody = objectMapper.writeValueAsString(requestDTO);
103102

104103
mockMvc.perform(post("/checklist")
105-
.content(requestBody)
106-
.contentType(MediaType.APPLICATION_JSON)
107-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
104+
.content(requestBody)
105+
.contentType(MediaType.APPLICATION_JSON)
106+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
108107
.andExpect(status().isCreated())
109108
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
110109
.andExpect(jsonPath("$.email", is(TOKEN_USER_MAIL)));
@@ -126,9 +125,9 @@ void givenChecklist_thenChecklistIsUpdated() throws Exception {
126125
final String requestBody = objectMapper.writeValueAsString(requestDTO);
127126

128127
mockMvc.perform(put("/checklist/{checklistID}", testChecklistId)
129-
.content(requestBody)
130-
.contentType(MediaType.APPLICATION_JSON)
131-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
128+
.content(requestBody)
129+
.contentType(MediaType.APPLICATION_JSON)
130+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
132131
.andExpect(status().isOk())
133132
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
134133
.andExpect(jsonPath("$.id", is(testChecklistId.toString())))
@@ -141,8 +140,8 @@ class DeleteChecklist {
141140
@Test
142141
void givenChecklistId_thenChecklistIsDeleted() throws Exception {
143142
mockMvc.perform(delete("/checklist/{checklistID}", testChecklistId)
144-
.contentType(MediaType.APPLICATION_JSON)
145-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
143+
.contentType(MediaType.APPLICATION_JSON)
144+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
146145
.andExpect(status().isOk());
147146
}
148147
}

personalization-service/src/test/java/de/muenchen/dbs/personalization/checklist/ChecklistServiceTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.mockito.Mockito.argThat;
66
import static org.mockito.Mockito.doNothing;
7-
import static org.mockito.Mockito.doReturn;
87
import static org.mockito.Mockito.times;
98
import static org.mockito.Mockito.verify;
109
import static org.mockito.Mockito.when;
1110

12-
import de.muenchen.dbs.personalization.IntegrationTestBase;
1311
import de.muenchen.dbs.personalization.checklist.domain.Checklist;
1412
import de.muenchen.dbs.personalization.common.NotFoundException;
1513
import java.time.Instant;
@@ -42,14 +40,13 @@ public class ChecklistServiceTest {
4240

4341
@BeforeEach
4442
public void setup() {
45-
JwtAuthenticationToken authentication = new JwtAuthenticationToken(
43+
final JwtAuthenticationToken authentication = new JwtAuthenticationToken(
4644
new Jwt("tokenvalue",
4745
Instant.now(),
4846
Instant.now().plusSeconds(3600),
4947
Map.of("alg", "HS256",
5048
"typ", "JWT"),
51-
Map.of("email", USER_EMAIL))
52-
);
49+
Map.of("email", USER_EMAIL)));
5350
SecurityContextHolder.getContext().setAuthentication(authentication);
5451
}
5552

personalization-service/src/test/java/de/muenchen/dbs/personalization/configuration/CacheControlConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class CacheControlConfigurationTest extends IntegrationTestBase {
3131
void testForCacheControlHeadersForEntityEndpoint() throws Exception {
3232

3333
mockMvc.perform(get(ENTITY_ENDPOINT_URL)
34-
.contentType(MediaType.APPLICATION_JSON)
35-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
34+
.contentType(MediaType.APPLICATION_JSON)
35+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)))
3636
.andExpect(status().isOk())
3737
.andExpect(header().exists(HttpHeaders.CACHE_CONTROL))
3838
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, EXPECTED_CACHE_CONTROL_HEADER_VALUES));

personalization-service/src/test/java/de/muenchen/dbs/personalization/configuration/UnicodeConfigurationTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class UnicodeConfigurationTest extends IntegrationTestBase {
5252
*/
5353
private static final String TEXT_ATTRIBUTE_COMPOSED = "\u00c4-\u00e9";
5454

55-
5655
@Autowired
5756
private ChecklistRepository checklistRepository;
5857

@@ -71,14 +70,14 @@ void testForNfcNormalization() throws Exception {
7170

7271
// When
7372
final ResultActions response = mockMvc.perform(MockMvcRequestBuilders.post(ENTITY_ENDPOINT_URL)
74-
.content(objectMapper.writeValueAsString(checklistCreateDTO))
75-
.contentType(MediaType.APPLICATION_JSON)
76-
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)));
73+
.content(objectMapper.writeValueAsString(checklistCreateDTO))
74+
.contentType(MediaType.APPLICATION_JSON)
75+
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(DEFAULT_JWT)));
7776

7877
assertThat(response).isNotNull();
7978
assertThat(response.andExpect(status().isCreated()));
8079

81-
ChecklistReadDTO responseChecklist = objectMapper.readValue(response.andReturn().getResponse().getContentAsString(), ChecklistReadDTO.class);
80+
final ChecklistReadDTO responseChecklist = objectMapper.readValue(response.andReturn().getResponse().getContentAsString(), ChecklistReadDTO.class);
8281

8382
final Checklist checklist = checklistRepository.findById(responseChecklist.id()).orElse(null);
8483

0 commit comments

Comments
 (0)