Review comments

This commit is contained in:
Niket Agrawal 2023-09-24 01:20:56 +05:30
parent c07c35882f
commit c8c573899f
2 changed files with 24 additions and 6 deletions

View File

@ -8,11 +8,14 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
public class BooleanDeserializer extends JsonDeserializer<Boolean> {
@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);
}
}

View File

@ -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