Search in sources :

Example 16 with MapBasedShadowStore

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

the class DiffSyncTest method patchEntity_moveProperty_normal.

@Test
public void patchEntity_moveProperty_normal() throws Exception {
    DiffSync<Person> sync = new DiffSync<Person>(new MapBasedShadowStore("x"), Person.class);
    List<PatchOperation> ops = new ArrayList<PatchOperation>();
    ops.add(new MoveOperation("/firstName", "/lastName"));
    VersionedPatch vPatch1 = new VersionedPatch(ops, 0, 0);
    Person person = new Person("Edmund", "Blackadder");
    Person patched = sync.apply(person, vPatch1);
    VersionedPatch diff = sync.diff(patched);
    // the server is acknowledge client version 1 (the client should be at that version by this time)
    assertEquals(1, diff.getClientVersion());
    // the server created the patch against server version 0 (but it will be 1 after the patch is created)
    assertEquals(0, diff.getServerVersion());
    assertEquals("Blackadder", patched.getFirstName());
    assertNull(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 17 with MapBasedShadowStore

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

the class DiffSyncTest method patchList_addNewItem_duplicate.

//
// Guaranteed Delivery - Duplicate packet scenario
//
@Test
public void patchList_addNewItem_duplicate() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-add-new-item");
    VersionedPatch versionedPatch = new VersionedPatch(patch.getOperations(), 0, 0);
    VersionedPatch versionedPatch2 = new VersionedPatch(patch.getOperations(), 0, 0);
    List<Todo> todos = getTodoList();
    List<Todo> patched = sync.apply(todos, versionedPatch, versionedPatch2);
    VersionedPatch diff = sync.diff(patched);
    // the server is acknowledge client version 1 (the client should be at that version by this time)
    assertEquals(1, diff.getClientVersion());
    // the server created the patch against server version 0 (but it will be 1 after the patch is created)
    assertEquals(0, diff.getServerVersion());
    // original should remain unchanged
    assertEquals(todos, getTodoList());
    assertNotEquals(patched, todos);
    assertEquals(4, patched.size());
    assertEquals(todos.get(0), patched.get(0));
    assertEquals(todos.get(1), patched.get(1));
    assertEquals(todos.get(2), patched.get(2));
    assertEquals(new Todo(null, "D", false), patched.get(3));
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 18 with MapBasedShadowStore

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

the class DiffSyncTest method patchList_emptyPatch.

//
// Apply patches - lists
//
@Test
public void patchList_emptyPatch() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-empty");
    List<Todo> todos = getTodoList();
    List<Todo> patched = sync.apply(todos, patch);
    assertEquals(patched, getTodoList());
    // original remains unchanged
    assertEquals(todos, getTodoList());
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 19 with MapBasedShadowStore

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

the class DiffSyncTest method patchList_addNewItem_normal.

//
// Guaranteed Delivery - Normal operations scenario
//
@Test
public void patchList_addNewItem_normal() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("patch-add-new-item");
    VersionedPatch versionedPatch = new VersionedPatch(patch.getOperations(), 0, 0);
    List<Todo> todos = getTodoList();
    List<Todo> patched = sync.apply(todos, versionedPatch);
    VersionedPatch diff = sync.diff(patched);
    // the server is acknowledge client version 1 (the client should be at that version by this time)
    assertEquals(1, diff.getClientVersion());
    // the server created the patch against server version 0 (but it will be 1 after the patch is created)
    assertEquals(0, diff.getServerVersion());
    // original should remain unchanged
    assertEquals(todos, getTodoList());
    assertNotEquals(patched, todos);
    assertEquals(4, patched.size());
    assertEquals(todos.get(0), patched.get(0));
    assertEquals(todos.get(1), patched.get(1));
    assertEquals(todos.get(2), patched.get(2));
    assertEquals(new Todo(null, "D", false), patched.get(3));
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) Patch(org.springframework.sync.Patch) Test(org.junit.Test)

Example 20 with MapBasedShadowStore

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

the class DiffSyncTest method patchEntity_stringAndBooleanProperties.

@Test
public void patchEntity_stringAndBooleanProperties() throws Exception {
    DiffSync<Todo> sync = new DiffSync<Todo>(new MapBasedShadowStore("x"), Todo.class);
    Patch patch = readJsonPatchFromResource("single-change-status-and-desc");
    Todo todo = new Todo(1L, "A", false);
    Todo patched = sync.apply(todo, patch);
    assertEquals(1L, patched.getId().longValue());
    assertEquals("BBB", patched.getDescription());
    assertTrue(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