polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [5/5] polygene-java git commit: Add coverage for JSON (de)serialization of Maps
Date Mon, 03 Apr 2017 08:09:41 GMT
Add coverage for JSON (de)serialization of Maps

POLYGENE-231


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/5296c355
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/5296c355
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/5296c355

Branch: refs/heads/develop
Commit: 5296c3551fbb8dc15107dc9e9ff28ab0c1d684a9
Parents: 6195ec9
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon Apr 3 10:08:58 2017 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon Apr 3 10:08:58 2017 +0200

----------------------------------------------------------------------
 .../JavaxJsonCollectionSerializationTest.java   | 176 +++++++++++++++++++
 1 file changed, 176 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/5296c355/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java
----------------------------------------------------------------------
diff --git a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java
b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java
index 517b630..0df54dc 100644
--- a/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java
+++ b/extensions/serialization-javaxjson/src/test/java/org/apache/polygene/serialization/javaxjson/JavaxJsonCollectionSerializationTest.java
@@ -17,8 +17,184 @@
  */
 package org.apache.polygene.serialization.javaxjson;
 
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonValue;
+import org.apache.polygene.api.injection.scope.Service;
+import org.apache.polygene.api.property.Property;
+import org.apache.polygene.api.serialization.SerializationException;
+import org.apache.polygene.api.type.MapType;
+import org.apache.polygene.api.type.ValueCompositeType;
+import org.apache.polygene.api.type.ValueType;
+import org.apache.polygene.api.value.ValueBuilder;
+import org.apache.polygene.bootstrap.ModuleAssembly;
+import org.apache.polygene.serialization.javaxjson.assembly.JavaxJsonSerializationAssembler;
+import org.apache.polygene.spi.serialization.JsonSerialization;
 import org.apache.polygene.test.serialization.AbstractCollectionSerializationTest;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 public class JavaxJsonCollectionSerializationTest extends AbstractCollectionSerializationTest
 {
+    @Override
+    public void assemble( ModuleAssembly module )
+    {
+        new JavaxJsonSerializationAssembler().assemble( module );
+        module.values( SomeValue.class );
+        super.assemble( module );
+    }
+
+    public interface SomeValue
+    {
+        Property<String> foo();
+    }
+
+    @Service
+    private JsonSerialization jsonSerialization;
+
+    @Service
+    private JavaxJsonFactories jsonFactories;
+
+    @Test
+    public void serializeMapWithStringKeysAsJsonObject()
+    {
+        Map<String, String> map = new HashMap<>();
+        map.put( "foo", "bar" );
+        map.put( "baz", "bazar" );
+
+        JsonValue json = jsonSerialization.toJson( map );
+        assertThat( json.getValueType(), is( JsonValue.ValueType.OBJECT ) );
+
+        JsonObject jsonObject = (JsonObject) json;
+        assertThat( jsonObject.getString( "foo" ), equalTo( "bar" ) );
+        assertThat( jsonObject.getString( "baz" ), equalTo( "bazar" ) );
+
+        MapType mapType = MapType.of( ValueType.STRING, ValueType.STRING );
+        Map<String, String> map2 = jsonSerialization.fromJson( module, mapType, json
);
+        assertThat( map2, equalTo( map ) );
+    }
+
+    @Test
+    public void deserializeJsonArrayOfEntryObjectsAsMapWithStringKeys()
+    {
+        JsonObjectBuilder fooEntryBuilder = jsonFactories.builderFactory().createObjectBuilder();
+        fooEntryBuilder.add( "key", "foo" );
+        fooEntryBuilder.add( "value", "bar" );
+        JsonObject fooEntry = fooEntryBuilder.build();
+
+        JsonObjectBuilder bazEntryBuilder = jsonFactories.builderFactory().createObjectBuilder();
+        bazEntryBuilder.add( "key", "baz" );
+        bazEntryBuilder.add( "value", "bazar" );
+        JsonObject bazEntry = bazEntryBuilder.build();
+
+        JsonArrayBuilder arrayBuilder = jsonFactories.builderFactory().createArrayBuilder();
+        arrayBuilder.add( fooEntry );
+        arrayBuilder.add( bazEntry );
+        JsonArray jsonArray = arrayBuilder.build();
+
+        MapType mapType = MapType.of( ValueType.STRING, ValueType.STRING );
+        Map<String, String> map = jsonSerialization.fromJson( module, mapType, jsonArray
);
+
+        assertThat( map.get( "foo" ), equalTo( "bar" ) );
+        assertThat( map.get( "baz" ), equalTo( "bazar" ) );
+    }
+
+    @Test
+    public void serializeMapWithComplexKeyToJsonArrayOfEntryObjects()
+    {
+        Map<SomeValue, String> map = new LinkedHashMap<>();
+        map.put( newSomeValue( "foo" ), "bar" );
+        map.put( newSomeValue( "baz" ), "bazar" );
+
+        JsonValue json = jsonSerialization.toJson( map );
+        assertThat( json.getValueType(), is( JsonValue.ValueType.ARRAY ) );
+
+        JsonArray jsonArray = (JsonArray) json;
+        JsonObject fooEntry = jsonArray.getJsonObject( 0 );
+        JsonObject bazEntry = jsonArray.getJsonObject( 1 );
+        assertThat( fooEntry.getJsonObject( "key" ).getString( "foo" ), equalTo( "foo" )
);
+        assertThat( fooEntry.getString( "value" ), equalTo( "bar" ) );
+        assertThat( bazEntry.getJsonObject( "key" ).getString( "foo" ), equalTo( "baz" )
);
+        assertThat( bazEntry.getString( "value" ), equalTo( "bazar" ) );
+
+        MapType mapType = MapType.of( ValueCompositeType.of( api.valueDescriptorFor( map.keySet().iterator().next()
) ),
+                                      ValueType.STRING );
+        Map<SomeValue, String> map2 = jsonSerialization.fromJson( module, mapType,
json );
+        assertThat( map2, equalTo( map ) );
+    }
+
+    @Test
+    public void serializeMapWithMixedKeysAsJsonArrayOfEntryObjects()
+    {
+        Map<Object, String> map = new LinkedHashMap<>();
+        map.put( "foo", "bar" );
+        map.put( newSomeValue( "baz" ), "bazar" );
+
+        JsonValue json = jsonSerialization.toJson( map );
+        assertThat( json.getValueType(), is( JsonValue.ValueType.ARRAY ) );
+
+        JsonArray jsonArray = (JsonArray) json;
+        JsonObject fooEntry = jsonArray.getJsonObject( 0 );
+        JsonObject bazEntry = jsonArray.getJsonObject( 1 );
+        assertThat( fooEntry.getString( "key" ), equalTo( "foo" ) );
+        assertThat( fooEntry.getString( "value" ), equalTo( "bar" ) );
+        assertThat( bazEntry.getJsonObject( "key" ).getString( "foo" ), equalTo( "baz" )
);
+        assertThat( bazEntry.getString( "value" ), equalTo( "bazar" ) );
+
+        MapType mapType = MapType.of( ValueType.OBJECT, ValueType.STRING );
+        Map<Object, String> map2 = jsonSerialization.fromJson( module, mapType, json
);
+        assertThat( map2, equalTo( map ) );
+    }
+
+    @Test
+    public void dontKnowHowToDeserializeMapWithMixedKeys()
+    {
+        JsonObjectBuilder fooKeyBuilder = jsonFactories.builderFactory().createObjectBuilder();
+        fooKeyBuilder.add( "foo", "foo" );
+        fooKeyBuilder.add( "_type", SomeValue.class.getName() );
+        JsonObject fooKey = fooKeyBuilder.build();
+
+        JsonObjectBuilder fooEntryBuilder = jsonFactories.builderFactory().createObjectBuilder();
+        fooEntryBuilder.add( "key", fooKey );
+        fooEntryBuilder.add( "value", "bar" );
+        JsonObject fooEntry = fooEntryBuilder.build();
+
+        JsonObjectBuilder bazEntryBuilder = jsonFactories.builderFactory().createObjectBuilder();
+        bazEntryBuilder.add( "key", "baz" );
+        bazEntryBuilder.add( "value", "bazar" );
+        JsonObject bazEntry = bazEntryBuilder.build();
+
+        JsonArrayBuilder arrayBuilder = jsonFactories.builderFactory().createArrayBuilder();
+        arrayBuilder.add( fooEntry );
+        arrayBuilder.add( bazEntry );
+        JsonArray jsonArray = arrayBuilder.build();
+
+        MapType mapType = MapType.of( ValueType.OBJECT, ValueType.STRING );
+        try
+        {
+            jsonSerialization.fromJson( module, mapType, jsonArray );
+            fail( "Should have failed deserialization" );
+        }
+        catch( SerializationException ex )
+        {
+            assertThat( ex.getMessage(),
+                        equalTo( "Don't know how to deserialize java.lang.Object from \"baz\""
) );
+        }
+    }
+
+    private SomeValue newSomeValue( String foo )
+    {
+        ValueBuilder<SomeValue> builder = valueBuilderFactory.newValueBuilder( SomeValue.class
);
+        builder.prototype().foo().set( foo );
+        return builder.newInstance();
+    }
 }


Mime
View raw message