javax.json.JsonPatch Java Examples

The following examples show how to use javax.json.JsonPatch. 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: JsonpTest.java    From ee8-sandbox with Apache License 2.0 8 votes vote down vote up
@Test
public void testJsonPatch() {
    JsonReader reader = Json.createReader(JsonpTest.class.getResourceAsStream("/persons.json"));
    JsonArray jsonaArray = reader.readArray();

    JsonPatch patch = Json.createPatchBuilder()        
            .replace("/0/name", "Duke Oracle")
            .remove("/1")
            .build();

    JsonArray result = patch.apply(jsonaArray);
    System.out.println(result.toString());
    
    Type type = new ArrayList<Person>() {}.getClass().getGenericSuperclass();

    List<Person> person = JsonbBuilder.create().fromJson(result.toString(), type);
    assertEquals("Duke Oracle", person.get(0).getName());

}
 
Example #2
Source File: ContactControllerTest.java    From http-patch-spring with MIT License 6 votes vote down vote up
@Test
@SneakyThrows
public void updateContactUsingJsonPatch_shouldReturn204_whenInputIsValidAndContactExists() {

    when(service.findContact(anyLong())).thenReturn(Optional.of(contactPersisted()));

    mockMvc.perform(patch("/contacts/{id}", 1L)
            .contentType(PatchMediaType.APPLICATION_JSON_PATCH_VALUE)
            .content(fromFile("json/contact/patch-with-valid-json-patch-payload.json")))
            .andDo(print())
            .andExpect(status().isNoContent());

    verify(mapper).asInput(any(Contact.class));
    verify(mapper).update(any(Contact.class), any(ContactResourceInput.class));

    verify(patchHelper).patch(any(JsonPatch.class), isA(ContactResourceInput.class), eq(ContactResourceInput.class));
    verifyNoMoreInteractions(patchHelper);

    ArgumentCaptor<Contact> contactArgumentCaptor = ArgumentCaptor.forClass(Contact.class);
    verify(service).findContact(anyLong());
    verify(service).updateContact(contactArgumentCaptor.capture());
    verifyNoMoreInteractions(service);

    assertThat(contactArgumentCaptor.getValue()).isEqualToComparingFieldByFieldRecursively(contactToUpdate());
}
 
Example #3
Source File: Patch.java    From FHIR with Apache License 2.0 6 votes vote down vote up
private FHIRPatch createPatch(JsonArray array) throws FHIROperationException {
    try {
        FHIRPatch patch = FHIRPatch.patch(array);
        JsonPatch jsonPatch = patch.as(FHIRJsonPatch.class).getJsonPatch();
        for (JsonValue value : jsonPatch.toJsonArray()) {
            // validate path
            String path = value.asJsonObject().getString("path");
            if ("/id".equals(path) ||
                    "/meta/versionId".equals(path) ||
                    "/meta/lastUpdated".equals(path)) {
                throw new IllegalArgumentException("Path: '" + path
                        + "' is not allowed in a patch operation.");
            }
        }
        return patch;
    } catch (Exception e) {
        String msg = "Invalid patch: " + e.getMessage();
        throw new FHIROperationException(msg, e)
                .withIssue(FHIRUtil.buildOperationOutcomeIssue(msg, IssueType.INVALID));
    }
}
 
Example #4
Source File: JsonPatchHttpMessageConverter.java    From http-patch-spring with MIT License 5 votes vote down vote up
@Override
protected JsonPatch readInternal(Class<? extends JsonPatch> clazz, HttpInputMessage inputMessage)
        throws HttpMessageNotReadableException {

    try (JsonReader reader = Json.createReader(inputMessage.getBody())) {
        return Json.createPatch(reader.readArray());
    } catch (Exception e) {
        throw new HttpMessageNotReadableException(e.getMessage(), inputMessage);
    }
}
 
Example #5
Source File: JsonPatchHttpMessageConverter.java    From http-patch-spring with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(JsonPatch jsonPatch, HttpOutputMessage outputMessage)
        throws HttpMessageNotWritableException {

    try (JsonWriter writer = Json.createWriter(outputMessage.getBody())) {
        writer.write(jsonPatch.toJsonArray());
    } catch (Exception e) {
        throw new HttpMessageNotWritableException(e.getMessage(), e);
    }
}
 
Example #6
Source File: ContactController.java    From http-patch-spring with MIT License 5 votes vote down vote up
@PatchMapping(path = "/{id}", consumes = PatchMediaType.APPLICATION_JSON_PATCH_VALUE)
public ResponseEntity<Void> updateContact(@PathVariable Long id,
                                          @RequestBody JsonPatch patchDocument) {

    Contact contact = service.findContact(id).orElseThrow(ResourceNotFoundException::new);
    ContactResourceInput contactResource = mapper.asInput(contact);
    ContactResourceInput contactResourcePatched = patchHelper.patch(patchDocument, contactResource, ContactResourceInput.class);

    mapper.update(contact, contactResourcePatched);
    service.updateContact(contact);

    return ResponseEntity.noContent().build();
}
 
Example #7
Source File: SwaggerOptions.java    From jaxrs-analyzer with Apache License 2.0 5 votes vote down vote up
private static JsonPatch readPatch(final String patchFile) {
    try {
        final JsonArray patchArray = Json.createReader(Files.newBufferedReader(Paths.get(patchFile))).readArray();
        return Json.createPatchBuilder(patchArray).build();
    } catch (Exception e) {
        LogProvider.error("Could not read JSON patch from the specified location, reason: " + e.getMessage());
        LogProvider.error("Patch won't be applied");
        LogProvider.debug(e);
        return null;
    }
}
 
Example #8
Source File: JsonPatchHttpMessageConverter.java    From http-patch-spring with MIT License 4 votes vote down vote up
@Override
protected boolean supports(Class<?> clazz) {
    return JsonPatch.class.isAssignableFrom(clazz);
}
 
Example #9
Source File: PatchHelper.java    From http-patch-spring with MIT License 4 votes vote down vote up
private JsonValue applyPatch(JsonPatch patch, JsonStructure target) {
    try {
        return patch.apply(target);
    } catch (Exception e) {
        throw new UnprocessableEntityException(e);
    }
}
 
Example #10
Source File: FHIRJsonPatch.java    From FHIR with Apache License 2.0 4 votes vote down vote up
FHIRJsonPatch(JsonPatch patch) {
    this.patch = Objects.requireNonNull(patch);
}
 
Example #11
Source File: FHIRJsonPatch.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public JsonPatch getJsonPatch() {
    return patch;
}
 
Example #12
Source File: PreComputedJsonpProvider.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public JsonPatch createPatch(final JsonArray array) {
    return jsonpProvider.createPatch(array);
}
 
Example #13
Source File: PreComputedJsonpProvider.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public JsonPatch createDiff(final JsonStructure source, final JsonStructure target) {
    return jsonpProvider.createDiff(source, target);
}
 
Example #14
Source File: ConfigurableBus.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public JsonPatch createPatch(final JsonArray array) {
    return provider.createPatch(array);
}
 
Example #15
Source File: ConfigurableBus.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public JsonPatch createDiff(final JsonStructure source, final JsonStructure target) {
    return provider.createDiff(source, target);
}
 
Example #16
Source File: SwaggerOptions.java    From jaxrs-analyzer with Apache License 2.0 4 votes vote down vote up
JsonPatch getJsonPatch() {
    return jsonPatch;
}