Review comments
This commit is contained in:
parent
c07c35882f
commit
c8c573899f
|
@ -8,11 +8,14 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
|
||||||
public class BooleanDeserializer extends JsonDeserializer<Boolean> {
|
public class BooleanDeserializer extends JsonDeserializer<Boolean> {
|
||||||
@Override
|
@Override
|
||||||
public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
||||||
String value = p.getText();
|
String value = parser.getText();
|
||||||
if (value != null && !value.equals("TRUE") && !value.equals("FALSE")) {
|
if (value != null && (value.equals("TRUE") || value.equals("1") || value.equals("+"))) {
|
||||||
throw new IllegalArgumentException("Boolean value must come as Capital TRUE or FALSE");
|
return Boolean.TRUE;
|
||||||
}
|
} else if (value != null && (value.equals("FALSE") || value.equals("0") || value.equals("-"))) {
|
||||||
return Boolean.valueOf(value);
|
return Boolean.FALSE;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Only values accepted as Boolean are TRUE, FALSE, +, -, 1 , 0");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -84,7 +84,22 @@ class ValidationControllerUnitTest {
|
||||||
.getResponse()
|
.getResponse()
|
||||||
.getContentAsString();
|
.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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue