Search in sources :

Example 6 with MapBasedShadowStore

use of org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore in project spring-sync by spring-projects.

the class DiffSyncTest method patchList_changeSingleEntityStatusAndDescription.

@Test
public void patchList_changeSingleEntityStatusAndDescription() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-change-single-status-and-desc");
    List<Todo> todos = getTodoList();
    List<Todo> patched = sync.apply(todos, patch);
    // original should remain unchanged
    assertEquals(todos, getTodoList());
    assertNotEquals(patched, todos);
    assertEquals(3, patched.size());
    assertEquals(todos.get(0), patched.get(0));
    assertEquals(new Todo(2L, "BBB", true), patched.get(1));
    assertEquals(todos.get(2), patched.get(2));
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 7 with MapBasedShadowStore

use of org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore in project spring-sync by spring-projects.

the class DiffSyncTest method patchEntity_moveProperty_lostReturnPacket.

@Test
public void patchEntity_moveProperty_lostReturnPacket() throws Exception {
    DiffSync<Person> sync = new DiffSync<Person>(new MapBasedShadowStore("x"), Person.class);
    Person person = new Person("Edmund", "Blackadder");
    List<PatchOperation> ops1 = new ArrayList<PatchOperation>();
    ops1.add(new MoveOperation("/firstName", "/lastName"));
    VersionedPatch vPatch1 = new VersionedPatch(ops1, 0, 0);
    Person patched = sync.apply(person, vPatch1);
    assertEquals("Blackadder", patched.getFirstName());
    assertNull(patched.getLastName());
    VersionedPatch lostDiff = sync.diff(patched);
    assertEquals(1, lostDiff.getClientVersion());
    assertEquals(0, lostDiff.getServerVersion());
    List<PatchOperation> ops2 = new ArrayList<PatchOperation>();
    ops2.add(new MoveOperation("/lastName", "/firstName"));
    VersionedPatch vPatch2 = new VersionedPatch(ops2, 0, 1);
    patched = sync.apply(patched, vPatch1, vPatch2);
    VersionedPatch diff = sync.diff(patched);
    assertEquals(2, diff.getClientVersion());
    assertEquals(0, diff.getServerVersion());
    assertNull(patched.getFirstName());
    assertEquals("Blackadder", patched.getLastName());
}
Also used : MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) MoveOperation(org.springframework.sync.MoveOperation) ArrayList(java.util.ArrayList) PatchOperation(org.springframework.sync.PatchOperation) Person(org.springframework.sync.Person) Test(org.junit.Test)

Example 8 with MapBasedShadowStore

use of org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore 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)

Example 9 with MapBasedShadowStore

use of org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore in project spring-sync by spring-projects.

the class DiffSyncTest method patchEntity_stringProperty.

@Test
public void patchEntity_stringProperty() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("single-change-description");
    Todo todo = new Todo(1L, "A", false);
    Todo patched = sync.apply(todo, patch);
    assertEquals(1L, patched.getId().longValue());
    assertEquals("AAA", patched.getDescription());
    assertFalse(patched.isComplete());
    // original remains unchanged
    assertEquals(1L, todo.getId().longValue());
    assertEquals("A", todo.getDescription());
    assertFalse(todo.isComplete());
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 10 with MapBasedShadowStore

use of org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore in project spring-sync by spring-projects.

the class DiffSyncTest method patchEntity_emptyPatch.

//
// Apply patches - single entity
//
@Test
public void patchEntity_emptyPatch() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-empty");
    Todo todo = new Todo(1L, "A", false);
    Todo patched = sync.apply(todo, patch);
    assertEquals(1L, patched.getId().longValue());
    assertEquals("A", patched.getDescription());
    assertFalse(patched.isComplete());
    // original remains unchanged
    assertEquals(1L, todo.getId().longValue());
    assertEquals("A", todo.getDescription());
    assertFalse(todo.isComplete());
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Aggregations

MapBasedShadowStore (org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore)26 Test (org.junit.Test)25 Todo (org.springframework.sync.Todo)22 Patch (org.springframework.sync.Patch)21 ArrayList (java.util.ArrayList)5 PatchOperation (org.springframework.sync.PatchOperation)5 MoveOperation (org.springframework.sync.MoveOperation)4 Person (org.springframework.sync.Person)4 PatchException (org.springframework.sync.PatchException)2 AddOperation (org.springframework.sync.AddOperation)1 PersistenceCallbackRegistry (org.springframework.sync.diffsync.PersistenceCallbackRegistry)1 ShadowStore (org.springframework.sync.diffsync.ShadowStore)1 MockMvc (org.springframework.test.web.servlet.MockMvc)1