added spring boot ControllerAdvice

This commit is contained in:
Luc Weinbrecht
2022-05-04 11:59:59 +02:00
parent 49119644a5
commit cefcb284d6
8 changed files with 139 additions and 5 deletions

View File

@@ -0,0 +1,27 @@
package de.weinbrecht.luc.bpm.architecture.loan.agreement.adapter.in.web;
import de.weinbrecht.luc.bpm.architecture.loan.agreement.domain.service.LoanAgreementException;
import io.github.domainprimitives.validation.InvariantException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
@ControllerAdvice
public class RestExceptionHandlerAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = LoanAgreementException.class)
protected ResponseEntity<Object> handleDomainException(LoanAgreementException ex, WebRequest request) {
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), INTERNAL_SERVER_ERROR, request);
}
@ExceptionHandler(value = InvariantException.class)
protected ResponseEntity<Object> handleInvariantConflict(InvariantException ex, WebRequest request) {
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), BAD_REQUEST, request);
}
}

View File

@@ -2,10 +2,6 @@ package de.weinbrecht.luc.bpm.architecture.loan.agreement.domain.service;
public class LoanAgreementException extends RuntimeException {
public LoanAgreementException(String message) {
super(message);
}
public LoanAgreementException(String message, Throwable cause) {
super(message, cause);
}

View File

@@ -0,0 +1,24 @@
package de.weinbrecht.luc.bpm.architecture.loan.agreement.adapter.in.web;
import de.weinbrecht.luc.bpm.architecture.loan.agreement.domain.service.LoanAgreementException;
import io.github.domainprimitives.validation.InvariantException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequiredArgsConstructor
@RestController
@RequestMapping("error")
public class DemoController {
@GetMapping("/invariant")
public void provideInvariant() {
throw new InvariantException("Foo", "Testing");
}
@GetMapping("/domain")
public void provideDomainException() {
throw new LoanAgreementException("Bar", null);
}
}

View File

@@ -0,0 +1,27 @@
package de.weinbrecht.luc.bpm.architecture.loan.agreement.adapter.in.web;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(DemoController.class)
class RestExceptionHandlerAdviceTest {
@Autowired
private MockMvc mockMvc;
@Test
void should_handle_invariants() throws Exception {
mockMvc.perform(get("/error/invariant")).andExpect(status().isBadRequest());
}
@Test
void should_handle_domain_exception() throws Exception {
mockMvc.perform(get("/error/domain")).andExpect(status().isInternalServerError());
}
}

View File

@@ -56,7 +56,7 @@ class LoanAgreementServiceTest {
assertThrows(LoanAgreementException.class,
() -> classUnderTest.create(loanAgreement, caseId));
verify(recommendationTrigger, never()).startLoanAgreement(caseId, any());
verify(recommendationTrigger, never()).startLoanAgreement(eq(caseId), any());
}
@Test

View File

@@ -0,0 +1,20 @@
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
import io.github.domainprimitives.validation.InvariantException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
@ControllerAdvice
public class RestExceptionHandlerAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = InvariantException.class)
protected ResponseEntity<Object> handleInvariantConflict(InvariantException ex, WebRequest request) {
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), BAD_REQUEST, request);
}
}

View File

@@ -0,0 +1,18 @@
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
import io.github.domainprimitives.validation.InvariantException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequiredArgsConstructor
@RestController
@RequestMapping("error")
public class DemoController {
@GetMapping()
public void provideInvariant() {
throw new InvariantException("Foo", "Testing");
}
}

View File

@@ -0,0 +1,22 @@
package de.weinbrecht.luc.bpm.architecture.recommendation.adapter.in.web;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(DemoController.class)
class RestExceptionHandlerAdviceTest {
@Autowired
private MockMvc mockMvc;
@Test
void should_handle_invariants() throws Exception {
mockMvc.perform(get("/error")).andExpect(status().isBadRequest());
}
}