Skip to content

Commit 4ecbc62

Browse files
committed
WIP
1 parent 3e0234a commit 4ecbc62

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

src/main/java/edu/kit/datamanager/pit/Application.java

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import edu.kit.datamanager.pit.pitservice.impl.TypingService;
3838
import edu.kit.datamanager.pit.typeregistry.ITypeRegistry;
3939
import edu.kit.datamanager.pit.typeregistry.impl.TypeRegistry;
40+
import edu.kit.datamanager.pit.web.ExtendedErrorAttributes;
4041
import edu.kit.datamanager.pit.web.converter.SimplePidRecordConverter;
4142
import edu.kit.datamanager.security.filter.KeycloakJwtProperties;
4243

@@ -100,6 +101,11 @@ public Logger logger(InjectionPoint injectionPoint) {
100101
return LoggerFactory.getLogger(targetClass.getCanonicalName());
101102
}
102103

104+
@Bean
105+
public ExtendedErrorAttributes errorAttributes(ObjectMapper objectMapper) {
106+
return new ExtendedErrorAttributes(objectMapper);
107+
}
108+
103109
@Bean
104110
public ITypeRegistry typeRegistry() {
105111
return new TypeRegistry();

src/main/java/edu/kit/datamanager/pit/common/RecordValidationException.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
import edu.kit.datamanager.pit.domain.PIDRecord;
77

8+
import java.io.Serial;
9+
810
/**
911
* Indicates that a PID was given which could not be resolved to answer the
1012
* request properly.
1113
*/
1214
public class RecordValidationException extends ResponseStatusException {
1315

1416
private static final String VALIDATION_OF_RECORD = "Validation of record ";
15-
private static final long serialVersionUID = 1L;
17+
@Serial
18+
private static final long serialVersionUID = -7287999233733933282L;
1619
private static final HttpStatus HTTP_STATUS = HttpStatus.BAD_REQUEST;
1720

18-
// For cases in which the PID record shold be appended to the error response.
19-
private final transient PIDRecord pidRecord;
21+
// For cases in which the PID record should be appended to the error response.
22+
private final PIDRecord pidRecord;
2023

2124
public RecordValidationException(PIDRecord pidRecord) {
2225
super(HTTP_STATUS, VALIDATION_OF_RECORD + pidRecord.getPid() + " failed.");

src/main/java/edu/kit/datamanager/pit/web/ExtendedErrorAttributes.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
import java.util.Map;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.boot.web.error.ErrorAttributeOptions;
76
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
8-
import org.springframework.stereotype.Component;
97
import org.springframework.web.context.request.WebRequest;
108

119
import com.fasterxml.jackson.databind.ObjectMapper;
1210

1311
import edu.kit.datamanager.pit.common.RecordValidationException;
1412

15-
@Component
1613
public class ExtendedErrorAttributes extends DefaultErrorAttributes {
1714

18-
@Autowired(required = true)
1915
ObjectMapper objectMapperBean;
2016

17+
public ExtendedErrorAttributes(ObjectMapper objectMapperBean) {
18+
this.objectMapperBean = objectMapperBean;
19+
}
20+
2121
@Override
2222
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
2323
final Map<String, Object> errorAttributes =
2424
super.getErrorAttributes(webRequest, options);
2525

2626
final Throwable error = super.getError(webRequest);
27-
if (error instanceof RecordValidationException) {
28-
final RecordValidationException validationError = (RecordValidationException) error;
27+
if (error instanceof RecordValidationException validationError) {
2928
try {
3029
errorAttributes.put("pid-record", objectMapperBean.writeValueAsString(validationError.getPidRecord()));
3130
} catch (Exception e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package edu.kit.datamanager.pit.web;
2+
3+
import java.util.Map;
4+
5+
import jakarta.servlet.http.HttpServletRequest;
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.boot.test.web.client.TestRestTemplate;
11+
import org.springframework.boot.web.error.ErrorAttributeOptions;
12+
import org.springframework.boot.web.servlet.error.ErrorAttributes;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.mock.web.MockHttpServletRequest;
16+
import org.springframework.test.context.ActiveProfiles;
17+
import org.springframework.web.context.WebApplicationContext;
18+
import org.springframework.web.context.request.WebRequest;
19+
import org.springframework.web.context.request.ServletWebRequest;
20+
21+
import static org.junit.jupiter.api.Assertions.*;
22+
23+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
24+
@AutoConfigureMockMvc
25+
@ActiveProfiles("test")
26+
public class ExtendedErrorAttributesTest {
27+
28+
@Autowired
29+
private TestRestTemplate restTemplate;
30+
31+
@Autowired
32+
private WebApplicationContext webApplicationContext;
33+
34+
@Autowired
35+
private ExtendedErrorAttributes errorAttributes;
36+
37+
@Test
38+
public void testExtendedErrorAttributes() {
39+
// Create a mock request to pass to the error attributes
40+
HttpServletRequest request = new MockHttpServletRequest();
41+
WebRequest webRequest = new ServletWebRequest(request);
42+
43+
// Get the error attributes
44+
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, ErrorAttributeOptions.defaults());
45+
46+
// Check if the custom attribute is present
47+
assertTrue(attributes.containsKey("pid-record"));
48+
}
49+
50+
@Test
51+
public void testExtendedErrorAttributesBeanRegistration() {
52+
// Check if the ExtendedErrorAttributes bean is registered
53+
ExtendedErrorAttributes extendedErrorAttributes = webApplicationContext.getBean(ExtendedErrorAttributes.class);
54+
assertNotNull(extendedErrorAttributes, "ExtendedErrorAttributes bean should be registered");
55+
}
56+
}

0 commit comments

Comments
 (0)