JsonNode result = patch.apply(serverState);
JsonNode targetJson = mapper.readValue(tmpOutputStream.toByteArray(), JsonNode.class); JsonPatch patch = JsonPatch.fromJson(mapper.readValue(request.getInputStream(), JsonNode.class)); JsonNode result = patch.apply(targetJson); ByteArrayOutputStream targetOutputStream = new ByteArrayOutputStream(); mapper.writeValue(targetOutputStream, result);
@Nonnull @Override public JsonNode migrate(@Nonnull final JsonNode input) throws SwaggerMigrationException { Objects.requireNonNull(input); try { return patch.apply(input); } catch (JsonPatchException e) { throw new SwaggerMigrationException(e); } } };
private JsonNode postMigrate(final JsonNode input) throws SwaggerMigrationException { final JsonNode typeNode = input.path("type"); if (!typeNode.isTextual()) { return input; } if (!"File".equals(typeNode.textValue())) { return input; } try { return fileTypePatch.apply(input); } catch (JsonPatchException e) { throw new SwaggerMigrationException(e); } }
/** * Applies a JsonPatch to an object */ @SuppressWarnings("unchecked") public static <T> T applyPatch(T originalObj, String patchString) throws JsonProcessingException, IOException, JsonPatchException { // Parse the patch to JsonNode JsonNode patchNode = objectMapper.readTree(patchString); // Create the patch JsonPatch patch = JsonPatch.fromJson(patchNode); // Convert the original object to JsonNode JsonNode originalObjNode = objectMapper.valueToTree(originalObj); // Apply the patch TreeNode patchedObjNode = patch.apply(originalObjNode); // Convert the patched node to an updated obj return objectMapper.treeToValue(patchedObjNode, (Class<T>) originalObj.getClass()); }
@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; } }
private static JsonNode patch(final JsonPatchEdit edit, final JsonNode target) { try { return edit.diff().jsonPatch().apply(target); } catch (final JsonPatchException e) { throw new RuntimeException(e.getMessage(), e); } }
/** * {@inheritDoc} */ @Override public void patchCluster(@NotBlank final String id, @NotNull final JsonPatch patch) throws GenieException { final ClusterEntity clusterEntity = this.findCluster(id); try { final Cluster clusterToPatch = JpaServiceUtils.toClusterDto(clusterEntity); log.debug("Will patch cluster {}. Original state: {}", id, clusterToPatch); final JsonNode clusterNode = MAPPER.readTree(clusterToPatch.toString()); final JsonNode postPatchNode = patch.apply(clusterNode); final Cluster patchedCluster = MAPPER.treeToValue(postPatchNode, Cluster.class); log.debug("Finished patching cluster {}. New state: {}", id, patchedCluster); this.updateEntityWithDtoContents(clusterEntity, patchedCluster); } catch (final JsonPatchException | IOException e) { log.error("Unable to patch cluster {} with patch {} due to exception.", id, patch, e); throw new GenieServerException(e.getLocalizedMessage(), e); } }
/** * {@inheritDoc} */ @Override public void patchCommand(@NotBlank final String id, @NotNull final JsonPatch patch) throws GenieException { final CommandEntity commandEntity = this.findCommand(id); try { final Command commandToPatch = JpaServiceUtils.toCommandDto(commandEntity); log.debug("Will patch command {}. Original state: {}", id, commandToPatch); final JsonNode commandNode = MAPPER.readTree(commandToPatch.toString()); final JsonNode postPatchNode = patch.apply(commandNode); final Command patchedCommand = MAPPER.treeToValue(postPatchNode, Command.class); log.debug("Finished patching command {}. New state: {}", id, patchedCommand); this.updateEntityWithDtoContents(commandEntity, patchedCommand); } catch (final JsonPatchException | IOException e) { log.error("Unable to patch cluster {} with patch {} due to exception.", id, patch, e); throw new GenieServerException(e.getLocalizedMessage(), e); } }
/** * {@inheritDoc} */ @Override public void patchApplication(@NotBlank final String id, @NotNull final JsonPatch patch) throws GenieException { final ApplicationEntity applicationEntity = this.findApplication(id); try { final Application appToPatch = JpaServiceUtils.toApplicationDto(applicationEntity); log.debug("Will patch application {}. Original state: {}", id, appToPatch); final JsonNode applicationNode = MAPPER.readTree(appToPatch.toString()); final JsonNode postPatchNode = patch.apply(applicationNode); final Application patchedApp = MAPPER.treeToValue(postPatchNode, Application.class); log.debug("Finished patching application {}. New state: {}", id, patchedApp); this.updateEntityWithDtoContents(applicationEntity, patchedApp); } catch (final JsonPatchException | IOException e) { log.error("Unable to patch application {} with patch {} due to exception.", id, patch, e); throw new GenieServerException(e.getLocalizedMessage(), e); } }
@Test(dataProvider = "getTests") public void testsFromTestSuitePass(final JsonNode source, final JsonPatch patch, final JsonNode expected, final boolean valid) { try { final JsonNode actual = patch.apply(source); if (!valid) fail("Test was expected to fail!!"); // Have to do that... TestNG tries to be too smart with regards // to iterable collections... assertTrue(actual.equals(expected)); } catch (JsonPatchException ignored) { if (valid) fail("Test was expected to succeed!!"); } } }
@Test public void cannotPatchNull() throws JsonPatchException { final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); try { patch.apply(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage( "jsonPatch.nullInput")); } }
@Test(dataProvider = "getPatchesOnly") public void generatedPatchAppliesCleanly(final JsonNode first, final JsonNode second) throws JsonPatchException { final JsonPatch patch = JsonDiff.asJsonPatch(first, second); final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(second); final JsonNode actual = patch.apply(first); assertThat(predicate.apply(actual)).overridingErrorMessage( "Generated patch failed to apply\nexpected: %s\nactual: %s", second, actual ).isTrue(); }
@Test public void whenOneOperationFailsNextOperationIsNotCalled() throws JsonPatchException { final String message = "foo"; when(op1.apply(any(JsonNode.class))) .thenThrow(new JsonPatchException(message)); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); try { patch.apply(FACTORY.nullNode()); fail("No exception thrown!!"); } catch (JsonPatchException e) { assertEquals(e.getMessage(), message); } verifyZeroInteractions(op2); } }
JsonNode targetJson = mapper.readValue(tmpOutputStream.toByteArray(), JsonNode.class); JsonPatch patch = JsonPatch.fromJson(mapper.readValue(request.getInputStream(), JsonNode.class)); JsonNode result = patch.apply(targetJson); ByteArrayOutputStream targetOutputStream = new ByteArrayOutputStream(); mapper.writeValue(targetOutputStream, result);
@Test public void operationsAreCalledInOrder() throws JsonPatchException { final JsonNode node1 = FACTORY.textNode("hello"); final JsonNode node2 = FACTORY.textNode("world"); when(op1.apply(node1)).thenReturn(node2); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); final ArgumentCaptor<JsonNode> captor = ArgumentCaptor.forClass(JsonNode.class); patch.apply(node1); verify(op1, only()).apply(same(node1)); verify(op2, only()).apply(captor.capture()); assertSame(captor.getValue(), node2); }
JsonPatch jsonPatch = CustomObjectMapper.getObjectMapper().convertValue(patch.getData(), JsonPatch.class); try { JsonNode converted = jsonPatch.apply(node); item = CustomObjectMapper.getObjectMapper().convertValue(converted, type); persistenceService.save(item);