package com.simperium.client;

import com.simperium.client.RemoteChange;

import com.simperium.test.MockBucket;

import com.simperium.models.Note;

import org.json.JSONObject;

import junit.framework.TestCase;

public class RemoteChangeTest extends TestCase {

    public void testParseRemoteAddOperation()
    throws Exception {
        String changeString = "{\"cv\":\"mock-cv\",\"ccids\":[\"abc\"],\"ev\":1, \"id\":\"mock\",\"clientid\":\"mock-client\",\"o\":\"M\",\"v\":{}}";
        JSONObject changeJSON = new JSONObject(changeString);

        RemoteChange change = RemoteChange.buildFromMap(changeJSON);

        assertTrue(change.isAddOperation());
        assertFalse(change.isModifyOperation());
        assertFalse(change.isRemoveOperation());

    }

    public void testParseRemoteDeleteOperation()
    throws Exception {
        String changeString = "{\"cv\":\"mock-cv\",\"ccids\":[\"abc\"],\"ev\":2, \"id\":\"mock\",\"clientid\":\"mock-client\",\"o\":\"-\"}";
        JSONObject changeJSON = new JSONObject(changeString);

        RemoteChange change = RemoteChange.buildFromMap(changeJSON);
        assertTrue(change.isRemoveOperation());
        assertFalse(change.isAddOperation());
        assertFalse(change.isModifyOperation());
    }

    public void testParseRemoteModifyOperation()
    throws Exception {
        String changeString = "{\"cv\":\"mock-cv\",\"ccids\":[\"abc\"],\"ev\":2,\"sv\":1,\"id\":\"mock\",\"clientid\":\"mock-client\",\"o\":\"M\",\"v\":{}}";
        JSONObject changeJSON = new JSONObject(changeString);

        RemoteChange change = RemoteChange.buildFromMap(changeJSON);

        assertTrue(change.isModifyOperation());
        assertFalse(change.isRemoveOperation());
        assertFalse(change.isAddOperation());
    }

}
@@ -204,16 +204,59 @@
 tCase;%0A%0A
+import static android.test.MoreAsserts.*;%0A%0A
 public c
@@ -1808,9 +1808,1054 @@
 %0A    %7D%0A%0A
+    /**%0A     * first we need an existing object%0A     */%0A    public void testCatchInvalidPatch()%0A    throws Exception %7B%0A        MockBucket%3CNote%3E notes = MockBucket.buildBucket(new Note.Schema());%0A        // insert a mock ghost%0A        Note note = notes.newObject(%22mock%22);%0A        note.setTitle(%22my hovercraft is full of eels%22);%0A        note.save();%0A%0A        // This change has an invalid diff-match-path value%0A        String changeJSON = %22%7B%5C%22cv%5C%22:%5C%22mock-cv%5C%22,%5C%22ccids%5C%22:%5B%5C%22abc%5C%22%5D,%5C%22ev%5C%22:2,%5C%22sv%5C%22:1,%5C%22id%5C%22:%5C%22mock%5C%22,%5C%22clientid%5C%22:%5C%22mock-client%5C%22,%5C%22o%5C%22:%5C%22M%5C%22,%5C%22v%5C%22:%7B%5C%22title%5C%22:%7B%5C%22v%5C%22:%5C%22=14%5C%5Ct-1%5C%5Ct+wa%5C%5Ct=10%5C%22,%5C%22o%5C%22:%5C%22d%5C%22%7D%7D%7D%22;%0A        RemoteChange change = RemoteChange.buildFromMap(new JSONObject(changeJSON));%0A%0A        boolean caught = false;%0A        try %7B%0A            change.apply(note);%0A        %7D catch (RemoteChangeInvalidException e) %7B%0A            caught = true;%0A            assertAssignableFrom(IllegalArgumentException.class, e.getCause());%0A        %7D%0A%0A        assertTrue(%22RemoteChangeInvalidException should have been caught%22, caught);%0A%0A    %7D%0A%0A%0A
 %7D

Test to reproduce #32