use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SchemaLoader method loadFromAssetsDirectory.
/**
* Creates an {@link SchemaProvider} by loading schema JSON files from the
* ./assets directory.
* @param assetsDirectoryPath Directory under ./assets, e.g., "schemas/commentsblog".
* @return A SchemaProvider that vends {@link ModelSchema}, each of which was derived
* from a schema JSON
*/
@NonNull
static SchemaProvider loadFromAssetsDirectory(@SuppressWarnings("SameParameterValue") String assetsDirectoryPath) {
List<ModelSchema> schemas = new ArrayList<>();
Gson gson = GsonFactory.instance();
for (String fileName : Assets.list(assetsDirectoryPath)) {
schemas.add(gson.fromJson(Assets.readAsString(fileName), ModelSchema.class));
}
return SchemaProvider.of(schemas.toArray(new ModelSchema[0]));
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class SchemaProvider method from.
/**
* Creates a SchemaProvider by extracting {@link ModelSchema} from the provided
* {@link ModelProvider}.
* @param modelOnlyProvider A provider which provides only models
* @return A provider which provides only schema
*/
@NonNull
static SchemaProvider from(@NonNull ModelProvider modelOnlyProvider) throws AmplifyException {
Objects.requireNonNull(modelOnlyProvider);
Map<String, ModelSchema> schemas = new HashMap<>();
for (Class<? extends Model> clazz : modelOnlyProvider.models()) {
ModelSchema schema = ModelSchema.fromModelClass(clazz);
schemas.put(schema.getName(), schema);
}
return new SchemaProvider(randomVersion(), schemas);
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationPersistenceInstrumentationTest method deletionIsObservedForPersistentRecord.
/**
* When an {@link PendingMutation.PersistentRecord} is deleted from the DataStore, we will expect
* to see an event on item consumer of {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}.
* @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
*/
@Test
public void deletionIsObservedForPersistentRecord() throws DataStoreException {
// We are observing a stream of changes to models.
// In this test, the <? extends Model> type happens to be PersistentRecord (itself, implementing Model.)
TestObserver<StorageItemChange<? extends Model>> storageObserver = storage.observe().test();
BlogOwner beatrice = BlogOwner.builder().name("Beatrice Stone").build();
ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
PendingMutation<BlogOwner> createBeatrice = PendingMutation.creation(beatrice, schema);
PendingMutation.PersistentRecord createBeatriceRecord = converter.toRecord(createBeatrice);
storage.save(createBeatriceRecord);
// Assert that we do observe the PersistentRecord being saved ...
assertEquals(createBeatriceRecord, storageObserver.awaitCount(1).values().get(0).item());
storageObserver.dispose();
TestObserver<StorageItemChange<? extends Model>> deletionObserver = storage.observe().test();
// Try to delete Beatrice's record.
storage.delete(createBeatriceRecord);
// Should receive a notification of the deletion on the observer.
// The notification refers to the deleted item, in its contents.
assertEquals(createBeatriceRecord, deletionObserver.awaitCount(1).values().get(0).item());
deletionObserver.dispose();
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationPersistenceInstrumentationTest method adapterCanSaveAndQueryPersistentRecords.
/**
* The adapter must be able to save a {@link PendingMutation.PersistentRecord}.
* When we query the adapter for that same {@link PendingMutation.PersistentRecord},
* we will expect to find an exact replica of the one we had saved.
* @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
*/
@Test
public void adapterCanSaveAndQueryPersistentRecords() throws DataStoreException {
final BlogOwner tonyDaniels = BlogOwner.builder().name("Tony Daniels").build();
ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
PendingMutation<BlogOwner> originalTonyCreation = PendingMutation.creation(tonyDaniels, schema);
// Save the creation mutation for Tony, as a PersistentRecord object.
PendingMutation.PersistentRecord originalTonyCreationAsRecord = converter.toRecord(originalTonyCreation);
storage.save(originalTonyCreationAsRecord);
// Now, lookup what PersistentRecord we have in the storage.
List<PendingMutation.PersistentRecord> recordsInStorage = storage.query(PendingMutation.PersistentRecord.class);
// There should be 1, and it should be the original creation for Tony.
assertEquals(1, recordsInStorage.size());
PendingMutation.PersistentRecord firstRecordFoundInStorage = recordsInStorage.get(0);
assertEquals(originalTonyCreationAsRecord, firstRecordFoundInStorage);
// After we convert back from PersistentRecord, we should get back a copy of
// what we created above
PendingMutation<BlogOwner> reconstructedCreationOfTony = converter.fromRecord(firstRecordFoundInStorage);
assertEquals(originalTonyCreation, reconstructedCreationOfTony);
}
use of com.amplifyframework.core.model.ModelSchema in project amplify-android by aws-amplify.
the class MutationPersistenceInstrumentationTest method updatesAreObservedForPersistentRecords.
/**
* When {@link LocalStorageAdapter#save(Model, StorageItemChange.Initiator, QueryPredicate, Consumer, Consumer)}
* is called to save a {@link PendingMutation.PersistentRecord}, we should expect to observe a change event
* /containing/ that record within it. It will be received by the value consumer of
* {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}.
*
* Similarly, when we update the {@link PendingMutation.PersistentRecord} that we had just saved,
* we should see an update notification on the subscription consumer. The type will be
* StorageItemChange, and inside of it ill be a reference to the {@link PendingMutation.PersistentRecord}.
*
* @throws DataStoreException from storage item change, or on failure to manipulate I/O to DataStore
*/
@Test
public void updatesAreObservedForPersistentRecords() throws DataStoreException {
// Establish a subscription to listen for storage change records
TestObserver<StorageItemChange<? extends Model>> storageObserver = storage.observe().test();
// Create a record for Joe, and a change to save him into storage
BlogOwner joeLastNameMisspelled = BlogOwner.builder().name("Joe Sweeneyy").build();
ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
PendingMutation<BlogOwner> createJoeWrongLastName = PendingMutation.creation(joeLastNameMisspelled, schema);
// Save our saveJoeWrongLastName change item, as a PersistentRecord.
PendingMutation.PersistentRecord createJoeWrongLastNameAsRecord = converter.toRecord(createJoeWrongLastName);
storage.save(createJoeWrongLastNameAsRecord);
// Now, suppose we have to update that pending mutation. Maybe it contained a bad item payload.
BlogOwner joeWithLastNameFix = BlogOwner.builder().name("Joe Sweeney").id(joeLastNameMisspelled.getId()).build();
PendingMutation<BlogOwner> createJoeCorrectLastName = PendingMutation.creation(joeWithLastNameFix, schema);
// Save an update (same model type, same unique ID) to the mutation we saved previously.
PendingMutation.PersistentRecord createJoeCorrectLastNameAsRecord = converter.toRecord(createJoeCorrectLastName);
storage.save(createJoeCorrectLastNameAsRecord);
// Our observer got the records to save Joe with wrong age, and also to save joe with right age
assertEquals(Arrays.asList(createJoeWrongLastNameAsRecord, createJoeCorrectLastNameAsRecord), Observable.fromIterable(storageObserver.awaitCount(2).values()).map(StorageItemChange::item).toList().blockingGet());
storageObserver.dispose();
}
Aggregations