Github user galen-pivotal commented on a diff in the pull request: https://github.com/apache/geode/pull/737#discussion_r135157183 --- Diff: geode-protobuf/src/test/java/org/apache/geode/serialization/codec/JSONCodecJUnitTest.java --- @@ -0,0 +1,226 @@ +/* + * 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.geode.serialization.codec; + +import static java.util.Arrays.asList; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.CacheFactory; +import org.apache.geode.distributed.ConfigurationProperties; +import org.apache.geode.internal.cache.GemFireCacheImpl; +import org.apache.geode.internal.cache.InternalCache; +import org.apache.geode.pdx.JSONFormatter; +import org.apache.geode.pdx.PdxInstance; +import org.apache.geode.pdx.PdxInstanceFactory; +import org.apache.geode.pdx.WritablePdxInstance; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; + +@Category(UnitTest.class) +public class JSONCodecJUnitTest { + + private String complexJSONString = "{\n" + " \"_id\": \"599c7d885df276ac3e0bf10a\",\n" + + " \"index\": 0,\n" + " \"guid\": \"395902d8-36ed-4178-ad70-2f720c557c55\",\n" + + " \"isActive\": true,\n" + " \"balance\": \"$3,152.82\",\n" + + " \"picture\": \"http://placehold.it/32x32\",\n" + " \"age\": 27,\n" + + " \"eyeColor\": \"blue\",\n" + " \"name\": \"Kristina Norman\",\n" + + " \"gender\": \"female\",\n" + " \"company\": \"ORBALIX\",\n" + + " \"email\": \"kristinanor...@orbalix.com\",\n" + + " \"phone\": \"+1 (983) 526-3433\",\n" + + " \"address\": \"400 Vermont Court, Denio, Wyoming, 7142\",\n" + + " \"about\": \"Mollit nostrud irure excepteur veniam aliqua. Non id tempor magna nisi ipsum minim. Culpa velit tempor culpa mollit cillum deserunt nisi culpa irure ut nostrud enim consectetur voluptate. Elit veniam velit enim minim. Sunt nostrud ea duis enim sit cillum.\",\n" + + " \"registered\": \"2015-03-11T02:22:45 +07:00\",\n" + " \"latitude\": -0.853065,\n" + + " \"longitude\": -29.749358,\n" + " \"tags\": [\n" + " \"laboris\",\n" + + " \"velit\",\n" + " \"non\",\n" + " \"est\",\n" + " \"anim\",\n" + + " \"amet\",\n" + " \"cupidatat\"\n" + " ],\n" + " \"friends\": [\n" + + " {\n" + " \"id\": 0,\n" + " \"name\": \"Roseann Roy\"\n" + " },\n" + + " {\n" + " \"id\": 1,\n" + " \"name\": \"Adriana Perry\"\n" + + " },\n" + " {\n" + " \"id\": 2,\n" + + " \"name\": \"Tyler Mccarthy\"\n" + " }\n" + " ],\n" + + " \"greeting\": \"Hello, Kristina Norman! You have 8 unread messages.\",\n" + + " \"favoriteFruit\": \"apple\"\n" + " }"; + private Cache cache; + + @Before + public void setUp() throws Exception { + CacheFactory cacheFactory = new CacheFactory(); + cacheFactory.set(ConfigurationProperties.MCAST_PORT, "0"); + cacheFactory.set(ConfigurationProperties.USE_CLUSTER_CONFIGURATION, "false"); + cacheFactory.set(ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION, "false"); + cache = cacheFactory.create(); + } + + @After + public void tearDown() throws Exception { + if (cache != null) { + cache.close(); + } + } + + @Test + public void testSimpleJSONEncode() throws Exception { + PdxInstanceFactory pdxInstanceFactory = + ((GemFireCacheImpl) cache).createPdxInstanceFactory(JSONFormatter.JSON_CLASSNAME, false); + + pdxInstanceFactory.writeString("string", "someString"); + pdxInstanceFactory.writeBoolean("boolean", true); + PdxInstance pdxInstance = pdxInstanceFactory.create(); + + byte[] encodedJSONByte = new JSONCodec().encode(pdxInstance); + + String expectedJsonString = + "{\n" + " \"string\" : \"someString\",\n" + " \"boolean\" : true\n" + "}"; + assertArrayEquals(expectedJsonString.getBytes(), encodedJSONByte); + } + + @Test + public void testComplexJSONEncode() { + PdxInstance pdxInstanceForComplexJSONString = createPDXInstanceForComplexJSONString(); + PdxInstance decodedJSONPdxInstance = new JSONCodec().decode(complexJSONString.getBytes()); + + assertEquals(pdxInstanceForComplexJSONString.getFieldNames(), + decodedJSONPdxInstance.getFieldNames()); + + List<String> fieldNames = asList("_id", "index", "guid", "isActive", "balance", "picture", + "age", "eyeColor", "name", "gender", "company", "email", "phone", "address", "about", + "registered", "latitude", "longitude", "tags", "friends", "greeting", "favoriteFruit"); + fieldNames.forEach( + fieldName -> assertEquals(pdxInstanceForComplexJSONString.getField(fieldName).getClass(), --- End diff -- If `fieldnames.forEach(..)` was refactored to a `PDXInstancesEqual` method, I think it might make the intent here as well as the meaning of `pdxFieldValues` clearer.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---