Review Comments
This commit is contained in:
parent
3ffb002aa2
commit
505301d390
|
@ -23,10 +23,6 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-validation</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -2,18 +2,32 @@ package com.baeldung.controller;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.baeldung.dto.BooleanObject;
|
import com.baeldung.dto.BooleanObject;
|
||||||
|
import com.baeldung.service.ValidationService;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class ValidationController {
|
public class ValidationController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ValidationService service;
|
||||||
|
|
||||||
@PostMapping("/validateBoolean")
|
@PostMapping("/validateBoolean")
|
||||||
public ResponseEntity<String> addUser(@RequestBody @Valid BooleanObject booleanObj) {
|
public ResponseEntity<String> processBooleanObject(@RequestBody @Valid BooleanObject booleanObj) {
|
||||||
|
return ResponseEntity.ok("BooleanObject is valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/validateBooleanAtService")
|
||||||
|
public ResponseEntity<String> processBooleanObjectAtService() {
|
||||||
|
BooleanObject boolObj = new BooleanObject();
|
||||||
|
boolObj.setBoolField(Boolean.TRUE);
|
||||||
|
boolObj.setTrueField(Boolean.FALSE);
|
||||||
|
service.processBoolean(boolObj);
|
||||||
return ResponseEntity.ok("BooleanObject is valid");
|
return ResponseEntity.ok("BooleanObject is valid");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package com.baeldung.controlleradvice;
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolationException;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
@ -27,4 +29,9 @@ public class GlobalExceptionHandler {
|
||||||
return ex.getMessage();
|
return ex.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ConstraintViolationException.class)
|
||||||
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
public String handleConstraintViolationException(ConstraintViolationException ex) {
|
||||||
|
return ex.getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,9 +7,6 @@ import javax.validation.constraints.NotNull;
|
||||||
import com.baeldung.deserializer.BooleanDeserializer;
|
import com.baeldung.deserializer.BooleanDeserializer;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class BooleanObject {
|
public class BooleanObject {
|
||||||
|
|
||||||
@NotNull(message = "boolField cannot be null")
|
@NotNull(message = "boolField cannot be null")
|
||||||
|
@ -23,4 +20,36 @@ public class BooleanObject {
|
||||||
|
|
||||||
@JsonDeserialize(using = BooleanDeserializer.class)
|
@JsonDeserialize(using = BooleanDeserializer.class)
|
||||||
Boolean boolStringVar;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import com.baeldung.dto.BooleanObject;
|
||||||
@Validated
|
@Validated
|
||||||
public class ValidationService {
|
public class ValidationService {
|
||||||
|
|
||||||
public void validateBoolean(@Valid BooleanObject booleanObj) {
|
public void processBoolean(@Valid BooleanObject booleanObj) {
|
||||||
// further processing
|
// further processing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,27 +8,36 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
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.context.junit.jupiter.SpringExtension;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import com.baeldung.dto.BooleanObject;
|
import com.baeldung.service.ValidationService;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
@WebMvcTest(controllers = ValidationController.class)
|
@WebMvcTest(controllers = ValidationController.class)
|
||||||
class ValidationControllerUnitTest {
|
class ValidationControllerUnitTest {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ObjectMapper objectMapper;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
static class EmployeeServiceImplTestContextConfiguration {
|
||||||
|
@Bean
|
||||||
|
public ValidationService validationService() {
|
||||||
|
return new ValidationService() {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ValidationService service;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNullBoolean() throws Exception {
|
void whenNullInputForBooleanField__thenHttpBadRequestAsHttpResponse() throws Exception {
|
||||||
BooleanObject boolObj = new BooleanObject();
|
|
||||||
boolObj.setBoolField(null);
|
String postBody = "{\"boolField\":null,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}";
|
||||||
String postBody = objectMapper.writeValueAsString(boolObj);
|
|
||||||
|
|
||||||
mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
||||||
.content(postBody))
|
.content(postBody))
|
||||||
|
@ -37,11 +46,9 @@ class ValidationControllerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testTrueBoolean() throws Exception {
|
void whenInvalidInputForTrueBooleanField__thenErrorResponse() throws Exception {
|
||||||
BooleanObject boolObj = new BooleanObject();
|
|
||||||
boolObj.setBoolField(Boolean.TRUE);
|
String postBody = "{\"boolField\":true,\"trueField\":false,\"falseField\":false,\"boolStringVar\":\"+\"}";
|
||||||
boolObj.setTrueField(Boolean.FALSE);
|
|
||||||
String postBody = objectMapper.writeValueAsString(boolObj);
|
|
||||||
|
|
||||||
String output = mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
String output = mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
||||||
.content(postBody))
|
.content(postBody))
|
||||||
|
@ -53,12 +60,9 @@ class ValidationControllerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFalseBoolean() throws Exception {
|
void whenInvalidInputForFalseBooleanField__thenErrorResponse() throws Exception {
|
||||||
BooleanObject boolObj = new BooleanObject();
|
|
||||||
boolObj.setBoolField(Boolean.TRUE);
|
String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":true,\"boolStringVar\":\"+\"}";
|
||||||
boolObj.setTrueField(Boolean.TRUE);
|
|
||||||
boolObj.setFalseField(Boolean.TRUE);
|
|
||||||
String postBody = objectMapper.writeValueAsString(boolObj);
|
|
||||||
|
|
||||||
String output = mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
String output = mockMvc.perform(post("/validateBoolean").contentType("application/json")
|
||||||
.content(postBody))
|
.content(postBody))
|
||||||
|
@ -70,25 +74,7 @@ class ValidationControllerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBooleanFromString() throws Exception {
|
void whenInvalidBooleanFromJson_thenErrorResponse() 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 {
|
|
||||||
|
|
||||||
String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"plus\"}";
|
String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"plus\"}";
|
||||||
|
|
||||||
|
@ -103,7 +89,7 @@ class ValidationControllerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testValidBean() throws Exception {
|
void whenAllBooleanFieldsValid_thenCorrectResponse() throws Exception {
|
||||||
|
|
||||||
String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}";
|
String postBody = "{\"boolField\":true,\"trueField\":true,\"falseField\":false,\"boolStringVar\":\"+\"}";
|
||||||
|
|
||||||
|
@ -116,4 +102,11 @@ class ValidationControllerUnitTest {
|
||||||
assertEquals("BooleanObject is valid", output);
|
assertEquals("BooleanObject is valid", output);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAllBooleanFieldsValid_whenServiceValidationFails_thenErrorResponse() throws Exception {
|
||||||
|
|
||||||
|
mockMvc.perform(post("/validateBooleanAtService").contentType("application/json"))
|
||||||
|
.andExpect(status().isInternalServerError());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
|
||||||
class BooleanUnitTest {
|
class BooleanUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBooleanFromString() {
|
void givenInputAsString_whenStringToBoolean_thenValidBooleanConversion() {
|
||||||
Boolean trueVar = Boolean.valueOf("TRUE");
|
Boolean trueVar = Boolean.valueOf("TRUE");
|
||||||
Boolean falseVar = Boolean.valueOf("false");
|
Boolean falseVar = Boolean.valueOf("false");
|
||||||
Boolean parsedVar = Boolean.parseBoolean("True");
|
Boolean parsedVar = Boolean.parseBoolean("True");
|
||||||
|
@ -18,7 +18,7 @@ class BooleanUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBoolean() {
|
void givenInputAsboolean_whenbooleanToBoolean_thenValidBooleanConversion() {
|
||||||
Boolean trueVar = Boolean.valueOf(true);
|
Boolean trueVar = Boolean.valueOf(true);
|
||||||
Boolean falseVar = Boolean.valueOf(false);
|
Boolean falseVar = Boolean.valueOf(false);
|
||||||
|
|
||||||
|
|
|
@ -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"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue