Skip to content

Commit 19e175a

Browse files
committed
Merge pull request #32 from ParsePlatform/grantland.cloud
Fix: Unable to receive null response from Cloud Code
2 parents 5c59ea1 + a0b351c commit 19e175a

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

Parse/src/main/java/com/parse/ParseCloud.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import java.util.List;
1212
import java.util.Map;
1313

14-
import org.json.JSONException;
15-
import org.json.JSONObject;
16-
1714
import bolts.Continuation;
1815
import bolts.Task;
1916

Parse/src/main/java/com/parse/ParseCloudCodeController.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99
package com.parse;
1010

11-
import org.json.JSONException;
1211
import org.json.JSONObject;
1312

1413
import java.util.Map;
@@ -46,11 +45,7 @@ public T then(Task<JSONObject> task) throws Exception {
4645
/* package for test */ Object convertCloudResponse(Object result) {
4746
if (result instanceof JSONObject) {
4847
JSONObject jsonResult = (JSONObject)result;
49-
try {
50-
result = jsonResult.get("result");
51-
} catch (JSONException e) {
52-
return result;
53-
}
48+
result = jsonResult.opt("result");
5449
}
5550

5651
ParseDecoder decoder = ParseDecoder.get();

Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.json.JSONArray;
1212
import org.json.JSONObject;
1313
import org.junit.Test;
14-
import org.skyscreamer.jsonassert.JSONCompareMode;
1514

1615
import java.io.ByteArrayInputStream;
1716
import java.io.IOException;
@@ -31,7 +30,6 @@
3130
import static org.mockito.Mockito.times;
3231
import static org.mockito.Mockito.verify;
3332
import static org.mockito.Mockito.when;
34-
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
3533

3634
public class ParseCloudCodeControllerTest {
3735

@@ -59,21 +57,6 @@ public void testConvertCloudResponseNullResponse() throws Exception {
5957
assertNull(result);
6058
}
6159

62-
@Test
63-
public void testConvertCloudResponseJsonResponseWithoutResultField() throws Exception {
64-
ParseHttpClient restClient = mock(ParseHttpClient.class);
65-
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
66-
JSONObject response = new JSONObject();
67-
response.put("foo", "bar");
68-
response.put("yarr", 1);
69-
70-
Object result = controller.convertCloudResponse(response);
71-
72-
assertThat(result, instanceOf(JSONObject.class));
73-
JSONObject jsonResult = (JSONObject)result;
74-
assertEquals(response, jsonResult, JSONCompareMode.NON_EXTENSIBLE);
75-
}
76-
7760
@Test
7861
public void testConvertCloudResponseJsonResponseWithResultField() throws Exception {
7962
ParseHttpClient restClient = mock(ParseHttpClient.class);
@@ -116,7 +99,7 @@ public void testCallFunctionInBackgroundCommand() throws Exception {
11699
}
117100

118101
@Test
119-
public void testCallFunctionInBackgroundSuccess() throws Exception {
102+
public void testCallFunctionInBackgroundSuccessWithResult() throws Exception {
120103
JSONObject json = new JSONObject();
121104
json.put("result", "test");
122105
String content = json.toString();
@@ -137,6 +120,27 @@ public void testCallFunctionInBackgroundSuccess() throws Exception {
137120
assertEquals("test", cloudCodeTask.getResult());
138121
}
139122

123+
@Test
124+
public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception {
125+
JSONObject json = new JSONObject();
126+
String content = json.toString();
127+
128+
ParseHttpResponse mockResponse = mock(ParseHttpResponse.class);
129+
when(mockResponse.getStatusCode()).thenReturn(200);
130+
when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes()));
131+
when(mockResponse.getTotalSize()).thenReturn(content.length());
132+
133+
ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
134+
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
135+
136+
Task<String> cloudCodeTask = controller.callFunctionInBackground(
137+
"test", new HashMap<String, Object>(), "sessionToken");
138+
ParseTaskUtils.wait(cloudCodeTask);
139+
140+
verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
141+
assertNull(cloudCodeTask.getResult());
142+
}
143+
140144
@Test
141145
public void testCallFunctionInBackgroundFailure() throws Exception {
142146
// TODO(mengyan): Remove once we no longer rely on retry logic.

0 commit comments

Comments
 (0)