protected static <T> JsonNode toArrayNode(final Collection<T> collection) { final ArrayNode node = JacksonUtils.nodeFactory().arrayNode(); for (final T element: collection) node.add(element.toString()); return node; }
private ProcessingMessage collectSyntaxErrors(final ProcessingReport report) { /* * OK, that's for issue #99 but that's ugly nevertheless. * * We want syntax error messages to appear in the exception text. */ final String msg = syntaxMessages.getMessage("core.invalidSchema"); final ArrayNode arrayNode = JacksonUtils.nodeFactory().arrayNode(); JsonNode node; for (final ProcessingMessage message: report) { node = message.asJson(); if ("syntax".equals(node.path("domain").asText())) arrayNode.add(node); } final StringBuilder sb = new StringBuilder(msg); sb.append("\nSyntax errors:\n"); sb.append(JacksonUtils.prettyPrint(arrayNode)); return new ProcessingMessage().setMessage(sb.toString()); } }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { if (additionalOK) return; final JsonNode instance = data.getInstance().getNode(); final Set<String> fields = Sets.newHashSet(instance.fieldNames()); fields.removeAll(properties); final Set<String> tmp = Sets.newHashSet(); for (final String field: fields) for (final String regex: patternProperties) if (RhinoHelper.regMatch(regex, field)) tmp.add(field); fields.removeAll(tmp); if (fields.isEmpty()) return; /* * Display extra properties in order in the report */ final ArrayNode node = JacksonUtils.nodeFactory().arrayNode(); for (final String field: Ordering.natural().sortedCopy(fields)) node.add(field); report.error(newMsg(data, bundle, "err.common.additionalProperties.notAllowed") .putArgument("unwanted", node)); }
@Test public void challengedConstructorRaisesAnException() { final Map<String, JsonNode> digests = Maps.newTreeMap(); digests.put(K1, JacksonUtils.nodeFactory().nullNode()); digests.put(CHALLENGED, JacksonUtils.nodeFactory().nullNode()); final SchemaDigest digest = new SchemaDigest(null, digests); final ProcessingReport report = mock(ProcessingReport.class); try { validatorBuilder.process(report, digest); fail("No exception thrown??"); } catch (ProcessingException ignored) { } }
@Test public void onlyRelevantValidatorsAreBuilt() throws ProcessingException { final Map<String, JsonNode> digests = Maps.newTreeMap(); digests.put(K1, JacksonUtils.nodeFactory().nullNode()); final SchemaDigest digest = new SchemaDigest(null, digests); final ProcessingReport report = mock(ProcessingReport.class); final ValidatorList context = validatorBuilder.process(report, digest); final List<KeywordValidator> list = Lists.newArrayList(context); assertEquals(list.size(), 1); assertSame(list.get(0).getClass(), Keyword1.class); }
@Test public void allRelevantValidatorsAreBuilt() throws ProcessingException { final Map<String, JsonNode> digests = Maps.newTreeMap(); digests.put(K1, JacksonUtils.nodeFactory().nullNode()); digests.put(K2, JacksonUtils.nodeFactory().nullNode()); final SchemaDigest digest = new SchemaDigest(null, digests); final ProcessingReport report = mock(ProcessingReport.class); final ValidatorList context = validatorBuilder.process(report, digest); final List<KeywordValidator> list = Lists.newArrayList(context); assertEquals(list.size(), 2); assertSame(list.get(0).getClass(), Keyword1.class); assertSame(list.get(1).getClass(), Keyword2.class); }
private ProcessingMessage validationLoopMessage(final FullData input) { final ArrayNode node = JacksonUtils.nodeFactory().arrayNode(); for (final SchemaURI uri: schemaURIs) node.add(uri.toString()); return input.newMessage() .put("domain", "validation") .setMessage(errmsg) .putArgument("alreadyVisited", new SchemaURI(input.getSchema())) .putArgument("instancePointer", pointer.toString()) .put("validationPath", node); } }
@Test(timeOut = 1000) public void circularReferencingDuringValidationIsDetected() throws IOException, ProcessingException, JsonPointerException { final JsonNode schemaNode = JsonLoader.fromResource("/other/issue102.json"); final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonValidator validator = factory.getValidator(); final MessageBundle bundle = MessageBundles.getBundle(JsonSchemaValidationBundle.class); try { validator.validate(schemaNode, JacksonUtils.nodeFactory().nullNode()); fail("No exception thrown!"); } catch (ProcessingException e) { final URI uri = URI.create("#/oneOf/1"); final ProcessingMessage message = e.getProcessingMessage(); final String expectedMessage = bundle.printf("err.common.validationLoop", uri, ""); assertMessage(message) .hasMessage(expectedMessage) .hasField("alreadyVisited", uri) .hasField("instancePointer", JsonPointer.empty().toString()) .hasField("validationPath", Arrays.asList("#", "#/oneOf/1")); } assertTrue(true); }
@Test public void cannotOverwriteAnAlreadyPresentSchema() { final String input = "http://json-schema.org/draft-04/schema#"; try { cfg.preloadSchema(input, JacksonUtils.nodeFactory().objectNode()); fail("No exception thrown!!"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), BUNDLE.printf("loadingCfg.duplicateURI", input)); } }
@Test public void cannotPreloadSchemaWithoutTopLevelId() { try { cfg.preloadSchema(JacksonUtils.nodeFactory().objectNode()); fail("No exception thrown!!"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("loadingCfg.noIDInSchema")); } } }
private static SchemaTree treeFromValue(final String keyword, final JsonNode node) { final ObjectNode schema = JacksonUtils.nodeFactory().objectNode(); schema.put(keyword, node); return new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema); }
private static boolean fillMessages(final ProcessingReport report, final MessageBuilder builder) { final Severity severity = LEVEL_MAP.get(report.getLogLevel()); final ArrayNode node = JacksonUtils.nodeFactory().arrayNode(); for (final ProcessingMessage processingMessage : report) { node.add(processingMessage.asJson()); } final String reportAsString = JacksonUtils.prettyPrint(node); final Message message = new Message("", reportAsString, severity); builder.append(message); return report.isSuccess(); }
public ProcessingMessageAssert hasNullField(final String name) { assertThat(msg.has(name)).isTrue(); assertEquals(msg.get(name), JacksonUtils.nodeFactory().nullNode()); return this; }
public ProcessingMessageAssert hasField(final String name, final Integer value) { assertThat(msg.has(name)).isTrue(); final JsonNode wanted = msg.get(name); final JsonNode input = JacksonUtils.nodeFactory().numberNode(value); assertEquals(input, wanted); return this; }
public <T> ProcessingMessageAssert hasField(final String name, final Collection<T> value) { assertThat(msg.has(name)).isTrue(); final JsonNode node = msg.get(name); assertThat(node.isArray()).isTrue(); final ArrayNode input = JacksonUtils.nodeFactory().arrayNode(); for (final T element: value) input.add(element.toString()); assertEquals(node, input); return this; }
@Test public void removingRootReturnsMissingNode() throws JsonPatchException { final JsonNode node = JacksonUtils.nodeFactory().nullNode(); final JsonPatchOperation op = new RemoveOperation(JsonPointer.empty()); final JsonNode ret = op.apply(node); assertTrue(ret.isMissingNode()); } }
@Test public void URIsAreNormalizedBehindTheScenes() throws ProcessingException { final String location = "http://toto/a/../b"; final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); final SchemaTree tree = loader.get(URI.create(location)); assertEquals(tree.getLoadingRef().toURI(), URI.create("http://toto/b#")); }
@Test public void refLoopsAreReported() { final ObjectNode node = JacksonUtils.nodeFactory().objectNode(); node.put("$ref", "#"); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), node); final ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree); try { processor.process(report, holder); fail("No exception thrown!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.refLoop", "#")); } }
@Test public void danglingRefsAreReported() { final ObjectNode node = JacksonUtils.nodeFactory().objectNode(); node.put("$ref", "#/a"); final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), node); final ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree); try { processor.process(report, holder); fail("No exception thrown!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.danglingRef", "#/a")); } } }
@Test public void preloadedSchemasAreNotFetchedAgain() throws ProcessingException, IOException { final String location = "http://foo.bar/baz#"; final URI uri = URI.create(location); final URIDownloader mock = mock(URIDownloader.class); final LoadingConfigurationBuilder builder = LoadingConfiguration .newBuilder().addScheme("http", mock) .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()); LoadingConfiguration cfg; SchemaLoader registry; cfg = builder.freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); //even if cache is disabled cfg = builder.setCacheSize(0).freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); }