From d2e0902630ccf06bb466360e036212f5060b9055 Mon Sep 17 00:00:00 2001 From: YiLi Date: Tue, 27 Feb 2018 15:26:41 +0800 Subject: [PATCH] Replace Jackson with GSON Change-Id: Ifffe7aca1116e2706d66ecb682697df521785b57 Issue-ID: HOLMES-115 Signed-off-by: YiLi --- pom.xml | 3 +- .../src/main/assembly/conf/rulemgt.yml | 2 +- rulemgt/pom.xml | 9 +++- .../bean/response/RuleQueryListResponse.java | 8 ++-- .../rulemgt/dcae/DcaeConfigurationPolling.java | 21 +++------ .../holmes/rulemgt/resources/RuleMgtResources.java | 50 +++++++++++----------- .../rulemgt/dcae/DcaeConfigurationPollingTest.java | 5 ++- .../rulemgt/resources/RuleMgtResourcesTest.java | 3 +- 8 files changed, 52 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index fa1bd43..93da107 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ org.onap.holmes.common holmes-actions - 1.0.0 + 1.1.0-SNAPSHOT io.dropwizard @@ -157,6 +157,7 @@ 1.6.5 test + diff --git a/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml b/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml index 29d56d7..3c94fa7 100644 --- a/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml +++ b/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml @@ -50,7 +50,7 @@ database: driverClass: org.postgresql.Driver user: holmes password: holmespwd - url: jdbc:postgresql://10.74.156.206:5432/holmes + url: jdbc:postgresql://10.96.33.33:5432/holmes properties: charSet: UTF-8 maxWaitForConnection: 1s diff --git a/rulemgt/pom.xml b/rulemgt/pom.xml index 78de592..feb40fd 100644 --- a/rulemgt/pom.xml +++ b/rulemgt/pom.xml @@ -155,7 +155,7 @@ org.apache.httpcomponents httpclient - 4.3.6 + 4.4 com.googlecode.json-simple @@ -193,6 +193,13 @@ guava 19.0 + + + org.codehaus.groovy.maven + gmaven-plugin + 1.0 + + diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java index 17903ab..aab6dc5 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java @@ -15,20 +15,18 @@ */ package org.onap.holmes.rulemgt.bean.response; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; import lombok.Getter; import lombok.Setter; import java.util.ArrayList; import java.util.List; -@JsonInclude(JsonInclude.Include.ALWAYS) @Getter @Setter public class RuleQueryListResponse { - @JsonProperty(value = "rules") + @SerializedName(value = "rules") private List correlationRules = new ArrayList(); - @JsonProperty(value = "totalcount") + @SerializedName(value = "totalcount") private int totalCount; } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java index 97da7ee..d0513ca 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java @@ -13,7 +13,6 @@ */ package org.onap.holmes.rulemgt.dcae; -import com.fasterxml.jackson.core.JsonProcessingException; import java.util.List; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -27,7 +26,7 @@ import org.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.Rule; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.JacksonUtil; +import org.onap.holmes.common.utils.GsonUtil; import org.onap.holmes.common.utils.Md5Util; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; @@ -56,7 +55,7 @@ public class DcaeConfigurationPolling implements Runnable { try { dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname); String md5 = Md5Util.md5(dcaeConfigurations); - if (prevResult && prevConfigMd5.equals(md5)){ + if (prevResult && prevConfigMd5.equals(md5)) { log.info("Operation aborted due to identical Configurations."); return; } @@ -64,8 +63,6 @@ public class DcaeConfigurationPolling implements Runnable { prevResult = false; } catch (CorrelationException e) { log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e); - } catch (JsonProcessingException e) { - log.info("Failed to generate the MD5 information for new configurations.", e); } if (dcaeConfigurations != null) { RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules(); @@ -87,17 +84,13 @@ public class DcaeConfigurationPolling implements Runnable { .readEntity(RuleQueryListResponse.class); } - private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException { + private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) + throws CorrelationException { boolean suc = false; for (Rule rule : dcaeConfigurations.getDefaultRules()) { RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule); Client client = ClientBuilder.newClient(new ClientConfig()); - String content = null; - try { - content = JacksonUtil.beanToJson(ruleCreateRequest); - } catch (JsonProcessingException e) { - throw new CorrelationException("Failed to convert the message object to a json string.", e); - } + String content = GsonUtil.beanToJson(ruleCreateRequest); WebTarget webTarget = client.target(url); Response response = webTarget.request(MediaType.APPLICATION_JSON) .put(Entity.entity(content, MediaType.APPLICATION_JSON)); @@ -109,8 +102,8 @@ public class DcaeConfigurationPolling implements Runnable { return suc; } - private void deleteAllCorrelationRules(List ruleResult4APIs){ - ruleResult4APIs.forEach(correlationRule ->{ + private void deleteAllCorrelationRules(List ruleResult4APIs) { + ruleResult4APIs.forEach(correlationRule -> { Client client = ClientBuilder.newClient(new ClientConfig()); WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId()); webTarget.request(MediaType.APPLICATION_JSON).delete(); diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java index dc21e0d..041377f 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.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, @@ -20,7 +20,6 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.swagger.annotations.SwaggerDefinition; -import java.io.IOException; import java.util.Locale; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; @@ -39,7 +38,7 @@ import net.sf.json.JSONObject; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.ExceptionUtil; -import org.onap.holmes.common.utils.JacksonUtil; +import org.onap.holmes.common.utils.GsonUtil; import org.onap.holmes.common.utils.LanguageUtil; import org.onap.holmes.common.utils.UserUtil; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; @@ -68,9 +67,10 @@ public class RuleMgtResources { response = RuleAddAndUpdateResponse.class) @Timed public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "The request entity of the HTTP call, which comprises \"rulename\"(required), " - + "\"loopcontrolname\"(required), \"content\"(required), \"enabled\"(required) " - + "and \"description\"(optional)", required = true) + @ApiParam(value = + "The request entity of the HTTP call, which comprises \"rulename\"(required), " + + "\"loopcontrolname\"(required), \"content\"(required), \"enabled\"(required) " + + "and \"description\"(optional)", required = true) RuleCreateRequest ruleCreateRequest) { Locale locale = LanguageUtil.getLocale(request); RuleAddAndUpdateResponse ruleChangeResponse; @@ -90,13 +90,15 @@ public class RuleMgtResources { @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class) @Timed public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleid\"(required), " - + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true) + @ApiParam(value = + "The request entity of the HTTP call, which comprises \"ruleid\"(required), " + + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true) RuleUpdateRequest ruleUpdateRequest) { Locale locale = LanguageUtil.getLocale(request); RuleAddAndUpdateResponse ruleChangeResponse; try { - ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); + ruleChangeResponse = ruleMgtWrapper + .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful"); return ruleChangeResponse; } catch (CorrelationException e) { @@ -128,9 +130,10 @@ public class RuleMgtResources { @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class) @Timed public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request, - @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), " - + "\"rulename\"(optional), \"creator\"(optional), " - + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}", + @ApiParam(value = + "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), " + + "\"rulename\"(optional), \"creator\"(optional), " + + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) { Locale locale = LanguageUtil.getLocale(request); RuleQueryListResponse ruleQueryListResponse; @@ -138,7 +141,8 @@ public class RuleMgtResources { try { ruleQueryListResponse = ruleMgtWrapper .getCorrelationRuleByCondition(ruleQueryCondition); - log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition)); + log.info("query rule successful by condition:" + JSONObject + .fromObject(ruleQueryCondition)); return ruleQueryListResponse; } catch (CorrelationException e) { log.error("query rule failed,cause query condition conversion failure", e); @@ -149,18 +153,16 @@ public class RuleMgtResources { private RuleQueryCondition getRuleQueryCondition(String queryRequest, HttpServletRequest request) { Locale locale = LanguageUtil.getLocale(request); - try { - RuleQueryCondition ruleQueryCondition = JacksonUtil - .jsonToBean(queryRequest, RuleQueryCondition.class); - if (queryRequest == null) { - ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); - } else if (queryRequest.indexOf("enabled") == -1) { - ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); + + RuleQueryCondition ruleQueryCondition = GsonUtil.jsonToBean(queryRequest, RuleQueryCondition.class); + if (queryRequest == null) { + if(ruleQueryCondition==null){ + ruleQueryCondition = new RuleQueryCondition(); } - return ruleQueryCondition; - } catch (IOException e) { - log.warn("queryRequest convert to json failed", e); - throw ExceptionUtil.buildExceptionResponse("The request format is invalid!"); + ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); + } else if (queryRequest.indexOf("enabled") == -1) { + ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL); } + return ruleQueryCondition; } } diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java index a330306..8d16198 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import static org.powermock.api.mockito.PowerMockito.when; +import com.alibaba.fastjson.JSONException; import com.fasterxml.jackson.databind.ObjectMapper; import javax.ws.rs.ProcessingException; import org.junit.Before; @@ -57,13 +58,13 @@ public class DcaeConfigurationPollingTest { public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception { PowerMock.resetAll(); thrown.expect(CorrelationException.class); - thrown.expectMessage("host"); + thrown.expectMessage("syntax error, pos 1"); PowerMockito.mockStatic(MicroServiceConfig.class); when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt")) .thenReturn("host"); PowerMock.createMock(DcaeConfigurationParser.class); PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host") - .andThrow(new CorrelationException("tests")).anyTimes(); + .andThrow(new CorrelationException("")).anyTimes(); PowerMock.replayAll(); Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations"); diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java index 692cc19..0c3bd89 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java @@ -16,6 +16,7 @@ package org.onap.holmes.rulemgt.resources; +import com.google.gson.JsonSyntaxException; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.WebApplicationException; import org.easymock.EasyMock; @@ -147,7 +148,7 @@ public class RuleMgtResourcesTest { @Test public void getCorrelationRules_param_translate_exception() { - thrown.expect(WebApplicationException.class); + thrown.expect(JsonSyntaxException.class); String queryRequest = "this is error param"; EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); -- 2.16.6