Search in sources :

Example 6 with ModelProvider

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

the class MutationPersistenceInstrumentationTest method obtainLocalStorageAndValidateModelSchema.

/**
 * Prepare an instance of {@link LocalStorageAdapter}. Evaluate its
 * emitted collection of ModelSchema, to ensure that
 * {@link PendingMutation.PersistentRecord} is among them.
 *
 * TODO: later, consider hiding system schema, such as the
 * {@link PendingMutation.PersistentRecord}, from the callback. This schema might be
 * an implementation detail, that is working as a leaky abstraction.
 *
 * @throws AmplifyException On failure to initialize the storage adapter,
 *                          or on failure to load model schema into registry
 */
@Before
public void obtainLocalStorageAndValidateModelSchema() throws AmplifyException {
    this.converter = new GsonPendingMutationConverter();
    getApplicationContext().deleteDatabase(DATABASE_NAME);
    ModelProvider modelProvider = SimpleModelProvider.withRandomVersion(BlogOwner.class);
    schemaRegistry = SchemaRegistry.instance();
    schemaRegistry.clear();
    schemaRegistry.register(modelProvider.models());
    LocalStorageAdapter localStorageAdapter = SQLiteStorageAdapter.forModels(schemaRegistry, modelProvider);
    this.storage = SynchronousStorageAdapter.delegatingTo(localStorageAdapter);
    List<ModelSchema> initializationResults = storage.initialize(getApplicationContext());
    // Evaluate the returned set of ModelSchema. Ensure that there is one
    // for the PendingMutation.PersistentRecord system class.
    assertTrue(Observable.fromIterable(initializationResults).map(ModelSchema::getName).toList().blockingGet().contains(PendingMutation.PersistentRecord.class.getSimpleName()));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) LocalStorageAdapter(com.amplifyframework.datastore.storage.LocalStorageAdapter) ModelProvider(com.amplifyframework.core.model.ModelProvider) SimpleModelProvider(com.amplifyframework.datastore.model.SimpleModelProvider) Before(org.junit.Before)

Example 7 with ModelProvider

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

the class CompoundModelProviderTest method compoundVersionIsStable.

/**
 * It should be possible to create multiple instance of {@link CompoundModelProvider}
 * from different component providers that have the same UUID. The compound versions
 * should be repeatably the same.
 */
@Test
public void compoundVersionIsStable() {
    // Arrange three model providers.
    // The first has one UUID
    // The seconds and third share a UUID.
    String uniqueVersion = UUID.randomUUID().toString();
    ModelProvider first = SimpleModelProvider.instance(uniqueVersion, Blog.class);
    String repeatedVersion = UUID.randomUUID().toString();
    ModelProvider second = SimpleModelProvider.instance(repeatedVersion, BlogOwner.class);
    ModelProvider third = SimpleModelProvider.instance(repeatedVersion, BlogOwner.class);
    // Act: create a couple of compounds, one of (A + B), and another of (A + C).
    CompoundModelProvider firstPlusSecond = CompoundModelProvider.of(first, second);
    CompoundModelProvider firstPlusThird = CompoundModelProvider.of(first, third);
    // Assert: both instances report the same version for the compound,
    // on the basis that the ipnut versions were all the same.
    assertEquals(firstPlusSecond.version(), firstPlusThird.version());
}
Also used : ModelProvider(com.amplifyframework.core.model.ModelProvider) Test(org.junit.Test)

Example 8 with ModelProvider

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

the class ModelProviderLocatorTest method locateValidModelProvider.

/**
 * Validate that the ModelProviderLocator can find a ModelProvider by class name,
 * and can make product use of it to load models for the system.
 * @throws DataStoreException Not expect in this test. Possible from {@link ModelProviderLocator#locate(String)}.
 */
@Test
public void locateValidModelProvider() throws DataStoreException {
    String className = Objects.requireNonNull(ValidModelProvider.class.getName());
    ModelProvider provider = ModelProviderLocator.locate(className);
    assertNotNull(provider);
    assertEquals(ValidModelProvider.getInstance(), provider);
    assertEquals(Collections.singleton(BoilerPlateModel.class), provider.models());
}
Also used : ModelProvider(com.amplifyframework.core.model.ModelProvider) RandomString(com.amplifyframework.testutils.random.RandomString) Test(org.junit.Test)

Example 9 with ModelProvider

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

the class CompoundModelProvider method of.

/**
 * Gets an {@link CompoundModelProvider} that provides all of the same models as the
 * constituent {@link ModelProvider} that are provided. The version of the returned
 * {@link CompoundModelProvider} shall be stable UUID hash of the versions of all
 * provided {@link ModelProvider}s.
 * @param modelProviders model providers
 * @return A compound provider, which provides the models of all of the input providers
 */
