JsonUtils for JSON format. 91/37291/1
authorYuanHu <yuan.hu1@zte.com.cn>
Wed, 21 Mar 2018 01:30:16 +0000 (09:30 +0800)
committerYuanHu <yuan.hu1@zte.com.cn>
Wed, 21 Mar 2018 01:30:16 +0000 (09:30 +0800)
JsonUtils for JSON format.

Issue-ID: SDC-1079

Change-Id: I8879652751a0c0ae9d2597e33997fc32a9baa816
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/entity/I18nString.java
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/JsonUtils.java [new file with mode: 0644]
sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResourceTest.java
sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/JsonUtilsTest.java [new file with mode: 0644]

index 10269a3..600ec8c 100644 (file)
@@ -26,12 +26,12 @@ import org.eclipse.jetty.http.HttpStatus;
 import org.onap.sdc.workflowdesigner.resources.entity.ExtActivityDisplayInfo;
 import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;
 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
+import org.onap.sdc.workflowdesigner.utils.JsonUtils;
 import org.onap.sdc.workflowdesigner.utils.RestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.codahale.metrics.annotation.Timed;
-import com.google.gson.Gson;
 
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -90,8 +90,7 @@ public class ExtendActivityResource {
    */
   private ExtendActivity[] retriveExtActivites(String sence) throws IOException {
     String json = FileCommonUtils.readString(EXT_ACTIVITIES_FILE_NAME);
-    Gson gson = new Gson();
-    return gson.fromJson(json, ExtendActivity[].class);
+    return JsonUtils.fromJson(json, ExtendActivity[].class);
   }
   
 
@@ -125,8 +124,7 @@ public class ExtendActivityResource {
    */
   private ExtActivityDisplayInfo retriveDisplayInfo(String sence) throws IOException {
     String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
-    Gson gson = new Gson();
-    return gson.fromJson(json, ExtActivityDisplayInfo.class);
+    return JsonUtils.fromJson(json, ExtActivityDisplayInfo.class);
   }
 
 }
index 1dc880d..1ed14f6 100644 (file)
@@ -19,6 +19,23 @@ public class I18nString {
   \r
   private String zh_CN;\r
 \r
+  /**\r
+   * \r
+   */\r
+  public I18nString() {\r
+    super();\r
+  }\r
+\r
+  /**\r
+   * @param en_US\r
+   * @param zh_CN\r
+   */\r
+  public I18nString(String en_US, String zh_CN) {\r
+    super();\r
+    this.en_US = en_US;\r
+    this.zh_CN = zh_CN;\r
+  }\r
+\r
   /**\r
    * @return the en_US\r
    */\r
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/JsonUtils.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/JsonUtils.java
new file mode 100644 (file)
index 0000000..09588ef
--- /dev/null
@@ -0,0 +1,41 @@
+/**\r
+ * Copyright (c) 2018 ZTE Corporation.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the Apache License, Version 2.0\r
+ * and the Eclipse Public License v1.0 which both accompany this distribution,\r
+ * and are available at http://www.eclipse.org/legal/epl-v10.html\r
+ * and http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Contributors:\r
+ *     ZTE - initial API and implementation and/or initial documentation\r
+ */\r
+package org.onap.sdc.workflowdesigner.utils;\r
+\r
+import com.google.gson.Gson;\r
+\r
+/**\r
+ *\r
+ */\r
+public class JsonUtils {\r
+  /**\r
+   * \r
+   * @param json\r
+   * @param clazz\r
+   * @return\r
+   */\r
+  public static <T> T fromJson(String json, Class<T> clazz) {\r
+    Gson gson = new Gson();\r
+    return gson.fromJson(json, clazz);\r
+  }\r
+\r
+  /**\r
+   * \r
+   * @param t\r
+   * @return\r
+   */\r
+  public static <T> String toJson(T t) {\r
+    Gson gson = new Gson();\r
+    return gson.toJson(t);\r
+  }\r
+  \r
+}\r
index 8d3fd5d..18a8597 100644 (file)
@@ -20,8 +20,7 @@ import org.junit.Before;
 import org.junit.Test;\r
 import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;\r
 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;\r
-\r
-import com.google.gson.Gson;\r
+import org.onap.sdc.workflowdesigner.utils.JsonUtils;\r
 \r
 /**\r
  *\r
@@ -49,8 +48,7 @@ public class ExtendActivityResourceTest {
     try {\r
       Response response = resource.getExtActivities("test");\r
       ExtendActivity[] extActivities = (ExtendActivity[]) response.getEntity();\r
-      Gson gson = new Gson();\r
-      FileCommonUtils.write("test.json", gson.toJson(extActivities));\r
+      FileCommonUtils.write("test.json", JsonUtils.toJson(extActivities));\r
       assertEquals(extActivities.length == 0, false);\r
     } catch (Exception e) {\r
       e.printStackTrace();\r
diff --git a/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/JsonUtilsTest.java b/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/JsonUtilsTest.java
new file mode 100644 (file)
index 0000000..2672f6b
--- /dev/null
@@ -0,0 +1,59 @@
+/**\r
+ * Copyright (c) 2018 ZTE Corporation.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the Apache License, Version 2.0\r
+ * and the Eclipse Public License v1.0 which both accompany this distribution,\r
+ * and are available at http://www.eclipse.org/legal/epl-v10.html\r
+ * and http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Contributors:\r
+ *     ZTE - initial API and implementation and/or initial documentation\r
+ */\r
+package org.onap.sdc.workflowdesigner.utils;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.sdc.workflowdesigner.resources.entity.I18nString;\r
+\r
+/**\r
+ *\r
+ */\r
+public class JsonUtilsTest {\r
+\r
+  /**\r
+   * @throws java.lang.Exception\r
+   */\r
+  @Before\r
+  public void setUp() throws Exception {}\r
+\r
+  /**\r
+   * @throws java.lang.Exception\r
+   */\r
+  @After\r
+  public void tearDown() throws Exception {}\r
+\r
+  /**\r
+   * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#fromJson(java.lang.String, java.lang.Class)}.\r
+   */\r
+  @Test\r
+  public void testFromJson() {\r
+    String i18n = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";\r
+    I18nString i18nString = JsonUtils.fromJson(i18n, I18nString.class);\r
+    assertNotNull(i18nString);\r
+  }\r
+\r
+  /**\r
+   * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#toJson(java.lang.Object)}.\r
+   */\r
+  @Test\r
+  public void testToJson() {\r
+    I18nString  i18nString = new I18nString("Service Task", "Service Task");\r
+    String i18n = JsonUtils.toJson(i18nString);\r
+    String expect = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";\r
+    assertEquals(expect, i18n);\r
+  }\r
+\r
+}\r