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()));
}
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());
}
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());
}
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);
}
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;
}
Aggregations