Skip to content

Commit

Permalink
Added JsonNodeValidator.isBoolean()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Sep 16, 2024
1 parent d75cda7 commit b73464f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ContainerNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.MissingNode;
Expand Down Expand Up @@ -158,4 +159,11 @@ public JsonNodeValidator<TextNode> isString()
isType(JsonNode::isTextual, "a String");
return self();
}

@Override
public JsonNodeValidator<BooleanNode> isBoolean()
{
isType(JsonNode::isBoolean, "a Boolean");
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ContainerNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.MissingNode;
Expand Down Expand Up @@ -141,4 +142,14 @@ public interface JsonNodeValidator<T extends JsonNode> extends
* @see JsonNode#isTextual()
*/
JsonNodeValidator<TextNode> isString();

/**
* Ensures that the node contains a Boolean.
*
* @return this
* @throws NullPointerException if the value is null
* @throws IllegalArgumentException if the value is not a Boolean
* @see JsonNode#isBoolean()
*/
JsonNodeValidator<BooleanNode> isBoolean();
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ private static List<?> asList(Object array, Class<?> type)
assert (type != null);
if (!type.getComponentType().isPrimitive())
return Arrays.asList((Object[]) array);
if (type == byte[].class)
{
byte[] valueAsArray = (byte[]) array;
int length = valueAsArray.length;
return IntStream.range(0, length).mapToObj(i -> valueAsArray[i]).
collect(Collectors.toCollection(() -> new ArrayList<>(length)));
}
if (type == boolean[].class)
{
boolean[] valueAsArray = (boolean[]) array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.cowwoc.requirements10.jackson.validator.JsonNodeValidator;
import com.github.cowwoc.requirements10.java.internal.scope.ApplicationScope;
import com.github.cowwoc.requirements10.test.TestValidatorsImpl;
import com.github.cowwoc.requirements10.test.scope.TestApplicationScope;
Expand Down Expand Up @@ -151,12 +152,42 @@ public void nodeIsBoolean()
try (ApplicationScope scope = new TestApplicationScope(NONE))
{
JsonNode node = om.createObjectNode().put("value", true).get("value");
boolean value = new TestValidatorsImpl(scope).requireThat(node, "node").isValue().getValue().
boolean value = new TestValidatorsImpl(scope).requireThat(node, "node").isBoolean().getValue().
booleanValue();
requireThat(value, "value").isTrue();
}
}

@Test
public void nodeIsValue() throws IOException
{
try (ApplicationScope scope = new TestApplicationScope(NONE))
{
JsonNode node = om.createObjectNode().
put("base64", "SGVsbG8gV29ybGQ=").
put("boolean", true).
putNull("null").
put("number", 5).
put("string", "Hello World");
JsonNodeValidator<JsonNode> validator = new TestValidatorsImpl(scope).requireThat(node, "node");

byte[] binaryValue = validator.property("base64").isValue().getValue().binaryValue();
requireThat(binaryValue, "binaryValue").isEqualTo("Hello World".getBytes(StandardCharsets.UTF_8));

boolean booleanValue = validator.property("boolean").isValue().getValue().booleanValue();
requireThat(booleanValue, "booleanValue").isTrue();

boolean isNull = validator.property("null").isValue().getValue().isNull();
requireThat(isNull, "nullValue").isTrue();

Number numberValue = validator.property("number").isValue().getValue().numberValue();
requireThat(numberValue, "numberValue").isEqualTo(5);

String stringValue = validator.property("string").isValue().getValue().textValue();
requireThat(stringValue, "stringValue").isEqualTo("Hello World");
}
}

@Test
public void nodeIsMissing()
{
Expand Down

0 comments on commit b73464f

Please sign in to comment.