From 1af5c95ba4d88db83b498afb6410dd67514cac8c Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Fri, 24 Jun 2022 11:36:37 -0700 Subject: [PATCH 1/6] For Flutter platform, do not format TIME or DATE_TIME object when converting from json. --- .../sqlite/SQLiteModelFieldTypeConverter.java | 7 ++ .../SQLiteModelFieldTypeConverterTest.java | 92 +++++++++++++++++++ .../com/amplifyframework/util/UserAgent.java | 8 ++ 3 files changed, 107 insertions(+) create mode 100644 aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java diff --git a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java index 2c55ed35a8..68d2f4d555 100644 --- a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java +++ b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java @@ -34,6 +34,7 @@ import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn; import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable; import com.amplifyframework.logging.Logger; +import com.amplifyframework.util.UserAgent; import com.google.gson.Gson; @@ -130,6 +131,9 @@ public static Object convertRawValueToTarget( case DATE: return value instanceof String ? value : ((Temporal.Date) value).format(); case DATE_TIME: + if (UserAgent.isFlutter() && value instanceof String) { + return value; + } OffsetDateTime offsetDateTime; if (value instanceof String) { offsetDateTime = OffsetDateTime.parse((String) value); @@ -141,6 +145,9 @@ public static Object convertRawValueToTarget( .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'"); return offsetDateTime.toInstant().atOffset(ZoneOffset.UTC).format(dateTimeFormatter); case TIME: + if (UserAgent.isFlutter() && value instanceof String) { + return value; + } String timeValue; if (value instanceof String) { timeValue = (String) value; diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java new file mode 100644 index 0000000000..411ceed29f --- /dev/null +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.amplifyframework.datastore.storage.sqlite; + +import com.amplifyframework.AmplifyException; +import com.amplifyframework.core.model.types.JavaFieldType; +import com.amplifyframework.util.GsonFactory; +import com.amplifyframework.util.UserAgent; + +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class SQLiteModelFieldTypeConverterTest { + + /** + * Reset the user agent before each test. + */ + @Before + public void reset() { + UserAgent.reset(); + } + + /** + * Test TIME conversion for Android (default platform). + */ + @Test + public void testConvertRawValueToTargetTimeAndroid() { + final String value = "16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.TIME; + final Gson gson = GsonFactory.instance(); + final String expected = "16:00:00.050020000Z"; + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + assertEquals(expected, actual); + } + + /** + * Test TIME converter for Flutter. + * @throws AmplifyException Not expected. + */ + @Test + public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { + UserAgent.configure(Map.of(UserAgent.Platform.FLUTTER, "1.0")); + final String value = "16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.TIME; + final Gson gson = GsonFactory.instance(); + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + final String expected = "16:00:00.050020000"; + assertEquals(expected, actual); + } + + /** + * Test DATE_TIME converter for Flutter. + * @throws AmplifyException Not expected. + */ + @Test + public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException { + UserAgent.configure(Map.of(UserAgent.Platform.FLUTTER, "1.0")); + final String value = "16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.DATE_TIME; + final Gson gson = GsonFactory.instance(); + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + final String expected = "16:00:00.050020000"; + assertEquals(expected, actual); + } +} diff --git a/core/src/main/java/com/amplifyframework/util/UserAgent.java b/core/src/main/java/com/amplifyframework/util/UserAgent.java index fc300aabb6..bf0b57e9f5 100644 --- a/core/src/main/java/com/amplifyframework/util/UserAgent.java +++ b/core/src/main/java/com/amplifyframework/util/UserAgent.java @@ -116,6 +116,14 @@ private static String forAndroid() { .toString(); } + /** + * Returns true if running on Flutter. + * @return Returns true if running on Flutter. + */ + public static boolean isFlutter() { + return string().contains(Platform.FLUTTER.libraryName); + } + /** * Enum to represent various platforms that use Amplify library for tracking * usage metrics. From 1fbe0d3e2ecdc872c39707de7df66f92be0d8b5f Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Fri, 24 Jun 2022 12:03:40 -0700 Subject: [PATCH 2/6] Responding to comments --- .../storage/sqlite/SQLiteModelFieldTypeConverterTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java index 411ceed29f..67424cc513 100644 --- a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -79,14 +79,14 @@ public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { @Test public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException { UserAgent.configure(Map.of(UserAgent.Platform.FLUTTER, "1.0")); - final String value = "16:00:00.050020000"; + final String value = "2020-01-01T16:00:00.050020000"; final JavaFieldType fieldType = JavaFieldType.DATE_TIME; final Gson gson = GsonFactory.instance(); final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( value, fieldType, gson); - final String expected = "16:00:00.050020000"; + final String expected = "2020-01-01T16:00:00.050020000"; assertEquals(expected, actual); } } From 68737d250d436db1a0196d8d516bed4c6cb92593 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Mon, 27 Jun 2022 10:56:32 -0700 Subject: [PATCH 3/6] unit test update --- .../sqlite/SQLiteModelFieldTypeConverterTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java index 67424cc513..69e59b7311 100644 --- a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -60,7 +60,7 @@ public void testConvertRawValueToTargetTimeAndroid() { */ @Test public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { - UserAgent.configure(Map.of(UserAgent.Platform.FLUTTER, "1.0")); + setUserAgent(); final String value = "16:00:00.050020000"; final JavaFieldType fieldType = JavaFieldType.TIME; final Gson gson = GsonFactory.instance(); @@ -72,13 +72,22 @@ public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { assertEquals(expected, actual); } + /** + * Set user agent to Flutter + * @throws AmplifyException not expected. + */ + private void setUserAgent() throws AmplifyException { + Map useragent = Map.of(UserAgent.Platform.FLUTTER, "1.0"); + UserAgent.configure(useragent); + } + /** * Test DATE_TIME converter for Flutter. * @throws AmplifyException Not expected. */ @Test public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException { - UserAgent.configure(Map.of(UserAgent.Platform.FLUTTER, "1.0")); + setUserAgent(); final String value = "2020-01-01T16:00:00.050020000"; final JavaFieldType fieldType = JavaFieldType.DATE_TIME; final Gson gson = GsonFactory.instance(); From afe42b534faf8272ef7076d1385446da4a26c757 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Mon, 27 Jun 2022 11:17:47 -0700 Subject: [PATCH 4/6] javadoc cleanup --- .../storage/sqlite/SQLiteModelFieldTypeConverterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java index 69e59b7311..f17cc42c56 100644 --- a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -73,7 +73,7 @@ public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { } /** - * Set user agent to Flutter + * Set user agent to Flutter. * @throws AmplifyException not expected. */ private void setUserAgent() throws AmplifyException { From ca340de0d67e835f7d0859e2a4db768a62e4c52a Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Mon, 27 Jun 2022 12:06:44 -0700 Subject: [PATCH 5/6] Unit test update --- .../storage/sqlite/SQLiteModelFieldTypeConverterTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java index f17cc42c56..966b82469a 100644 --- a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; +import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -77,8 +78,9 @@ public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { * @throws AmplifyException not expected. */ private void setUserAgent() throws AmplifyException { - Map useragent = Map.of(UserAgent.Platform.FLUTTER, "1.0"); - UserAgent.configure(useragent); + Map map = new HashMap<>(); + map.put(UserAgent.Platform.FLUTTER, "1.0"UserAgent.Platform.FLUTTER, "1.0"); + UserAgent.configure(map); } /** From c7a9aac5ed1df064107d5279de0efb03f5198713 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Mon, 27 Jun 2022 12:23:27 -0700 Subject: [PATCH 6/6] Update unit tests --- .../storage/sqlite/SQLiteModelFieldTypeConverterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java index 966b82469a..16673d3a4b 100644 --- a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -79,7 +79,7 @@ public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { */ private void setUserAgent() throws AmplifyException { Map map = new HashMap<>(); - map.put(UserAgent.Platform.FLUTTER, "1.0"UserAgent.Platform.FLUTTER, "1.0"); + map.put(UserAgent.Platform.FLUTTER, "1.0"); UserAgent.configure(map); }