diff --git a/spring-boot-modules/spring-boot-validations/pom.xml b/spring-boot-modules/spring-boot-validations/pom.xml index 9be58df247..13044471af 100644 --- a/spring-boot-modules/spring-boot-validations/pom.xml +++ b/spring-boot-modules/spring-boot-validations/pom.xml @@ -23,10 +23,6 @@ org.springframework.boot spring-boot-starter-validation - - org.projectlombok - lombok - diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java index 8597c9617c..d4ea9a6336 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java @@ -2,18 +2,32 @@ package com.baeldung.controller; import javax.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.baeldung.dto.BooleanObject; +import com.baeldung.service.ValidationService; @RestController public class ValidationController { + @Autowired + ValidationService service; + @PostMapping("/validateBoolean") - public ResponseEntity addUser(@RequestBody @Valid BooleanObject booleanObj) { + public ResponseEntity processBooleanObject(@RequestBody @Valid BooleanObject booleanObj) { + return ResponseEntity.ok("BooleanObject is valid"); + } + + @PostMapping("/validateBooleanAtService") + public ResponseEntity processBooleanObjectAtService() { + BooleanObject boolObj = new BooleanObject(); + boolObj.setBoolField(Boolean.TRUE); + boolObj.setTrueField(Boolean.FALSE); + service.processBoolean(boolObj); return ResponseEntity.ok("BooleanObject is valid"); } } diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java index 4c696cb70d..82f0839acf 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java @@ -2,6 +2,8 @@ package com.baeldung.controlleradvice; import java.util.stream.Collectors; +import javax.validation.ConstraintViolationException; + import org.springframework.http.HttpStatus; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -27,4 +29,9 @@ public class GlobalExceptionHandler { return ex.getMessage(); } + @ExceptionHandler(ConstraintViolationException.class) + @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) + public String handleConstraintViolationException(ConstraintViolationException ex) { + return ex.getMessage(); + } } diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java index 77ca18b02c..410324d3b5 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java @@ -7,9 +7,6 @@ import javax.validation.constraints.NotNull; import com.baeldung.deserializer.BooleanDeserializer; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import lombok.Data; - -@Data public class BooleanObject { @NotNull(message = "boolField cannot be null") @@ -23,4 +20,36 @@ public class BooleanObject { @JsonDeserialize(using = BooleanDeserializer.class) Boolean boolStringVar; + + public Boolean getBoolField() { + return boolField; + } + + public void setBoolField(Boolean boolField) { + this.boolField = boolField; + } + + public Boolean getTrueField() { + return trueField; + } + + public void setTrueField(Boolean trueField) { + this.trueField = trueField; + } + + public Boolean getFalseField() { + return falseField; + } + + public void setFalseField(Boolean falseField) { + this.falseField = falseField; + } + + public Boolean getBoolStringVar() { + return boolStringVar; + } + + public void setBoolStringVar(Boolean boolStringVar) { + this.boolStringVar = boolStringVar; + } } diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java index 5b96344e46..3fc7160bd5 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java @@ -11,7 +11,7 @@ import com.baeldung.dto.BooleanObject; @Validated public class ValidationService { - public void validateBoolean(@Valid BooleanObject booleanObj) { + public void processBoolean(@Valid BooleanObject booleanObj) { // further processing } } diff --git a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java index 73db4eb4eb..51cdb22780 100644 --- a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java @@ -8,27 +8,36 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; -import com.baeldung.dto.BooleanObject; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.baeldung.service.ValidationService; @ExtendWith(SpringExtension.class) @WebMvcTest(controllers = ValidationController.class) class ValidationControllerUnitTest { - @Autowired - private ObjectMapper objectMapper; - @Autowired private MockMvc mockMvc; + @TestConfiguration + static class EmployeeServiceImplTestContextConfiguration { + @Bean + public ValidationService validationService() { + return new ValidationService() { + }; + } + } + + @Autowired + ValidationService service; + @Test - void testNullBoolean() throws Exception { - BooleanObject boolObj = new BooleanObject(); - boolObj.setBoolField(null); - String postBody = objectMapper.writeValueAsString(boolObj); + void whenNullInputForBooleanField__thenHttpBadRequestAsHttpResponse() throws Exception { + + String postBody = "{\"boolField\":null,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) @@ -37,11 +46,9 @@ class ValidationControllerUnitTest { } @Test - void testTrueBoolean() throws Exception { - BooleanObject boolObj = new BooleanObject(); - boolObj.setBoolField(Boolean.TRUE); - boolObj.setTrueField(Boolean.FALSE); - String postBody = objectMapper.writeValueAsString(boolObj); + void whenInvalidInputForTrueBooleanField__thenErrorResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":false,\"falseField\":false,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) @@ -53,12 +60,9 @@ class ValidationControllerUnitTest { } @Test - void testFalseBoolean() throws Exception { - BooleanObject boolObj = new BooleanObject(); - boolObj.setBoolField(Boolean.TRUE); - boolObj.setTrueField(Boolean.TRUE); - boolObj.setFalseField(Boolean.TRUE); - String postBody = objectMapper.writeValueAsString(boolObj); + void whenInvalidInputForFalseBooleanField__thenErrorResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":true,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) @@ -70,25 +74,7 @@ class ValidationControllerUnitTest { } @Test - void testBooleanFromString() throws Exception { - BooleanObject boolObj = new BooleanObject(); - boolObj.setBoolField(Boolean.TRUE); - boolObj.setTrueField(Boolean.TRUE); - boolObj.setFalseField(Boolean.FALSE); - boolObj.setBoolStringVar(true); - String postBody = objectMapper.writeValueAsString(boolObj); - - String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") - .content(postBody)) - .andReturn() - .getResponse() - .getContentAsString(); - - assertEquals("Only values accepted as Boolean are + and -", output); - } - - @Test - void testInvalidBooleanFromJson() throws Exception { + void whenInvalidBooleanFromJson_thenErrorResponse() throws Exception { String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"plus\"}"; @@ -103,7 +89,7 @@ class ValidationControllerUnitTest { } @Test - void testValidBean() throws Exception { + void whenAllBooleanFieldsValid_thenCorrectResponse() throws Exception { String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; @@ -116,4 +102,11 @@ class ValidationControllerUnitTest { assertEquals("BooleanObject is valid", output); } + + @Test + void givenAllBooleanFieldsValid_whenServiceValidationFails_thenErrorResponse() throws Exception { + + mockMvc.perform(post("/validateBooleanAtService").contentType("application/json")) + .andExpect(status().isInternalServerError()); + } } diff --git a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java index 8c9375e72f..30270ee512 100644 --- a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; class BooleanUnitTest { @Test - void testBooleanFromString() { + void givenInputAsString_whenStringToBoolean_thenValidBooleanConversion() { Boolean trueVar = Boolean.valueOf("TRUE"); Boolean falseVar = Boolean.valueOf("false"); Boolean parsedVar = Boolean.parseBoolean("True"); @@ -18,7 +18,7 @@ class BooleanUnitTest { } @Test - void testBoolean() { + void givenInputAsboolean_whenbooleanToBoolean_thenValidBooleanConversion() { Boolean trueVar = Boolean.valueOf(true); Boolean falseVar = Boolean.valueOf(false); diff --git a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java deleted file mode 100644 index c67acea13d..0000000000 --- a/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.service; - -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import javax.validation.ConstraintViolationException; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import com.baeldung.dto.BooleanObject; - -@ExtendWith(SpringExtension.class) -@SpringBootTest -class ValidationServiceUnitTest { - - @Autowired - ValidationService service; - - @Test - void validateBoolean() { - BooleanObject boolObj = new BooleanObject(); - boolObj.setBoolField(Boolean.valueOf(true)); - boolObj.setTrueField(Boolean.FALSE); - Throwable throwableError = assertThrows(ConstraintViolationException.class, () -> { - service.validateBoolean(boolObj); - }); - assertTrue(throwableError.getLocalizedMessage() - .endsWith("trueField must have true value")); - } - -}