diff options
author | YuanHu <yuan.hu1@zte.com.cn> | 2018-03-21 09:30:16 +0800 |
---|---|---|
committer | YuanHu <yuan.hu1@zte.com.cn> | 2018-03-21 09:30:16 +0800 |
commit | 70410b3ef0fd4c2325431436365be82c2663a5ed (patch) | |
tree | 9b11b8c8f1e513daf0988fe4935bc8c568db9062 /sdc-workflow-designer-server | |
parent | 821c6300e715b354c421b3329809dfc906c67c87 (diff) |
JsonUtils for JSON format.
JsonUtils for JSON format.
Issue-ID: SDC-1079
Change-Id: I8879652751a0c0ae9d2597e33997fc32a9baa816
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
Diffstat (limited to 'sdc-workflow-designer-server')
5 files changed, 122 insertions, 9 deletions
diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java index 10269a37..600ec8ca 100644 --- a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java +++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java @@ -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); } } diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/entity/I18nString.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/entity/I18nString.java index 1dc880d8..1ed14f6f 100644 --- a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/entity/I18nString.java +++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/entity/I18nString.java @@ -20,6 +20,23 @@ public class I18nString { private String zh_CN;
/**
+ *
+ */
+ public I18nString() {
+ super();
+ }
+
+ /**
+ * @param en_US
+ * @param zh_CN
+ */
+ public I18nString(String en_US, String zh_CN) {
+ super();
+ this.en_US = en_US;
+ this.zh_CN = zh_CN;
+ }
+
+ /**
* @return the en_US
*/
public String getEn_US() {
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 index 00000000..09588ef3 --- /dev/null +++ b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/utils/JsonUtils.java @@ -0,0 +1,41 @@ +/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+package org.onap.sdc.workflowdesigner.utils;
+
+import com.google.gson.Gson;
+
+/**
+ *
+ */
+public class JsonUtils {
+ /**
+ *
+ * @param json
+ * @param clazz
+ * @return
+ */
+ public static <T> T fromJson(String json, Class<T> clazz) {
+ Gson gson = new Gson();
+ return gson.fromJson(json, clazz);
+ }
+
+ /**
+ *
+ * @param t
+ * @return
+ */
+ public static <T> String toJson(T t) {
+ Gson gson = new Gson();
+ return gson.toJson(t);
+ }
+
+}
diff --git a/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResourceTest.java b/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResourceTest.java index 8d3fd5d3..18a8597a 100644 --- a/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResourceTest.java +++ b/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResourceTest.java @@ -20,8 +20,7 @@ import org.junit.Before; import org.junit.Test;
import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;
import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
-
-import com.google.gson.Gson;
+import org.onap.sdc.workflowdesigner.utils.JsonUtils;
/**
*
@@ -49,8 +48,7 @@ public class ExtendActivityResourceTest { try {
Response response = resource.getExtActivities("test");
ExtendActivity[] extActivities = (ExtendActivity[]) response.getEntity();
- Gson gson = new Gson();
- FileCommonUtils.write("test.json", gson.toJson(extActivities));
+ FileCommonUtils.write("test.json", JsonUtils.toJson(extActivities));
assertEquals(extActivities.length == 0, false);
} catch (Exception e) {
e.printStackTrace();
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 index 00000000..2672f6bd --- /dev/null +++ b/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/utils/JsonUtilsTest.java @@ -0,0 +1,59 @@ +/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the Apache License, Version 2.0
+ * and the Eclipse Public License v1.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ * ZTE - initial API and implementation and/or initial documentation
+ */
+package org.onap.sdc.workflowdesigner.utils;
+
+import static org.junit.Assert.*;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.sdc.workflowdesigner.resources.entity.I18nString;
+
+/**
+ *
+ */
+public class JsonUtilsTest {
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {}
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {}
+
+ /**
+ * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#fromJson(java.lang.String, java.lang.Class)}.
+ */
+ @Test
+ public void testFromJson() {
+ String i18n = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";
+ I18nString i18nString = JsonUtils.fromJson(i18n, I18nString.class);
+ assertNotNull(i18nString);
+ }
+
+ /**
+ * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#toJson(java.lang.Object)}.
+ */
+ @Test
+ public void testToJson() {
+ I18nString i18nString = new I18nString("Service Task", "Service Task");
+ String i18n = JsonUtils.toJson(i18nString);
+ String expect = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";
+ assertEquals(expect, i18n);
+ }
+
+}
|