Search in sources :

Example 1 with SerializedCustomType

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

the class SerializedCustomTypeAdapterTest method nestedSerializedCustomTypeSerializeSerializationAndDeserialization.

/**
 * Test Nested SerializedCustomType serialization and deserialization.
 *
 * @throws JSONException On illegal json found by JSONAssert
 */
@Test
public void nestedSerializedCustomTypeSerializeSerializationAndDeserialization() throws JSONException {
    Map<String, Object> addressSerializedData1 = new HashMap<>();
    addressSerializedData1.put("line1", "222 Somewhere far");
    addressSerializedData1.put("line2", null);
    addressSerializedData1.put("state", "CA");
    addressSerializedData1.put("postalCode", "123456");
    SerializedCustomType address1 = SerializedCustomType.builder().serializedData(addressSerializedData1).customTypeSchema(null).build();
    Map<String, Object> addressSerializedData2 = new HashMap<>();
    addressSerializedData2.put("line1", "444 Somewhere close");
    addressSerializedData2.put("line2", "Apt 3");
    addressSerializedData2.put("state", "WA");
    addressSerializedData2.put("postalCode", "123456");
    SerializedCustomType address2 = SerializedCustomType.builder().serializedData(addressSerializedData2).customTypeSchema(null).build();
    ArrayList<SerializedCustomType> addresses = new ArrayList<>();
    addresses.add(address1);
    addresses.add(address2);
    Map<String, Object> phoneSerializedData = new HashMap<>();
    phoneSerializedData.put("countryCode", "1");
    phoneSerializedData.put("areaCode", "415");
    phoneSerializedData.put("phone", "6666666");
    SerializedCustomType phone = SerializedCustomType.builder().serializedData(phoneSerializedData).customTypeSchema(null).build();
    Map<String, Object> contactSerializedData = new HashMap<>();
    contactSerializedData.put("email", "tester@testing.com");
    contactSerializedData.put("phone", phone);
    SerializedCustomType contact = SerializedCustomType.builder().serializedData(contactSerializedData).customTypeSchema(null).build();
    ArrayList<String> stringList = new ArrayList<>();
    stringList.add("string1");
    stringList.add("string2");
    Map<String, Object> personalSerializedData = new HashMap<>();
    personalSerializedData.put("name", "Tester Testing");
    personalSerializedData.put("mailingAddresses", addresses);
    personalSerializedData.put("contact", contact);
    personalSerializedData.put("arrayList", stringList);
    SerializedCustomType person = SerializedCustomType.builder().serializedData(personalSerializedData).customTypeSchema(null).build();
    String expectedResourcePath = "nested-serialized-custom-type-se-deserialization.json";
    String expectedJson = Resources.readAsJson(expectedResourcePath).toString(2);
    String actualJson = new JSONObject(gson.toJson(person)).toString(2);
    JSONAssert.assertEquals(expectedJson, actualJson, true);
    SerializedCustomType recovered = gson.fromJson(expectedJson, SerializedCustomType.class);
    Assert.assertEquals(person, recovered);
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 2 with SerializedCustomType

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

the class AppSyncRequestFactoryTest method validateMutationOnUpdateSerializedModelNestsSerializedCustomTypeWithOnlyChangedFields.

/**
 * Validates creation of a serialized model nests serialized custom types.
 *
 * @throws JSONException from JSONAssert.assertEquals JSON parsing error
 * @throws AmplifyException from ModelSchema.fromModelClass to convert model to schema
 */
@Test
public void validateMutationOnUpdateSerializedModelNestsSerializedCustomTypeWithOnlyChangedFields() throws JSONException, AmplifyException {
    buildSerializedModelNestsSerializedCustomTypeSchemas();
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    Map<String, Object> phoneSerializedData = new HashMap<>();
    phoneSerializedData.put("country", "+1");
    phoneSerializedData.put("area", "415");
    phoneSerializedData.put("number", "6666666");
    SerializedCustomType phone = SerializedCustomType.builder().serializedData(phoneSerializedData).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Phone")).build();
    Map<String, Object> bioSerializedData = new HashMap<>();
    bioSerializedData.put("email", "test@testing.com");
    bioSerializedData.put("phone", phone);
    SerializedCustomType bio = SerializedCustomType.builder().serializedData(bioSerializedData).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Bio")).build();
    Map<String, Object> personSerializedData = new HashMap<>();
    personSerializedData.put("id", "123456");
    personSerializedData.put("bio", bio);
    SerializedModel person = SerializedModel.builder().serializedData(personSerializedData).modelSchema(schemaRegistry.getModelSchemaForModelClass("Person")).build();
    String actual = AppSyncRequestFactory.buildUpdateRequest(schemaRegistry.getModelSchemaForModelClass("Person"), person, 1, QueryPredicates.all(), DEFAULT_STRATEGY).getContent();
    JSONAssert.assertEquals(Resources.readAsString("update-nested-serialized-model-custom-type.txt"), actual, true);
    clearSerializedModelNestsSerializedCustomTypeSchemas();
}
Also used : HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 3 with SerializedCustomType

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

the class AppSyncRequestFactoryTest method validateMutationOnCreationSerializedModelNestsSerializedCustomType.

/**
 * Validates creation of a serialized model nests serialized custom types.
 *
 * @throws JSONException from JSONAssert.assertEquals JSON parsing error
 * @throws AmplifyException from ModelSchema.fromModelClass to convert model to schema
 */
@Test
public void validateMutationOnCreationSerializedModelNestsSerializedCustomType() throws JSONException, AmplifyException {
    buildSerializedModelNestsSerializedCustomTypeSchemas();
    SchemaRegistry schemaRegistry = SchemaRegistry.instance();
    Map<String, Object> phoneSerializedData = new HashMap<>();
    phoneSerializedData.put("country", "+1");
    phoneSerializedData.put("area", "415");
    phoneSerializedData.put("number", "6666666");
    SerializedCustomType phone = SerializedCustomType.builder().serializedData(phoneSerializedData).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Phone")).build();
    Map<String, Object> bioSerializedData = new HashMap<>();
    bioSerializedData.put("email", "test@testing.com");
    bioSerializedData.put("phone", phone);
    SerializedCustomType bio = SerializedCustomType.builder().serializedData(bioSerializedData).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Bio")).build();
    Map<String, Object> addressSerializedData1 = new HashMap<>();
    addressSerializedData1.put("line1", "222 Somewhere far");
    addressSerializedData1.put("line2", "apt 3");
    addressSerializedData1.put("city", "SFO");
    addressSerializedData1.put("state", "CA");
    addressSerializedData1.put("postalCode", "94105");
    SerializedCustomType address1 = SerializedCustomType.builder().serializedData(addressSerializedData1).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Address")).build();
    Map<String, Object> addressSerializedData2 = new HashMap<>();
    addressSerializedData2.put("line1", "333 Somewhere close");
    addressSerializedData2.put("line2", null);
    addressSerializedData2.put("city", "SEA");
    addressSerializedData2.put("state", "WA");
    addressSerializedData2.put("postalCode", "00000");
    SerializedCustomType address2 = SerializedCustomType.builder().serializedData(addressSerializedData2).customTypeSchema(schemaRegistry.getCustomTypeSchemaMap().get("Address")).build();
    ArrayList<SerializedCustomType> addressesList = new ArrayList<>();
    addressesList.add(address1);
    addressesList.add(address2);
    Map<String, Object> personSerializedData = new HashMap<>();
    personSerializedData.put("id", "123456");
    personSerializedData.put("name", "Tester Testing");
    personSerializedData.put("bio", bio);
    personSerializedData.put("mailingAddresses", addressesList);
    SerializedModel person = SerializedModel.builder().serializedData(personSerializedData).modelSchema(schemaRegistry.getModelSchemaForModelClass("Person")).build();
    String actual = AppSyncRequestFactory.buildCreationRequest(schemaRegistry.getModelSchemaForModelClass("Person"), person, DEFAULT_STRATEGY).getContent();
    JSONAssert.assertEquals(Resources.readAsString("create-nested-serialized-model-custom-type.txt"), actual, true);
    clearSerializedModelNestsSerializedCustomTypeSchemas();
}
Also used : HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) ArrayList(java.util.ArrayList) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 4 with SerializedCustomType

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

