Search in sources :

Example 1 with Command

use of com.fitpay.android.utils.Command in project fitpay-android-sdk by fitpay.

the class UserEventStreamSyncTest method testWebviewCommunicatorUsesUserEventStream.

@Test
public void testWebviewCommunicatorUsesUserEventStream() throws Exception {
    // setup a user and device
    this.user = getUser();
    assertNotNull(user);
    Device device = createDevice(user, getTestDevice());
    Activity context = Mockito.mock(Activity.class);
    DeviceService deviceService = new DeviceService();
    deviceService.setPaymentDeviceConnector(new MockPaymentDeviceConnector());
    // pretend to launch the webview and act like the user has logged into the WV, this should
    // cause the user event stream subscription to occur
    WebViewCommunicatorImpl wvc = new WebViewCommunicatorImpl(context, -1);
    wvc.setDeviceService(deviceService);
    wvc.sendUserData(null, device.getDeviceIdentifier(), ApiManager.getInstance().getApiService().getToken().getAccessToken(), user.getId());
    boolean subscribed = false;
    for (int i = 0; i < 10; i++) {
        subscribed = UserEventStreamManager.isSubscribed(user.getId());
        if (!subscribed) {
            Thread.sleep(500);
        }
    }
    assertTrue(UserEventStreamManager.isSubscribed(user.getId()));
    // now let's get the platform to initiate a SYNC by adding a card, the automatic sync
    // from user event stream is enabled by default, therefore we should see a sync requeset
    // come out onto the RxBus
    final CountDownLatch syncLatch = new CountDownLatch(1);
    final List<SyncRequest> syncRequests = new ArrayList<>();
    NotificationManager.getInstance().addListener(new Listener() {

        @Override
        public Map<Class, Command> getCommands() {
            mCommands.put(SyncRequest.class, data -> handleSyncRequest((SyncRequest) data));
            return mCommands;
        }

        public void handleSyncRequest(SyncRequest request) {
            syncRequests.add(request);
            syncLatch.countDown();
        }
    });
    CreditCard createdCard = createCreditCard(user, getTestCreditCard("9999504454545450"));
    final CountDownLatch latch = new CountDownLatch(1);
    createdCard.acceptTerms(new ApiCallback<CreditCard>() {

        @Override
        public void onSuccess(CreditCard result) {
            latch.countDown();
        }

        @Override
        public void onFailure(int errorCode, String errorMessage) {
            latch.countDown();
        }
    });
    syncLatch.await(30000, TimeUnit.MILLISECONDS);
    assertTrue(syncRequests.size() > 0);
    SyncRequest syncRequest = syncRequests.get(0);
    assertNotNull(syncRequest.getSyncId());
    assertNotNull(syncRequest.getSyncInfo());
    assertEquals(user.getId(), syncRequest.getSyncInfo().getUserId());
    assertEquals(device.getDeviceIdentifier(), syncRequest.getSyncInfo().getDeviceId());
    assertEquals(SyncInitiator.PLATFORM, syncRequest.getSyncInfo().getInitiator());
    assertEquals(syncRequest.getSyncId(), syncRequest.getSyncInfo().getSyncId());
    assertEquals(ApiManager.getConfig().get("clientId"), syncRequest.getSyncInfo().getClientId());
    assertNotNull(syncRequest.getConnector());
    assertNotNull(syncRequest.getDevice());
    assertEquals(device.getDeviceIdentifier(), syncRequest.getDevice().getDeviceIdentifier());
    assertNotNull(syncRequest.getUser());
    assertEquals(user.getId(), syncRequest.getUser().getId());
    // now let's close the webview and ensure the subscription is removed
    wvc.close();
    assertFalse(UserEventStreamManager.isSubscribed(user.getId()));
}
Also used : Assert.assertEquals(junit.framework.Assert.assertEquals) UserEventStreamManager(com.fitpay.android.api.sse.UserEventStreamManager) WebViewCommunicatorImpl(com.fitpay.android.webview.impl.WebViewCommunicatorImpl) NotificationManager(com.fitpay.android.utils.NotificationManager) ArrayList(java.util.ArrayList) Assert.assertTrue(junit.framework.Assert.assertTrue) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent) ApiCallback(com.fitpay.android.api.callbacks.ApiCallback) CreditCard(com.fitpay.android.api.models.card.CreditCard) MockPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector) Assert.assertNotNull(junit.framework.Assert.assertNotNull) ApiManager(com.fitpay.android.api.ApiManager) DeviceService(com.fitpay.android.paymentdevice.DeviceService) Map(java.util.Map) UserEventStreamListener(com.fitpay.android.api.sse.UserEventStreamListener) Listener(com.fitpay.android.utils.Listener) SyncRequest(com.fitpay.android.paymentdevice.models.SyncRequest) Test(org.junit.Test) Device(com.fitpay.android.api.models.device.Device) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Command(com.fitpay.android.utils.Command) List(java.util.List) SyncInitiator(com.fitpay.android.api.enums.SyncInitiator) Activity(android.app.Activity) Assert.assertFalse(junit.framework.Assert.assertFalse) UserEventStreamListener(com.fitpay.android.api.sse.UserEventStreamListener) Listener(com.fitpay.android.utils.Listener) Device(com.fitpay.android.api.models.device.Device) DeviceService(com.fitpay.android.paymentdevice.DeviceService) MockPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector) ArrayList(java.util.ArrayList) Activity(android.app.Activity) CountDownLatch(java.util.concurrent.CountDownLatch) CreditCard(com.fitpay.android.api.models.card.CreditCard) WebViewCommunicatorImpl(com.fitpay.android.webview.impl.WebViewCommunicatorImpl) SyncRequest(com.fitpay.android.paymentdevice.models.SyncRequest) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Command

