Skip to content

Fix: Unable to receive null response from Cloud Code #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Parse/src/main/java/com/parse/ParseCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import bolts.Continuation;
import bolts.Task;

Expand Down
7 changes: 1 addition & 6 deletions Parse/src/main/java/com/parse/ParseCloudCodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package com.parse;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;
Expand Down Expand Up @@ -46,11 +45,7 @@ public T then(Task<JSONObject> task) throws Exception {
/* package for test */ Object convertCloudResponse(Object result) {
if (result instanceof JSONObject) {
JSONObject jsonResult = (JSONObject)result;
try {
result = jsonResult.get("result");
} catch (JSONException e) {
return result;
}
result = jsonResult.opt("result");
}

ParseDecoder decoder = ParseDecoder.get();
Expand Down
40 changes: 22 additions & 18 deletions Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONCompareMode;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -31,7 +30,6 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;

public class ParseCloudCodeControllerTest {

Expand Down Expand Up @@ -59,21 +57,6 @@ public void testConvertCloudResponseNullResponse() throws Exception {
assertNull(result);
}

@Test
public void testConvertCloudResponseJsonResponseWithoutResultField() throws Exception {
ParseHttpClient restClient = mock(ParseHttpClient.class);
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
JSONObject response = new JSONObject();
response.put("foo", "bar");
response.put("yarr", 1);

Object result = controller.convertCloudResponse(response);

assertThat(result, instanceOf(JSONObject.class));
JSONObject jsonResult = (JSONObject)result;
assertEquals(response, jsonResult, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
public void testConvertCloudResponseJsonResponseWithResultField() throws Exception {
ParseHttpClient restClient = mock(ParseHttpClient.class);
Expand Down Expand Up @@ -116,7 +99,7 @@ public void testCallFunctionInBackgroundCommand() throws Exception {
}

@Test
public void testCallFunctionInBackgroundSuccess() throws Exception {
public void testCallFunctionInBackgroundSuccessWithResult() throws Exception {
JSONObject json = new JSONObject();
json.put("result", "test");
String content = json.toString();
Expand All @@ -137,6 +120,27 @@ public void testCallFunctionInBackgroundSuccess() throws Exception {
assertEquals("test", cloudCodeTask.getResult());
}

@Test
public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception {
JSONObject json = new JSONObject();
String content = json.toString();

ParseHttpResponse mockResponse = mock(ParseHttpResponse.class);
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes()));
when(mockResponse.getTotalSize()).thenReturn(content.length());

ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);

Task<String> cloudCodeTask = controller.callFunctionInBackground(
"test", new HashMap<String, Object>(), "sessionToken");
ParseTaskUtils.wait(cloudCodeTask);

verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
assertNull(cloudCodeTask.getResult());
}

@Test
public void testCallFunctionInBackgroundFailure() throws Exception {
// TODO(mengyan): Remove once we no longer rely on retry logic.
Expand Down