You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sj...@apache.org on 2017/02/08 15:22:14 UTC

[3/4] brooklyn-server git commit: MultimapSerializer works with non-String keys

MultimapSerializer works with non-String keys


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/148ac04c
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/148ac04c
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/148ac04c

Branch: refs/heads/master
Commit: 148ac04c0655e2207171073f7bf4e5d1dba7e751
Parents: 79977e7
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Wed Feb 8 12:22:28 2017 +0000
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Wed Feb 8 14:21:00 2017 +0000

----------------------------------------------------------------------
 .../util/core/json/MultimapSerializer.java      |  2 +-
 .../util/core/json/MultimapSerializerTest.java  | 72 ++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/148ac04c/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java b/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
index 53ce3f5..a6522fa 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
@@ -56,7 +56,7 @@ public class MultimapSerializer extends StdSerializer<Multimap<?, ?>> {
 
     private void writeEntries(Multimap<?, ?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
         for (Map.Entry<?, ? extends Collection<?>> entry : value.asMap().entrySet()) {
-            provider.findKeySerializer(provider.constructType(String.class), null)
+            provider.findKeySerializer(provider.constructType(Object.class), null)
                     .serialize(entry.getKey(), jgen, provider);
             provider.defaultSerializeValue(Lists.newArrayList(entry.getValue()), jgen);
         }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/148ac04c/core/src/test/java/org/apache/brooklyn/util/core/json/MultimapSerializerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/util/core/json/MultimapSerializerTest.java b/core/src/test/java/org/apache/brooklyn/util/core/json/MultimapSerializerTest.java
new file mode 100644
index 0000000..902e377
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/util/core/json/MultimapSerializerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License 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 org.apache.brooklyn.util.core.json;
+
+import static org.testng.Assert.assertEquals;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.entity.stock.BasicEntity;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.fasterxml.jackson.core.Version;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.MultimapBuilder;
+
+public class MultimapSerializerTest extends BrooklynAppUnitTestSupport {
+
+    ObjectMapper mapper;
+
+    @Override
+    @BeforeMethod(alwaysRun = true)
+    public void setUp() throws Exception {
+        super.setUp();
+        mapper = new ObjectMapper();
+        SimpleModule mapperModule = new SimpleModule("MultimapSerializerTest", new Version(0, 0, 0, "ignored", null, null));
+        mapperModule.addSerializer(new MultimapSerializer());
+        mapper.registerModule(mapperModule);
+    }
+
+    @Test
+    public void testSerializeStringKey() throws Exception {
+        Multimap<String, Integer> map = MultimapBuilder.hashKeys().arrayListValues().build();
+        map.put("a", 1);
+        map.put("a", 2);
+        map.put("a", 3);
+        String json = mapper.writer().writeValueAsString(map);
+        assertEquals(json, "{\"a\":[1,2,3]}");
+    }
+
+    @Test
+    public void testSerializeEntityKey() throws Exception {
+        Entity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class));
+        Multimap<Entity, Integer> map = MultimapBuilder.hashKeys().arrayListValues().build();
+        map.put(entity, 1);
+        map.put(entity, 2);
+        map.put(entity, 3);
+        String json = mapper.writer().writeValueAsString(map);
+        assertEquals(json, "{\"BasicEntityImpl{id=" + entity.getId() + "}" + "\":[1,2,3]}");
+    }
+
+}
\ No newline at end of file