Skip to content

Commit 306f993

Browse files
authored
fix: diverse chores (#307)
* fix: remove duplicated authentication manager * fix: sonar coverage * fix: send 4xx in case of validation errors * fix: jacoco coverage in sonar * make test stable
1 parent 5e669a4 commit 306f993

File tree

4 files changed

+114
-18
lines changed

4 files changed

+114
-18
lines changed

pom.xml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
<!-- JaCoCo Properties -->
2424
<jacoco.version>0.8.12</jacoco.version>
25-
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
26-
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
27-
<sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
2825
<sonar.language>java</sonar.language>
2926
</properties>
3027

@@ -197,20 +194,29 @@
197194
<version>${jacoco.version}</version>
198195
<executions>
199196
<execution>
200-
<id>jacoco-initialize</id>
201-
<goals>
202-
<goal>prepare-agent</goal>
203-
</goals>
197+
<id>prepare-agent</id>
198+
<goals>
199+
<goal>prepare-agent</goal>
200+
</goals>
204201
</execution>
205202
<execution>
206-
<id>jacoco-site</id>
207-
<phase>package</phase>
208-
<goals>
209-
<goal>report</goal>
210-
</goals>
203+
<id>report</id>
204+
<goals>
205+
<goal>report</goal>
206+
</goals>
207+
<configuration>
208+
<formats>
209+
<format>XML</format>
210+
</formats>
211+
</configuration>
211212
</execution>
212213
</executions>
213214
</plugin>
215+
<plugin>
216+
<groupId>org.sonarsource.scanner.maven</groupId>
217+
<artifactId>sonar-maven-plugin</artifactId>
218+
<version>3.9.1.2184</version>
219+
</plugin>
214220
</plugins>
215221
</build>
216222

src/main/java/com/sterul/opencookbookapiserver/configurations/security/WebSecurityConfiguration.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ public class WebSecurityConfiguration {
6161
@Autowired
6262
private PasswordEncoder passwordEncoder;
6363

64-
@Autowired
65-
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
66-
// Configure service to check if credentials are valid
67-
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
68-
}
69-
7064
private RequestMatcher allowedPathRequestMatcher() {
7165
return (HttpServletRequest request) -> AUTH_WHITELIST.stream()
7266
.anyMatch(whitelistedUrl -> new AntPathRequestMatcher(whitelistedUrl).matches(request));
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.sterul.opencookbookapiserver.controllers;
2+
3+
import static org.springframework.http.HttpStatus.BAD_REQUEST;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.springframework.core.Ordered;
9+
import org.springframework.core.annotation.Order;
10+
import org.springframework.validation.BindingResult;
11+
import org.springframework.validation.FieldError;
12+
import org.springframework.web.bind.MethodArgumentNotValidException;
13+
import org.springframework.web.bind.annotation.ControllerAdvice;
14+
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
import org.springframework.web.bind.annotation.ResponseStatus;
17+
18+
@Order(Ordered.HIGHEST_PRECEDENCE)
19+
@ControllerAdvice
20+
public class CustomControllerAdvice {
21+
22+
@ResponseStatus(BAD_REQUEST)
23+
@ResponseBody
24+
@ExceptionHandler(MethodArgumentNotValidException.class)
25+
public Error methodArgumentNotValidException(MethodArgumentNotValidException ex) {
26+
BindingResult result = ex.getBindingResult();
27+
List<org.springframework.validation.FieldError> fieldErrors = result.getFieldErrors();
28+
return processFieldErrors(fieldErrors);
29+
}
30+
31+
private Error processFieldErrors(List<org.springframework.validation.FieldError> fieldErrors) {
32+
Error error = new Error(BAD_REQUEST.value(), "validation error");
33+
for (org.springframework.validation.FieldError fieldError : fieldErrors) {
34+
error.addFieldError(fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
35+
}
36+
return error;
37+
}
38+
39+
static class Error {
40+
private final int status;
41+
private final String message;
42+
private List<FieldError> fieldErrors = new ArrayList<>();
43+
44+
Error(int status, String message) {
45+
this.status = status;
46+
this.message = message;
47+
}
48+
49+
public int getStatus() {
50+
return status;
51+
}
52+
53+
public String getMessage() {
54+
return message;
55+
}
56+
57+
public void addFieldError(String objectName, String path, String message) {
58+
FieldError error = new FieldError(objectName, path, message);
59+
fieldErrors.add(error);
60+
}
61+
62+
public List<FieldError> getFieldErrors() {
63+
return fieldErrors;
64+
}
65+
}
66+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.sterul.opencookbookapiserver.integration;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.ActiveProfiles;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.testcontainers.junit.jupiter.Testcontainers;
13+
14+
@SpringBootTest
15+
@AutoConfigureMockMvc
16+
@ActiveProfiles("integration-test")
17+
@Testcontainers
18+
class CustomControllerAdviceTest extends IntegrationTest {
19+
20+
@Autowired
21+
private MockMvc mockMvc;
22+
23+
@Test
24+
void testThatInvalidRequestFormatProduces400() throws Exception {
25+
mockMvc.perform(post("/api/v1/users/resetPassword")
26+
.contentType("application/json")
27+
.content("{}"))
28+
.andExpect(status().isBadRequest());
29+
}
30+
}

0 commit comments

Comments
 (0)