/** * Main syntax checking function * * <p>This method only checks that the keyword's type is of the correct * type, and reports an error if it isn't; if it is, it handles the rest * of syntax checking to {@link #checkValue(Collection, MessageBundle, * ProcessingReport, SchemaTree)}.</p> * * @param pointers the list of JSON Pointers to fill (see description) * @param bundle the message bundle to use * @param report the processing report to use * @param tree the schema * @throws InvalidSchemaException keyword is invalid */ @Override public final void checkSyntax(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); final NodeType type = NodeType.getNodeType(node); if (!types.contains(type)) { report.error(newMsg(tree, bundle, "common.incorrectType") .putArgument("found", type).putArgument("expected", types)); return; } checkValue(pointers, bundle, report, tree); }
@Test(dataProvider = "validTypes") public void syntaxCheckingSucceedsOnValidTypes(final JsonNode node) throws ProcessingException { final AbstractSyntaxChecker checker = spy(new DummyChecker()); final ProcessingReport report = mock(ProcessingReport.class); final ObjectNode schema = FACTORY.objectNode(); schema.put(KEYWORD, node); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); checker.checkSyntax(null, BUNDLE, report, tree); verify(checker).checkValue(null, BUNDLE, report, tree); verify(report, never()).error(anyMessage()); }
@DataProvider protected final Iterator<Object[]> getValueTests() { if (valueTests.isMissingNode()) return Iterators.emptyIterator(); final List<Object[]> list = Lists.newArrayList(); String msg; JsonNode msgNode; JsonNode msgParams; JsonNode msgData; for (final JsonNode node: valueTests) { msgNode = node.get("message"); msgParams = node.get("msgParams"); msgData = node.get("msgData"); msg = msgNode == null ? null : buildMessage(msgNode.textValue(), msgParams, msgData); list.add(new Object[]{ node.get("schema"), msg, node.get("valid").booleanValue(), msgData }); } return list.iterator(); }
@Test( dependsOnMethods = "keywordIsSupportedInThisDictionary", dataProvider = "getPointerTests" ) public final void pointerDelegationWorksCorrectly(final JsonNode schema, final ArrayNode expectedPointers) throws ProcessingException, JsonPointerException { final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); checker.checkSyntax(pointers, BUNDLE, report, tree); final List<JsonPointer> expected = Lists.newArrayList(); for (final JsonNode node: expectedPointers) expected.add(new JsonPointer(node.textValue())); assertEquals(pointers, expected); }
@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); }
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(); } } }
checker = dict.entries().get(keyword); invalidTypes = checker == null ? null : EnumSet.complementOf(checker.getValidTypes());
/** * Main syntax checking function * * <p>This method only checks that the keyword's type is of the correct * type, and reports an error if it isn't; if it is, it handles the rest * of syntax checking to {@link #checkValue(Collection, MessageBundle, * ProcessingReport, SchemaTree)}.</p> * * @param pointers the list of JSON Pointers to fill (see description) * @param bundle the message bundle to use * @param report the processing report to use * @param tree the schema * @throws InvalidSchemaException keyword is invalid */ @Override public final void checkSyntax(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); final NodeType type = NodeType.getNodeType(node); if (!types.contains(type)) { report.error(newMsg(tree, bundle, "common.incorrectType") .putArgument("found", type).putArgument("expected", types)); return; } checkValue(pointers, bundle, report, tree); }
checker.checkSyntax(pointers, bundle, report, tree);
@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)); }
private static String buildMessage(final String key, final JsonNode params, final JsonNode data) { final ProcessingMessage message = new ProcessingMessage() .setMessage(BUNDLE.getMessage(key)); if (params != null) { String name; JsonNode value; for (final JsonNode node: params) { name = node.textValue(); value = data.get(name); message.putArgument(name, valueToArgument(value)); } } return message.getMessage(); }
/** * Main syntax checking function * * <p>This method only checks that the keyword's type is of the correct * type, and reports an error if it isn't; if it is, it handles the rest * of syntax checking to {@link #checkValue(Collection, MessageBundle, * ProcessingReport, SchemaTree)}.</p> * * @param pointers the list of JSON Pointers to fill (see description) * @param bundle the message bundle to use * @param report the processing report to use * @param tree the schema * @throws InvalidSchemaException keyword is invalid */ @Override public final void checkSyntax(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); final NodeType type = NodeType.getNodeType(node); if (!types.contains(type)) { report.error(newMsg(tree, bundle, "common.incorrectType") .putArgument("found", type).putArgument("expected", types)); return; } checkValue(pointers, bundle, report, tree); }
checker.checkSyntax(pointers, bundle, report, tree);
checker.checkSyntax(pointers, bundle, report, tree);
@Test public void checkingWillNotDiveIntoUnknownKeywords() throws ProcessingException { final ObjectNode node = FACTORY.objectNode(); node.put(K1, K1); final ObjectNode schema = FACTORY.objectNode(); schema.put("foo", node); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); final ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree); processor.process(report, holder); verify(checker, never()).checkSyntax(anyCollectionOf(JsonPointer.class), any(MessageBundle.class), anyReport(), anySchema()); }
@Test( dependsOnMethods = "keywordIsSupportedInThisDictionary", dataProvider = "getValueTests" ) public final void valueTestsSucceed(final JsonNode schema, final String msg, final boolean success, final ObjectNode msgData) throws ProcessingException { final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); checker.checkSyntax(pointers, BUNDLE, report, tree); if (success) { verify(report, never()).error(anyMessage()); return; } final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); verify(report).error(captor.capture()); final ProcessingMessage message = captor.getValue(); assertMessage(message).isSyntaxError(keyword, msg, tree) .hasContents(msgData); }