/** * Test whether a numeric instance is a long * * <p>We use both a test on the instance type and Jackson's {@link * JsonNode#canConvertToLong()}. The first test is needed since the * latter method will also return true if the value is a decimal which * integral part fits into a long, and we don't want that.</p> * * @param node the node to test * @return true if both conditions are true */ private static boolean valueIsLong(final JsonNode node) { return NodeType.getNodeType(node) == NodeType.INTEGER && node.canConvertToLong(); } }
private static boolean valueIsLong(final JsonNode node) { if (!node.canConvertToLong()) return false; if (NodeType.getNodeType(node) == NodeType.INTEGER) return true; return node.decimalValue().remainder(BigDecimal.ONE) .compareTo(BigDecimal.ZERO) == 0; }
key = entry.getKey(); value = entry.getValue(); type = NodeType.getNodeType(value); switch (type) { case OBJECT:
public SchemaContext(final FullData data) { schema = data.getSchema(); final JsonTree tree = data.getInstance(); instanceType = tree != null ? NodeType.getNodeType(tree.getNode()) : null; }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final NodeType type = NodeType.getNodeType(data.getInstance().getNode()); if (!types.contains(type)) report.error(newMsg(data, bundle, "err.common.typeNoMatch") .putArgument("found", type) .putArgument("expected", toArrayNode(types))); }
private static Object valueToArgument(final JsonNode value) { final NodeType type = NodeType.getNodeType(value); switch (type) { case STRING: return value.textValue(); case INTEGER: return value.bigIntegerValue(); case NUMBER: case NULL: case OBJECT: case ARRAY: return value; case BOOLEAN: return value.booleanValue(); // case ARRAY: // final List<Object> list = Lists.newArrayList(); // for (final JsonNode element: value) // list.add(valueToArgument(element)); // return list; default: throw new UnsupportedOperationException(); } } }
@Test( dataProvider = "unsupported", dependsOnMethods = "attributeIsBeingAskedWhatIsSupports" ) public void unsupportedTypeDoesNotTriggerValidatorBuild(final JsonNode node) throws ProcessingException { final ObjectNode schema = FACTORY.objectNode(); schema.put("format", FMT); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, NodeType.getNodeType(node)); final ValidatorList in = new ValidatorList(context, Collections.<KeywordValidator>emptyList()); final ValidatorList out = processor.process(report, in); final List<KeywordValidator> validators = Lists.newArrayList(out); assertTrue(validators.isEmpty()); } }
private static Object valueToArgument(final JsonNode value) { final NodeType type = NodeType.getNodeType(value); switch (type) { case STRING: return value.textValue(); case INTEGER: return value.bigIntegerValue(); case NUMBER: return value.decimalValue().toPlainString(); case NULL: return value; case BOOLEAN: return value.booleanValue(); case ARRAY: final List<Object> list = Lists.newArrayList(); for (final JsonNode element: value) list.add(valueToArgument(element)); return list; default: throw new UnsupportedOperationException(); } } }
@Test(dataProvider = "sampleData") public void onlyRelevantDigestsAreBuilt(final JsonNode node) throws ProcessingException { final NodeType type = NodeType.getNodeType(node); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final SchemaContext context = new SchemaContext(tree, type); final ProcessingReport report = mock(ProcessingReport.class); final SchemaDigest digest = schemaDigester.process(report, context); verify(digester1).digest(schema); verify(digester2).digest(schema); final Map<String,JsonNode> digests = digest.getDigests(); if (TYPES1.contains(type)) assertSame(digests.get(K1), digest1); else assertFalse(digests.containsKey(K1)); if (TYPES2.contains(type)) assertSame(digests.get(K2), digest2); else assertFalse(digests.containsKey(K2)); }
type = NodeType.getNodeType(element); if (type != NodeType.INTEGER) report.error(newMsg(tree, bundle, "incorrectElementType")
final NodeType type = NodeType.getNodeType(instance);
final NodeType type = NodeType.getNodeType(instance);
private static void computeUnchanged(final Map<JsonPointer, JsonNode> ret, final JsonPointer pointer, final JsonNode first, final JsonNode second) { if (EQUIVALENCE.equivalent(first, second)) { ret.put(pointer, second); return; } final NodeType firstType = NodeType.getNodeType(first); final NodeType secondType = NodeType.getNodeType(second); if (firstType != secondType) return; // nothing in common // We know they are both the same type, so... switch (firstType) { case OBJECT: computeObject(ret, pointer, first, second); break; case ARRAY: computeArray(ret, pointer, first, second); default: /* nothing */ } }
private boolean checkLDOProperty(final ProcessingReport report, final MessageBundle bundle, final SchemaTree tree, final int index, final String name, final NodeType expected, final String key) throws ProcessingException { final JsonNode node = getNode(tree).get(index).get(name); if (node == null) return false; final NodeType type = NodeType.getNodeType(node); if (type == expected) return true; report.error(LDOMsg(tree, bundle, key, index).put("expected", expected) .putArgument("found", type)); return false; } }
private boolean checkLDOProperty(final ProcessingReport report, final MessageBundle bundle, final SchemaTree tree, final int index, final String name, final NodeType expected, final String key) throws ProcessingException { final JsonNode node = getNode(tree).get(index).get(name); if (node == null) return false; final NodeType type = NodeType.getNodeType(node); if (type == expected) return true; report.error(LDOMsg(tree, bundle, key, index).put("expected", expected) .putArgument("found", type)); return false; } }
private boolean checkLDOProperty(final ProcessingReport report, final MessageBundle bundle, final SchemaTree tree, final int index, final String name, final NodeType expected, final String key) throws ProcessingException { final JsonNode node = getNode(tree).get(index).get(name); if (node == null) return false; final NodeType type = NodeType.getNodeType(node); if (type == expected) return true; report.error(LDOMsg(tree, bundle, key, index).put("expected", expected) .putArgument("found", type)); return false; } }
private static Object valueToArgument(final JsonNode value) { final NodeType type = NodeType.getNodeType(value); switch (type) { case STRING: return value.textValue(); case INTEGER: return value.bigIntegerValue(); case NUMBER: case NULL: return value; case BOOLEAN: return value.booleanValue(); case ARRAY: final List<Object> list = Lists.newArrayList(); for (final JsonNode element: value) list.add(valueToArgument(element)); return list; default: throw new UnsupportedOperationException(); } } }
@Test(dataProvider = "invalidTypes") public void syntaxCheckingFailsOnInvalidTypes(final JsonNode node) throws ProcessingException { final NodeType type = NodeType.getNodeType(node); final ObjectNode schema = FACTORY.objectNode(); schema.put(KEYWORD, node); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final AbstractSyntaxChecker checker = spy(new DummyChecker()); final ProcessingReport report = mock(ProcessingReport.class); final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); checker.checkSyntax(null, BUNDLE, report, tree); verify(report).error(captor.capture()); verify(checker, never()).checkValue(null, BUNDLE, report, tree); final ProcessingMessage msg = captor.getValue(); assertMessage(msg).hasField("keyword", KEYWORD).hasField("schema", tree) .hasMessage(BUNDLE.printf("common.incorrectType", type, VALID_TYPES)) .hasField("domain", "syntax") .hasField("expected", EnumSet.of(ARRAY, INTEGER, STRING)) .hasField("found", NodeType.getNodeType(node)); }
@Test(dataProvider = "notSchemas") public void syntaxProcessorYellsOnNonSchemas(final JsonNode node) throws ProcessingException { final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), node); final ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree); processor.process(report, holder); verify(report).log(same(LogLevel.ERROR), captor.capture()); final ProcessingMessage message = captor.getValue(); final NodeType type = NodeType.getNodeType(node); assertMessage(message) .hasMessage(BUNDLE.printf("core.notASchema", type)) .hasField("found", type); }
@Test( dependsOnMethods = "keywordIsSupportedInThisDictionary", dataProvider = "invalidTypes" ) public final void invalidTypesAreReportedAsErrors(final JsonNode node) throws ProcessingException { final SchemaTree tree = treeFromValue(keyword, node); final NodeType type = NodeType.getNodeType(node); final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); checker.checkSyntax(pointers, BUNDLE, report, tree); verify(report).error(captor.capture()); final ProcessingMessage msg = captor.getValue(); final String message = BUNDLE.printf("common.incorrectType", type, EnumSet.complementOf(invalidTypes)); assertMessage(msg).isSyntaxError(keyword, message, tree) .hasField("expected", EnumSet.complementOf(invalidTypes)) .hasField("found", type); }