com.github.fge.jsonpatch.JsonPatchException Java Examples
The following examples show how to use
com.github.fge.jsonpatch.JsonPatchException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 6 votes |
@Test public void shouldAddToCollection() throws PatchBuilderException, IOException, JsonPatchException { Map<String, BuildConfigurationRef> dependencies = new HashMap<>(); BuildConfigurationRef dependency1 = BuildConfigurationRef.refBuilder().id("1").build(); dependencies.put(dependency1.getId(), dependency1); BuildConfiguration buildConfiguration = BuildConfiguration.builder().id("1").dependencies(dependencies).build(); Map<String, BuildConfigurationRef> addDependencies = new HashMap<>(); BuildConfigurationRef dependency2 = BuildConfigurationRef.refBuilder().id("2").build(); addDependencies.put(dependency2.getId(), dependency2); String patchString = new BuildConfigurationPatchBuilder().addDependencies(addDependencies).getJsonPatch(); BuildConfiguration updatedBuildConfiguration = applyPatch(buildConfiguration, patchString); dependencies.putAll(addDependencies); Assertions.assertThat(updatedBuildConfiguration.getDependencies()) .containsKeys(dependency1.getId(), dependency2.getId()); }
Example #2
Source File: CommandRestController.java From genie with Apache License 2.0 | 6 votes |
/** * Patch a command using JSON Patch. * * @param id The id of the command to patch * @param patch The JSON Patch instructions * @throws NotFoundException When no {@link Command} with the given {@literal id} exists * @throws PreconditionFailedException When {@literal id} and the {@literal updateCommand} id don't match * @throws GenieServerException When the patch can't be applied */ @PatchMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.NO_CONTENT) public void patchCommand( @PathVariable("id") final String id, @RequestBody final JsonPatch patch ) throws NotFoundException, PreconditionFailedException, GenieServerException { log.info("Called to patch command {} with patch {}", id, patch); final Command currentCommand = DtoConverters.toV3Command(this.persistenceService.getCommand(id)); try { log.debug("Will patch cluster {}. Original state: {}", id, currentCommand); final JsonNode commandNode = GenieObjectMapper.getMapper().valueToTree(currentCommand); final JsonNode postPatchNode = patch.apply(commandNode); final Command patchedCommand = GenieObjectMapper.getMapper().treeToValue(postPatchNode, Command.class); log.debug("Finished patching command {}. New state: {}", id, patchedCommand); this.persistenceService.updateCommand(id, DtoConverters.toV4Command(patchedCommand)); } catch (final JsonPatchException | IOException e) { log.error("Unable to patch command {} with patch {} due to exception.", id, patch, e); throw new GenieServerException(e.getLocalizedMessage(), e); } }
Example #3
Source File: ClusterRestController.java From genie with Apache License 2.0 | 6 votes |
/** * Patch a cluster using JSON Patch. * * @param id The id of the cluster to patch * @param patch The JSON Patch instructions * @throws NotFoundException If no cluster with {@literal id} exists * @throws PreconditionFailedException If the ids don't match * @throws GenieServerException If the patch can't be applied */ @PatchMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.NO_CONTENT) public void patchCluster( @PathVariable("id") final String id, @RequestBody final JsonPatch patch ) throws NotFoundException, PreconditionFailedException, GenieServerException { log.info("[patchCluster] Called with id {} with patch {}", id, patch); final Cluster currentCluster = DtoConverters.toV3Cluster(this.persistenceService.getCluster(id)); try { log.debug("Will patch cluster {}. Original state: {}", id, currentCluster); final JsonNode clusterNode = GenieObjectMapper.getMapper().valueToTree(currentCluster); final JsonNode postPatchNode = patch.apply(clusterNode); final Cluster patchedCluster = GenieObjectMapper.getMapper().treeToValue(postPatchNode, Cluster.class); log.debug("Finished patching cluster {}. New state: {}", id, patchedCluster); this.persistenceService.updateCluster(id, DtoConverters.toV4Cluster(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); } }
Example #4
Source File: ApplicationRestController.java From genie with Apache License 2.0 | 6 votes |
/** * Patch an application using JSON Patch. * * @param id The id of the application to patch * @param patch The JSON Patch instructions * @throws NotFoundException If no application with the given id exists * @throws PreconditionFailedException When the id in the update doesn't match * @throws GenieServerException If the patch can't be successfully applied */ @PatchMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.NO_CONTENT) public void patchApplication( @PathVariable("id") final String id, @RequestBody final JsonPatch patch ) throws NotFoundException, PreconditionFailedException, GenieServerException { log.info("Called to patch application {} with patch {}", id, patch); final Application currentApp = DtoConverters.toV3Application(this.persistenceService.getApplication(id)); try { log.debug("Will patch application {}. Original state: {}", id, currentApp); final JsonNode applicationNode = GenieObjectMapper.getMapper().valueToTree(currentApp); final JsonNode postPatchNode = patch.apply(applicationNode); final Application patchedApp = GenieObjectMapper.getMapper().treeToValue(postPatchNode, Application.class); log.debug("Finished patching application {}. New state: {}", id, patchedApp); this.persistenceService.updateApplication(id, DtoConverters.toV4Application(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); } }
Example #5
Source File: UserParams.java From aerogear-unifiedpush-server with Apache License 2.0 | 6 votes |
@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; }
Example #6
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 6 votes |
@Test public void shouldReplaceCollection() throws PatchBuilderException, IOException, JsonPatchException { BuildConfigurationRef dependency1 = BuildConfigurationRef.refBuilder().id("1").build(); BuildConfigurationRef dependency2 = BuildConfigurationRef.refBuilder().id("2").build(); BuildConfigurationRef dependency2a = BuildConfigurationRef.refBuilder().id("2").build(); BuildConfigurationRef dependency3 = BuildConfigurationRef.refBuilder().id("3").build(); BuildConfigurationRef dependency4 = BuildConfigurationRef.refBuilder().id("4").build(); Map<String, BuildConfigurationRef> dependencies = new HashMap<>(); dependencies.put(dependency1.getId(), dependency1); dependencies.put(dependency2.getId(), dependency2); BuildConfiguration buildConfiguration = BuildConfiguration.builder().id("1").dependencies(dependencies).build(); Map<String, BuildConfigurationRef> newDependencies = new HashMap<>(); newDependencies.put(dependency2a.getId(), dependency2a); newDependencies.put(dependency3.getId(), dependency3); newDependencies.put(dependency4.getId(), dependency4); String patchString = new BuildConfigurationPatchBuilder().replaceDependencies(newDependencies).getJsonPatch(); BuildConfiguration updatedBuildConfiguration = applyPatch(buildConfiguration, patchString); Assertions.assertThat(updatedBuildConfiguration.getDependencies()) .containsOnly(newDependencies.entrySet().toArray(new Map.Entry[3])); }
Example #7
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 6 votes |
@Test public void shouldAddToMap() throws PatchBuilderException, IOException, JsonPatchException { Map<String, String> genericParameters = new HashMap<>(); genericParameters.put("k", "v"); BuildConfiguration buildConfiguration = BuildConfiguration.builder() .id("1") .parameters(genericParameters) .build(); Map<String, String> addParameters = Collections.singletonMap("k2", "v2"); String patchString = new BuildConfigurationPatchBuilder().addParameters(addParameters).getJsonPatch(); BuildConfiguration updatedBuildConfiguration = applyPatch(buildConfiguration, patchString); genericParameters.putAll(addParameters); Assertions.assertThat(updatedBuildConfiguration.getParameters()) .contains(genericParameters.entrySet().toArray(new Map.Entry[2])); }
Example #8
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 6 votes |
@Test public void shouldReplaceRef() throws PatchBuilderException, IOException, JsonPatchException { ProjectRef project = ProjectRef.refBuilder().id("1").name("Project 1").build(); ProjectRef newProject = ProjectRef.refBuilder().id("2").name("Project 2").build(); BuildConfiguration buildConfiguration = BuildConfiguration.builder() .id("1") .name("BC 1") .project(project) .build(); String patchString = new BuildConfigurationPatchBuilder().replaceProject(newProject) .replaceName("Build Configuration 1") .getJsonPatch(); BuildConfiguration updatedBuildConfiguration = applyPatch(buildConfiguration, patchString); Assert.assertEquals(newProject, updatedBuildConfiguration.getProject()); }
Example #9
Source File: TollBoothHiveDestinationAdminClientTest.java From data-highway with Apache License 2.0 | 6 votes |
@Before public void before() { PatchSetEmitter modificationEmitter = new PatchSetEmitter() { @Override public void emit(PatchSet roadPatch) { try { JsonNode roadJson = Optional .ofNullable(store.get(roadPatch.getDocumentId())) .map(r -> mapper.convertValue(r, JsonNode.class)) .orElse(NullNode.instance); JsonNode patchJson = mapper.convertValue(roadPatch.getOperations(), JsonNode.class); JsonPatch jsonPatch = JsonPatch.fromJson(patchJson); JsonNode newRoadJson = jsonPatch.apply(roadJson); Road nnewRoad = mapper.convertValue(newRoadJson, Road.class); store.put(roadPatch.getDocumentId(), nnewRoad); } catch (IOException | JsonPatchException e) { throw new RuntimeException(e); } } }; underTest = new TollBoothHiveDestinationAdminClient(store, modificationEmitter); }
Example #10
Source File: JsonPatchApplier.java From data-highway with Apache License 2.0 | 5 votes |
public JsonNode apply(JsonNode document, List<PatchOperation> patchOperations) throws PatchApplicationException { try { JsonNode patch = mapper.convertValue(patchOperations, JsonNode.class); JsonPatch jsonPatch = JsonPatch.fromJson(patch); return jsonPatch.apply(document); } catch (IOException | JsonPatchException e) { String message = String.format("Unable to apply patch to document. document: %s, patch: %s", document, patchOperations); throw new PatchApplicationException(message, e); } }
Example #11
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 5 votes |
@Test public void shouldReplaceSimpleValue() throws PatchBuilderException, IOException, JsonPatchException { BuildConfiguration buildConfiguration = BuildConfiguration.builder().id("1").description("Hello Tom!").build(); String newDescription = "Hi Jerry"; String patchString = new BuildConfigurationPatchBuilder().replaceDescription(newDescription).getJsonPatch(); BuildConfiguration updatedBuildConfiguration = applyPatch(buildConfiguration, patchString); Assert.assertEquals(newDescription, updatedBuildConfiguration.getDescription()); }
Example #12
Source File: BuildConfigurationPatchTest.java From pnc with Apache License 2.0 | 5 votes |
private BuildConfiguration applyPatch(BuildConfiguration buildConfiguration, String patchString) throws IOException, JsonPatchException { logger.info("Original: " + mapper.writeValueAsString(buildConfiguration)); logger.info("Json patch:" + patchString); JsonPatch patch = JsonPatch.fromJson(mapper.readValue(patchString, JsonNode.class)); JsonNode result = patch.apply(mapper.valueToTree(buildConfiguration)); logger.info("Patched: " + mapper.writeValueAsString(result)); return mapper.treeToValue(result, BuildConfiguration.class); }
Example #13
Source File: BuildConfigurationSerializationTest.java From pnc with Apache License 2.0 | 5 votes |
@Test public void shouldPatchBuildConfiguration() throws PatchBuilderException, IOException, JsonPatchException { ObjectMapper mapper = ObjectMapperProvider.getInstance(); // given Instant now = Instant.now(); Map<String, String> initialParameters = Collections.singletonMap("KEY", "VALUE"); BuildConfiguration buildConfiguration = BuildConfiguration.builder() .id("1") .name("name") .creationTime(now) .parameters(initialParameters) .build(); // when BuildConfigurationPatchBuilder patchBuilder = new BuildConfigurationPatchBuilder(); patchBuilder.replaceName("new name"); Map<String, String> newParameter = Collections.singletonMap("KEY 2", "VALUE 2"); patchBuilder.addParameters(newParameter); JsonNode targetJson = mapper.valueToTree(buildConfiguration); JsonPatch patch = JsonPatch.fromJson(mapper.readValue(patchBuilder.getJsonPatch(), JsonNode.class)); JsonNode result = patch.apply(targetJson); // then BuildConfiguration deserialized = mapper.treeToValue(result, BuildConfiguration.class); Assert.assertEquals(now, deserialized.getCreationTime()); Assert.assertEquals("new name", deserialized.getName()); Map<String, String> finalParameters = new HashMap<>(initialParameters); finalParameters.putAll(newParameter); assertThat(deserialized.getParameters()).containsAllEntriesOf(finalParameters); }
Example #14
Source File: TollboothRoadAdminClientTest.java From data-highway with Apache License 2.0 | 5 votes |
@Before public void before() { road1 = new Road(); road1.setName("road1"); road1.setTopicName("road1"); road1.setDescription("description"); road1.setContactEmail("contactEmail"); road1.setEnabled(false); status = new KafkaStatus(); status.setTopicCreated(false); road1.setStatus(status); road1.setDeleted(false); PatchSetEmitter patchSetEmitter = new PatchSetEmitter() { @Override public void emit(PatchSet patchSet) { try { JsonNode roadJson = Optional .ofNullable(store.get(patchSet.getDocumentId())) .map(r -> mapper.convertValue(r, JsonNode.class)) .orElse(NullNode.instance); JsonNode patchJson = mapper.convertValue(patchSet.getOperations(), JsonNode.class); JsonPatch jsonPatch = JsonPatch.fromJson(patchJson); JsonNode newRoadJson = jsonPatch.apply(roadJson); Road nnewRoad = mapper.convertValue(newRoadJson, Road.class); store.put(patchSet.getDocumentId(), nnewRoad); } catch (IOException | JsonPatchException e) { throw new RuntimeException(e); } } }; store = new HashMap<>(); client = new TollboothRoadAdminClient(Collections.unmodifiableMap(store), patchSetEmitter); }
Example #15
Source File: MemoryRoadAdminClient.java From data-highway with Apache License 2.0 | 5 votes |
JsonNode applyPatch(JsonNode road, PatchSet patch) { try { JsonNode jsonNodePatch = mapper.convertValue(patch.getOperations(), JsonNode.class); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNodePatch); return jsonPatch.apply(road); } catch (IOException | JsonPatchException e) { throw new ServiceException(e); } }
Example #16
Source File: CustomerRestController.java From tutorials with MIT License | 4 votes |
private Customer applyPatchToCustomer(JsonPatch patch, Customer targetCustomer) throws JsonPatchException, JsonProcessingException { JsonNode patched = patch.apply(objectMapper.convertValue(targetCustomer, JsonNode.class)); return objectMapper.treeToValue(patched, Customer.class); }
Example #17
Source File: TollboothSchemaStoreClientTest.java From data-highway with Apache License 2.0 | 4 votes |
@Before public void before() { mapper.registerModule(new SchemaSerializationModule()); road1 = new Road(); road1.setName("road1"); road1.setTopicName("road1"); road1.setDescription("description"); road1.setContactEmail("contactEmail"); road1.setEnabled(false); status = new KafkaStatus(); status.setTopicCreated(false); road1.setStatus(status); road1.setSchemas(Collections.emptyMap()); road1.setDeleted(false); schema1 = SchemaBuilder.builder().record("a").fields().name("v").type().booleanType().noDefault().endRecord(); schema2 = SchemaBuilder .builder() .record("a") .fields() .name("v") .type() .booleanType() .booleanDefault(false) .endRecord(); schema3 = SchemaBuilder .builder() .record("a") .fields() .name("v") .type() .booleanType() .booleanDefault(false) .optionalString("u") .endRecord(); schema4 = SchemaBuilder .builder() .record("a") .fields() .name("v") .type() .booleanType() .booleanDefault(false) .requiredString("u") .endRecord(); schemaVersion1 = new SchemaVersion(schema1, 1, false); schemaVersion2 = new SchemaVersion(schema2, 2, false); schemaVersion3 = new SchemaVersion(schema3, 3, false); schemaVersionsMap = ImmutableMap.of(schemaVersion1.getVersion(), schemaVersion1, schemaVersion2.getVersion(), schemaVersion2, schemaVersion3.getVersion(), schemaVersion3); PatchSetEmitter patchSetEmitter = new PatchSetEmitter() { @Override public void emit(PatchSet patchSet) { try { JsonNode roadJson = Optional .ofNullable(store.get(patchSet.getDocumentId())) .map(r -> mapper.convertValue(r, JsonNode.class)) .orElse(NullNode.instance); JsonNode patchJson = mapper.convertValue(patchSet.getOperations(), JsonNode.class); JsonPatch jsonPatch = JsonPatch.fromJson(patchJson); JsonNode newRoadJson = jsonPatch.apply(roadJson); Road nnewRoad = mapper.convertValue(newRoadJson, Road.class); store.put(patchSet.getDocumentId(), nnewRoad); } catch (IOException | JsonPatchException e) { throw new RuntimeException(e); } } }; store = new HashMap<>(); store.put("road1", road1); client = new TollboothSchemaStoreClient(Collections.unmodifiableMap(store), patchSetEmitter); }