Search in sources :

Example 1 with LastSyncMetadata

use of com.amplifyframework.datastore.syncengine.LastSyncMetadata in project amplify-android by aws-amplify.

the class SyncStatusTest method syncStatusGetReturnSyncedStatus.

/**
 * Get returns sync status.
 */
@Test
public void syncStatusGetReturnSyncedStatus() {
    final LastSyncMetadata lastSyncMetadata = LastSyncMetadata.baseSyncedAt(BlogOwner.class.getName(), Time.now());
    List<LastSyncMetadata> resultList = new ArrayList<>();
    resultList.add(lastSyncMetadata);
    Consumer<DataStoreException> onObservationError = value -> {
    };
    SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
    when(mockSqlQueryProcessor.queryOfflineData(eq(LastSyncMetadata.class), any(), any())).thenReturn(resultList);
    DataStoreConfiguration mockDataStoreConfig = mock(DataStoreConfiguration.class);
    when(mockDataStoreConfig.getSyncIntervalInMinutes()).thenReturn(5L);
    SyncStatus subject = new SyncStatus(mockSqlQueryProcessor, mockDataStoreConfig);
    boolean result = subject.get(LastSyncMetadata.class.getName(), onObservationError);
    Assert.assertTrue(result);
}
Also used : LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) ArrayList(java.util.ArrayList) Time(com.amplifyframework.util.Time) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) List(java.util.List) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 2 with LastSyncMetadata

use of com.amplifyframework.datastore.syncengine.LastSyncMetadata in project amplify-android by aws-amplify.

the class SyncStatusTest method syncStatusGetReturnNotSyncedStatus.

/**
 * Get returns false for sync status.
 */
@Test
public void syncStatusGetReturnNotSyncedStatus() {
    final LastSyncMetadata lastSyncMetadata = LastSyncMetadata.baseSyncedAt(BlogOwner.class.getName(), Time.now());
    List<LastSyncMetadata> resultList = new ArrayList<>();
    resultList.add(lastSyncMetadata);
    Consumer<DataStoreException> onObservationError = value -> {
    };
    SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
    when(mockSqlQueryProcessor.queryOfflineData(eq(LastSyncMetadata.class), any(), any())).thenReturn(resultList);
    DataStoreConfiguration mockDataStoreConfig = mock(DataStoreConfiguration.class);
    when(mockDataStoreConfig.getSyncIntervalInMinutes()).thenReturn(0L);
    SyncStatus subject = new SyncStatus(mockSqlQueryProcessor, mockDataStoreConfig);
    boolean result = subject.get(LastSyncMetadata.class.getName(), onObservationError);
    Assert.assertFalse(result);
}
Also used : LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) ArrayList(java.util.ArrayList) Time(com.amplifyframework.util.Time) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) List(java.util.List) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) DataStoreException(com.amplifyframework.datastore.DataStoreException) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 3 with LastSyncMetadata

use of com.amplifyframework.datastore.syncengine.LastSyncMetadata in project amplify-android by aws-amplify.

the class SyncStatus method get.

/**
 * Method returns sync status.
 * @param modelClassName model class name.
 * @param onObservationError invoked on error.
 * @return returns the sync status of true or false.
 */
public boolean get(@NonNull String modelClassName, @NonNull Consumer<DataStoreException> onObservationError) {
    LastSyncMetadata lastSyncMetadata;
    boolean syncStatus = false;
    try {
        lastSyncMetadata = getLastSyncMetaData(modelClassName, onObservationError);
        if (lastSyncMetadata.getLastSyncTime() != null) {
            syncStatus = (Time.now() - lastSyncMetadata.getLastSyncTime()) < TimeUnit.MINUTES.toMillis(dataStoreConfiguration.getSyncIntervalInMinutes());
        }
    } catch (DataStoreException exception) {
        onObservationError.accept(exception);
    }
    return syncStatus;
}
Also used : LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Example 4 with LastSyncMetadata

use of com.amplifyframework.datastore.syncengine.LastSyncMetadata in project amplify-android by aws-amplify.

the class SyncStatus method getLastSyncMetaData.

private LastSyncMetadata getLastSyncMetaData(@NonNull String modelClassName, @NonNull Consumer<DataStoreException> onObservationError) throws DataStoreException {
    QueryPredicate hasMatchingModelClassName = QueryField.field("modelClassName").eq(modelClassName);
    List<LastSyncMetadata> syncedList = sqlQueryProcessor.queryOfflineData(LastSyncMetadata.class, Where.matches(hasMatchingModelClassName), onObservationError);
    return extractSingleResult(modelClassName, syncedList);
}
Also used : LastSyncMetadata(com.amplifyframework.datastore.syncengine.LastSyncMetadata) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate)

Aggregations

LastSyncMetadata (com.amplifyframework.datastore.syncengine.LastSyncMetadata)4 DataStoreException (com.amplifyframework.datastore.DataStoreException)3 Consumer (com.amplifyframework.core.Consumer)2 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)2 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)2 Time (com.amplifyframework.util.Time)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)1