Skip to content

Commit a432d00

Browse files
author
Ben Lei
committed
Enable hanlding delete record response of different format
1 parent 3fa63e8 commit a432d00

12 files changed

+411
-249
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.skygear.skygear;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
import static junit.framework.Assert.assertTrue;
10+
import static junit.framework.Assert.fail;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class MultiRecordDeleteByIDResponseHandlerUnitTest {
15+
@Test
16+
public void testMultiRecordDeleteByIDResponseNormalFlow() throws Exception {
17+
JSONArray results = new JSONArray();
18+
19+
JSONObject jsonObject1 = new JSONObject();
20+
jsonObject1.put("_recordType", "Note");
21+
jsonObject1.put("_recordID", "48092492-0791-4120-B314-022202AD3970");
22+
results.put(jsonObject1);
23+
24+
JSONObject jsonObject2 = new JSONObject();
25+
jsonObject2.put("_recordType", "Note");
26+
jsonObject2.put("_recordID", "48092492-0791-4120-B314-022202AD3971");
27+
results.put(jsonObject2);
28+
29+
JSONObject resultObject = new JSONObject();
30+
resultObject.put("result", results);
31+
32+
final boolean[] checkpoints = new boolean[] { false };
33+
MultiRecordDeleteByIDResponseHandler handler = new MultiRecordDeleteByIDResponseHandler() {
34+
@Override
35+
public void onDeleteSuccess(String[] result) {
36+
assertEquals("48092492-0791-4120-B314-022202AD3970", result[0]);
37+
assertEquals("48092492-0791-4120-B314-022202AD3971", result[1]);
38+
39+
checkpoints[0] = true;
40+
}
41+
42+
@Override
43+
public void onDeleteFail(Error error) {
44+
fail("Should not get error callback");
45+
}
46+
};
47+
48+
handler.onSuccess(resultObject);
49+
assertTrue(checkpoints[0]);
50+
}
51+
52+
@Test
53+
public void testMultiRecordDeleteByIDResponseErrorFlow() throws JSONException {
54+
final boolean[] checkpoints = new boolean[] { false };
55+
MultiRecordDeleteByIDResponseHandler handler = new MultiRecordDeleteByIDResponseHandler() {
56+
@Override
57+
public void onDeleteSuccess(String[] result) {
58+
fail("Should not call success callback");
59+
}
60+
61+
@Override
62+
public void onDeleteFail(Error error) {
63+
assertEquals("Test error", error.getDetailMessage());
64+
checkpoints[0] = true;
65+
}
66+
};
67+
68+
handler.onFail(new Error("Test error"));
69+
assertTrue(checkpoints[0]);
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.skygear.skygear;
2+
3+
import android.support.test.runner.AndroidJUnit4;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static junit.framework.Assert.*;
12+
13+
@RunWith(AndroidJUnit4.class)
14+
public class MultiRecordDeleteResponseHandlerUnitTest {
15+
@Test
16+
public void testMultiRecordDeleteResponseNormalFlow() throws Exception {
17+
JSONArray results = new JSONArray();
18+
19+
JSONObject jsonObject1 = new JSONObject();
20+
jsonObject1.put("_recordType", "Note");
21+
jsonObject1.put("_recordID", "48092492-0791-4120-B314-022202AD3970");
22+
results.put(jsonObject1);
23+
24+
JSONObject jsonObject2 = new JSONObject();
25+
jsonObject2.put("_recordType", "Note");
26+
jsonObject2.put("_recordID", "48092492-0791-4120-B314-022202AD3971");
27+
results.put(jsonObject2);
28+
29+
30+
JSONObject resultObject = new JSONObject();
31+
resultObject.put("result", results);
32+
33+
final boolean[] checkpoints = new boolean[] { false };
34+
MultiRecordDeleteResponseHandler handler = new MultiRecordDeleteResponseHandler() {
35+
@Override
36+
public void onDeleteSuccess(Record[] result) {
37+
assertEquals("Note", result[0].getType());
38+
assertEquals("48092492-0791-4120-B314-022202AD3970", result[0].getId());
39+
assertTrue(result[0].deleted);
40+
41+
assertEquals("Note", result[1].getType());
42+
assertEquals("48092492-0791-4120-B314-022202AD3971", result[1].getId());
43+
assertTrue(result[1].deleted);
44+
45+
checkpoints[0] = true;
46+
}
47+
48+
@Override
49+
public void onDeleteFail(Error error) {
50+
fail("Should not get error callback");
51+
}
52+
};
53+
54+
handler.onSuccess(resultObject);
55+
assertTrue(checkpoints[0]);
56+
}
57+
58+
@Test
59+
public void testMultiRecordDeleteResponseErrorFlow() throws JSONException {
60+
final boolean[] checkpoints = new boolean[] { false };
61+
MultiRecordDeleteResponseHandler handler = new MultiRecordDeleteResponseHandler() {
62+
@Override
63+
public void onDeleteSuccess(Record[] result) {
64+
fail("Should not call success callback");
65+
}
66+
67+
@Override
68+
public void onDeleteFail(Error error) {
69+
assertEquals("Test error", error.getDetailMessage());
70+
checkpoints[0] = true;
71+
}
72+
};
73+
74+
handler.onFail(new Error("Test error"));
75+
assertTrue(checkpoints[0]);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.skygear.skygear;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
import static junit.framework.Assert.assertTrue;
9+
import static junit.framework.Assert.fail;
10+
11+
public class RecordDeleteByIDResponseHandlerUnitTest {
12+
@Test
13+
public void testRecordDeleteByIDResponseNormalFlow() throws Exception {
14+
JSONObject jsonObject = new JSONObject();
15+
jsonObject.put("_recordType", "Note");
16+
jsonObject.put("_recordID", "48092492-0791-4120-B314-022202AD3970");
17+
18+
JSONArray results = new JSONArray();
19+
results.put(jsonObject);
20+
21+
JSONObject resultObject = new JSONObject();
22+
resultObject.put("result", results);
23+
24+
final boolean[] checkpoints = new boolean[] { false };
25+
RecordDeleteByIDResponseHandler handler = new RecordDeleteByIDResponseHandler() {
26+
@Override
27+
public void onDeleteSuccess(String result) {
28+
assertEquals("48092492-0791-4120-B314-022202AD3970", result);
29+
checkpoints[0] = true;
30+
}
31+
32+
@Override
33+
public void onDeleteFail(Error error) {
34+
fail("Should not get error callback");
35+
}
36+
};
37+
38+
handler.onSuccess(resultObject);
39+
assertTrue(checkpoints[0]);
40+
}
41+
42+
@Test
43+
public void testRecordDeleteByIDResponseErrorFlow() {
44+
final boolean[] checkpoints = new boolean[] { false };
45+
RecordDeleteByIDResponseHandler handler = new RecordDeleteByIDResponseHandler() {
46+
@Override
47+
public void onDeleteSuccess(String result) {
48+
fail("Should not call success callback");
49+
}
50+
51+
@Override
52+
public void onDeleteFail(Error error) {
53+
assertEquals("Test error", error.getDetailMessage());
54+
checkpoints[0] = true;
55+
}
56+
};
57+
58+
handler.onFail(new Error("Test error"));
59+
assertTrue(checkpoints[0]);
60+
}
61+
}

skygear/src/androidTest/java/io/skygear/skygear/RecordDeleteRequestUnitTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.Map;
3232

3333
import static junit.framework.Assert.assertEquals;
34+
import static junit.framework.Assert.assertFalse;
35+
import static junit.framework.Assert.assertTrue;
3436

3537
@RunWith(AndroidJUnit4.class)
3638
public class RecordDeleteRequestUnitTest {
@@ -86,6 +88,8 @@ public void testRecordDeleteRequestNormalFlow() throws Exception {
8688
recordIdentifiers.getJSONObject(1).getString("_recordID")
8789
);
8890

91+
assertTrue((boolean) data.get("atomic"));
92+
8993
request.validate();
9094
}
9195

@@ -126,6 +130,25 @@ public void testRecordDeleteRequestAcceptRecordIDs() throws Exception {
126130
recordIdentifiers.getJSONObject(1).getString("_recordID")
127131
);
128132

133+
assertTrue((boolean) data.get("atomic"));
134+
135+
request.validate();
136+
}
137+
138+
@Test
139+
public void testRecordDeleteRequestNonAtomic() throws Exception {
140+
RecordDeleteRequest request = new RecordDeleteRequest(
141+
"Note",
142+
new String[]{
143+
"9C0F4536-FEA7-42DB-B8EF-561CCD175E06",
144+
"05A2946A-72DC-4F20-99F9-129BD1FCB52A"
145+
},
146+
instrumentationPublicDatabase
147+
);
148+
request.setAtomic(false);
149+
150+
assertFalse(request.data.containsKey("atomic"));
151+
129152
request.validate();
130153
}
131154

0 commit comments

Comments
 (0)