Search in sources :

Example 1 with UserDataReport

use of org.infobip.mobile.messaging.api.data.UserDataReport in project mobile-messaging-sdk-android by infobip.

the class UserDataSyncTest method test_add_birthdate.

@Test
public void test_add_birthdate() throws Exception {
    UserData userData = new UserData();
    Calendar calendar = Calendar.getInstance();
    calendar.set(2000, 1, 1);
    userData.setBirthdate(new Date(calendar.getTimeInMillis()));
    mobileMessaging.syncUserData(userData);
    verify(mobileApiData, after(1000).times(1)).reportUserData(anyString(), reportCaptor.capture());
    UserDataReport report = reportCaptor.getValue();
    assertEquals(userData.getBirthdate(), DateTimeUtil.DateFromYMDString((String) report.getPredefinedUserData().get("birthdate")));
}
Also used : Calendar(java.util.Calendar) UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) Test(org.junit.Test)

Example 2 with UserDataReport

use of org.infobip.mobile.messaging.api.data.UserDataReport in project mobile-messaging-sdk-android by infobip.

the class UserDataReporter method sync.

public void sync(final MobileMessaging.ResultListener listener, final UserData userData) {
    if (userData == null) {
        return;
    }
    mobileMessagingCore.saveUnreportedUserData(userData);
    new MRetryableTask<UserData, UserData>() {

        @Override
        public UserData run(UserData[] userData) {
            if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
                MobileMessagingLogger.w("Can't report system data without valid registration");
                throw InternalSdkError.NO_VALID_REGISTRATION.getException();
            }
            UserDataReport request = UserDataMapper.toUserDataReport(userData[0].getPredefinedUserData(), userData[0].getCustomUserData());
            MobileMessagingLogger.v("USER DATA >>>", request);
            UserDataReport response = mobileApiData.reportUserData(userData[0].getExternalUserId(), request);
            MobileMessagingLogger.v("USER DATA <<<", response);
            return UserDataMapper.fromUserDataReport(userData[0].getExternalUserId(), response.getPredefinedUserData(), response.getCustomUserData());
        }

        @Override
        public void after(UserData userData) {
            mobileMessagingCore.setUserDataReported(userData);
            broadcaster.userDataReported(userData);
            if (listener != null) {
                listener.onResult(userData);
            }
        }

        @Override
        public void error(Throwable error) {
            MobileMessagingLogger.e("MobileMessaging API returned error (user data)! ", error);
            mobileMessagingCore.setLastHttpException(error);
            stats.reportError(MobileMessagingStatsError.USER_DATA_SYNC_ERROR);
            if (error instanceof BackendBaseExceptionWithContent) {
                BackendBaseExceptionWithContent errorWithContent = (BackendBaseExceptionWithContent) error;
                mobileMessagingCore.setUserDataReported(errorWithContent.getContent(UserData.class));
                if (listener != null) {
                    listener.onError(MobileMessagingError.createFrom(error));
                }
            } else if (error instanceof BackendInvalidParameterException) {
                mobileMessagingCore.setUserDataReportedWithError();
                if (listener != null) {
                    listener.onError(MobileMessagingError.createFrom(error));
                }
            } else {
                MobileMessagingLogger.v("User data synchronization will be postponed to a later time due to communication error");
                if (listener != null) {
                    listener.onResult(UserData.merge(mobileMessagingCore.getUserData(), userData));
                }
            }
            broadcaster.error(MobileMessagingError.createFrom(error));
        }
    }.retryWith(retryPolicy(listener)).execute(executor, userData);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) BackendInvalidParameterException(org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException) UserData(org.infobip.mobile.messaging.UserData) UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport) BackendBaseExceptionWithContent(org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)

Example 3 with UserDataReport

use of org.infobip.mobile.messaging.api.data.UserDataReport in project mobile-messaging-sdk-android by infobip.

the class UserDataSyncTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    reportCaptor = forClass(UserDataReport.class);
    dataCaptor = forClass(UserData.class);
    given(mobileApiData.reportUserData(anyString(), any(UserDataReport.class))).willReturn(new UserDataReport());
}
Also used : UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport)

Example 4 with UserDataReport

use of org.infobip.mobile.messaging.api.data.UserDataReport in project mobile-messaging-sdk-android by infobip.

the class UserDataSyncTest method test_remove_custom_element.

@Test
public void test_remove_custom_element() throws Exception {
    UserData userData = new UserData();
    userData.setCustomUserDataElement("myKey1", new CustomUserDataValue("Some string"));
    userData.setCustomUserDataElement("myKey2", new CustomUserDataValue(12345));
    userData.setCustomUserDataElement("myKey2", new CustomUserDataValue(new Date()));
    userData.removeCustomUserDataElement("myKey1");
    userData.removeCustomUserDataElement("myKey2");
    userData.removeCustomUserDataElement("myKey3");
    mobileMessaging.syncUserData(userData);
    verify(mobileApiData, after(1000).times(1)).reportUserData(anyString(), reportCaptor.capture());
    UserDataReport report = reportCaptor.getValue();
    assertEquals(null, report.getCustomUserData().get("myKey1"));
    assertEquals(null, report.getCustomUserData().get("myKey2"));
    assertEquals(null, report.getCustomUserData().get("myKey3"));
}
Also used : UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport) Date(java.util.Date) Test(org.junit.Test)

Example 5 with UserDataReport

use of org.infobip.mobile.messaging.api.data.UserDataReport in project mobile-messaging-sdk-android by infobip.

the class UserDataStoreTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    captor = ArgumentCaptor.forClass(UserData.class);
    given(mobileApiData.reportUserData(anyString(), any(UserDataReport.class))).willReturn(new UserDataReport());
}
Also used : UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport)

Aggregations

UserDataReport (org.infobip.mobile.messaging.api.data.UserDataReport)6 Date (java.util.Date)3 Test (org.junit.Test)3 Calendar (java.util.Calendar)1 UserData (org.infobip.mobile.messaging.UserData)1 MRetryableTask (org.infobip.mobile.messaging.mobile.common.MRetryableTask)1 BackendBaseExceptionWithContent (org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)1 BackendInvalidParameterException (org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException)1 Matchers.anyString (org.mockito.Matchers.anyString)1