Add JsonUtil and use it 17/60117/2
authortalig <talig@amdocs.com>
Sun, 12 Aug 2018 08:53:00 +0000 (11:53 +0300)
committertalig <talig@amdocs.com>
Sun, 12 Aug 2018 08:53:00 +0000 (11:53 +0300)
Get rid of org.openecomp.core.utilities.json.JsonUtil

Change-Id: I1dff97931dd76dde80c383baca9ff9e28e23d8ad
Issue-ID: SDC-1606
Signed-off-by: talig <talig@amdocs.com>
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/InternalEmptyObject.java [deleted file]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImpl.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/utilities/JsonUtil.java [new file with mode: 0644]
workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImplTest.java

diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/InternalEmptyObject.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/InternalEmptyObject.java
deleted file mode 100644 (file)
index efbd440..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright © 2016-2018 European Support Limited
- *
- * Licensed 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.onap.sdc.workflow.api.types.activityspec;
-
-import org.codehaus.jackson.annotate.JsonAutoDetect;
-
-/**
- * Object of this class can be used to create empty Response body like "{}".
- */
-@JsonAutoDetect
-public class InternalEmptyObject {
-
-}
index 05f2031..726e831 100644 (file)
@@ -34,7 +34,7 @@ import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecData;
 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecElementType;
 import org.onap.sdc.workflow.persistence.types.ActivitySpecEntity;
 import org.onap.sdc.workflow.services.ActivitySpecConstant;
-import org.openecomp.core.utilities.json.JsonUtil;
+import org.onap.sdc.workflow.services.utilities.JsonUtil;
 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/utilities/JsonUtil.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/utilities/JsonUtil.java
new file mode 100644 (file)
index 0000000..3e197a9
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2018 European Support Limited
+ *
+ * Licensed 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.onap.sdc.workflow.services.utilities;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonSyntaxException;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+public class JsonUtil {
+
+    private static final Gson gson = new Gson();
+
+    private JsonUtil() {
+    }
+
+    /**
+     * Object 2 json string.
+     *
+     * @param obj the obj
+     * @return the string
+     */
+    public static String object2Json(Object obj) {
+        return sbObject2Json(obj).toString();
+
+    }
+
+    /**
+     * Sb object 2 json string builder.
+     *
+     * @param obj the obj
+     * @return the string builder
+     */
+    public static StringBuilder sbObject2Json(Object obj) {
+        return new StringBuilder(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
+    }
+
+    /**
+     * Json 2 object t.
+     *
+     * @param <T>      the type parameter
+     * @param json     the json
+     * @param classOfT the class of t
+     * @return the t
+     */
+    public static <T> T json2Object(String json, Class<T> classOfT) {
+        T typ;
+        try {
+            try (Reader br = new StringReader(json)) {
+                typ = gson.fromJson(br, classOfT);
+            }
+        } catch (JsonIOException | JsonSyntaxException | IOException exception) {
+            throw new RuntimeException(exception);
+        }
+        return typ;
+    }
+
+    /**
+     * Json 2 object t.
+     *
+     * @param <T>      the type parameter
+     * @param is       the is
+     * @param classOfT the class of t
+     * @return the t
+     */
+    public static <T> T json2Object(InputStream is, Class<T> classOfT) {
+        T type;
+        try (Reader br = new BufferedReader(new InputStreamReader(is))) {
+            type = new Gson().fromJson(br, classOfT);
+        } catch (JsonIOException | JsonSyntaxException | IOException exception) {
+            throw new RuntimeException(exception);
+        }
+        return type;
+    }
+
+}
index 3438093..a748dd5 100644 (file)
@@ -19,7 +19,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 
 import com.amdocs.zusammen.datatypes.Id;
 import com.amdocs.zusammen.datatypes.item.Item;
-import com.google.gson.Gson;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -32,12 +31,13 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.sdc.workflow.RestPath;
-import org.onap.sdc.workflow.services.types.Workflow;
 import org.onap.sdc.workflow.services.WorkflowManager;
 import org.onap.sdc.workflow.services.types.Page;
 import org.onap.sdc.workflow.services.types.PagingRequest;
 import org.onap.sdc.workflow.services.types.RequestSpec;
 import org.onap.sdc.workflow.services.types.Sort;
+import org.onap.sdc.workflow.services.types.Workflow;
+import org.onap.sdc.workflow.services.utilities.JsonUtil;
 import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.ResultActions;
@@ -49,7 +49,6 @@ public class WorkflowControllerTest {
     private static final String USER_ID = "userId";
     private static final String MISSING_USER_HEADER_ERROR =
             "Missing request header 'USER_ID' for method parameter of type String";
-    private static final Gson GSON = new Gson();
     private static final String DEFAULT_SORT_VALUE = "name:asc";
 
     private MockMvc mockMvc;
@@ -187,8 +186,8 @@ public class WorkflowControllerTest {
         item.setId(new Id("abc"));
         Workflow reqWorkflow = createWorkflow(1, false);
         mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
-                                                         .content(GSON.toJson(reqWorkflow))).andDo(print())
-               .andExpect(status().isCreated());
+                                .content(JsonUtil.object2Json(reqWorkflow))).andDo(print())
+                .andExpect(status().isCreated());
         verify(workflowManagerMock).create(reqWorkflow);
     }
 