use of com.fitpay.android.utils.Command in project fitpay-android-sdk by fitpay.

the class WebViewCommunicatorTest method testSendUserData.

@Test
// can only be run manually since needs valid user token
@Ignore
public void testSendUserData() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    @EventCallback.Status String[] status = new String[1];
    final Listener callbackListener = new Listener() {

        @Override
        public Map<Class, Command> getCommands() {
            mCommands.put(EventCallback.class, data -> {
                status[0] = ((EventCallback) data).getStatus();
                latch.countDown();
            });
            return super.getCommands();
        }
    };
    NotificationManager.getInstance().addListener(callbackListener, Schedulers.immediate());
    String deviceId = "72f8f402-9afd-4856-9675-c6f2f54a6753";
    String token = "eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjNDQ1MTY5OS00YWM1LTQ2ZWQtOTVhNy1iZTIyYTM5NTQ2Y2QiLCJzdWIiOiI1NzUwODY1ZC1iMGQ4LTQwYTYtOWQ4NS1mNmM4NjNmN2E2YzYiLCJzY29wZSI6WyJ1c2VyLnJlYWQiLCJ1c2VyLndyaXRlIiwidHJhbnNhY3Rpb25zLnJlYWQiLCJkZXZpY2VzLndyaXRlIiwiZGV2aWNlcy5yZWFkIiwiY3JlZGl0Q2FyZHMud3JpdGUiLCJjcmVkaXRDYXJkcy5yZWFkIl0sImNsaWVudF9pZCI6InBhZ2FyZSIsImNpZCI6InBhZ2FyZSIsImF6cCI6InBhZ2FyZSIsInVzZXJfaWQiOiI1NzUwODY1ZC1iMGQ4LTQwYTYtOWQ4NS1mNmM4NjNmN2E2YzYiLCJvcmlnaW4iOiJ1YWEiLCJ1c2VyX25hbWUiOiJwQHAuY29tIXBhZ2FyZSIsImVtYWlsIjoicEBwLmNvbSIsImF1dGhfdGltZSI6MTQ3OTk5NTI1MiwicmV2X3NpZyI6ImUwYjM5MDZiIiwiaWF0IjoxNDc5OTk1MjUyLCJleHAiOjE0Nzk5OTcwNTIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6ODA4MC91YWEvb2F1dGgvdG9rZW4iLCJ6aWQiOiJ1YWEiLCJhdWQiOlsicGFnYXJlIiwidXNlciIsInRyYW5zYWN0aW9ucyIsImRldmljZXMiLCJjcmVkaXRDYXJkcyJdfQ.kRwsY3AKZ-fF4sWZQylThbbwPmHiqmS9D2Xd24Ux3KY-N9S7ZfUOunPa1wMa2jcxzZfBJ5FSdFm61Gqci99x85-PvwD7rUOJBr6AoBSRukzqf6S1m2owVAcWVJLsH8meGbFX_L-NG7yMwJAYiJQ2AGqBUkwvfm3fizpM-jBoKfqrkAjFeUceBiqGdFKljpcqyxHIhjQGOLxlJKOdYNQcr_aMQHg4yh8d_k5LggrKz6XeS7X8-Zj9sylWK4qxgt016o6qyUxgPKZb0GMYsu-XBi9fCAg2yjzttHCQTvk3dhAtw0Yb-HHW4DP6X9EdRpyaXptbWYHOThxnfYE7g0k7rQ";
    String userId = "5750865d-b0d8-40a6-9d85-f6c863f7a6c6";
    Activity context = Mockito.mock(Activity.class);
    WebViewCommunicator wvc = new WebViewCommunicatorImpl(context, -1);
    wvc.sendUserData(null, deviceId, token, userId);
    latch.await(60, TimeUnit.SECONDS);
    assertNotNull("status value", status[0]);
    assertEquals("status value", "OK", status[0]);
    NotificationManager.getInstance().removeListener(callbackListener);
}
Also used : Listener(com.fitpay.android.utils.Listener) Command(com.fitpay.android.utils.Command) WebViewCommunicator(com.fitpay.android.webview.WebViewCommunicator) Activity(android.app.Activity) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Activity (android.app.Activity)2 Command (com.fitpay.android.utils.Command)2 Listener (com.fitpay.android.utils.Listener)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 ApiManager (com.fitpay.android.api.ApiManager)1 ApiCallback (com.fitpay.android.api.callbacks.ApiCallback)1 SyncInitiator (com.fitpay.android.api.enums.SyncInitiator)1 UserStreamEvent (com.fitpay.android.api.models.UserStreamEvent)1 CreditCard (com.fitpay.android.api.models.card.CreditCard)1 Device (com.fitpay.android.api.models.device.Device)1 UserEventStreamListener (com.fitpay.android.api.sse.UserEventStreamListener)1 UserEventStreamManager (com.fitpay.android.api.sse.UserEventStreamManager)1 DeviceService (com.fitpay.android.paymentdevice.DeviceService)1 MockPaymentDeviceConnector (com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector)1 SyncRequest (com.fitpay.android.paymentdevice.models.SyncRequest)1 NotificationManager (com.fitpay.android.utils.NotificationManager)1 WebViewCommunicator (com.fitpay.android.webview.WebViewCommunicator)1 WebViewCommunicatorImpl (com.fitpay.android.webview.impl.WebViewCommunicatorImpl)1 ArrayList (java.util.ArrayList)1