From 83307e37a264972f4f9c60a114f40a375e32634b Mon Sep 17 00:00:00 2001 From: YiLi Date: Sun, 11 Feb 2018 17:39:05 +0800 Subject: [PATCH] Test Replace Jackson with GSON Change-Id: I7ac92dd83a4bf7dcfb70199f1d0f61e600db8904 Issue-ID: HOLMES-115 Signed-off-by: YiLi --- holmes-actions/pom.xml | 21 ++- .../java/org/onap/holmes/common/aai/AaiQuery.java | 6 +- .../onap/holmes/common/aai/AaiResponseUtil.java | 173 +++++++++++---------- .../holmes/common/config/MicroServiceConfig.java | 3 +- .../org/onap/holmes/common/dmaap/Publisher.java | 12 +- .../dropwizard/ioc/bundle/AutoConfigBundle.java | 24 ++- .../org/onap/holmes/common/aai/AaiQueryTest.java | 49 +----- .../holmes/common/aai/AaiResponseUtilTest.java | 24 +-- .../org/onap/holmes/common/api/stat/AlarmTest.java | 18 +++ 9 files changed, 145 insertions(+), 185 deletions(-) diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index 850a03a..57f2eaa 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -22,12 +22,6 @@ org.onap.msb.java-sdk msb-java-sdk - - - - - - org.apache.geronimo.specs @@ -57,11 +51,21 @@ com.fasterxml.jackson.dataformat jackson-dataformat-yaml + + com.fasterxml.jackson.core + jackson-databind + io.swagger swagger-jersey2-jaxrs + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + + org.projectlombok @@ -169,6 +173,11 @@ gson 2.8.2 + + com.alibaba + fastjson + 1.1.23 + diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index b80c40e..48e6ec0 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -65,11 +65,7 @@ public class AaiQuery { private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException { String response = getResourceLinksResponse(vserverId, vserverName); - try { - return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink(); - } catch (Exception e) { - throw new CorrelationException("Failed to get aai resource link", e); - } + return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink(); } private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException { diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java index 37bfc1d..a8d3c0d 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,13 +15,15 @@ */ package org.onap.holmes.common.aai; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.aai.entity.RelationshipList; import org.onap.holmes.common.aai.entity.RelationshipList.RelatedToProperty; import org.onap.holmes.common.aai.entity.RelationshipList.Relationship; import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData; @@ -33,41 +35,36 @@ import org.onap.holmes.common.aai.entity.VnfEntity; public class AaiResponseUtil { public static final String RELATIONSHIP_LIST = "relationship-list"; - public List convertJsonToVmResourceLink(String responseJson) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonNode = mapper.readTree(responseJson); + public List convertJsonToVmResourceLink(String responseJson) + { List vmResourceLinkList = new ArrayList<>(); - if (jsonNode.has("result-data")) { - JsonNode resultData = jsonNode.get("result-data"); + String resultDataKey = "result-data"; + JSONObject jsonNode = JSON.parseObject(responseJson); + if (jsonNode.get(resultDataKey) != null) { + JSONArray resultData = jsonNode.getJSONArray(resultDataKey); vmResourceLinkList = convertResultDataList(resultData); } return vmResourceLinkList; } - public VmEntity convertJsonToVmEntity(String responseJson) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonNode = mapper.readTree(responseJson); - if (!jsonNode.iterator().hasNext()) { + public VmEntity convertJsonToVmEntity(String responseJson) { + JSONObject jsonObject = JSON.parseObject(responseJson); + if (jsonObject == null ||jsonObject.isEmpty()) { return null; } VmEntity vmEntity = new VmEntity(); - vmEntity.setInMaint(getBooleanElementByNode(jsonNode, "in-maint")); - vmEntity.setClosedLoopDisable(getBooleanElementByNode(jsonNode,"is-closed-loop-disabled")); - vmEntity.setProvStatus(getTextElementByNode(jsonNode, "prov-status")); - vmEntity.setResourceVersion(getTextElementByNode(jsonNode,"resource-version")); - vmEntity.setVserverId(getTextElementByNode(jsonNode,"vserver-id")); - vmEntity.setVserverName(getTextElementByNode(jsonNode,"vserver-name")); - vmEntity.setVserverName2(getTextElementByNode(jsonNode,"vserver-name2")); - vmEntity.setVserverSelflink(getTextElementByNode(jsonNode,"vserver-selflink")); - - if (jsonNode.has(RELATIONSHIP_LIST)) { - JsonNode relationshipListNode = jsonNode.get(RELATIONSHIP_LIST); - if (relationshipListNode.has("relationship")) { - JsonNode relationshipNode = relationshipListNode.get("relationship"); - vmEntity.getRelationshipList().setRelationships(convertRelationships(relationshipNode)); - } - } + vmEntity.setInMaint(getBooleanElementByNode(jsonObject, "in-maint")); + vmEntity.setClosedLoopDisable( + getBooleanElementByNode(jsonObject, "is-closed-loop-disabled")); + vmEntity.setProvStatus(getTextElementByNode(jsonObject, "prov-status")); + vmEntity.setResourceVersion(getTextElementByNode(jsonObject, "resource-version")); + vmEntity.setVserverId(getTextElementByNode(jsonObject, "vserver-id")); + vmEntity.setVserverName(getTextElementByNode(jsonObject, "vserver-name")); + vmEntity.setVserverName2(getTextElementByNode(jsonObject, "vserver-name2")); + vmEntity.setVserverSelflink(getTextElementByNode(jsonObject, "vserver-selflink")); + + setRelationShips(jsonObject, vmEntity.getRelationshipList()); if (vmEntity.getRelationshipList().getRelationships() == null) { vmEntity.getRelationshipList().setRelationships(Collections.emptyList()); } @@ -75,110 +72,128 @@ public class AaiResponseUtil { } public VnfEntity convertJsonToVnfEntity(String responseJson) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonNode = mapper.readTree(responseJson); + JSONObject jsonObject = JSON.parseObject(responseJson); - if(!jsonNode.elements().hasNext()) + if (jsonObject.isEmpty()) { return null; + } VnfEntity vnfEntity = new VnfEntity(); - vnfEntity.setInMaint(getBooleanElementByNode(jsonNode, "in-maint")); - vnfEntity.setClosedLoopDisabled(getBooleanElementByNode(jsonNode, "is-closed-loop-disabled")); - vnfEntity.setOrchestrationStatus(getTextElementByNode(jsonNode, "orchestration-status")); - vnfEntity.setProvStatus(getTextElementByNode(jsonNode, "prov-status")); - vnfEntity.setResourceVersion(getTextElementByNode(jsonNode,"resource-version")); - vnfEntity.setServiceId(getTextElementByNode(jsonNode,"service-id")); - vnfEntity.setVnfId(getTextElementByNode(jsonNode,"vnf-id")); - vnfEntity.setVnfName(getTextElementByNode(jsonNode,"vnf-name")); - vnfEntity.setVnfType(getTextElementByNode(jsonNode,"vnf-type")); - - if (jsonNode.has(RELATIONSHIP_LIST)) { - JsonNode relationshipListNode = jsonNode.get(RELATIONSHIP_LIST); - if (relationshipListNode.has("relationship")) { - JsonNode relationshipNode = relationshipListNode.get("relationship"); - vnfEntity.getRelationshipList().setRelationships(convertRelationships(relationshipNode)); - } - } + vnfEntity.setInMaint(getBooleanElementByNode(jsonObject, "in-maint")); + vnfEntity.setClosedLoopDisabled( + getBooleanElementByNode(jsonObject, "is-closed-loop-disabled")); + vnfEntity.setOrchestrationStatus(getTextElementByNode(jsonObject, "orchestration-status")); + vnfEntity.setProvStatus(getTextElementByNode(jsonObject, "prov-status")); + vnfEntity.setResourceVersion(getTextElementByNode(jsonObject, "resource-version")); + vnfEntity.setServiceId(getTextElementByNode(jsonObject, "service-id")); + vnfEntity.setVnfId(getTextElementByNode(jsonObject, "vnf-id")); + vnfEntity.setVnfName(getTextElementByNode(jsonObject, "vnf-name")); + vnfEntity.setVnfType(getTextElementByNode(jsonObject, "vnf-type")); + + setRelationShips(jsonObject, vnfEntity.getRelationshipList()); if (vnfEntity.getRelationshipList().getRelationships() == null) { vnfEntity.getRelationshipList().setRelationships(Collections.emptyList()); } return vnfEntity; } - private List convertResultDataList(JsonNode resultData) { + private void setRelationShips(JSONObject jsonObject, RelationshipList relationshipList) { + if (jsonObject.get(RELATIONSHIP_LIST) != null) { + JSONObject relationshipListNode = jsonObject.getJSONObject(RELATIONSHIP_LIST); + String relationship = "relationship"; + if (relationshipListNode.get(relationship) != null) { + JSONArray relationshipNode = relationshipListNode.getJSONArray(relationship); + relationshipList + .setRelationships(convertRelationships(relationshipNode)); + } + } + } + + private List convertResultDataList(JSONArray resultData) { List vmResourceLinkList = new ArrayList<>(); - resultData.forEach(node ->{ - if (node.has("resource-link") && node.has("resource-type")) { + String resourceLink = "resource-link"; + String resourceType = "resource-type"; + for (int i = 0; i < resultData.size(); i++) { + JSONObject jsonObject = resultData.getJSONObject(i); + if (jsonObject.get(resourceLink) != null + && jsonObject.get(resourceType) != null) { VmResourceLink vmResourceLink = new VmResourceLink(); - vmResourceLink.setResourceLink(getTextElementByNode(node, "resource-link")); - vmResourceLink.setResourceType(getTextElementByNode(node, "resource-type")); + vmResourceLink.setResourceLink(getTextElementByNode(jsonObject, resourceLink)); + vmResourceLink.setResourceType(getTextElementByNode(jsonObject, resourceType)); vmResourceLinkList.add(vmResourceLink); } - }); + } return vmResourceLinkList; } - private List convertRelationships(JsonNode relationshipNode) { + private List convertRelationships(JSONArray relationshipNode) { List relationshipList = new ArrayList<>(); - relationshipNode.forEach(node ->{ + for (int i = 0; i < relationshipNode.size(); i++) { Relationship relationship = new Relationship(); - relationship.setRelatedLink(getTextElementByNode(node, "related-link")); - relationship.setRelatedTo(getTextElementByNode(node, "related-to")); - if (node.has("related-to-property")) { - JsonNode relatedToPropertyNode = node.get("related-to-property"); + JSONObject jsonObject = relationshipNode.getJSONObject(i); + + relationship.setRelatedLink(getTextElementByNode(jsonObject, "related-link")); + relationship.setRelatedTo(getTextElementByNode(jsonObject, "related-to")); + if (jsonObject.get("related-to-property") != null) { + JSONArray relatedToPropertyNode = jsonObject.getJSONArray("related-to-property"); relationship.setRelatedToPropertyList( convertRelatedToProperty(relatedToPropertyNode)); } else { relationship.setRelatedToPropertyList(Collections.emptyList()); } - if (node.has("relationship-data")) { - JsonNode relationshipDataNode = node.get("relationship-data"); + if (jsonObject.get("relationship-data") != null) { + JSONArray relationshipDataNode = jsonObject.getJSONArray("relationship-data"); relationship .setRelationshipDataList(convertRelationshipDate(relationshipDataNode)); } else { relationship.setRelationshipDataList(Collections.emptyList()); } relationshipList.add(relationship); - }); + } + return relationshipList; } - private List convertRelationshipDate(JsonNode relationshipDataNode) { + private List convertRelationshipDate(JSONArray relationshipDataNode) { List relationshipDataList = new ArrayList<>(); - relationshipDataNode.forEach(node ->{ + for (int i = 0; i < relationshipDataNode.size(); i++) { + JSONObject jsonObject = relationshipDataNode.getJSONObject(i); RelationshipData relationshipData = new RelationshipData(); relationshipData.setRelationshipKey( - getTextElementByNode(node,"relationship-key")); + getTextElementByNode(jsonObject, "relationship-key")); relationshipData.setRelationshipValue( - getTextElementByNode(node,"relationship-value")); + getTextElementByNode(jsonObject, "relationship-value")); relationshipDataList.add(relationshipData); - }); + relationshipDataList.add(relationshipData); + + } return relationshipDataList; } - private List convertRelatedToProperty(JsonNode relatedToPropertyNode) { + private List convertRelatedToProperty(JSONArray relatedToPropertyNode) { List relatedToPropertyList = new ArrayList<>(); - relatedToPropertyNode.forEach(node ->{ + for (int i = 0; i < relatedToPropertyNode.size(); i++) { + JSONObject jsonObject = relatedToPropertyNode.getJSONObject(i); RelatedToProperty relatedToProperty = new RelatedToProperty(); relatedToProperty - .setPropertyKey(getTextElementByNode(node, "property-key")); + .setPropertyKey(getTextElementByNode(jsonObject, "property-key")); relatedToProperty.setPropertyValue( - getTextElementByNode(node, "property-value")); + getTextElementByNode(jsonObject, "property-value")); relatedToPropertyList.add(relatedToProperty); - }); + } return relatedToPropertyList; } - private String getTextElementByNode(JsonNode jsonNode,String name){ - if(jsonNode.has(name)){ - return jsonNode.get(name).asText(); + private String getTextElementByNode(JSONObject jsonNode, String name) { + if (jsonNode.get(name) != null) { + return jsonNode.getString(name); } return null; } - private Boolean getBooleanElementByNode(JsonNode jsonNode,String name){ - if(jsonNode.has(name)){ - return jsonNode.get(name).asBoolean(); + private Boolean getBooleanElementByNode(JSONObject jsonNode, String name) { + if (jsonNode.get(name) != null) { + return jsonNode.getBoolean(name); } return null; } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java index 204f352..9a1b0ea 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java @@ -21,7 +21,6 @@ import javax.ws.rs.core.Response; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONArray; import net.sf.json.JSONObject; -import org.glassfish.jersey.client.ClientConfig; import org.onap.holmes.common.constant.AlarmConst; @Slf4j @@ -63,7 +62,7 @@ public class MicroServiceConfig { } private static String execQuery(String queryString) { - Client client = ClientBuilder.newClient(new ClientConfig()); + Client client = ClientBuilder.newBuilder().build(); Response response = client.target(queryString).request().get(); return response.readEntity(String.class); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java index 91e9d42..5c6965a 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java @@ -15,8 +15,7 @@ */ package org.onap.holmes.common.dmaap; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.alibaba.fastjson.JSON; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -43,14 +42,7 @@ public class Publisher { public boolean publish(PolicyMsg msg) throws CorrelationException { Client client = ClientBuilder.newClient(new ClientConfig()); - ObjectMapper mapper = new ObjectMapper(); - String content = null; - try { - content = mapper.writeValueAsString(msg); - } catch (JsonProcessingException e) { - throw new CorrelationException("Failed to convert the message object to a json string.", - e); - } + String content = JSON.toJSONString(msg); WebTarget webTarget = client.target(url); Response response = null; try { diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dropwizard/ioc/bundle/AutoConfigBundle.java b/holmes-actions/src/main/java/org/onap/holmes/common/dropwizard/ioc/bundle/AutoConfigBundle.java index 2b8e114..b8552b2 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dropwizard/ioc/bundle/AutoConfigBundle.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dropwizard/ioc/bundle/AutoConfigBundle.java @@ -15,16 +15,23 @@ */ package org.onap.holmes.common.dropwizard.ioc.bundle; +import com.codahale.metrics.health.HealthCheck; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.Lists; +import io.dropwizard.Configuration; +import io.dropwizard.ConfiguredBundle; +import io.dropwizard.lifecycle.ServerLifecycleListener; +import io.dropwizard.servlets.tasks.Task; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; - import javax.ws.rs.Path; import javax.ws.rs.ext.Provider; - import org.eclipse.jetty.util.component.LifeCycle; import org.glassfish.hk2.api.ServiceLocator; import org.glassfish.hk2.api.ServiceLocatorFactory; @@ -49,19 +56,6 @@ import org.reflections.util.FilterBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.codahale.metrics.health.HealthCheck; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.Lists; - -import io.dropwizard.Configuration; -import io.dropwizard.ConfiguredBundle; -import io.dropwizard.configuration.ConfigurationSourceProvider; -import io.dropwizard.lifecycle.Managed; -import io.dropwizard.lifecycle.ServerLifecycleListener; -import io.dropwizard.servlets.tasks.Task; -import io.dropwizard.setup.Bootstrap; -import io.dropwizard.setup.Environment; - /** * complete the integration of hK2 container and dropwizard * diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java index b3f1eb6..e0d5bf7 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java @@ -16,8 +16,8 @@ package org.onap.holmes.common.aai; import static org.easymock.EasyMock.anyObject; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; import static org.powermock.api.mockito.PowerMockito.when; import java.util.HashMap; @@ -110,36 +110,7 @@ public class AaiQueryTest { - @Test - public void testAaiQuery_getAaiVmData_exception() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to convert aai vm response data to vm entity"); - aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks"); - - aaiResponseUtil = new AaiResponseUtil(); - Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - - PowerMockito.mockStatic(HttpsUtils.class); - Map headers = new HashMap<>(); - headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); - headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); - headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); - headers.put("Accept", "application/json"); - String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; - - when(HttpsUtils.get(url, headers)).thenReturn(""); - - PowerMockito.mockStatic(MicroServiceConfig.class); - when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80"); - PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") - .andReturn("/aai/v11/cloud-infrastructure"); - - PowerMock.replayAll(); - Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); - PowerMock.verifyAll(); - } @Test public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception { @@ -189,23 +160,7 @@ public class AaiQueryTest { assertThat(resource, equalTo("le-vserver-id-val-51834")); } - @Test - public void testAaiQuery_getVmResourceLinks_exception() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to get aai resource link"); - aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse"); - - aaiResponseUtil = new AaiResponseUtil(); - Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - - PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(""); - PowerMock.replayAll(); - String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2"); - PowerMock.verifyAll(); - assertThat(resource, equalTo("le-vserver-id-val-51834")); - } @Test public void testAaiQuery_getResourceLinksResponse() throws Exception { diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiResponseUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiResponseUtilTest.java index fee98b9..3d20af9 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiResponseUtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiResponseUtilTest.java @@ -16,8 +16,9 @@ package org.onap.holmes.common.aai; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.List; @@ -112,12 +113,6 @@ public class AaiResponseUtilTest { assertThat(actual.isEmpty(), equalTo(true)); } - @Test - public void testAaiResponseUtil_convert_resource_link_throw_IOException() throws IOException { - thrown.expect(IOException.class); - String json = "{**}"; - aaiResponseUtil.convertJsonToVmResourceLink(json); - } @Test public void testAaiResponseUtil_convert_VmEntity_success() throws IOException { @@ -142,12 +137,6 @@ public class AaiResponseUtilTest { assertThat(actual.getVserverSelflink(), equalTo("example-vserver-selflink-val-2")); } - @Test - public void testAaiResponseUtil_convert_VmEntity_throw_IOException() throws IOException { - thrown.expect(IOException.class); - String json = "{**}"; - aaiResponseUtil.convertJsonToVmEntity(json); - } @Test public void testAaiResponseUtil_convert_VmEntity_input_empty() throws IOException { @@ -221,13 +210,6 @@ public class AaiResponseUtilTest { .getRelationshipValue(), equalTo("Demonstration3")); } - @Test - public void testAaiResponseUtil_throw_IOException() throws IOException { - thrown.expect(IOException.class); - String json = "{**}"; - aaiResponseUtil.convertJsonToVnfEntity(json); - } - @Test public void testAaiResponseUtil_convert_VnfEntity_input_empty() throws IOException { String json = "{}"; diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java index 92658d6..d701f3d 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java @@ -16,6 +16,11 @@ package org.onap.holmes.common.api.stat; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import java.util.Date; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertFalse; @@ -29,6 +34,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.onap.holmes.common.api.entity.ServiceNode; public class AlarmTest { @@ -190,4 +196,16 @@ public class AlarmTest { alarm.setCenterType(centerType); assertThat(centerType, equalTo(alarm.getCenterType())); } + @Test + public void TestJson(){ + ServiceNode serviceNode = new ServiceNode(); + serviceNode.setIp("111"); + String jsonString = "{\"uid\":\"189024\", \"region\":\"SouthChina\", \"order\":123}"; + String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}"; + + JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); + JSONArray jsonArray = jsonObject.getJSONArray("students"); + System.out.printf("jsonObject:"+jsonArray); +// System.out.println("uid:" + retMap.get("uid") + ", " + "region:" + retMap.get("region") + ", " + "order:" + retMap.get("order")); + } } -- 2.16.6