use of com.fitpay.android.paymentdevice.DeviceSyncManager 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);
}
Aggregations