ParseCloud { public static <T> Task<T> callFunctionInBackground(@NonNull final String name, @NonNull final Map<String, ?> params) { return ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation<String, Task<T>>() { @Override public Task<T> then(Task<String> task) { String sessionToken = task.getResult(); return getCloudCodeController().callFunctionInBackground(name, params, sessionToken); } }); } private  ParseCloud(); static Task<T> callFunctionInBackground(@NonNull final String name,
                                                       @NonNull final Map<String, ?> params); static T callFunction(@NonNull String name, @NonNull Map<String, ?> params); static void callFunctionInBackground(@NonNull String name, @NonNull Map<String, ?> params,
                                                    @NonNull FunctionCallback<T> callback);  }
@Test public void testCallFunctionAsync() throws Exception { ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result"); ParseCorePlugins.getInstance().registerCloudCodeController(controller); Map<String, Object> parameters = new HashMap<>(); parameters.put("key1", Arrays.asList(1, 2, 3)); parameters.put("key2", "value1"); Task cloudCodeTask = ParseCloud.callFunctionInBackground("name", parameters); ParseTaskUtils.wait(cloudCodeTask); verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters), isNull(String.class)); assertTrue(cloudCodeTask.isCompleted()); assertNull(cloudCodeTask.getError()); assertThat(cloudCodeTask.getResult(), instanceOf(String.class)); assertEquals("result", cloudCodeTask.getResult()); }
@Test public void testCallFunctionNullCallback() { ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result"); ParseCorePlugins.getInstance().registerCloudCodeController(controller); Map<String, Object> parameters = new HashMap<>(); parameters.put("key1", Arrays.asList(1, 2, 3)); parameters.put("key2", "value1"); ParseCloud.callFunctionInBackground("name", parameters, null); verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters), isNull(String.class)); }
@Test public void testCallFunctionNormalCallback() throws Exception { ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result"); ParseCorePlugins.getInstance().registerCloudCodeController(controller); Map<String, Object> parameters = new HashMap<>(); parameters.put("key1", Arrays.asList(1, 2, 3)); parameters.put("key2", "value1"); final Semaphore done = new Semaphore(0); ParseCloud.callFunctionInBackground("name", parameters, new FunctionCallback<Object>() { @Override public void done(Object result, ParseException e) { assertNull(e); assertThat(result, instanceOf(String.class)); assertEquals("result", result); done.release(); } }); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters), isNull(String.class)); }