the class SQLiteStorageAdapter method getValueOfListCustomTypeField.

private List<SerializedCustomType> getValueOfListCustomTypeField(String fieldTargetType, List<Map<String, Object>> listItems) {
    // if the filed is optional and has null value instead of an array
    if (listItems == null) {
        return null;
    }
    final CustomTypeSchema nestedCustomTypeSchema = schemaRegistry.getCustomTypeSchemaForCustomTypeClass(fieldTargetType);
    List<SerializedCustomType> listOfCustomType = new ArrayList<>();
    for (Map<String, Object> listItem : listItems) {
        SerializedCustomType customType = createSerializedCustomType(nestedCustomTypeSchema, listItem);
        listOfCustomType.add(customType);
    }
    return listOfCustomType;
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) ArrayList(java.util.ArrayList)

Example 5 with SerializedCustomType

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

the class SQLiteStorageAdapter method createSerializedCustomType.

private SerializedCustomType createSerializedCustomType(CustomTypeSchema customTypeSchema, Map<String, Object> data) {
    final Map<String, Object> serializedData = new HashMap<>();
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        CustomTypeField field = customTypeSchema.getFields().get(entry.getKey());
        if (field == null) {
            continue;
        }
        if (field.isCustomType() && entry.getValue() != null) {
            if (field.isArray()) {
                @SuppressWarnings("unchecked") List<Map<String, Object>> listItems = (List<Map<String, Object>>) entry.getValue();
                List<SerializedCustomType> listOfCustomType = getValueOfListCustomTypeField(field.getTargetType(), listItems);
                serializedData.put(entry.getKey(), listOfCustomType);
            } else {
                final CustomTypeSchema nestedCustomTypeSchema = schemaRegistry.getCustomTypeSchemaForCustomTypeClass(field.getTargetType());
                @SuppressWarnings("unchecked") Map<String, Object> nestedData = (Map<String, Object>) entry.getValue();
                serializedData.put(entry.getKey(), createSerializedCustomType(nestedCustomTypeSchema, nestedData));
            }
        } else {
            serializedData.put(entry.getKey(), entry.getValue());
        }
    }
    return SerializedCustomType.builder().serializedData(serializedData).customTypeSchema(customTypeSchema).build();
}
Also used : CustomTypeSchema(com.amplifyframework.core.model.CustomTypeSchema) HashMap(java.util.HashMap) SerializedCustomType(com.amplifyframework.core.model.SerializedCustomType) CustomTypeField(com.amplifyframework.core.model.CustomTypeField) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)3 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)2 List (java.util.List)2 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)1 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)1 ModelField (com.amplifyframework.core.model.ModelField)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1