Search in sources :

Example 6 with Capture

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"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Capture(com.parse.boltsinternal.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 7 with Capture

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Shadows(org.robolectric.Shadows) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Date(java.util.Date) Assert.assertNotSame(org.junit.Assert.assertNotSame) RunWith(org.junit.runner.RunWith) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) Parcel(android.os.Parcel) ShadowLooper.shadowMainLooper(org.robolectric.shadows.ShadowLooper.shadowMainLooper) Assert.assertSame(org.junit.Assert.assertSame) Manifest(android.Manifest) Capture(com.parse.boltsinternal.Capture) Continuation(com.parse.boltsinternal.Continuation) JSONObject(org.json.JSONObject) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) After(org.junit.After) Mockito.doReturn(org.mockito.Mockito.doReturn) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) PAUSED(org.robolectric.annotation.LooperMode.Mode.PAUSED) Semaphore(java.util.concurrent.Semaphore) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode) Mockito.when(org.mockito.Mockito.when) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) Task(com.parse.boltsinternal.Task) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) ExpectedException(org.junit.rules.ExpectedException) Capture(com.parse.boltsinternal.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 8 with Capture

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));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Capture(com.parse.boltsinternal.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 9 with Capture

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"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Capture(com.parse.boltsinternal.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 10 with Capture

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");
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(com.parse.boltsinternal.Capture) Test(org.junit.Test)

Aggregations

Capture (com.parse.boltsinternal.Capture)14 Semaphore (java.util.concurrent.Semaphore)9 Test (org.junit.Test)9 JSONObject (org.json.JSONObject)8 ArrayList (java.util.ArrayList)6 Task (com.parse.boltsinternal.Task)5 Continuation (com.parse.boltsinternal.Continuation)4 TaskCompletionSource (com.parse.boltsinternal.TaskCompletionSource)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ContentValues (android.content.ContentValues)3 Context (android.content.Context)3 Cursor (android.database.Cursor)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 TextUtils (android.text.TextUtils)3 Pair (android.util.Pair)3 ConstraintMatcher (com.parse.OfflineQueryLogic.ConstraintMatcher)3 Arrays (java.util.Arrays)3 LinkedList (java.util.LinkedList)3