From 6300c412904f9a36be6f40098ac7f6fa74b38b9b Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 17 Sep 2023 22:36:14 +0530 Subject: [PATCH 01/10] BAEL-6695 Boolean Validation in Spring Boot BAEL-6695 Boolean Validation in Spring Boot --- spring-boot-modules/pom.xml | 1 + .../spring-boot-validations/pom.xml | 38 +++++++ .../main/java/com/baeldung/Application.java | 12 ++ .../controller/ValidationController.java | 19 ++++ .../GlobalExceptionHandler.java | 37 +++++++ .../deserializer/BooleanDeserializer.java | 18 +++ .../java/com/baeldung/dto/BooleanObject.java | 26 +++++ .../baeldung/service/ValidationService.java | 17 +++ .../ValidationControllerUnitTest.java | 104 ++++++++++++++++++ .../com/baeldung/dto/BooleanUnitTest.java | 28 +++++ .../service/ValidationServiceUnitTest.java | 35 ++++++ 11 files changed, 335 insertions(+) create mode 100644 spring-boot-modules/spring-boot-validations/pom.xml create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/Application.java create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java create mode 100644 spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java create mode 100644 spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java create mode 100644 spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java create mode 100644 spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 1c4b2bb38f..e0afbbddaf 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -104,6 +104,7 @@ spring-boot-springdoc-2 spring-boot-documentation spring-boot-3-url-matching + spring-boot-validations diff --git a/spring-boot-modules/spring-boot-validations/pom.xml b/spring-boot-modules/spring-boot-validations/pom.xml new file mode 100644 index 0000000000..e89c7f997b --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + spring-boot-validations + spring-boot-validations + Demo of Validations in Spring Boot + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-validation + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/Application.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..c0490d50c6 --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} 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 new file mode 100644 index 0000000000..8597c9617c --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controller/ValidationController.java @@ -0,0 +1,19 @@ +package com.baeldung.controller; + +import javax.validation.Valid; + +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; + +@RestController +public class ValidationController { + + @PostMapping("/validateBoolean") + public ResponseEntity addUser(@RequestBody @Valid BooleanObject booleanObj) { + 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 new file mode 100644 index 0000000000..601da40e23 --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/controlleradvice/GlobalExceptionHandler.java @@ -0,0 +1,37 @@ +package com.baeldung.controlleradvice; + +import java.util.stream.Collectors; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.fasterxml.jackson.databind.exc.InvalidFormatException; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + public String handleValidationException(MethodArgumentNotValidException ex) { + return ex.getBindingResult() + .getFieldErrors() + .stream() + .map(e -> e.getDefaultMessage()) + .collect(Collectors.joining(",")); + } + + @ExceptionHandler(IllegalArgumentException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + public String handleIllegalArugmentException(IllegalArgumentException ex) { + return ex.getMessage(); + } + + @ExceptionHandler(InvalidFormatException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + public String handleInvalidFormatException(InvalidFormatException ex) { + return ex.getOriginalMessage(); + } +} diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java new file mode 100644 index 0000000000..c9f30dc6e3 --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java @@ -0,0 +1,18 @@ +package com.baeldung.deserializer; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +public class BooleanDeserializer extends JsonDeserializer { + @Override + public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + String value = p.getText(); + if (value != null && !value.equals("TRUE") && !value.equals("FALSE")) { + throw new IllegalArgumentException("Boolean value must come as Capital TRUE or FALSE"); + } + return Boolean.valueOf(value); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..77ca18b02c --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/dto/BooleanObject.java @@ -0,0 +1,26 @@ +package com.baeldung.dto; + +import javax.validation.constraints.AssertFalse; +import javax.validation.constraints.AssertTrue; +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") + Boolean boolField; + + @AssertTrue(message = "trueField must have true value") + Boolean trueField; + + @AssertFalse(message = "falseField must have false value") + Boolean falseField; + + @JsonDeserialize(using = BooleanDeserializer.class) + Boolean 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 new file mode 100644 index 0000000000..5b96344e46 --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/service/ValidationService.java @@ -0,0 +1,17 @@ +package com.baeldung.service; + +import javax.validation.Valid; + +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import com.baeldung.dto.BooleanObject; + +@Service +@Validated +public class ValidationService { + + public void validateBoolean(@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 new file mode 100644 index 0000000000..78950e4c0e --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/controller/ValidationControllerUnitTest.java @@ -0,0 +1,104 @@ +package com.baeldung.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +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.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; + +import com.baeldung.dto.BooleanObject; +import com.fasterxml.jackson.databind.ObjectMapper; + +@ExtendWith(SpringExtension.class) +@WebMvcTest(controllers = ValidationController.class) +class ValidationControllerUnitTest { + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private MockMvc mockMvc; + + @Test + void testNullBoolean() throws Exception { + BooleanObject boolObj = new BooleanObject(); + boolObj.setBoolField(null); + String postBody = objectMapper.writeValueAsString(boolObj); + + mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andExpect(status().isBadRequest()); + + } + + @Test + void testTrueBoolean() throws Exception { + BooleanObject boolObj = new BooleanObject(); + boolObj.setBoolField(Boolean.TRUE); + boolObj.setTrueField(Boolean.FALSE); + String postBody = objectMapper.writeValueAsString(boolObj); + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("trueField must have true value", output); + } + + @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); + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("falseField must have false value", output); + } + + @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("Boolean value must come as Capital TRUE or FALSE", output); + } + + @Test + void testValidBean() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"TRUE\"}"; + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("BooleanObject is valid", output); + + } +} 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 new file mode 100644 index 0000000000..8c9375e72f --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/dto/BooleanUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.dto; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class BooleanUnitTest { + + @Test + void testBooleanFromString() { + Boolean trueVar = Boolean.valueOf("TRUE"); + Boolean falseVar = Boolean.valueOf("false"); + Boolean parsedVar = Boolean.parseBoolean("True"); + + assertEquals(Boolean.TRUE, trueVar); + assertEquals(Boolean.FALSE, falseVar); + assertEquals(Boolean.TRUE, parsedVar); + } + + @Test + void testBoolean() { + Boolean trueVar = Boolean.valueOf(true); + Boolean falseVar = Boolean.valueOf(false); + + assertEquals(Boolean.TRUE, trueVar); + assertEquals(Boolean.FALSE, falseVar); + } +} 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 new file mode 100644 index 0000000000..c67acea13d --- /dev/null +++ b/spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java @@ -0,0 +1,35 @@ +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")); + } + +} From c07c35882fdeadb4695835b305e309d80414565e Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 17 Sep 2023 23:10:10 +0530 Subject: [PATCH 02/10] Removing unused ExceptionHandler --- .../baeldung/controlleradvice/GlobalExceptionHandler.java | 7 ------- 1 file changed, 7 deletions(-) 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 601da40e23..4c696cb70d 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 @@ -8,8 +8,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; -import com.fasterxml.jackson.databind.exc.InvalidFormatException; - @RestControllerAdvice public class GlobalExceptionHandler { @@ -29,9 +27,4 @@ public class GlobalExceptionHandler { return ex.getMessage(); } - @ExceptionHandler(InvalidFormatException.class) - @ResponseStatus(value = HttpStatus.BAD_REQUEST) - public String handleInvalidFormatException(InvalidFormatException ex) { - return ex.getOriginalMessage(); - } } From c8c573899fd16d47f2388313656137551a704862 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 24 Sep 2023 01:20:56 +0530 Subject: [PATCH 03/10] Review comments --- .../deserializer/BooleanDeserializer.java | 13 ++++++++----- .../ValidationControllerUnitTest.java | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java index c9f30dc6e3..5b50b75675 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java @@ -8,11 +8,14 @@ import com.fasterxml.jackson.databind.JsonDeserializer; public class BooleanDeserializer extends JsonDeserializer { @Override - public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - String value = p.getText(); - if (value != null && !value.equals("TRUE") && !value.equals("FALSE")) { - throw new IllegalArgumentException("Boolean value must come as Capital TRUE or FALSE"); + public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException { + String value = parser.getText(); + if (value != null && (value.equals("TRUE") || value.equals("1") || value.equals("+"))) { + return Boolean.TRUE; + } else if (value != null && (value.equals("FALSE") || value.equals("0") || value.equals("-"))) { + return Boolean.FALSE; + } else { + throw new IllegalArgumentException("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0"); } - return Boolean.valueOf(value); } } \ No newline at end of file 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 78950e4c0e..0ff786bd4c 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 @@ -84,7 +84,22 @@ class ValidationControllerUnitTest { .getResponse() .getContentAsString(); - assertEquals("Boolean value must come as Capital TRUE or FALSE", output); + assertEquals("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0", output); + } + + @Test + void testInvalidBooleanFromJson() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"6\"}"; + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0", output); + } @Test From 96ff2a982aad1455dc663936346126605c87e4d2 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 24 Sep 2023 02:08:16 +0530 Subject: [PATCH 04/10] Updated Test case --- .../com/baeldung/controller/ValidationControllerUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0ff786bd4c..f312e1259c 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 @@ -105,7 +105,7 @@ class ValidationControllerUnitTest { @Test void testValidBean() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"TRUE\"}"; + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) From 00671fd084febdf9afb23cf9a56f099d1cbc9a86 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Tue, 26 Sep 2023 18:53:59 +0530 Subject: [PATCH 05/10] Review Comments --- .../java/com/baeldung/deserializer/BooleanDeserializer.java | 6 +++--- .../baeldung/controller/ValidationControllerUnitTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java index 5b50b75675..01a8e0eba0 100644 --- a/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java +++ b/spring-boot-modules/spring-boot-validations/src/main/java/com/baeldung/deserializer/BooleanDeserializer.java @@ -10,12 +10,12 @@ public class BooleanDeserializer extends JsonDeserializer { @Override public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException { String value = parser.getText(); - if (value != null && (value.equals("TRUE") || value.equals("1") || value.equals("+"))) { + if (value != null && value.equals("+")) { return Boolean.TRUE; - } else if (value != null && (value.equals("FALSE") || value.equals("0") || value.equals("-"))) { + } else if (value != null && value.equals("-")) { return Boolean.FALSE; } else { - throw new IllegalArgumentException("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0"); + throw new IllegalArgumentException("Only values accepted as Boolean are + and -"); } } } \ No newline at end of file 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 f312e1259c..73db4eb4eb 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 @@ -84,13 +84,13 @@ class ValidationControllerUnitTest { .getResponse() .getContentAsString(); - assertEquals("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0", output); + assertEquals("Only values accepted as Boolean are + and -", output); } @Test void testInvalidBooleanFromJson() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"6\"}"; + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"plus\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) @@ -98,7 +98,7 @@ class ValidationControllerUnitTest { .getResponse() .getContentAsString(); - assertEquals("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0", output); + assertEquals("Only values accepted as Boolean are + and -", output); } From 3ffb002aa2390cfb6f2daf5d5903e27612c0a219 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Thu, 5 Oct 2023 11:56:36 +0530 Subject: [PATCH 06/10] POM Indentation --- .../spring-boot-validations/pom.xml | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/spring-boot-modules/spring-boot-validations/pom.xml b/spring-boot-modules/spring-boot-validations/pom.xml index e89c7f997b..9be58df247 100644 --- a/spring-boot-modules/spring-boot-validations/pom.xml +++ b/spring-boot-modules/spring-boot-validations/pom.xml @@ -1,38 +1,39 @@ - - 4.0.0 - - com.baeldung.spring-boot-modules - spring-boot-modules - 1.0.0-SNAPSHOT - - spring-boot-validations - spring-boot-validations - Demo of Validations in Spring Boot - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.boot - spring-boot-starter-validation - - - org.projectlombok - lombok - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + 4.0.0 + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + spring-boot-validations + spring-boot-validations + Demo of Validations in Spring Boot + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-validation + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + \ No newline at end of file From 505301d390f0215f50f898f414df6ca30c1336d0 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Thu, 5 Oct 2023 19:09:19 +0530 Subject: [PATCH 07/10] Review Comments --- .../spring-boot-validations/pom.xml | 4 - .../controller/ValidationController.java | 16 +++- .../GlobalExceptionHandler.java | 7 ++ .../java/com/baeldung/dto/BooleanObject.java | 35 ++++++++- .../baeldung/service/ValidationService.java | 2 +- .../ValidationControllerUnitTest.java | 73 +++++++++---------- .../com/baeldung/dto/BooleanUnitTest.java | 4 +- .../service/ValidationServiceUnitTest.java | 35 --------- 8 files changed, 90 insertions(+), 86 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-validations/src/test/java/com/baeldung/service/ValidationServiceUnitTest.java 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")); - } - -} From 7854c7336b2c329e6525b118a7648abd6a628c5b Mon Sep 17 00:00:00 2001 From: Maiklins Date: Thu, 5 Oct 2023 23:54:24 +0200 Subject: [PATCH 08/10] Remove double underscores from test names --- .../baeldung/controller/ValidationControllerUnitTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 51cdb22780..759007ed36 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 @@ -35,7 +35,7 @@ class ValidationControllerUnitTest { ValidationService service; @Test - void whenNullInputForBooleanField__thenHttpBadRequestAsHttpResponse() throws Exception { + void whenNullInputForBooleanField_thenHttpBadRequestAsHttpResponse() throws Exception { String postBody = "{\"boolField\":null,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; @@ -46,7 +46,7 @@ class ValidationControllerUnitTest { } @Test - void whenInvalidInputForTrueBooleanField__thenErrorResponse() throws Exception { + void whenInvalidInputForTrueBooleanField_thenErrorResponse() throws Exception { String postBody = "{\"boolField\":true,\"trueField\":false,\"falseField\":false,\"boolStringVar\":\"+\"}"; @@ -60,7 +60,7 @@ class ValidationControllerUnitTest { } @Test - void whenInvalidInputForFalseBooleanField__thenErrorResponse() throws Exception { + void whenInvalidInputForFalseBooleanField_thenErrorResponse() throws Exception { String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":true,\"boolStringVar\":\"+\"}"; From 78d0fa3656aec1f42290eb23ac8f31ffe902b13a Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sat, 7 Oct 2023 22:47:58 +0530 Subject: [PATCH 09/10] Updated Junits --- .../java/com/baeldung/dto/BooleanObject.java | 1 + .../ValidationControllerUnitTest.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) 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 410324d3b5..750b23fe11 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 @@ -15,6 +15,7 @@ public class BooleanObject { @AssertTrue(message = "trueField must have true value") Boolean trueField; + @NotNull(message = "falseField cannot be null") @AssertFalse(message = "falseField must have false value") Boolean falseField; 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 759007ed36..ad0e3ecdfa 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 @@ -109,4 +109,30 @@ class ValidationControllerUnitTest { mockMvc.perform(post("/validateBooleanAtService").contentType("application/json")) .andExpect(status().isInternalServerError()); } + + @Test + void whenNullInputForTrueBooleanField_thenCorrectResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":null,\"falseField\":false,\"boolStringVar\":\"+\"}"; + + mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andExpect(status().isOk()); + + } + + @Test + void whenNullInputForFalseBooleanField_thenHttpBadRequestAsHttpResponse() throws Exception { + + String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":null,\"boolStringVar\":\"+\"}"; + + String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") + .content(postBody)) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals("falseField cannot be null", output); + + } } From 4e0b27b017cc7d30a997ec2efc4ade1706da1263 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sat, 14 Oct 2023 21:55:00 +0530 Subject: [PATCH 10/10] Review Comments --- .../ValidationControllerUnitTest.java | 13 ------------- .../java/com/baeldung/dto/BooleanUnitTest.java | 17 +++++------------ 2 files changed, 5 insertions(+), 25 deletions(-) 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 ad0e3ecdfa..f05d76e3f1 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 @@ -36,18 +36,15 @@ class ValidationControllerUnitTest { @Test void whenNullInputForBooleanField_thenHttpBadRequestAsHttpResponse() throws Exception { - String postBody = "{\"boolField\":null,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) .andExpect(status().isBadRequest()); - } @Test void whenInvalidInputForTrueBooleanField_thenErrorResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":false,\"falseField\":false,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") @@ -61,7 +58,6 @@ class ValidationControllerUnitTest { @Test void whenInvalidInputForFalseBooleanField_thenErrorResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":true,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") @@ -75,7 +71,6 @@ class ValidationControllerUnitTest { @Test void whenInvalidBooleanFromJson_thenErrorResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"plus\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") @@ -85,12 +80,10 @@ class ValidationControllerUnitTest { .getContentAsString(); assertEquals("Only values accepted as Boolean are + and -", output); - } @Test void whenAllBooleanFieldsValid_thenCorrectResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") @@ -100,30 +93,25 @@ class ValidationControllerUnitTest { .getContentAsString(); assertEquals("BooleanObject is valid", output); - } @Test void givenAllBooleanFieldsValid_whenServiceValidationFails_thenErrorResponse() throws Exception { - mockMvc.perform(post("/validateBooleanAtService").contentType("application/json")) .andExpect(status().isInternalServerError()); } @Test void whenNullInputForTrueBooleanField_thenCorrectResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":null,\"falseField\":false,\"boolStringVar\":\"+\"}"; mockMvc.perform(post("/validateBoolean").contentType("application/json") .content(postBody)) .andExpect(status().isOk()); - } @Test void whenNullInputForFalseBooleanField_thenHttpBadRequestAsHttpResponse() throws Exception { - String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":null,\"boolStringVar\":\"+\"}"; String output = mockMvc.perform(post("/validateBoolean").contentType("application/json") @@ -133,6 +121,5 @@ class ValidationControllerUnitTest { .getContentAsString(); assertEquals("falseField cannot be null", output); - } } 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 30270ee512..9ab04794c6 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 @@ -8,21 +8,14 @@ class BooleanUnitTest { @Test void givenInputAsString_whenStringToBoolean_thenValidBooleanConversion() { - Boolean trueVar = Boolean.valueOf("TRUE"); - Boolean falseVar = Boolean.valueOf("false"); - Boolean parsedVar = Boolean.parseBoolean("True"); - - assertEquals(Boolean.TRUE, trueVar); - assertEquals(Boolean.FALSE, falseVar); - assertEquals(Boolean.TRUE, parsedVar); + assertEquals(Boolean.TRUE, Boolean.valueOf("TRUE")); + assertEquals(Boolean.FALSE, Boolean.valueOf("false")); + assertEquals(Boolean.TRUE, Boolean.parseBoolean("True")); } @Test void givenInputAsboolean_whenbooleanToBoolean_thenValidBooleanConversion() { - Boolean trueVar = Boolean.valueOf(true); - Boolean falseVar = Boolean.valueOf(false); - - assertEquals(Boolean.TRUE, trueVar); - assertEquals(Boolean.FALSE, falseVar); + assertEquals(Boolean.TRUE, Boolean.valueOf(true)); + assertEquals(Boolean.FALSE, Boolean.valueOf(false)); } }