javax.json.JsonMergePatch Java Examples

The following examples show how to use javax.json.JsonMergePatch. 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: ContactControllerTest.java    From http-patch-spring with MIT License 6 votes vote down vote up
@Test
@SneakyThrows
public void updateContactUsingJsonMergePatch_shouldReturn204_whenInputIsValidAndContactExists() {

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

    mockMvc.perform(patch("/contacts/{id}", 1L)
            .contentType(PatchMediaType.APPLICATION_MERGE_PATCH_VALUE)
            .content(fromFile("json/contact/patch-with-valid-json-merge-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).mergePatch(any(JsonMergePatch.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 #2
Source File: JsonMergePatchHttpMessageConverter.java    From http-patch-spring with MIT License 5 votes vote down vote up
@Override
protected JsonMergePatch readInternal(Class<? extends JsonMergePatch> clazz, HttpInputMessage inputMessage)
        throws HttpMessageNotReadableException {

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

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

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

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

    return ResponseEntity.noContent().build();
}
 
Example #5
Source File: PatchHelper.java    From http-patch-spring with MIT License 5 votes vote down vote up
private JsonValue applyMergePatch(JsonMergePatch mergePatch, JsonValue target) {
    try {
        return mergePatch.apply(target);
    } catch (Exception e) {
        throw new UnprocessableEntityException(e);
    }
}
 
Example #6
Source File: JsonMergeDiffExampleTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenSourceAndTarget_shouldCreateMergePatch() {
    JsonMergeDiffExample jsonMergeDiffExample = new JsonMergeDiffExample();
    JsonMergePatch mergePatch = jsonMergeDiffExample.createMergePatch();
    JsonString jsonString = (JsonString) mergePatch.toJsonValue();

    assertThat(jsonString.getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}");
}
 
Example #7
Source File: JsonMergePatchHttpMessageConverter.java    From http-patch-spring with MIT License 4 votes vote down vote up
@Override
protected boolean supports(Class<?> clazz) {
    return JsonMergePatch.class.isAssignableFrom(clazz);
}
 
Example #8
Source File: JsonMergePatchExample.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
public JsonValue changeValue() {
    JsonValue source = Json.createValue("{\"colour\":\"blue\"}");
    JsonValue patch = Json.createValue("{\"colour\":\"red\"}");
    JsonMergePatch jsonMergePatch = Json.createMergePatch(patch);
    return jsonMergePatch.apply(source);
}
 
Example #9
Source File: JsonMergePatchExample.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
public JsonValue addValue() {
    JsonValue source = Json.createValue("{\"colour\":\"blue\"}");
    JsonValue patch = Json.createValue("{\"blue\":\"light\"}");
    JsonMergePatch jsonMergePatch = Json.createMergePatch(patch);
    return jsonMergePatch.apply(source);
}
 
Example #10
Source File: JsonMergePatchExample.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
public JsonValue deleteValue() {
    JsonValue source = Json.createValue("{\"colour\":\"blue\"}");
    JsonValue patch = Json.createValue("{\"colour\":null}");
    JsonMergePatch jsonMergePatch = Json.createMergePatch(patch);
    return jsonMergePatch.apply(source);
}
 
Example #11
Source File: JsonMergeDiffExample.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
public JsonMergePatch createMergePatch(){
    JsonValue source = Json.createValue("{\"colour\":\"blue\"}");
    JsonValue target = Json.createValue("{\"colour\":\"red\"}");
    JsonMergePatch jsonMergePatch = Json.createMergeDiff(source, target);
    return jsonMergePatch;
}
 
Example #12
Source File: PreComputedJsonpProvider.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public JsonMergePatch createMergePatch(final JsonValue patch) {
    return jsonpProvider.createMergePatch(patch);
}
 
Example #13
Source File: PreComputedJsonpProvider.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public JsonMergePatch createMergeDiff(final JsonValue source, final JsonValue target) {
    return jsonpProvider.createMergeDiff(source, target);
}
 
Example #14
Source File: ConfigurableBus.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public JsonMergePatch createMergePatch(final JsonValue patch) {
    return provider.createMergePatch(patch);
}
 
Example #15
Source File: ConfigurableBus.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public JsonMergePatch createMergeDiff(final JsonValue source, final JsonValue target) {
    return provider.createMergeDiff(source, target);
}