Search in sources :

Example 61 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class FieldFinderTest method extractsSerializedModelFieldValue.

/**
 * Extracts the field value for a serialized model.
 * @throws AmplifyException On failure to derive ModelSchema or to convert Java model to Map
 * @throws NoSuchFieldException If field name does not exist for model
 */
@Test
public void extractsSerializedModelFieldValue() throws AmplifyException, NoSuchFieldException {
    String username = "foo";
    User user = User.builder().username(username).build();
    ModelSchema modelSchema = ModelSchema.fromModelClass(User.class);
    Map<String, Object> map = ModelConverter.toMap(user, modelSchema);
    SerializedModel serializedModel = SerializedModel.builder().serializedData(map).modelSchema(modelSchema).build();
    Object extractedValue = FieldFinder.extractFieldValue(serializedModel, "username");
    Assert.assertEquals(username, extractedValue);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) User(com.amplifyframework.testmodels.ratingsblog.User) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Example 62 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method arrangeDataEmittingSubscription.

@SuppressWarnings("SameParameterValue")
private static <T extends Model> void arrangeDataEmittingSubscription(AppSync appSync, ModelSchema modelSchema, SubscriptionType subscriptionType, GraphQLResponse<ModelWithMetadata<T>> response) throws DataStoreException {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        final int dataConsumerIndex = 2;
        Consumer<GraphQLResponse<ModelWithMetadata<T>>> onData = invocation.getArgument(dataConsumerIndex);
        onData.accept(response);
        return new NoOpCancelable();
    };
    arrangeSubscription(appSync, answer, modelSchema, subscriptionType);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 63 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SyncProcessorTest method syncExpressionsAppliedOnSyncRequest.

/**
 * Verify that the syncExpressions from the DataStoreConfiguration are applied to the sync request.
 * @throws AmplifyException On failure interacting with storage adapter
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void syncExpressionsAppliedOnSyncRequest() throws AmplifyException, InterruptedException {
    // Arrange some responses from AppSync
    AppSyncMocking.sync(appSync).mockSuccessResponse(Post.class, DELETED_DRUM_POST).mockSuccessResponse(BlogOwner.class, BLOGGER_JAMESON);
    // Act: hydrate the local store.
    TestObserver<Void> hydrationObserver = syncProcessor.hydrate().test();
    // Assert that hydration completed without error.
    assertTrue(hydrationObserver.await(OP_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    hydrationObserver.assertNoErrors();
    hydrationObserver.assertComplete();
    // Mock the AppSync interface, and verify that it gets calls with the expected predicate
    ModelSchema blogOwnerSchema = ModelSchema.fromModelClass(BlogOwner.class);
    verify(appSync, times(1)).buildSyncRequest(blogOwnerSchema, null, 1_000, BlogOwner.NAME.beginsWith("J"));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Post(com.amplifyframework.testmodels.commentsblog.Post) Test(org.junit.Test)

Example 64 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SqlCommandTest method queryWithSortBy.

/**
 * Validates that a query, with an order by clause is generated correctly.
 * @throws DataStoreException From {@link SQLCommandFactory#queryFor(ModelSchema, QueryOptions)}
 */
@Test
public void queryWithSortBy() throws DataStoreException {
    final ModelSchema personSchema = getPersonModelSchema();
    final SqlCommand sqlCommand = sqlCommandFactory.queryFor(personSchema, Where.matchesAll().sorted(new QuerySortBy("lastName", QuerySortOrder.ASCENDING), new QuerySortBy("firstName", QuerySortOrder.DESCENDING)));
    assertNotNull(sqlCommand);
    assertEquals(PERSON_BASE_QUERY + " ORDER BY `Person`.`lastName` ASC, `Person`.`firstName` DESC;", sqlCommand.sqlStatement());
    assertEquals(0, sqlCommand.getBindings().size());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Test(org.junit.Test)

Example 65 with ModelSchema

use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.

the class SqlCommandTest method validModelSchemaReturnsExpectedSqlCommand.

/**
 * Test if a valid {@link ModelSchema} returns an expected
 * CREATE TABLE SQL command.
 */
@Test
public void validModelSchemaReturnsExpectedSqlCommand() {
    final ModelSchema personSchema = getPersonModelSchema();
    final SqlCommand sqlCommand = sqlCommandFactory.createTableFor(personSchema);
    assertEquals("Person", sqlCommand.tableName());
    assertEquals("CREATE TABLE IF NOT EXISTS `Person` (" + "`id` TEXT PRIMARY KEY NOT NULL, " + "`age` INTEGER, " + "`firstName` TEXT NOT NULL, " + "`lastName` TEXT NOT NULL);", sqlCommand.sqlStatement());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Aggregations

ModelSchema (com.amplifyframework.core.model.ModelSchema)109 Test (org.junit.Test)69 SerializedModel (com.amplifyframework.core.model.SerializedModel)34 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)30 Model (com.amplifyframework.core.model.Model)28 DataStoreException (com.amplifyframework.datastore.DataStoreException)26 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)22 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)21 AmplifyException (com.amplifyframework.AmplifyException)19 Consumer (com.amplifyframework.core.Consumer)19 List (java.util.List)17 NonNull (androidx.annotation.NonNull)14 Cancelable (com.amplifyframework.core.async.Cancelable)14 TimeUnit (java.util.concurrent.TimeUnit)14 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)13 Action (com.amplifyframework.core.Action)12 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)12 Collections (java.util.Collections)12 ModelProvider (com.amplifyframework.core.model.ModelProvider)11