Search in sources :

Example 36 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateUpdateWithPredicateGeneration.

/**
 * Checks that we're getting the expected output for a mutation with predicate.
 * @throws DataStoreException If the output does not match.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateUpdateWithPredicateGeneration() throws AmplifyException, JSONException {
    String blogOwnerId = "926d7ee8-4ea5-40c0-8e62-3fb80b2a2edd";
    BlogOwner owner = BlogOwner.builder().name("John Doe").id(blogOwnerId).build();
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    AppSyncGraphQLRequest<?> request = AppSyncRequestFactory.buildUpdateRequest(schema, owner, 42, BlogOwner.WEA.contains("ther"), DEFAULT_STRATEGY);
    JSONAssert.assertEquals(Resources.readAsString("update-blog-owner-with-predicate.txt"), request.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 37 with ModelSchema

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

the class AppSyncRequestFactoryTest method validatePredicateGroupForSyncExpressionIsNotWrappedWithAnd.

/**
 * If a QueryPredicateGroup is provided, it should be parsed as is, and not be wrapped with another AND group.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validatePredicateGroupForSyncExpressionIsNotWrappedWithAnd() throws AmplifyException, JSONException {
    String id = "426f8e8d-ea0f-4839-a73f-6a2a38565ba1";
    ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class);
    QueryPredicate predicate = BlogOwner.ID.eq(id).and(Blog.NAME.eq("Spaghetti"));
    final GraphQLRequest<Iterable<Post>> request = AppSyncRequestFactory.buildSyncRequest(schema, null, null, predicate);
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-with-predicate-group.txt"), request.getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Test(org.junit.Test)

Example 38 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateCustomTypeRequestGenerationForBaseSync.

/**
 * Validates the construction of a base-sync query document for models with custom types.
 * @throws DataStoreException On failure to interrogate fields in Parent.class
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals
 */
@Test
public void validateCustomTypeRequestGenerationForBaseSync() throws AmplifyException, JSONException {
    ModelSchema schema = ModelSchema.fromModelClass(Parent.class);
    JSONAssert.assertEquals(Resources.readAsString("base-sync-request-document-for-parent.txt"), AppSyncRequestFactory.buildSyncRequest(schema, null, null, QueryPredicates.all(), DEFAULT_STRATEGY).getContent(), true);
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) Test(org.junit.Test)

Example 39 with ModelSchema

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

the class AppSyncRequestFactoryTest method validateMutationGenerationOnCreateComment.

/**
 * Validates creation of a "create a model" request.
 * @throws DataStoreException On failure to interrogate the model fields.
 * @throws AmplifyException On failure to parse ModelSchema from model class
 * @throws JSONException from JSONAssert.assertEquals.
 */
@Test
public void validateMutationGenerationOnCreateComment() throws AmplifyException, JSONException {
    Post post = Post.justId("9a4295d6-8225-495a-a531-beffc8b7ae7d");
    Comment comment = Comment.builder().id("426f8e8d-ea0f-4839-a73f-6a2a38565ba1").content("toast").post(post).build();
    ModelSchema schema = ModelSchema.fromModelClass(Comment.class);
    JSONAssert.assertEquals(Resources.readAsString("create-comment-request.txt"), AppSyncRequestFactory.buildCreationRequest(schema, comment, DEFAULT_STRATEGY).getContent(), true);
}
Also used : Comment(com.amplifyframework.testmodels.commentsblog.Comment) ModelSchema(com.amplifyframework.core.model.ModelSchema) Post(com.amplifyframework.testmodels.commentsblog.Post) Test(org.junit.Test)

Example 40 with ModelSchema

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

the class AppSyncRequestFactoryTest method buildSerializedModelNestsSerializedCustomTypeSchemas.

private void buildSerializedModelNestsSerializedCustomTypeSchemas() {
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    CustomTypeField addressLine1Field = CustomTypeField.builder().name("line1").isRequired(true).targetType("String").build();
    CustomTypeField addressLine2Field = CustomTypeField.builder().name("line2").isRequired(false).targetType("String").build();
    CustomTypeField addressCityField = CustomTypeField.builder().name("city").isRequired(true).targetType("String").build();
    CustomTypeField addressStateField = CustomTypeField.builder().name("state").isRequired(true).targetType("String").build();
    CustomTypeField addressPostalCodeField = CustomTypeField.builder().name("postalCode").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> addressFields = new HashMap<>();
    addressFields.put("line1", addressLine1Field);
    addressFields.put("line2", addressLine2Field);
    addressFields.put("city", addressCityField);
    addressFields.put("state", addressStateField);
    addressFields.put("postalCode", addressPostalCodeField);
    CustomTypeSchema addressSchema = CustomTypeSchema.builder().name("Address").pluralName("Addresses").fields(addressFields).build();
    CustomTypeField phoneCountryField = CustomTypeField.builder().name("country").isRequired(true).targetType("String").build();
    CustomTypeField phoneAreaField = CustomTypeField.builder().name("area").isRequired(true).targetType("String").build();
    CustomTypeField phoneNumberField = CustomTypeField.builder().name("number").isRequired(true).targetType("String").build();
    Map<String, CustomTypeField> phoneFields = new HashMap<>();
    phoneFields.put("country", phoneCountryField);
    phoneFields.put("area", phoneAreaField);
    phoneFields.put("number", phoneNumberField);
    CustomTypeSchema phoneSchema = CustomTypeSchema.builder().name("Phone").pluralName("Phones").fields(phoneFields).build();
    CustomTypeField bioEmailField = CustomTypeField.builder().name("email").isRequired(false).targetType("String").build();
    CustomTypeField bioPhoneField = CustomTypeField.builder().name("phone").isCustomType(true).targetType("Phone").build();
    Map<String, CustomTypeField> bioFields = new HashMap<>();
    bioFields.put("email", bioEmailField);
    bioFields.put("phone", bioPhoneField);
    CustomTypeSchema bioSchema = CustomTypeSchema.builder().name("Bio").fields(bioFields).build();
    ModelField personBioField = ModelField.builder().name("bio").targetType("Bio").isCustomType(true).isRequired(true).build();
    ModelField personNameField = ModelField.builder().name("name").targetType("String").isRequired(true).build();
    ModelField personMailingAddressesField = ModelField.builder().name("mailingAddresses").targetType("Address").isCustomType(true).isArray(true).build();
    ModelField personIdField = ModelField.builder().name("id").targetType("String").isRequired(true).build();
    Map<String, ModelField> personFields = new HashMap<>();
    personFields.put("bio", personBioField);
    personFields.put("name", personNameField);
    personFields.put("mailingAddresses", personMailingAddressesField);
    personFields.put("id", personIdField);
    ModelSchema personSchema = ModelSchema.builder().name("Person").pluralName("People").fields(personFields).modelClass(SerializedModel.class).build();
    schemaRegistry.register("Address", addressSchema);
    schemaRegistry.register("Phone", phoneSchema);
    schemaRegistry.register("Bio", bioSchema);
    schemaRegistry.register("Person", personSchema);
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

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