Search in sources :

Example 1 with PatchException

use of org.springframework.sync.PatchException in project spring-sync by spring-projects.

the class DiffSyncTest method patchList_patchFailingOperationFirst.

@Test
public void patchList_patchFailingOperationFirst() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-failing-operation-first");
    List<Todo> todos = getTodoList();
    List<Todo> patched = null;
    try {
        patched = sync.apply(todos, patch);
        fail();
    } catch (PatchException e) {
        // original should remain unchanged
        assertEquals(todos, getTodoList());
        assertNull(patched);
    }
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) PatchException(org.springframework.sync.PatchException) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 2 with PatchException

use of org.springframework.sync.PatchException in project spring-sync by spring-projects.

the class JsonPatchPatchConverter method convert.

/**
	 * Constructs a {@link Patch} object given a JsonNode.
	 * @param jsonNode a JsonNode containing the JSON Patch
	 * @return a {@link Patch}
	 */
public Patch convert(JsonNode jsonNode) {
    if (!(jsonNode instanceof ArrayNode)) {
        throw new IllegalArgumentException("JsonNode must be an instance of ArrayNode");
    }
    ArrayNode opNodes = (ArrayNode) jsonNode;
    List<PatchOperation> ops = new ArrayList<PatchOperation>(opNodes.size());
    for (Iterator<JsonNode> elements = opNodes.elements(); elements.hasNext(); ) {
        JsonNode opNode = elements.next();
        String opType = opNode.get("op").textValue();
        String path = opNode.get("path").textValue();
        JsonNode valueNode = opNode.get("value");
        Object value = valueFromJsonNode(path, valueNode);
        String from = opNode.has("from") ? opNode.get("from").textValue() : null;
        if (opType.equals("test")) {
            ops.add(new TestOperation(path, value));
        } else if (opType.equals("replace")) {
            ops.add(new ReplaceOperation(path, value));
        } else if (opType.equals("remove")) {
            ops.add(new RemoveOperation(path));
        } else if (opType.equals("add")) {
            ops.add(new AddOperation(path, value));
        } else if (opType.equals("copy")) {
            ops.add(new CopyOperation(path, from));
        } else if (opType.equals("move")) {
            ops.add(new MoveOperation(path, from));
        } else {
            throw new PatchException("Unrecognized operation type: " + opType);
        }
    }
    return new Patch(ops);
}
Also used : ArrayList(java.util.ArrayList) RemoveOperation(org.springframework.sync.RemoveOperation) JsonNode(com.fasterxml.jackson.databind.JsonNode) AddOperation(org.springframework.sync.AddOperation) TestOperation(org.springframework.sync.TestOperation) CopyOperation(org.springframework.sync.CopyOperation) ReplaceOperation(org.springframework.sync.ReplaceOperation) MoveOperation(org.springframework.sync.MoveOperation) PatchOperation(org.springframework.sync.PatchOperation) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PatchException(org.springframework.sync.PatchException) Patch(org.springframework.sync.Patch)

Example 3 with PatchException

use of org.springframework.sync.PatchException in project spring-sync by spring-projects.

the class DiffSyncTest method patchList_patchFailingOperationInMiddle.

@Test
public void patchList_patchFailingOperationInMiddle() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-failing-operation-in-middle");
    List<Todo> todos = getTodoList();
    List<Todo> patched = null;
    try {
        patched = sync.apply(todos, patch);
        fail();
    } catch (PatchException e) {
        // original should remain unchanged
        assertEquals(todos, getTodoList());
        assertNull(patched);
    }
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) PatchException(org.springframework.sync.PatchException) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Aggregations

Patch (org.springframework.sync.Patch)3 PatchException (org.springframework.sync.PatchException)3 Test (org.junit.Test)2 Todo (org.springframework.sync.Todo)2 MapBasedShadowStore (org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ArrayList (java.util.ArrayList)1 AddOperation (org.springframework.sync.AddOperation)1 CopyOperation (org.springframework.sync.CopyOperation)1 MoveOperation (org.springframework.sync.MoveOperation)1 PatchOperation (org.springframework.sync.PatchOperation)1 RemoveOperation (org.springframework.sync.RemoveOperation)1 ReplaceOperation (org.springframework.sync.ReplaceOperation)1 TestOperation (org.springframework.sync.TestOperation)1