JooHyukKim commented on code in PR #4790:
URL: https://github.com/apache/zeppelin/pull/4790#discussion_r1729131501


##########
zeppelin-interpreter/src/test/java/org/apache/zeppelin/cluster/event/ClusterMessageSerDeserTest.java:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.zeppelin.cluster.event;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class ClusterMessageSerDeserTest {
+
+    private ClusterMessage clusterMessage;
+
+    @BeforeEach
+    void setUp() {
+        clusterMessage = new 
ClusterMessage(ClusterEvent.BROADCAST_NEW_PARAGRAPH);
+    }
+
+    @Test
+    void toJson() {
+        // Given
+        clusterMessage.put("key1", "value1");
+        clusterMessage.put("key2", "value2");
+        clusterMessage.setMsgId(UUID.randomUUID().toString());
+
+        // First, let's check the serialized JSON serialization
+        String serializedJSON = 
ClusterMessage.serializeMessage(clusterMessage);
+        assertEquals(
+                a2q("{\n" +
+                        "  'clusterEvent': 'BROADCAST_NEW_PARAGRAPH',\n" +
+                        "  'data': {\n" +
+                        "    'key1': 'value1',\n" +
+                        "    'key2': 'value2'\n" +
+                        "  },\n" +
+                        "  'msgId': '" + clusterMessage.getMsgId() + "'\n" +
+                        "}"),
+                serializedJSON);
+
+        // Now, let's check the serialized JSON deserialization
+        ClusterMessage deserialized = 
ClusterMessage.deserializeMessage(serializedJSON);
+        assertEquals(clusterMessage.clusterEvent, deserialized.clusterEvent);
+        assertEquals(clusterMessage.getData(), deserialized.getData());
+        assertEquals(clusterMessage.getMsgId(), deserialized.getMsgId());
+    }
+
+    @Test
+    void fromJson_withInvalidField() {
+        String invalidJson = a2q("{" +
+                "'clusterEvent':'SET_RUNNERS_PERMISSIONS'," +
+                "'data':{'key1':'value1','key2':'value2'}," +
+                "'invalidField':'invalidValue'" +
+                "}");
+
+        ClusterMessage deserialized = 
ClusterMessage.deserializeMessage(invalidJson);
+        assertEquals(ClusterEvent.SET_RUNNERS_PERMISSIONS, 
deserialized.clusterEvent);
+        assertEquals(mapOf("key1", "value1", "key2", "value2"), 
deserialized.getData());
+        assertNull(deserialized.getMsgId());
+    }
+
+    @Test
+    void fromJson_withNothing() {
+        // When only the empty JSON object is provided,
+        ClusterMessage deserialized = ClusterMessage.deserializeMessage("{}");
+
+        // Then, all fields should be null or empty
+        assertNull(deserialized.clusterEvent);
+        assertNull(deserialized.getData());
+        assertNull(deserialized.getMsgId());
+    }
+
+    @Test
+    void fromJson_withNullFields() {
+        String jsonWithNullFields = a2q("{" +
+                "'clusterEvent':null," +
+                "'data':null," +
+                "'msgId':null" +
+                "}");
+
+        ClusterMessage deserialized = 
ClusterMessage.deserializeMessage(jsonWithNullFields);
+        assertNull(deserialized.clusterEvent);
+        assertNull(deserialized.getData());
+        assertNull(deserialized.getMsgId());
+    }
+
+    @Test
+    void toJson_withEmptyData() {
+        ClusterMessage original = new 
ClusterMessage(ClusterEvent.CLEAR_PERMISSION);
+        original.setMsgId("");
+        original.getData().clear();
+
+        String serializedJSON = ClusterMessage.serializeMessage(original);
+        assertEquals(
+                a2q("{\n" +
+                        "  'clusterEvent': 'CLEAR_PERMISSION',\n" +
+                        "  'data': {},\n" +
+                        "  'msgId': ''\n" +
+                        "}"),
+                serializedJSON);
+
+        ClusterMessage deserialized = 
ClusterMessage.deserializeMessage(serializedJSON);
+        assertEquals(original.clusterEvent, deserialized.clusterEvent);
+        assertEquals(original.getData(), deserialized.getData());
+        assertEquals(original.getMsgId(), deserialized.getMsgId());
+    }
+
+    @Test
+    void fromJson_withLargeDataSet() {
+        Map<String, String> largeData = new HashMap<>();
+        for (int i = 0; i < 1000; i++) {
+            largeData.put("key" + i, "value" + i);
+        }
+
+        ClusterMessage original = new 
ClusterMessage(ClusterEvent.SET_READERS_PERMISSIONS);
+        original.setMsgId(UUID.randomUUID().toString());
+        original.put(largeData);
+
+        String serializedJSON = ClusterMessage.serializeMessage(original);
+        ClusterMessage deserialized = 
ClusterMessage.deserializeMessage(serializedJSON);
+
+        assertEquals(original.clusterEvent, deserialized.clusterEvent);
+        assertEquals(original.getData(), deserialized.getData());
+        assertEquals(original.getMsgId(), deserialized.getMsgId());
+    }
+
+    private Map<String, String> mapOf(
+            String key, String value,
+            String key2, String value2
+    ) {
+        Map<String, String> map = new HashMap<>();
+        map.put(key, value);
+        map.put(key2, value2);
+        return map;
+    }
+
+    private static String a2q(String json) {
+        return json.replace("'", "\"");
+    }

Review Comment:
   > Is a2q really necessary? If not, I think using a pure JSON format without 
a2q could make the test clearer. Keeping the test focused on the target, which 
in this case is JSON serialization/deserialization, could help keep the test 
simple. 
   
   Hmmm..  You are right method `a2q()` sure is not necessary. But from a 
different point of view, `a2q()` is just a little helper method to make it 
easier to read the json input so we can focus more on JSON 
serialization/deserialization. Some people do use helper methods to actually 
focus on the SUT in this case the JSON serialization/deserialization. 
   
   What I would like to see from you is some guidelines or references on how 
helper methods should be written so tests are more on focus.
   
   > Also, in this PR HeliumRegistrySerializerTest does not use a2q.
   
   Agreed 👏🏽👏🏽 I will use `a2q` in all tests introduced in this PR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to