Refine search
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 JsonNode digest(final JsonNode schema) final ObjectNode ret = FACTORY.objectNode(); final ObjectNode propertyDeps = FACTORY.objectNode(); ret.put("propertyDeps", propertyDeps); final ArrayNode schemaDeps = FACTORY.arrayNode(); ret.put("schemaDeps", schemaDeps); = JacksonUtils.asMap(schema.get(keyword)); case ARRAY: final JsonNode node = sortedSet(value); if (node.size() != 0) propertyDeps.put(key, node); break; case STRING: propertyDeps.put(key, FACTORY.arrayNode() .add(value.textValue()));
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ObjectNode propertyDeps = FACTORY.objectNode(); ret.put("propertyDeps", propertyDeps); final ArrayNode schemaDeps = FACTORY.arrayNode(); ret.put("schemaDeps", schemaDeps); final List<String> list = Lists.newArrayList(); final Map<String, JsonNode> map = JacksonUtils.asMap(schema.get(keyword)); String key; JsonNode value; for (final Map.Entry<String, JsonNode> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); if (value.isObject()) // schema dep list.add(key); else // property dep propertyDeps.put(key, sortedSet(value)); } for (final String s: Ordering.natural().sortedCopy(list)) schemaDeps.add(s); return ret; }
public static void main(final String... args) throws IOException, ProcessingException { final JsonNode googleAPI = JsonLoader.fromResource("/other/google-json-api.json"); final Map<String, JsonNode> googleSchemas = JacksonUtils.asMap(googleAPI.get("schemas")); long begin, current; begin = System.currentTimeMillis(); doValidate(googleSchemas, -1); current = System.currentTimeMillis(); System.out.println("Initial validation :" + (current - begin) + " ms"); begin = System.currentTimeMillis(); for (int i = 0; i < 500; i++) { doValidate(googleSchemas, i); if (i % 20 == 0) { current = System.currentTimeMillis(); System.out.println(String.format("Iteration %d (in %d ms)", i, current - begin)); } } final long end = System.currentTimeMillis(); System.out.println("END -- time in ms: " + (end - begin)); System.exit(0); }
public DependenciesValidator(final JsonNode digest) { super("dependencies"); /* * Property dependencies */ final ImmutableMultimap.Builder<String, String> mapBuilder = ImmutableMultimap.builder(); final Map<String, JsonNode> map = JacksonUtils.asMap(digest.get("propertyDeps")); String key; for (final Map.Entry<String, JsonNode> entry: map.entrySet()) { key = entry.getKey(); for (final JsonNode element: entry.getValue()) mapBuilder.put(key, element.textValue()); } propertyDeps = mapBuilder.build(); /* * Schema dependencies */ final ImmutableSet.Builder<String> setBuilder = ImmutableSet.builder(); for (final JsonNode node: digest.get("schemaDeps")) setBuilder.add(node.textValue()); schemaDeps = setBuilder.build(); }
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; }
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 ProcessingMessageAssert hasNullField(final String name) { assertThat(msg.has(name)).isTrue(); assertEquals(msg.get(name), JacksonUtils.nodeFactory().nullNode()); return this; }
@Override public JsonNode transform(JsonNode patched) throws IOException { Iterator<Map.Entry<String, JsonNode>> nodeIterator = patched.get("message").fields(); while (nodeIterator.hasNext()) { Map.Entry<String, JsonNode> entry = nodeIterator.next(); if (!KNOWN_KEYS.contains(entry.getKey())) { String json = format(MOVE_OP, entry.getKey()); try { patched = JsonPatch.fromJson(JacksonUtils.getReader().readTree(json)).apply(patched); } catch (JsonPatchException e) { throw new RuntimeException("move operation could not be applied", e); } } } return patched; } }
protected JsonPatchOperationTest(final String prefix) throws IOException { final String resource = "/jsonpatch/" + prefix + ".json"; final JsonNode node = JsonLoader.fromResource(resource); errors = node.get("errors"); ops = node.get("ops"); reader = JacksonUtils.getReader().withType(JsonPatchOperation.class); }
@Override protected final void checkValue(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final JsonNode node = getNode(tree); final Map<String, JsonNode> map = Maps.newTreeMap(); map.putAll(JacksonUtils.asMap(node)); String key; JsonNode value; for (final Map.Entry<String, JsonNode> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); if (value.isObject()) pointers.add(JsonPointer.of(keyword, key)); else checkDependency(report, bundle, entry.getKey(), tree); } }
@Test(dataProvider = "getNonObjectInputs") public void nonObjectSerDeserWorksCorrectly(final JsonNode input) throws IOException { final String in = input.toString(); final JsonMergePatch deserialized = mapper.readValue(in, JsonMergePatch.class); assertNotNull(deserialized); assertSame(deserialized.getClass(), NonObjectMergePatch.class); final String out = mapper.writeValueAsString(deserialized); final JsonNode serialized = JacksonUtils.getReader().readTree(out); assertTrue(EQUIVALENCE.equivalent(input, serialized)); }
final ObjectNode ret = input.isObject() ? (ObjectNode) input.deepCopy() : JacksonUtils.nodeFactory().objectNode();
@Test public void cannotPreloadSchemaWithoutTopLevelId() { try { cfg.preloadSchema(JacksonUtils.nodeFactory().objectNode()); fail("No exception thrown!!"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("loadingCfg.noIDInSchema")); } } }
@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)); }
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; }
@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()); } }
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(); }
@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) { } }
/** * No-arg constructor (see description) */ public JsonNodeReader() { this(JacksonUtils.newMapper()); }