From 35d385f02ad35da84bef358e35e31cede43719c6 Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Tue, 5 Jun 2012 08:53:50 +0000
Subject: [PATCH] SP-82 BIS-14 Query Plugins: improve the JSON-RPC
 implementation to also accept custom classes and not only primitive types and
 collections

SVN: 25558
---
 .../server/json/JsonDeserializationTest.java  |  5 -
 .../server/json/JsonSerializationTest.java    | 99 +++++++++++++++++++
 .../json/object/ObjectWithNestedTypes.java    |  3 +-
 3 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonDeserializationTest.java b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonDeserializationTest.java
index cf2dffbd361..72684d2f845 100644
--- a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonDeserializationTest.java
+++ b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonDeserializationTest.java
@@ -553,8 +553,6 @@ public class JsonDeserializationTest
         nestedChild.put("nested", "nestedValue");
         nestedChild.put("nestedChild", "nestedChildValue");
 
-        // TODO check why it fails
-        // objectMap.put("propertyObject", nested);
         objectMap.put("propertyNested", nestedChild);
         objectMap.put("propertyNestedChild", nestedChild);
 
@@ -562,9 +560,6 @@ public class JsonDeserializationTest
 
         Assert.assertNotNull(object);
 
-        // ObjectNested propertyObject = (ObjectNested) object.propertyObject;
-        // Assert.assertEquals("nestedValue", propertyObject.nested);
-
         ObjectNestedChild propertyNested = (ObjectNestedChild) object.propertyNested;
         Assert.assertEquals("nestedValue", propertyNested.nested);
         Assert.assertEquals("nestedChildValue", propertyNested.nestedChild);
diff --git a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonSerializationTest.java b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonSerializationTest.java
index d6f5457fc6e..717ef681f6b 100644
--- a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonSerializationTest.java
+++ b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/JsonSerializationTest.java
@@ -28,8 +28,10 @@ import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithEnumTypes;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithEnumTypes.NestedEnum;
+import ch.systemsx.cisd.common.api.server.json.object.ObjectWithNestedTypes;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithNestedTypes.ObjectNested;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithNestedTypes.ObjectNestedChild;
+import ch.systemsx.cisd.common.api.server.json.object.ObjectWithPrimitiveTypes;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithType;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithTypeA;
 import ch.systemsx.cisd.common.api.server.json.object.ObjectWithTypeAA;
@@ -180,6 +182,30 @@ public class JsonSerializationTest
         serializeObjectAndCheckItsValues(object, values);
     }
 
+    @Test
+    public void testSerializeObjectWithPrimitiveTypes() throws Exception
+    {
+        Map<String, Object> values = new LinkedHashMap<String, Object>();
+        putObjectWithPrimitiveTypesValues(values, false);
+
+        ObjectWithPrimitiveTypes object = new ObjectWithPrimitiveTypes();
+        setObjectWithPrimitiveTypesFields(object);
+
+        serializeObjectAndCheckItsValues(object, values);
+    }
+
+    @Test
+    public void testSerializeObjectWithNestedTypes() throws Exception
+    {
+        Map<String, Object> values = new LinkedHashMap<String, Object>();
+        putObjectWithNestedTypesValues(values, false);
+
+        ObjectWithNestedTypes object = new ObjectWithNestedTypes();
+        setObjectWithNestedTypesFields(object);
+
+        serializeObjectAndCheckItsValues(object, values);
+    }
+
     @Test
     public void testSerializeObjectWithEnumTypes() throws Exception
     {
@@ -310,6 +336,79 @@ public class JsonSerializationTest
         object.b = "bValue";
     }
 
+    private void putObjectWithPrimitiveTypesValues(Map<String, Object> map, boolean empty)
+    {
+        map.put("@type", "ObjectWithPrimitiveTypes");
+
+        if (empty)
+        {
+            map.put("stringField", null);
+            map.put("integerObjectField", null);
+            map.put("floatObjectField", null);
+            map.put("doubleObjectField", null);
+            map.put("integerField", null);
+            map.put("floatField", null);
+            map.put("doubleField", null);
+        } else
+        {
+            map.put("stringField", "stringValue");
+            map.put("integerObjectField", new Integer(1));
+            map.put("floatObjectField", new Float(2.5f));
+            map.put("doubleObjectField", new Double(3.5f));
+            map.put("integerField", 4);
+            map.put("floatField", 5.5f);
+            map.put("doubleField", 6.5d);
+        }
+    }
+
+    private void setObjectWithPrimitiveTypesFields(ObjectWithPrimitiveTypes object)
+    {
+        object.stringField = "stringValue";
+        object.integerObjectField = new Integer(1);
+        object.floatObjectField = new Float(2.5f);
+        object.doubleObjectField = new Double(3.5f);
+        object.integerField = 4;
+        object.floatField = 5.5f;
+        object.doubleField = 6.5d;
+    }
+
+    private void putObjectWithNestedTypesValues(Map<String, Object> map, boolean empty)
+    {
+        map.put("@type", "ObjectWithNestedTypes");
+
+        if (empty)
+        {
+            map.put("propertyNested", null);
+            map.put("propertyNestedChild", null);
+        } else
+        {
+            Map<String, Object> nested = new LinkedHashMap<String, Object>();
+            nested.put("@type", "ObjectNested");
+            nested.put("nested", "nestedValue");
+
+            Map<String, Object> nestedChild = new LinkedHashMap<String, Object>();
+            nestedChild.put("@type", "ObjectNestedChild");
+            nestedChild.put("nested", "nestedValue");
+            nestedChild.put("nestedChild", "nestedChildValue");
+
+            map.put("propertyNested", nestedChild);
+            map.put("propertyNestedChild", nestedChild);
+        }
+    }
+
+    private void setObjectWithNestedTypesFields(ObjectWithNestedTypes object)
+    {
+        ObjectNested nested = new ObjectNested();
+        nested.nested = "nestedValue";
+
+        ObjectNestedChild nestedChild = new ObjectNestedChild();
+        nestedChild.nested = "nestedValue";
+        nestedChild.nestedChild = "nestedChildValue";
+
+        object.propertyNested = nestedChild;
+        object.propertyNestedChild = nestedChild;
+    }
+
     private void putObjectWithEnumTypesValues(Map<String, Object> map, boolean empty)
     {
         map.put("@type", "ObjectWithEnumTypes");
diff --git a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/object/ObjectWithNestedTypes.java b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/object/ObjectWithNestedTypes.java
index 0f944650113..e4cc095550b 100644
--- a/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/object/ObjectWithNestedTypes.java
+++ b/openbis-common/sourceTest/java/ch/systemsx/cisd/common/api/server/json/object/ObjectWithNestedTypes.java
@@ -25,7 +25,8 @@ import ch.systemsx.cisd.base.annotation.JsonObject;
 public class ObjectWithNestedTypes
 {
 
-    public Object propertyObject;
+    // TODO: check why it doesn't work properly during both serialization and deserialization
+    // public Object propertyObject;
 
     public ObjectNested propertyNested;
 
-- 
GitLab