@NonNull
public static CompoundModelProvider of(@NonNull ModelProvider... modelProviders) {
    final Map<String, ModelSchema> modelSchemaMap = new HashMap<>();
    final Map<String, CustomTypeSchema> customTypeSchemaMap = new HashMap<>();
    StringBuilder componentVersionBuffer = new StringBuilder();
    for (ModelProvider componentProvider : modelProviders) {
        componentVersionBuffer.append(componentProvider.version());
        modelSchemaMap.putAll(componentProvider.modelSchemas());
        customTypeSchemaMap.putAll(componentProvider.customTypeSchemas());
    }
    String version = UUID.nameUUIDFromBytes(componentVersionBuffer.toString().getBytes()).toString();
    SimpleModelProvider delegateProvider = SimpleModelProvider.instance(version, modelSchemaMap, customTypeSchemaMap);
    return new CompoundModelProvider(delegateProvider);
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) ModelProvider(com.amplifyframework.core.model.ModelProvider) NonNull(androidx.annotation.NonNull)

Example 10 with ModelProvider

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

the class ModelProviderLocator method locate.

@SuppressWarnings({ "SameParameterValue", "unchecked" })
static ModelProvider locate(@NonNull String modelProviderClassName) throws DataStoreException {
    Objects.requireNonNull(modelProviderClassName);
    final Class<? extends ModelProvider> modelProviderClass;
    try {
        // noinspection unchecked It's very unlikely that someone cooked up a different type at this FQCN.
        modelProviderClass = (Class<? extends ModelProvider>) Class.forName(modelProviderClassName);
    } catch (ClassNotFoundException modelProviderClassNotFoundError) {
        throw new DataStoreException("Failed to find code-generated model provider.", modelProviderClassNotFoundError, "Validate that " + modelProviderClassName + " is built into your project.");
    }
    if (!ModelProvider.class.isAssignableFrom(modelProviderClass)) {
        throw new DataStoreException("Located class as " + modelProviderClass.getName() + ", but it does not implement " + ModelProvider.class.getName() + ".", "Validate that " + modelProviderClass.getName() + " has not been modified since the time " + "it was code-generated.");
    }
    final Method getInstanceMethod;
    try {
        getInstanceMethod = modelProviderClass.getDeclaredMethod(GET_INSTANCE_ACCESSOR_METHOD_NAME);
    } catch (NoSuchMethodException noGetInstanceMethodError) {
        throw new DataStoreException("Found a code-generated model provider = " + modelProviderClass.getName() + ", however " + "it had no static method named getInstance()!", noGetInstanceMethodError, "Validate that " + modelProviderClass.getName() + " has not been modified since the time " + "it was code-generated.");
    }
    final ModelProvider locatedModelProvider;
    try {
        locatedModelProvider = (ModelProvider) getInstanceMethod.invoke(null);
    } catch (IllegalAccessException getInstanceIsNotAccessibleError) {
        throw new DataStoreException("Tried to call " + modelProviderClass.getName() + GET_INSTANCE_ACCESSOR_METHOD_NAME + ", but " + "this method did not have public access.", getInstanceIsNotAccessibleError, "Validate that " + modelProviderClass.getName() + " has not been modified since the time " + "it was code-generated.");
    } catch (InvocationTargetException wrappedExceptionFromGetInstance) {
        throw new DataStoreException("An exception was thrown from " + modelProviderClass.getName() + GET_INSTANCE_ACCESSOR_METHOD_NAME + " while invoking via reflection.", wrappedExceptionFromGetInstance, "This is not expected to occur. Contact AWS.");
    }
    return locatedModelProvider;
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ModelProvider(com.amplifyframework.core.model.ModelProvider) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ModelProvider (com.amplifyframework.core.model.ModelProvider)11 ModelSchema (com.amplifyframework.core.model.ModelSchema)5 Before (org.junit.Before)5 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)4 DataStoreException (com.amplifyframework.datastore.DataStoreException)4 NonNull (androidx.annotation.NonNull)3 Model (com.amplifyframework.core.model.Model)3 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)3 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)3 SimpleModelProvider (com.amplifyframework.datastore.model.SimpleModelProvider)3 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)3 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)3 Completable (io.reactivex.rxjava3.core.Completable)3 Single (io.reactivex.rxjava3.core.Single)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.Test)3 AmplifyException (com.amplifyframework.AmplifyException)2 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)2 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)2