Search in sources :

Example 1 with MockPaymentDeviceConnector

use of com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector in project fitpay-android-sdk by fitpay.

the class DeviceService method configure.

protected void configure(Intent intent) {
    if (null == intent) {
        FPLog.e(TAG, "DeviceService can not be configured with a null Intent.  Current connector: " + paymentDeviceConnector);
        return;
    }
    if (null != intent.getExtras() && intent.hasExtra(EXTRA_PAYMENT_SERVICE_TYPE)) {
        paymentDeviceConnectorType = intent.getExtras().getString(EXTRA_PAYMENT_SERVICE_TYPE);
        if (null != paymentDeviceConnectorType) {
            switch(paymentDeviceConnectorType) {
                case PAYMENT_SERVICE_TYPE_MOCK:
                    {
                        paymentDeviceConnector = new MockPaymentDeviceConnector();
                        break;
                    }
                case PAYMENT_SERVICE_TYPE_FITPAY_BLE:
                    {
                        String bluetoothAddress = intent.getExtras().getString(BluetoothPaymentDeviceConnector.EXTRA_BLUETOOTH_ADDRESS);
                        paymentDeviceConnector = new BluetoothPaymentDeviceConnector(this, bluetoothAddress);
                        break;
                    }
                default:
                    {
                        FPLog.w(TAG, "payment service type is not one of the known types.  type: " + paymentDeviceConnectorType);
                    }
            }
            if (null == paymentDeviceConnector) {
                try {
                    Class paymentDeviceConnectorClass = forName(paymentDeviceConnectorType);
                    paymentDeviceConnector = (IPaymentDeviceConnector) paymentDeviceConnectorClass.newInstance();
                    paymentDeviceConnector.setContext(this);
                } catch (Exception e) {
                    FPLog.e(TAG, e);
                }
            }
        }
    }
    if (null != paymentDeviceConnector && intent.hasExtra(EXTRA_PAYMENT_SERVICE_CONFIG)) {
        configParams = intent.getExtras().getString(EXTRA_PAYMENT_SERVICE_CONFIG);
        Properties props = null;
        try {
            props = convertCommaSeparatedList(configParams);
        } catch (IOException e) {
            FPLog.e(TAG, "unable to load properties. Reason: " + e.getMessage());
        }
        if (null != props) {
            paymentDeviceConnector.init(props);
        }
    }
    if (null != paymentDeviceConnector) {
        paymentDeviceConnector.reset();
    }
}
Also used : BluetoothPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.ble.BluetoothPaymentDeviceConnector) MockPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException)

Example 2 with MockPaymentDeviceConnector

use of com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector 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 3 with MockPaymentDeviceConnector

use of com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector in project fitpay-android-sdk by fitpay.

the class MockPaymentDeviceTest method setUp.

@Before
public void setUp() throws Exception {
    paymentDeviceService = new MockPaymentDeviceConnector();
    manager = NotificationManager.getInstance();
}
Also used : MockPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector) Before(org.junit.Before)

Example 4 with MockPaymentDeviceConnector

use of com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector in project fitpay-android-sdk by fitpay.

the class DeviceSyncManagerTest method testActionsSetup.

