usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject [33/36] usergrid git commit: Ensure we persist entities with nested arrays. The nested data will be serialized as binary and not specifically typed.
Date Wed, 26 Aug 2015 15:40:44 GMT
Ensure we persist entities with nested arrays.  The nested data will be serialized as binary
and not specifically typed.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/2391bfd0
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/2391bfd0
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/2391bfd0

Branch: refs/heads/entity-size
Commit: 2391bfd09d69c2797e440beb638d8bfa3532df4f
Parents: 7318607
Author: Michael Russo <michaelarusso@gmail.com>
Authored: Tue Aug 25 13:36:24 2015 -0700
Committer: Michael Russo <michaelarusso@gmail.com>
Committed: Tue Aug 25 13:36:24 2015 -0700

----------------------------------------------------------------------
 .../corepersistence/CpEntityMapUtilsTest.java   | 79 ++++++++++++++++++++
 .../model/entity/MapToEntityConverter.java      | 10 ++-
 2 files changed, 88 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/2391bfd0/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
----------------------------------------------------------------------
diff --git a/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
b/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
index 5d492a0..1656fa6 100644
--- a/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
+++ b/stack/core/src/test/java/org/apache/usergrid/corepersistence/CpEntityMapUtilsTest.java
@@ -69,6 +69,37 @@ public class CpEntityMapUtilsTest {
         assertUserWithBlocks( cpEntity );
     }
 
+    @Test
+    public void testNestedArrayToMap() {
+
+        /*** This tests example property input of
+
+             {
+                "nestedarray" : [ [ "fred" ] ]
+             }
+
+         ****/
+
+        Map<String, Object> properties = new LinkedHashMap<String, Object>()
{{
+            put( "nestedarray",
+                new ArrayList<ArrayList<String>>() {{
+                    add(0, new ArrayList<String>() {{
+                        add(0, "fred");
+                        }});
+                }}
+            );
+            put( "block", new ArrayList<Object>() {{
+                add( new LinkedHashMap<String, Object>() {{ put("name", "fred"); }});
+                add( new LinkedHashMap<String, Object>() {{ put("name", "gertrude");
}});
+                add( new LinkedHashMap<String, Object>() {{ put("name", "mina"); }});
+            }});
+        }};
+
+        Entity cpEntity = CpEntityMapUtils.fromMap( properties, "user", true );
+        assertUserWithBlocks( cpEntity );
+    }
+
+
 
     @Test
     public void testSerialization() throws JsonProcessingException, IOException {
@@ -112,6 +143,54 @@ public class CpEntityMapUtilsTest {
     }
 
 
+    @Test
+    public void testNestedArraySerialization() throws JsonProcessingException, IOException
{
+
+        /*** This tests example property input of
+
+         {
+         "nestedarray" : [ [ "fred" ] ]
+         }
+
+         ****/
+
+        Map<String, Object> properties = new LinkedHashMap<String, Object>()
{{
+            put( "nestedarray",
+                new ArrayList<ArrayList<String>>() {{
+                    add(0, new ArrayList<String>() {{
+                        add(0, "fred");
+                    }});
+                }}
+            );
+            put( "block", new ArrayList<Object>() {{
+                add( new LinkedHashMap<String, Object>() {{ put("name", "fred"); }});
+                add( new LinkedHashMap<String, Object>() {{ put("name", "gertrude");
}});
+                add( new LinkedHashMap<String, Object>() {{ put("name", "mina"); }});
+            }});
+        }};
+
+        org.apache.usergrid.persistence.model.entity.Entity entity =
+            new org.apache.usergrid.persistence.model.entity.Entity(
+                new SimpleId( "user" ) );
+        entity = CpEntityMapUtils.fromMap( entity, properties, null, true );
+
+        assertUserWithBlocks( entity );
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT,
"@class");
+
+        String entityString = mapper.writeValueAsString( entity );
+        //log.debug("Serialized to JSON: " + entityString );
+
+        TypeReference<Entity> tr = new TypeReference<Entity>() {};
+        entity = mapper.readValue( entityString, tr );
+        //log.debug("Round-tripped entity: " + CpEntityMapUtils.toMap(entity) );
+
+        assertUserWithBlocks( entity );
+    }
+
+
     private void assertUserWithBlocks( org.apache.usergrid.persistence.model.entity.Entity
e ) {
 
         assertTrue( e.getField("block") instanceof ListField );

http://git-wip-us.apache.org/repos/asf/usergrid/blob/2391bfd0/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
index 79f8973..323d424 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/MapToEntityConverter.java
@@ -145,7 +145,15 @@ public class MapToEntityConverter{
             return newList;
 
         } else if ( sample instanceof List ) {
-            return processListForField( list ); // recursion
+            List<Object> newList = new ArrayList<Object>();
+            for (Object o : list) {
+                if (o instanceof List) {
+                    newList.add(processListForField((List) o));
+                } else {
+                    newList.add(o);
+                }
+            }
+            return newList;
 
         } else {
             return list;


Mime
View raw message