@@ -197,9 +196,9 @@ public class WorkflowControllerTest {
         Workflow reqWorkflow = new Workflow();
         reqWorkflow.setName("Invalid workflow name %");
         mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
-                                                         .content(GSON.toJson(reqWorkflow))).andDo(print())
-               .andExpect(status().isBadRequest())
-               .andExpect(jsonPath("$.message", is("Workflow name must contain only letters, digits and underscores")));
+                                .content(JsonUtil.object2Json(reqWorkflow))).andDo(print())
+                .andExpect(status().isBadRequest()).andExpect(
+                jsonPath("$.message", is("Workflow name must contain only letters, digits and underscores")));
     }
 
     private void mockManagerList3() {
index 2b3c49c..2ed1cc1 100644 (file)
@@ -14,7 +14,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
-import com.google.gson.Gson;
 import java.util.Arrays;
 import java.util.Collection;
 import org.junit.Before;
@@ -28,6 +27,7 @@ import org.onap.sdc.workflow.persistence.types.ParameterEntity;
 import org.onap.sdc.workflow.persistence.types.ParameterType;
 import org.onap.sdc.workflow.services.WorkflowVersionManager;
 import org.onap.sdc.workflow.services.types.WorkflowVersion;
+import org.onap.sdc.workflow.services.utilities.JsonUtil;
 import org.openecomp.sdc.versioning.dao.types.Version;
 import org.springframework.http.HttpStatus;
 import org.springframework.mock.web.MockHttpServletResponse;
@@ -42,8 +42,6 @@ public class WorkflowVersionControllerTest {
     private static final String VERSION1_ID = "version_id_1";
     private static final String VERSION2_ID = "version_id_2";
 
-    private static final Gson GSON = new Gson();
-
     private MockMvc mockMvc;
 
     @Mock
@@ -76,7 +74,7 @@ public class WorkflowVersionControllerTest {
         WorkflowVersion version = new WorkflowVersion();
         version.setDescription("VersionDescription");
         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
-                                .contentType(APPLICATION_JSON).content(GSON.toJson(version)))
+                                .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(version)))
                 .andExpect(status().isCreated());
 
         verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version);
@@ -91,7 +89,7 @@ public class WorkflowVersionControllerTest {
         version.setInputs(inputs);
         version.setDescription("VersionDescription");
         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
-                                .contentType(APPLICATION_JSON).content(GSON.toJson(version)))
+                                .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(version)))
                 .andExpect(status().isBadRequest());
 
     }
@@ -115,7 +113,8 @@ public class WorkflowVersionControllerTest {
 
         MockHttpServletResponse result = mockMvc.perform(
                 put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
-                        .contentType(APPLICATION_JSON).content(GSON.toJson(version))).andReturn().getResponse();
+                        .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(version))).andReturn()
+                                                 .getResponse();
 
         assertEquals(HttpStatus.OK.value(), result.getStatus());
         version.setId(VERSION1_ID);
index 999097c..bbae56e 100644 (file)
@@ -55,7 +55,7 @@ import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecData;
 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecElementType;
 import org.onap.sdc.workflow.persistence.types.ActivitySpecEntity;
 import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter;
-import org.openecomp.core.utilities.json.JsonUtil;
+import org.onap.sdc.workflow.services.utilities.JsonUtil;
 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
 import org.openecomp.sdc.versioning.dao.types.Version;