use of com.parse.boltsinternal.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSendInBackgroundWithCallbackSuccess.
@Test
public void testSendInBackgroundWithCallbackSuccess() throws Exception {
// Mock controller
ParsePushController controller = mock(ParsePushController.class);
when(controller.sendInBackground(any(ParsePush.State.class), nullable(String.class))).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushController(controller);
// Make sample ParsePush data and call method
ParsePush push = new ParsePush();
JSONObject data = new JSONObject();
data.put("key", "value");
List<String> channels = new ArrayList<>();
channels.add("test");
channels.add("testAgain");
push.builder.expirationTime((long) 1000).data(data).channelSet(channels);
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.sendInBackground(e -> {
exceptionCapture.set(e);
done.release();
});
shadowMainLooper().idle();
// Make sure controller is executed and state parameter is correct
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
verify(controller, times(1)).sendInBackground(stateCaptor.capture(), nullable(String.class));
ParsePush.State state = stateCaptor.getValue();
assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
assertEquals(2, state.channelSet().size());
assertTrue(state.channelSet().contains("test"));
assertTrue(state.channelSet().contains("testAgain"));
}
use of com.parse.boltsinternal.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParseUserTest method testSaveEventuallyWhenServerError.
// region testSaveEventuallyWhenServerError
@Test
public void testSaveEventuallyWhenServerError() throws Exception {
Shadows.shadowOf(RuntimeEnvironment.application).grantPermissions(Manifest.permission.ACCESS_NETWORK_STATE);
Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application).applicationId(BuildConfig.LIBRARY_PACKAGE_NAME).server("https://api.parse.com/1").enableLocalDataStore().build();
ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration);
JSONObject mockResponse = new JSONObject();
mockResponse.put("objectId", "objectId");
mockResponse.put("email", "email@parse.com");
mockResponse.put("username", "username");
mockResponse.put("sessionToken", "r:sessionToken");
mockResponse.put("createdAt", ParseDateFormat.getInstance().format(new Date(1000)));
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(2000)));
ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK");
when(plugins.restClient()).thenReturn(restClient);
Parse.initialize(configuration, plugins);
ParseUser user = ParseUser.logIn("username", "password");
assertFalse(user.isDirty());
user.put("field", "data");
assertTrue(user.isDirty());
mockResponse = new JSONObject();
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(3000)));
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 200, "OK");
final CountDownLatch saveCountDown1 = new CountDownLatch(1);
final Capture<Exception> exceptionCapture = new Capture<>();
user.saveInBackground().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveCountDown1.countDown();
return null;
});
assertTrue(saveCountDown1.await(5, TimeUnit.SECONDS));
assertNull(exceptionCapture.get());
assertFalse(user.isDirty());
user.put("field", "other data");
assertTrue(user.isDirty());
mockResponse = new JSONObject();
mockResponse.put("error", "Save is not allowed");
mockResponse.put("code", 141);
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 400, "Bad Request");
final CountDownLatch saveEventuallyCountDown = new CountDownLatch(1);
user.saveEventually().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveEventuallyCountDown.countDown();
return null;
});
assertTrue(saveEventuallyCountDown.await(5, TimeUnit.SECONDS));
assertTrue(exceptionCapture.get() instanceof ParseException);
assertEquals(ParseException.SCRIPT_ERROR, ((ParseException) exceptionCapture.get()).getCode());
assertEquals("Save is not allowed", exceptionCapture.get().getMessage());
assertTrue(user.isDirty());
// Simulate reboot
Parse.destroy();
Parse.initialize(configuration, plugins);
user = ParseUser.getCurrentUser();
assertTrue(user.isDirty());
assertEquals("other data", user.get("field"));
user.put("field", "another data");
mockResponse = new JSONObject();
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(4000)));
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 200, "OK");
final CountDownLatch saveCountDown2 = new CountDownLatch(1);
user.saveInBackground().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveCountDown2.countDown();
return null;
});
assertTrue(saveCountDown2.await(5, TimeUnit.SECONDS));
assertNull(exceptionCapture.get());
assertFalse(user.isDirty());
}
use of com.parse.boltsinternal.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSendMessageInBackgroundWithCallback.
@Test
public void testSendMessageInBackgroundWithCallback() throws Exception {
// Mock controller
ParsePushController controller = mock(ParsePushController.class);
when(controller.sendInBackground(any(ParsePush.State.class), nullable(String.class))).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushController(controller);
// Make sample ParsePush data and call method
ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
query.getBuilder().whereEqualTo("foo", "bar");
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
ParsePush.sendMessageInBackground("test", query, e -> {
exceptionCapture.set(e);
done.release();
});
shadowMainLooper().idle();
// Make sure controller is executed and state parameter is correct
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
verify(controller, times(1)).sendInBackground(stateCaptor.capture(), nullable(String.class));
ParsePush.State state = stateCaptor.getValue();
// Verify query state
ParseQuery.State<ParseInstallation> queryState = state.queryState();
JSONObject queryStateJson = queryState.toJSON(PointerEncoder.get());
assertEquals("bar", queryStateJson.getJSONObject("where").getString("foo"));
// Verify message
assertEquals("test", state.data().getString(ParsePush.KEY_DATA_MESSAGE));
}
use of com.parse.boltsinternal.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSendInBackgroundWithCallbackFail.
@Test
public void testSendInBackgroundWithCallbackFail() throws Exception {
// Mock controller
ParsePushController controller = mock(ParsePushController.class);
final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
when(controller.sendInBackground(any(ParsePush.State.class), nullable(String.class))).thenReturn(Task.<Void>forError(exception));
ParseCorePlugins.getInstance().registerPushController(controller);
// Make sample ParsePush data and call method
ParsePush push = new ParsePush();
JSONObject data = new JSONObject();
data.put("key", "value");
List<String> channels = new ArrayList<>();
channels.add("test");
channels.add("testAgain");
push.builder.expirationTime((long) 1000).data(data).channelSet(channels);
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.sendInBackground(e -> {
exceptionCapture.set(e);
done.release();
});
shadowMainLooper().idle();
// Make sure controller is executed and state parameter is correct
assertSame(exception, exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
verify(controller, times(1)).sendInBackground(stateCaptor.capture(), nullable(String.class));
ParsePush.State state = stateCaptor.getValue();
assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
assertEquals(2, state.channelSet().size());
assertTrue(state.channelSet().contains("test"));
assertTrue(state.channelSet().contains("testAgain"));
}
use of com.parse.boltsinternal.Capture in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testUnsubscribeInBackgroundWithCallbackFail.
@Test
public void testUnsubscribeInBackgroundWithCallbackFail() throws Exception {
ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
ParsePush.unsubscribeInBackground("test", e -> {
exceptionCapture.set(e);
done.release();
});
shadowMainLooper().idle();
assertSame(exception, exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).unsubscribeInBackground("test");
}
Aggregations