Review Comments
This commit is contained in:
parent
3ffb002aa2
commit
505301d390
|
@ -23,10 +23,6 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
@ -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<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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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