@Before
@Override
public void testActionsSetup() throws Exception {
    SharedPreferences sp = Mockito.mock(SharedPreferences.class);
    Mockito.when(sp.getAll()).thenReturn(Collections.emptyMap());
    Mockito.when(sp.getString(Matchers.eq("lastCommitId"), (String) Matchers.isNull())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return lastCommitId;
        }
    });
    SharedPreferences.Editor spEditor = Mockito.mock(SharedPreferences.Editor.class);
    Mockito.when(sp.edit()).thenReturn(spEditor);
    Mockito.when(spEditor.putString(Matchers.eq("lastCommitId"), Matchers.anyString())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            lastCommitId = (String) invocation.getArguments()[1];
            return spEditor;
        }
    });
    Mockito.when(spEditor.commit()).thenReturn(true);
    mContext = Mockito.mock(Context.class);
    Mockito.when(mContext.getSharedPreferences(Matchers.anyString(), Matchers.eq(Context.MODE_PRIVATE))).thenReturn(sp);
    syncManager = new DeviceSyncManager(mContext);
    syncManager.onCreate();
    syncManagerCallback = new DeviceSyncManager.DeviceSyncManagerCallback() {

        @Override
        public void syncRequestAdded(SyncRequest request) {
        }

        @Override
        public void syncTaskStarting(SyncRequest request) {
        }

        @Override
        public void syncTaskStarted(SyncRequest request) {
        }

        @Override
        public void syncTaskCompleted(SyncRequest request) {
            if (executionLatch != null) {
                executionLatch.countDown();
            }
        }
    };
    syncManager.registerDeviceSyncManagerCallback(syncManagerCallback);
    mockPaymentDevice = new MockPaymentDeviceConnector();
    userName = TestUtils.getRandomLengthString(5, 10) + "@" + TestUtils.getRandomLengthString(5, 10) + "." + TestUtils.getRandomLengthString(4, 10);
    pin = TestUtils.getRandomLengthNumber(4, 4);
    UserCreateRequest userCreateRequest = getNewTestUser(userName, pin);
    createUser(userCreateRequest);
    assertTrue(doLogin(new LoginIdentity.Builder().setPassword(pin).setUsername(userName).build()));
    this.user = getUser();
    this.device = createDevice(this.user, getTestDevice());
    assertNotNull(this.device);
    String pan = "9999504454545450";
    CreditCard creditCard = getTestCreditCard(pan);
    CreditCard createdCard = createCreditCard(user, creditCard);
    assertNotNull("card not created", createdCard);
    Properties props = new Properties();
    props.put(MockPaymentDeviceConnector.CONFIG_CONNECTED_RESPONSE_TIME, "0");
    mockPaymentDevice.init(props);
    assertEquals("payment service is not initialized", States.INITIALIZED, mockPaymentDevice.getState());
    mockPaymentDevice.connect();
    int count = 0;
    while (mockPaymentDevice.getState() != States.CONNECTED || ++count < 5) {
        Thread.sleep(500);
    }
    assertEquals("payment service should be connected", States.CONNECTED, mockPaymentDevice.getState());
    this.executionLatch = new CountDownLatch(1);
    this.listener = new SyncCompleteListener();
    NotificationManager.getInstance().addListenerToCurrentThread(this.listener);
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) MockPaymentDeviceConnector(com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) CreditCard(com.fitpay.android.api.models.card.CreditCard) SyncRequest(com.fitpay.android.paymentdevice.models.SyncRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LoginIdentity(com.fitpay.android.api.models.user.LoginIdentity) DeviceSyncManager(com.fitpay.android.paymentdevice.DeviceSyncManager) UserCreateRequest(com.fitpay.android.api.models.user.UserCreateRequest) Before(org.junit.Before)

Aggregations

MockPaymentDeviceConnector (com.fitpay.android.paymentdevice.impl.mock.MockPaymentDeviceConnector)4 CreditCard (com.fitpay.android.api.models.card.CreditCard)2 SyncRequest (com.fitpay.android.paymentdevice.models.SyncRequest)2 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Before (org.junit.Before)2 Activity (android.app.Activity)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 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 Device (com.fitpay.android.api.models.device.Device)1 LoginIdentity (com.fitpay.android.api.models.user.LoginIdentity)1 UserCreateRequest (com.fitpay.android.api.models.user.UserCreateRequest)1 UserEventStreamListener (com.fitpay.android.api.sse.UserEventStreamListener)1 UserEventStreamManager (com.fitpay.android.api.sse.UserEventStreamManager)1 DeviceService (com.fitpay.android.paymentdevice.DeviceService)1 DeviceSyncManager (com.fitpay.android.paymentdevice.DeviceSyncManager)1