diff options
author | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-08-12 10:46:53 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-12 10:46:53 +0000 |
commit | 95296a3adaeef238250c24b5f98eea83bc07f3a3 (patch) | |
tree | c5db2bf86df34f90d7c5d7db2147777f4cd809ba | |
parent | 8a8a24731d5b269b2b675ea9d02ca9b7609055b2 (diff) | |
parent | a158feee847d1c591f1089fbb75c0fba8f1a3e27 (diff) |
Merge "Add JsonUtil and use it"
6 files changed, 110 insertions, 43 deletions
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 index efbd4400..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/InternalEmptyObject.java +++ /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 { - -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImpl.java index 05f2031b..726e831c 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImpl.java @@ -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 index 00000000..3e197a9f --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/utilities/JsonUtil.java @@ -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; + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java index 3438093d..a748dd5f 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java @@ -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() { diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java index 2b3c49c1..2ed1cc16 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java @@ -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); diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImplTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImplTest.java index 999097c2..bbae56e3 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImplTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ActivitySpecRepositoryImplTest.java @@ -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; |