From 20a1514bf93035472d4f940f00a357106d4bec1f Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Mon, 7 Aug 2017 12:30:35 +0800 Subject: Change the groupid from openo to onap Change the groupid and package paths to onap. Change-Id: I8432e9ac2c979bbc36e10e6a702c6f04fc41446a Issue-ID: HOLMES-7 Signed-off-by: Guangrong Fu --- rulemgt/pom.xml | 18 +- .../org/onap/holmes/rulemgt/RuleActiveApp.java | 61 +++ .../org/onap/holmes/rulemgt/RuleAppConfig.java | 58 +++ .../bean/request/CorrelationCheckRule4Engine.java | 27 ++ .../bean/request/CorrelationDeployRule4Engine.java | 31 ++ .../bean/request/CorrelationRestRequest.java | 27 ++ .../rulemgt/bean/request/RuleCreateRequest.java | 33 ++ .../rulemgt/bean/request/RuleDeleteRequest.java | 28 ++ .../rulemgt/bean/request/RuleQueryCondition.java | 34 ++ .../rulemgt/bean/request/RuleUpdateRequest.java | 33 ++ .../bean/response/RuleAddAndUpdateResponse.java | 29 ++ .../bean/response/RuleQueryListResponse.java | 34 ++ .../rulemgt/bean/response/RuleResult4API.java | 42 +++ .../rulemgt/bolt/enginebolt/EngineService.java | 66 ++++ .../rulemgt/bolt/enginebolt/EngineWrapper.java | 87 +++++ .../holmes/rulemgt/constant/RuleMgtConstant.java | 29 ++ .../onap/holmes/rulemgt/db/CorrelationRuleDao.java | 106 ++++++ .../holmes/rulemgt/db/CorrelationRuleQueryDao.java | 128 +++++++ .../rulemgt/db/mapper/CorrelationRuleMapper.java | 48 +++ .../holmes/rulemgt/resources/RuleMgtResources.java | 161 ++++++++ .../holmes/rulemgt/wrapper/RuleMgtWrapper.java | 244 ++++++++++++ .../org/openo/holmes/rulemgt/RuleActiveApp.java | 61 --- .../org/openo/holmes/rulemgt/RuleAppConfig.java | 58 --- .../bean/request/CorrelationCheckRule4Engine.java | 27 -- .../bean/request/CorrelationDeployRule4Engine.java | 31 -- .../bean/request/CorrelationRestRequest.java | 27 -- .../rulemgt/bean/request/RuleCreateRequest.java | 33 -- .../rulemgt/bean/request/RuleDeleteRequest.java | 28 -- .../rulemgt/bean/request/RuleQueryCondition.java | 34 -- .../rulemgt/bean/request/RuleUpdateRequest.java | 33 -- .../bean/response/RuleAddAndUpdateResponse.java | 29 -- .../bean/response/RuleQueryListResponse.java | 34 -- .../rulemgt/bean/response/RuleResult4API.java | 42 --- .../rulemgt/bolt/enginebolt/EngineService.java | 66 ---- .../rulemgt/bolt/enginebolt/EngineWrapper.java | 88 ----- .../holmes/rulemgt/constant/RuleMgtConstant.java | 29 -- .../holmes/rulemgt/db/CorrelationRuleDao.java | 107 ------ .../holmes/rulemgt/db/CorrelationRuleQueryDao.java | 129 ------- .../rulemgt/db/mapper/CorrelationRuleMapper.java | 48 --- .../holmes/rulemgt/resources/RuleMgtResources.java | 168 --------- .../holmes/rulemgt/wrapper/RuleMgtWrapper.java | 245 ------------ .../org/onap/holmes/rulemgt/RuleActiveAppTest.java | 36 ++ .../org/onap/holmes/rulemgt/RuleAppConfigTest.java | 59 +++ .../request/CorrelationCheckRule4EngineTest.java | 32 ++ .../request/CorrelationDeployRule4EngineTest.java | 40 ++ .../bean/request/CorrelationRestRequestTest.java | 34 ++ .../bean/request/RuleCreateRequestTest.java | 58 +++ .../bean/request/RuleDeleteRequestTest.java | 33 ++ .../bean/request/RuleQueryConditionTest.java | 65 ++++ .../bean/request/RuleUpdateRequestTest.java | 58 +++ .../response/RuleAddAndUpdateResponseTest.java | 32 ++ .../bean/response/RuleQueryListResponseTest.java | 45 +++ .../rulemgt/bean/response/RuleResult4APITest.java | 99 +++++ .../rulemgt/bolt/enginebolt/EngineServiceTest.java | 56 +++ .../rulemgt/bolt/enginebolt/EngineWrapperTest.java | 183 +++++++++ .../rulemgt/db/CorrelationRuleQueryDaoTest.java | 161 ++++++++ .../db/mapper/CorrelationRuleMapperTest.java | 52 +++ .../rulemgt/resources/RuleMgtResourcesTest.java | 182 +++++++++ .../holmes/rulemgt/wrapper/RuleMgtWrapperTest.java | 411 ++++++++++++++++++++ .../openo/holmes/rulemgt/RuleActiveAppTest.java | 36 -- .../openo/holmes/rulemgt/RuleAppConfigTest.java | 59 --- .../request/CorrelationCheckRule4EngineTest.java | 32 -- .../request/CorrelationDeployRule4EngineTest.java | 40 -- .../bean/request/CorrelationRestRequestTest.java | 34 -- .../bean/request/RuleCreateRequestTest.java | 58 --- .../bean/request/RuleDeleteRequestTest.java | 33 -- .../bean/request/RuleQueryConditionTest.java | 65 ---- .../bean/request/RuleUpdateRequestTest.java | 58 --- .../response/RuleAddAndUpdateResponseTest.java | 32 -- .../bean/response/RuleQueryListResponseTest.java | 45 --- .../rulemgt/bean/response/RuleResult4APITest.java | 99 ----- .../rulemgt/bolt/enginebolt/EngineServiceTest.java | 56 --- .../rulemgt/bolt/enginebolt/EngineWrapperTest.java | 184 --------- .../rulemgt/db/CorrelationRuleQueryDaoTest.java | 162 -------- .../db/mapper/CorrelationRuleMapperTest.java | 52 --- .../rulemgt/resources/RuleMgtResourcesTest.java | 182 --------- .../holmes/rulemgt/wrapper/RuleMgtWrapperTest.java | 412 --------------------- 77 files changed, 2947 insertions(+), 2969 deletions(-) create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAppConfig.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequest.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleActiveApp.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequest.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequest.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleQueryCondition.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequest.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponse.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleResult4API.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java delete mode 100644 rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleActiveAppTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleAppConfigTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequestTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleQueryConditionTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleResult4APITest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequestTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleQueryConditionTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleResult4APITest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java delete mode 100644 rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java (limited to 'rulemgt') diff --git a/rulemgt/pom.xml b/rulemgt/pom.xml index e4af4c6..a4e1388 100644 --- a/rulemgt/pom.xml +++ b/rulemgt/pom.xml @@ -19,9 +19,9 @@ 4.0.0 - org.openo.holmes.rule-management + org.onap.holmes.rule-management holmes-rulemgt-parent - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT holmes-rulemgt @@ -31,8 +31,9 @@ - org.openo.holmes.actions + org.onap.holmes.common holmes-actions + 1.0.0-SNAPSHOT org.glassfish.jersey.containers @@ -44,11 +45,6 @@ - - org.glassfish.jersey.containers - jersey-container-servlet-core - 2.22.2 - org.easymock easymock @@ -103,10 +99,6 @@ org.slf4j slf4j-api - - org.openo.client.cli - swagger-sdk - io.swagger swagger-jersey2-jaxrs @@ -241,7 +233,7 @@ implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> - org.openo.holmes.rulemgt.RuleActiveApp + org.onap.holmes.rulemgt.RuleActiveApp diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java new file mode 100644 index 0000000..8dc697c --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java @@ -0,0 +1,61 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt; + +import io.dropwizard.setup.Environment; +import java.io.IOException; +import lombok.extern.slf4j.Slf4j; +import org.onap.holmes.common.dropwizard.ioc.bundle.IOCApplication; +import org.onap.holmes.common.api.entity.ServiceRegisterEntity; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.utils.MSBRegisterUtil; + +@Slf4j +public class RuleActiveApp extends IOCApplication< RuleAppConfig > { + + public static void main( String[] args ) throws Exception { + new RuleActiveApp().run( args ); + } + + @Override + public String getName() { + return "Holmes Rule Management ActiveApp APP "; + } + + @Override + public void run(RuleAppConfig configuration, Environment environment) throws Exception { + super.run(configuration, environment); + + try { + new MSBRegisterUtil().register(initServiceEntity()); + } catch (IOException e) { + log.warn("Micro service registry httpclient close failure",e); + } + + } + + private ServiceRegisterEntity initServiceEntity() { + ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity(); + serviceRegisterEntity.setServiceName("holmes-rule-mgmt"); + serviceRegisterEntity.setProtocol("REST"); + serviceRegisterEntity.setVersion("v1"); + serviceRegisterEntity.setUrl("/onapapi/holmes-rule-mgmt/v1"); + serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0); + serviceRegisterEntity.setVisualRange("1|0"); + return serviceRegisterEntity; + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAppConfig.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAppConfig.java new file mode 100644 index 0000000..20f96c2 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAppConfig.java @@ -0,0 +1,58 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.dropwizard.Configuration; +import io.dropwizard.db.DataSourceFactory; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import org.hibernate.validator.constraints.NotEmpty; +import org.jvnet.hk2.annotations.Service; + +@Service +public class RuleAppConfig extends Configuration { + + @NotEmpty + private String defaultName = "Holmes Rule Management"; + + @NotEmpty + private String apidescription = "Holmes rule management rest API"; + + @Valid + @NotNull + private DataSourceFactory database = new DataSourceFactory(); + + @JsonProperty("database") + public DataSourceFactory getDataSourceFactory() { + return database; + } + + @JsonProperty("database") + public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { + this.database = dataSourceFactory; + } + + + public String getApidescription() { + return apidescription; + } + + public void setApidescription(String apidescription) { + this.apidescription = apidescription; + } + +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java new file mode 100644 index 0000000..c36fce7 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java @@ -0,0 +1,27 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CorrelationCheckRule4Engine { + + private String content; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java new file mode 100644 index 0000000..7c94395 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java @@ -0,0 +1,31 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CorrelationDeployRule4Engine { + @JsonProperty(value = "content") + private String content; + + @JsonProperty(value = "engineid") + private String engineId; + +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequest.java new file mode 100644 index 0000000..e79cc62 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequest.java @@ -0,0 +1,27 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CorrelationRestRequest { + + private String rootURL; +} \ No newline at end of file diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java new file mode 100644 index 0000000..f090c3b --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class RuleCreateRequest { + @JsonProperty(value = "rulename") + private String ruleName; + @JsonProperty + private String description; + @JsonProperty + private String content; + @JsonProperty + private int enabled; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java new file mode 100644 index 0000000..7cc4fff --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java @@ -0,0 +1,28 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + + +@Setter +@Getter +public class RuleDeleteRequest { + @JsonProperty(value = "ruleid") + private String ruleId; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java new file mode 100644 index 0000000..f3b9b91 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java @@ -0,0 +1,34 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RuleQueryCondition { + + @JsonProperty(value = "ruleid") + private String rid; + @JsonProperty(value = "rulename") + private String name; + private int enabled; + private String creator; + private String modifier; + +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java new file mode 100644 index 0000000..e2811e3 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RuleUpdateRequest { + @JsonProperty + private String description; + @JsonProperty + private String content; + @JsonProperty + private int enabled; + @JsonProperty(value="ruleid") + private String ruleId; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java new file mode 100644 index 0000000..7fe432e --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java @@ -0,0 +1,29 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@JsonInclude(JsonInclude.Include.ALWAYS) +@Getter +@Setter +public class RuleAddAndUpdateResponse{ + @JsonProperty(value="ruleid") + private String ruleId; +} 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 new file mode 100644 index 0000000..17903ab --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java @@ -0,0 +1,34 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +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") + private List correlationRules = new ArrayList(); + @JsonProperty(value = "totalcount") + private int totalCount; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java new file mode 100644 index 0000000..c444b22 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java @@ -0,0 +1,42 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; + +@JsonInclude(JsonInclude.Include.ALWAYS) +@Setter +@Getter +public class RuleResult4API { + @JsonProperty(value = "ruleid") + private String ruleId; + @JsonProperty(value = "rulename") + private String ruleName; + private String description; + private String content; + @JsonProperty(value = "createtime") + private Date createTime; + private String creator; + @JsonProperty(value = "updatetime") + private Date updateTime; + private String modifier; + private int enabled; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java new file mode 100644 index 0000000..f3e8d5c --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -0,0 +1,66 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bolt.enginebolt; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; +import org.glassfish.jersey.client.ClientConfig; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import org.onap.holmes.common.config.MicroServiceConfig; + +@Slf4j +@Service +public class EngineService { + + protected Response delete(String packageName) throws IOException { + Client client = createClient(); + WebTarget webTarget = client + .target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName); + return webTarget.request(MediaType.APPLICATION_JSON).delete(); + } + + private Client createClient() { + ClientConfig clientConfig = new ClientConfig(); + return ClientBuilder.newClient(clientConfig); + } + + protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine) + throws IOException { + Client client = createClient(); + ObjectMapper mapper = new ObjectMapper(); + String content = mapper.writeValueAsString(correlationCheckRule4Engine); + WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH); + return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON)); + } + + protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException { + Client client = createClient(); + ObjectMapper mapper = new ObjectMapper(); + String content = mapper.writeValueAsString(correlationDeployRule4Engine); + WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH); + return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON)); + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java new file mode 100644 index 0000000..733cae0 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -0,0 +1,87 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bolt.enginebolt; + +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONObject; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import org.onap.holmes.common.exception.CorrelationException; + +@Service +@Slf4j +public class EngineWrapper { + + @Inject + private EngineService engineService; + + public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException { + Response response; + try { + response = engineService.deploy(correlationRule); + } catch (Exception e) { + throw new CorrelationException("Failed to call the rule deployment RESTful API.", e); + } + if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { + log.info("Succeeded in calling the rule deployment RESTful API from the engine management service."); + try { + JSONObject json = JSONObject.fromObject(response.readEntity(String.class)); + return json.get(RuleMgtConstant.PACKAGE).toString(); + } catch (Exception e) { + throw new CorrelationException("Failed to parse the value returned by the engine management service.", e); + } + } else { + throw new CorrelationException("Failed to deploy the rule!"); + } + } + + public boolean deleteRuleFromEngine(String packageName) throws CorrelationException { + Response response; + try { + response = engineService.delete(packageName); + } catch (Exception e) { + throw new CorrelationException("Failed to call the rule deleting RESTful API.", e); + } + if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { + log.info("Succeeded in calling the rule deleting RESTful API from the engine management service."); + return true; + } else { + throw new CorrelationException("Failed to delete the rule!"); + } + } + + public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine) + throws CorrelationException { + log.info("Rule Contents: " + correlationCheckRule4Engine.getContent()); + Response response; + try { + response = engineService.check(correlationCheckRule4Engine); + } catch (Exception e) { + throw new CorrelationException("Failed to call the rule verification RESTful API.", e); + } + if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { + log.info("Succeeded in calling the rule verification RESTful API from the engine management service."); + return true; + } else { + log.info(response.getStatus() + " " + response.getStatusInfo() + " " + response.getEntity()); + throw new CorrelationException("Failed to verify the rule. The contents of the rule are invalid."); + } + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java new file mode 100644 index 0000000..debfb05 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java @@ -0,0 +1,29 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.constant; + +public class RuleMgtConstant { + + private RuleMgtConstant() { + + } + public static final int STATUS_RULE_OPEN = 1; + public static final int STATUS_RULE_CLOSE = 0; + public static final int STATUS_RULE_ALL = 2; + public static final String PACKAGE = "package"; + public static final String ENGINE_PATH = "/onapapi/holmes-engine-mgmt/v1/rule"; + public static final int RESPONSE_STATUS_OK = 200; +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java new file mode 100644 index 0000000..2a422c4 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java @@ -0,0 +1,106 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.db; + +import java.util.List; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.rulemgt.db.mapper.CorrelationRuleMapper; +import org.skife.jdbi.v2.sqlobject.Bind; +import org.skife.jdbi.v2.sqlobject.BindBean; +import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys; +import org.skife.jdbi.v2.sqlobject.SqlQuery; +import org.skife.jdbi.v2.sqlobject.SqlUpdate; +import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; + +@RegisterMapper(CorrelationRuleMapper.class) +public abstract class CorrelationRuleDao { + + @GetGeneratedKeys + @SqlUpdate("INSERT INTO APLUS_RULE (NAME,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID) VALUES (:name,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid)") + protected abstract int addRule(@BindBean CorrelationRule correlationRule); + + @SqlUpdate("UPDATE APLUS_RULE SET DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime WHERE RID=:rid") + protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule); + + @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid") + protected abstract int deleteRuleByRid(@Bind("rid") String rid); + + @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid AND NAME=:name") + protected abstract int deleteRuleByRidAndName(@Bind("rid") String rid, @Bind("name") String name); + + @SqlQuery("SELECT * FROM APLUS_RULE") + protected abstract List queryAllRules(); + + @SqlQuery("SELECT * FROM APLUS_RULE WHERE RID=:rid") + protected abstract CorrelationRule queryRuleById(@Bind("rid") String rid); + + @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name") + protected abstract CorrelationRule queryRuleByName(@Bind("name") String name); + + private void deleteRule2DbInner(CorrelationRule correlationRule) { + String name = correlationRule.getName() != null ? correlationRule.getName().trim() : ""; + String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : ""; + if (!"".equals(name) && !"".equals(rid)) { + deleteRuleByRidAndName(rid, name); + } else if (!"".equals(rid)) { + deleteRuleByRid(rid); + } + } + + public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException { + try { + addRule(correlationRule); + return correlationRule; + } catch (Exception e) { + throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e); + } + } + + public void updateRule(CorrelationRule correlationRule) throws CorrelationException { + try { + updateRuleByRid(correlationRule); + } catch (Exception e) { + throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e); + } + } + + public void deleteRule(CorrelationRule correlationRule) throws CorrelationException { + try { + deleteRule2DbInner(correlationRule); + } catch (Exception e) { + throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e); + } + } + + + public CorrelationRule queryRuleByRid(String rid) throws CorrelationException { + try { + return queryRuleById(rid); + } catch (Exception e) { + throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e); + } + } + + public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException { + try { + return queryRuleByName(name); + } catch (Exception e) { + throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e); + } + } +} + diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java new file mode 100644 index 0000000..322006d --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java @@ -0,0 +1,128 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.db; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import javax.inject.Inject; +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.DbDaoUtil; +import org.skife.jdbi.v2.Handle; +import org.skife.jdbi.v2.Query; + +@Service +@Slf4j +public class CorrelationRuleQueryDao { + + @Inject + private DbDaoUtil dbDaoUtil; + + public List getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition) + throws CorrelationException { + List correlationRules = new ArrayList(); + Handle handle = null; + String whereStr = getWhereStrByRequestEntity(ruleQueryCondition); + try { + StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr); + handle = dbDaoUtil.getHandle(); + Query query = handle.createQuery(querySql.toString()); + for (Object value : query.list()) { + CorrelationRule correlationRule = getCorrelationRule((Map) value); + correlationRules.add(correlationRule); + } + return correlationRules; + } catch (Exception e) { + log.warn("Failed to query the rule: id =" + ruleQueryCondition.getRid() + "."); + throw new CorrelationException("Failed to query the rule.", e); + } finally { + dbDaoUtil.close(handle); + } + } + + private CorrelationRule getCorrelationRule(Map value) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setName((String) value.get("name")); + correlationRule.setRid((String) value.get("rid")); + correlationRule.setDescription((String) value.get("description")); + correlationRule.setEnabled((Integer) value.get("enable")); + correlationRule.setTemplateID((Integer) value.get("templateID")); + correlationRule.setEngineID((String) value.get("engineID")); + correlationRule.setEngineType((String) value.get("engineType")); + correlationRule.setCreator((String) value.get("creator")); + correlationRule.setCreateTime((Date) value.get("createTime")); + correlationRule.setModifier((String) value.get("updator")); + correlationRule.setUpdateTime((Date) value.get("updateTime")); + correlationRule.setParams((Properties) value.get("params")); + correlationRule.setContent((String) value.get("content")); + correlationRule.setVendor((String) value.get("vendor")); + correlationRule.setPackageName((String) value.get("package")); + return correlationRule; + } + + private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException { + try { + Class clazz = ruleQueryCondition.getClass(); + Field[] fields = clazz.getDeclaredFields(); + String whereSql = " WHERE "; + + for (Field field : fields) { + // Jacoco will cause an exception when calculating the coverage of the UT + // Remove this if jacoco solves this problem in the future + if (field.getName().contains("jacoco")) { + continue; + } + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), + clazz); + Method getMethod = pd.getReadMethod(); + Object o = getMethod.invoke(ruleQueryCondition); + if (o != null) { + String tempName = field.getName(); + if ("enabled".equals(tempName)) { + int enabled = (int) o; + if (enabled != RuleMgtConstant.STATUS_RULE_ALL) { + whereSql = whereSql + "enable =" + enabled; + whereSql += " AND "; + } + } else if ("name".equals(tempName)) { + if (!"".equals(o.toString().trim())) { + whereSql = whereSql + field.getName() + " like '%" + o + "%' AND "; + } + } else if (!"".equals(o.toString().trim())) { + whereSql = whereSql + field.getName() + "='" + o + "' AND "; + } + } + } + whereSql = whereSql.trim(); + if(!"WHERE".equals(whereSql)){ + return whereSql.substring(0, whereSql.length() - "AND".length()); + } + return ""; + } catch (Exception e) { + throw new CorrelationException("An error occurred while building the query SQL.", e); + } + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java new file mode 100644 index 0000000..d40253f --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java @@ -0,0 +1,48 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.db.mapper; + +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.skife.jdbi.v2.StatementContext; +import org.skife.jdbi.v2.tweak.ResultSetMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +public class CorrelationRuleMapper implements ResultSetMapper { + @Override + public CorrelationRule map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setName(resultSet.getString("name")); + correlationRule.setRid(resultSet.getString("rid")); + correlationRule.setDescription(resultSet.getString("description")); + correlationRule.setEnabled(resultSet.getInt("enable")); + correlationRule.setTemplateID(resultSet.getInt("templateID")); + correlationRule.setEngineID(resultSet.getString("engineID")); + correlationRule.setEngineType(resultSet.getString("engineType")); + correlationRule.setCreator(resultSet.getString("creator")); + correlationRule.setCreateTime(resultSet.getDate("createTime")); + correlationRule.setModifier(resultSet.getString("updator")); + correlationRule.setUpdateTime(resultSet.getDate("updateTime")); + correlationRule.setParams((Properties)resultSet.getObject("params")); + correlationRule.setContent(resultSet.getString("content")); + correlationRule.setVendor(resultSet.getString("vendor")); + correlationRule.setPackageName(resultSet.getString("package")); + return correlationRule; + } + +} 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 new file mode 100644 index 0000000..04578f7 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java @@ -0,0 +1,161 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.resources; + +import com.codahale.metrics.annotation.Timed; +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; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONObject; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.entity.ServiceRegisterEntity; +import org.onap.holmes.common.config.MicroServiceConfig; +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.LanguageUtil; +import org.onap.holmes.common.utils.MSBRegisterUtil; +import org.onap.holmes.common.utils.UserUtil; +import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; +import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest; +import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; +import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; +import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; + +@Service +@SwaggerDefinition +@Path("/rule") +@Api(tags = {"CorrelationRules"}) +@Produces(MediaType.APPLICATION_JSON) +@Slf4j +public class RuleMgtResources { + + @Inject + private RuleMgtWrapper ruleMgtWrapper; + + @PUT + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class) + @Timed + public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request, + @ApiParam(value = "alarm+ rule create request.
[rulename]:required
[content]:required
[enabled]:required", required = true) RuleCreateRequest ruleCreateRequest) { + Locale locale = LanguageUtil.getLocale(request); + RuleAddAndUpdateResponse ruleChangeResponse; + try { + ruleChangeResponse = ruleMgtWrapper + .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest); + log.info("create rule:" + ruleCreateRequest.getRuleName() + " success."); + return ruleChangeResponse; + } catch (CorrelationException e) { + log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e); + throw ExceptionUtil.buildExceptionResponse(e.getMessage()); + } + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class) + @Timed + public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request, + @ApiParam(value = "alarm+ rule update request.
[ruleid]:required", required = true) RuleUpdateRequest ruleUpdateRequest) { + Locale locale = LanguageUtil.getLocale(request); + RuleAddAndUpdateResponse ruleChangeResponse; + try { + ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); + log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful"); + return ruleChangeResponse; + } catch (CorrelationException e) { + log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e); + throw ExceptionUtil.buildExceptionResponse(e.getMessage()); + } + } + + @DELETE + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.") + @Timed + public boolean deleteCorrelationRule(@Context HttpServletRequest request, + @ApiParam(value = "alarm+ rule delete request.
[ruleid]:required", required = true) RuleDeleteRequest ruleDeleteRequest) { + Locale locale = LanguageUtil.getLocale(request); + try { + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful"); + return true; + } catch (CorrelationException e) { + log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e); + throw ExceptionUtil.buildExceptionResponse(e.getMessage()); + } + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class) + @Timed + public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request, + @ApiParam(value = "query condition:
" + " [ruleid]:Rule ID;
" + + "[rulename]:Rule name;
" + "[creator]:creator of the rule;
" + + "[modifier]:modifier of the rule;
" + + "[enabled]: 0 is Enabled,1 is disabled;
for example:
{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) { + Locale locale = LanguageUtil.getLocale(request); + RuleQueryListResponse ruleQueryListResponse; + RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request); + try { + ruleQueryListResponse = ruleMgtWrapper + .getCorrelationRuleByCondition(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); + throw ExceptionUtil.buildExceptionResponse(e.getMessage()); + } + } + + 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); + } + return ruleQueryCondition; + } catch (IOException e) { + log.warn("queryRequest convert to json failed", e); + throw ExceptionUtil.buildExceptionResponse("The request format is invalid!"); + } + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java new file mode 100644 index 0000000..beffa56 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java @@ -0,0 +1,244 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.wrapper; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import javax.inject.Singleton; +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; +import org.onap.holmes.rulemgt.bean.response.RuleResult4API; +import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import org.onap.holmes.rulemgt.db.CorrelationRuleDao; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; +import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest; +import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; +import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; +import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper; +import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao; + + +@Service +@Singleton +@Slf4j +public class RuleMgtWrapper { + + @Inject + private CorrelationRuleQueryDao correlationRuleQueryDao; + @Inject + private EngineWrapper engineWarpper; + @Inject + private DbDaoUtil daoUtil; + + private CorrelationRuleDao correlationRuleDao; + + @PostConstruct + public void initDaoUtil() { + correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class); + } + + public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest) + throws CorrelationException { + if (ruleCreateRequest == null) { + throw new CorrelationException("The request object can not be empty!"); + } + CorrelationRule correlationRule = convertCreateRequest2Rule(creator, + ruleCreateRequest); + checkCorrelation(correlationRule); + CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName()); + if (ruleTemp != null) { + throw new CorrelationException("A rule with the same name already exists."); + } + correlationRule.setPackageName(deployRule2Engine(correlationRule)); + CorrelationRule result = correlationRuleDao.saveRule(correlationRule); + RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse(); + ruleAddAndUpdateResponse.setRuleId(result.getRid()); + return ruleAddAndUpdateResponse; + } + + public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest) + throws CorrelationException { + if (ruleUpdateRequest == null) { + throw new CorrelationException("The request object can not be empty!"); + } + CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId()); + if (oldCorrelationRule == null) { + throw new CorrelationException("You're trying to update a rule which does not exist in the system."); + } + CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier, + ruleUpdateRequest, oldCorrelationRule.getName()); + checkCorrelation(newCorrelationRule); + RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse(); + ruleChangeResponse.setRuleId(newCorrelationRule.getRid()); + if (!haveChange(newCorrelationRule, oldCorrelationRule)) { + return ruleChangeResponse; + } + if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) { + engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName()); + } + newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule)); + correlationRuleDao.updateRule(newCorrelationRule); + return ruleChangeResponse; + } + + private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException { + int enabled = correlationRule.getEnabled(); + String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim(); + String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim(); + if ("".equals(content)) { + throw new CorrelationException("The contents of the rule can not be empty!"); + } + if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE + && enabled != RuleMgtConstant.STATUS_RULE_OPEN) { + throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed."); + } + if ("".equals(ruleName)) { + throw new CorrelationException("The name of the rule can not be empty."); + } + } + + private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) { + String newContent = newCorrelationRule.getContent(); + String oldContent = oldCorrelationRule.getContent(); + int newEnabled = newCorrelationRule.getEnabled(); + int oldEnabled = oldCorrelationRule.getEnabled(); + String newDes = newCorrelationRule.getDescription(); + String oldDes = oldCorrelationRule.getDescription(); + if (newContent.equals(oldContent) && newEnabled == oldEnabled && newDes.equals(oldDes)) { + return false; + } + return true; + } + + public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest) + throws CorrelationException { + if (ruleDeleteRequest == null) { + throw new CorrelationException("The request object can not be empty!"); + } + CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId()); + if (correlationRule == null) { + log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database."); + throw new CorrelationException("You're trying to delete a rule which does not exist in the system."); + } + if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) { + engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName()); + } + correlationRuleDao.deleteRule(correlationRule); + } + + private CorrelationRule convertCreateRequest2Rule(String userName, + RuleCreateRequest ruleCreateRequest) throws CorrelationException { + String tempContent = ruleCreateRequest.getContent(); + CorrelationRule correlationRule = new CorrelationRule(); + String ruleId = "rule_" + System.currentTimeMillis(); + String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription(); + correlationRule.setRid(ruleId); + if (tempContent != null) { + correlationRule.setContent(tempContent.trim()); + } + correlationRule.setDescription(description); + correlationRule.setCreateTime(new Date()); + correlationRule.setUpdateTime(new Date()); + correlationRule.setName(ruleCreateRequest.getRuleName()); + correlationRule.setEngineID("correlation-d"); + correlationRule.setEngineType(""); + correlationRule.setTemplateID(0); + correlationRule.setVendor(""); + correlationRule.setCreator(userName); + correlationRule.setModifier(userName); + correlationRule.setEnabled(ruleCreateRequest.getEnabled()); + return correlationRule; + } + + private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier, + RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException { + CorrelationRule correlationRule = new CorrelationRule(); + String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription(); + correlationRule.setRid(ruleUpdateRequest.getRuleId()); + correlationRule.setContent(ruleUpdateRequest.getContent()); + correlationRule.setDescription(description); + correlationRule.setEnabled(ruleUpdateRequest.getEnabled()); + correlationRule.setUpdateTime(new Date()); + correlationRule.setModifier(modifier); + correlationRule.setName(ruleName); + return correlationRule; + } + + private String deployRule2Engine(CorrelationRule correlationRule) + throws CorrelationException { + if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && ( + correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) { + return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule)); + } + return ""; + } + + public RuleQueryListResponse getCorrelationRuleByCondition( + RuleQueryCondition ruleQueryCondition) throws CorrelationException { + List correlationRule = correlationRuleQueryDao + .getCorrelationRulesByCondition(ruleQueryCondition); + RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); + ruleQueryListResponse.setTotalCount(correlationRule.size()); + ruleQueryListResponse + .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule)); + return ruleQueryListResponse; + } + + private List correlationRules2RuleResult4APIs( + List correlationRules) { + List ruleResult4APIs = new ArrayList(); + for (CorrelationRule correlationRule : correlationRules) { + RuleResult4API ruleResult4API = new RuleResult4API(); + String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription(); + ruleResult4API.setRuleId(correlationRule.getRid()); + ruleResult4API.setRuleName(correlationRule.getName()); + ruleResult4API.setDescription(description); + ruleResult4API.setContent(correlationRule.getContent()); + ruleResult4API.setCreateTime(correlationRule.getCreateTime()); + ruleResult4API.setCreator(correlationRule.getCreator()); + ruleResult4API.setUpdateTime(correlationRule.getUpdateTime()); + ruleResult4API.setModifier(correlationRule.getModifier()); + ruleResult4API.setEnabled(correlationRule.getEnabled()); + ruleResult4APIs.add(ruleResult4API); + } + return ruleResult4APIs; + } + + private CorrelationDeployRule4Engine correlationRules2DeployRule( + CorrelationRule correlationRule) { + CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); + correlationDeployRule4Engine.setContent(correlationRule.getContent()); + correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID()); + return correlationDeployRule4Engine; + } + + private CorrelationCheckRule4Engine correlationRules2CheckRule( + CorrelationRule correlationRule) { + CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine(); + correlationCheckRule4Engine.setContent(correlationRule.getContent()); + return correlationCheckRule4Engine; + } +} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleActiveApp.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleActiveApp.java deleted file mode 100644 index 96a1f00..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleActiveApp.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt; - -import io.dropwizard.setup.Environment; -import java.io.IOException; -import lombok.extern.slf4j.Slf4j; -import org.openo.dropwizard.ioc.bundle.IOCApplication; -import org.openo.holmes.common.api.entity.ServiceRegisterEntity; -import org.openo.holmes.common.config.MicroServiceConfig; -import org.openo.holmes.common.utils.MSBRegisterUtil; - -@Slf4j -public class RuleActiveApp extends IOCApplication < RuleAppConfig > { - - public static void main( String[] args ) throws Exception { - new RuleActiveApp().run( args ); - } - - @Override - public String getName() { - return "Holmes Rule Management ActiveApp APP "; - } - - @Override - public void run(RuleAppConfig configuration, Environment environment) throws Exception { - super.run(configuration, environment); - - try { - new MSBRegisterUtil().register(initServiceEntity()); - } catch (IOException e) { - log.warn("Micro service registry httpclient close failure",e); - } - - } - - private ServiceRegisterEntity initServiceEntity() { - ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity(); - serviceRegisterEntity.setServiceName("holmes-rule-mgmt"); - serviceRegisterEntity.setProtocol("REST"); - serviceRegisterEntity.setVersion("v1"); - serviceRegisterEntity.setUrl("/openoapi/holmes-rule-mgmt/v1"); - serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0); - serviceRegisterEntity.setVisualRange("1|0"); - return serviceRegisterEntity; - } -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java deleted file mode 100644 index 4fcb486..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt; - -import com.fasterxml.jackson.annotation.JsonProperty; -import io.dropwizard.Configuration; -import io.dropwizard.db.DataSourceFactory; -import javax.validation.Valid; -import javax.validation.constraints.NotNull; -import org.hibernate.validator.constraints.NotEmpty; -import org.jvnet.hk2.annotations.Service; - -@Service -public class RuleAppConfig extends Configuration { - - @NotEmpty - private String defaultName = "Holmes Rule Management"; - - @NotEmpty - private String apidescription = "Holmes rule management rest API"; - - @Valid - @NotNull - private DataSourceFactory database = new DataSourceFactory(); - - @JsonProperty("database") - public DataSourceFactory getDataSourceFactory() { - return database; - } - - @JsonProperty("database") - public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { - this.database = dataSourceFactory; - } - - - public String getApidescription() { - return apidescription; - } - - public void setApidescription(String apidescription) { - this.apidescription = apidescription; - } - -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java deleted file mode 100644 index 1475ce9..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class CorrelationCheckRule4Engine { - - private String content; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java deleted file mode 100644 index 3ed4c4d..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class CorrelationDeployRule4Engine { - @JsonProperty(value = "content") - private String content; - - @JsonProperty(value = "engineid") - private String engineId; - -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java deleted file mode 100644 index 463eb2a..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class CorrelationRestRequest { - - private String rootURL; -} \ No newline at end of file diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequest.java deleted file mode 100644 index 9407b99..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequest.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@Setter -@Getter -public class RuleCreateRequest { - @JsonProperty(value = "rulename") - private String ruleName; - @JsonProperty - private String description; - @JsonProperty - private String content; - @JsonProperty - private int enabled; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequest.java deleted file mode 100644 index ac9d795..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequest.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - - -@Setter -@Getter -public class RuleDeleteRequest { - @JsonProperty(value = "ruleid") - private String ruleId; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleQueryCondition.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleQueryCondition.java deleted file mode 100644 index c68b40d..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleQueryCondition.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class RuleQueryCondition { - - @JsonProperty(value = "ruleid") - private String rid; - @JsonProperty(value = "rulename") - private String name; - private int enabled; - private String creator; - private String modifier; - -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequest.java deleted file mode 100644 index 6ce265a..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequest.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class RuleUpdateRequest { - @JsonProperty - private String description; - @JsonProperty - private String content; - @JsonProperty - private int enabled; - @JsonProperty(value="ruleid") - private String ruleId; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java deleted file mode 100644 index e5597b8..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@JsonInclude(JsonInclude.Include.ALWAYS) -@Getter -@Setter -public class RuleAddAndUpdateResponse{ - @JsonProperty(value="ruleid") - private String ruleId; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponse.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponse.java deleted file mode 100644 index a720eca..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponse.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -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") - private List correlationRules = new ArrayList(); - @JsonProperty(value = "totalcount") - private int totalCount; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleResult4API.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleResult4API.java deleted file mode 100644 index ee948d0..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/response/RuleResult4API.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -import java.util.Date; - -@JsonInclude(JsonInclude.Include.ALWAYS) -@Setter -@Getter -public class RuleResult4API { - @JsonProperty(value = "ruleid") - private String ruleId; - @JsonProperty(value = "rulename") - private String ruleName; - private String description; - private String content; - @JsonProperty(value = "createtime") - private Date createTime; - private String creator; - @JsonProperty(value = "updatetime") - private Date updateTime; - private String modifier; - private int enabled; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java deleted file mode 100644 index e792fbf..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bolt.enginebolt; - -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import lombok.extern.slf4j.Slf4j; -import org.glassfish.jersey.client.ClientConfig; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.config.MicroServiceConfig; -import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.openo.holmes.rulemgt.constant.RuleMgtConstant; - -@Slf4j -@Service -public class EngineService { - - protected Response delete(String packageName) throws IOException { - Client client = createClient(); - WebTarget webTarget = client - .target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName); - return webTarget.request(MediaType.APPLICATION_JSON).delete(); - } - - private Client createClient() { - ClientConfig clientConfig = new ClientConfig(); - return ClientBuilder.newClient(clientConfig); - } - - protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine) - throws IOException { - Client client = createClient(); - ObjectMapper mapper = new ObjectMapper(); - String content = mapper.writeValueAsString(correlationCheckRule4Engine); - WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH); - return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON)); - } - - protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException { - Client client = createClient(); - ObjectMapper mapper = new ObjectMapper(); - String content = mapper.writeValueAsString(correlationDeployRule4Engine); - WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH); - return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON)); - } -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java deleted file mode 100644 index 8e9055b..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bolt.enginebolt; - -import javax.inject.Inject; -import javax.ws.rs.core.Response; -import lombok.extern.slf4j.Slf4j; -import net.sf.json.JSONObject; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.openo.holmes.rulemgt.constant.RuleMgtConstant; - -@Service -@Slf4j -public class EngineWrapper { - - @Inject - private EngineService engineService; - - public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException { - Response response; - try { - response = engineService.deploy(correlationRule); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED, e); - } - if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Call deploy rule rest interface in engine successfully."); - try { - JSONObject json = JSONObject.fromObject(response.readEntity(String.class)); - return json.get(RuleMgtConstant.PACKAGE).toString(); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e); - } - } else { - throw new CorrelationException(I18nProxy.ENGINE_DEPLOY_RULE_FAILED); - } - } - - public boolean deleteRuleFromEngine(String packageName) throws CorrelationException { - Response response; - try { - response = engineService.delete(packageName); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED, e); - } - if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Call delete rule rest interface in engine successfully."); - return true; - } else { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED); - } - } - - public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine) - throws CorrelationException { - log.info("content:" + correlationCheckRule4Engine.getContent()); - Response response; - try { - response = engineService.check(correlationCheckRule4Engine); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED, e); - } - if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Call check rule rest interface in engine successfully."); - return true; - } else { - log.info(response.getStatus() + " " + response.getStatusInfo() + " " + response.getEntity()); - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS); - } - } -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java deleted file mode 100644 index a9b891a..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.constant; - -public class RuleMgtConstant { - - private RuleMgtConstant() { - - } - public static final int STATUS_RULE_OPEN = 1; - public static final int STATUS_RULE_CLOSE = 0; - public static final int STATUS_RULE_ALL = 2; - public static final String PACKAGE = "package"; - public static final String ENGINE_PATH = "/openoapi/holmes-engine-mgmt/v1/rule"; - public static final int RESPONSE_STATUS_OK = 200; -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java deleted file mode 100644 index 3a7f300..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.db; - -import java.util.List; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.db.mapper.CorrelationRuleMapper; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.BindBean; -import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.SqlUpdate; -import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; - -@RegisterMapper(CorrelationRuleMapper.class) -public abstract class CorrelationRuleDao { - - @GetGeneratedKeys - @SqlUpdate("INSERT INTO APLUS_RULE (NAME,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID) VALUES (:name,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid)") - protected abstract int addRule(@BindBean CorrelationRule correlationRule); - - @SqlUpdate("UPDATE APLUS_RULE SET DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime WHERE RID=:rid") - protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule); - - @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid") - protected abstract int deleteRuleByRid(@Bind("rid") String rid); - - @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid AND NAME=:name") - protected abstract int deleteRuleByRidAndName(@Bind("rid") String rid, @Bind("name") String name); - - @SqlQuery("SELECT * FROM APLUS_RULE") - protected abstract List queryAllRules(); - - @SqlQuery("SELECT * FROM APLUS_RULE WHERE RID=:rid") - protected abstract CorrelationRule queryRuleById(@Bind("rid") String rid); - - @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name") - protected abstract CorrelationRule queryRuleByName(@Bind("name") String name); - - private void deleteRule2DbInner(CorrelationRule correlationRule) { - String name = correlationRule.getName() != null ? correlationRule.getName().trim() : ""; - String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : ""; - if (!"".equals(name) && !"".equals(rid)) { - deleteRuleByRidAndName(rid, name); - } else if (!"".equals(rid)) { - deleteRuleByRid(rid); - } - } - - public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException { - try { - addRule(correlationRule); - return correlationRule; - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR,e); - } - } - - public void updateRule(CorrelationRule correlationRule) throws CorrelationException { - try { - updateRuleByRid(correlationRule); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR,e); - } - } - - public void deleteRule(CorrelationRule correlationRule) throws CorrelationException { - try { - deleteRule2DbInner(correlationRule); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR,e); - } - } - - - public CorrelationRule queryRuleByRid(String rid) throws CorrelationException { - try { - return queryRuleById(rid); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR,e); - } - } - - public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException { - try { - return queryRuleByName(name); - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR,e); - } - } -} - diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java deleted file mode 100644 index 487684e..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.db; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import javax.inject.Inject; -import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.DbDaoUtil; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.openo.holmes.rulemgt.constant.RuleMgtConstant; -import org.skife.jdbi.v2.Handle; -import org.skife.jdbi.v2.Query; - -@Service -@Slf4j -public class CorrelationRuleQueryDao { - - @Inject - private DbDaoUtil dbDaoUtil; - - public List getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition) - throws CorrelationException { - List correlationRules = new ArrayList(); - Handle handle = null; - String whereStr = getWhereStrByRequestEntity(ruleQueryCondition); - try { - StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr); - handle = dbDaoUtil.getHandle(); - Query query = handle.createQuery(querySql.toString()); - for (Object value : query.list()) { - CorrelationRule correlationRule = getCorrelationRule((Map) value); - correlationRules.add(correlationRule); - } - return correlationRules; - } catch (Exception e) { - log.warn("Query rule: rule id =" + ruleQueryCondition.getRid() + " failed"); - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED, e); - } finally { - dbDaoUtil.close(handle); - } - } - - private CorrelationRule getCorrelationRule(Map value) { - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setName((String) value.get("name")); - correlationRule.setRid((String) value.get("rid")); - correlationRule.setDescription((String) value.get("description")); - correlationRule.setEnabled((Integer) value.get("enable")); - correlationRule.setTemplateID((Integer) value.get("templateID")); - correlationRule.setEngineID((String) value.get("engineID")); - correlationRule.setEngineType((String) value.get("engineType")); - correlationRule.setCreator((String) value.get("creator")); - correlationRule.setCreateTime((Date) value.get("createTime")); - correlationRule.setModifier((String) value.get("updator")); - correlationRule.setUpdateTime((Date) value.get("updateTime")); - correlationRule.setParams((Properties) value.get("params")); - correlationRule.setContent((String) value.get("content")); - correlationRule.setVendor((String) value.get("vendor")); - correlationRule.setPackageName((String) value.get("package")); - return correlationRule; - } - - private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException { - try { - Class clazz = ruleQueryCondition.getClass(); - Field[] fields = clazz.getDeclaredFields(); - String whereSql = " WHERE "; - - for (Field field : fields) { - // Jacoco will cause an exception when calculating the coverage of the UT - // Remove this if jacoco solves this problem in the future - if (field.getName().contains("jacoco")) { - continue; - } - PropertyDescriptor pd = new PropertyDescriptor(field.getName(), - clazz); - Method getMethod = pd.getReadMethod(); - Object o = getMethod.invoke(ruleQueryCondition); - if (o != null) { - String tempName = field.getName(); - if ("enabled".equals(tempName)) { - int enabled = (int) o; - if (enabled != RuleMgtConstant.STATUS_RULE_ALL) { - whereSql = whereSql + "enable =" + enabled; - whereSql += " AND "; - } - } else if ("name".equals(tempName)) { - if (!"".equals(o.toString().trim())) { - whereSql = whereSql + field.getName() + " like '%" + o + "%' AND "; - } - } else if (!"".equals(o.toString().trim())) { - whereSql = whereSql + field.getName() + "='" + o + "' AND "; - } - } - } - whereSql = whereSql.trim(); - if(!"WHERE".equals(whereSql)){ - return whereSql.substring(0, whereSql.length() - "AND".length()); - } - return ""; - } catch (Exception e) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e); - } - } -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java deleted file mode 100644 index 437e0dd..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.db.mapper; - -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.skife.jdbi.v2.StatementContext; -import org.skife.jdbi.v2.tweak.ResultSetMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Properties; - -public class CorrelationRuleMapper implements ResultSetMapper { - @Override - public CorrelationRule map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setName(resultSet.getString("name")); - correlationRule.setRid(resultSet.getString("rid")); - correlationRule.setDescription(resultSet.getString("description")); - correlationRule.setEnabled(resultSet.getInt("enable")); - correlationRule.setTemplateID(resultSet.getInt("templateID")); - correlationRule.setEngineID(resultSet.getString("engineID")); - correlationRule.setEngineType(resultSet.getString("engineType")); - correlationRule.setCreator(resultSet.getString("creator")); - correlationRule.setCreateTime(resultSet.getDate("createTime")); - correlationRule.setModifier(resultSet.getString("updator")); - correlationRule.setUpdateTime(resultSet.getDate("updateTime")); - correlationRule.setParams((Properties)resultSet.getObject("params")); - correlationRule.setContent(resultSet.getString("content")); - correlationRule.setVendor(resultSet.getString("vendor")); - correlationRule.setPackageName(resultSet.getString("package")); - return correlationRule; - } - -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java deleted file mode 100644 index f3256b2..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java +++ /dev/null @@ -1,168 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.resources; - -import com.codahale.metrics.annotation.Timed; -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.annotation.PostConstruct; -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import lombok.extern.slf4j.Slf4j; -import net.sf.json.JSONObject; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.api.entity.ServiceRegisterEntity; -import org.openo.holmes.common.config.MicroServiceConfig; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.ExceptionUtil; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.common.utils.JacksonUtil; -import org.openo.holmes.common.utils.LanguageUtil; -import org.openo.holmes.common.utils.MSBRegisterUtil; -import org.openo.holmes.common.utils.UserUtil; -import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest; -import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest; -import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; -import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse; -import org.openo.holmes.rulemgt.constant.RuleMgtConstant; -import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper; - -@Service -@SwaggerDefinition -@Path("/rule") -@Api(tags = {"CorrelationRules"}) -@Produces(MediaType.APPLICATION_JSON) -@Slf4j -public class RuleMgtResources { - - @Inject - private RuleMgtWrapper ruleMgtWrapper; - - @PUT - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class) - @Timed - public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "alarm+ rule create request.
[rulename]:required
[content]:required
[enabled]:required", required = true) RuleCreateRequest ruleCreateRequest) { - Locale locale = LanguageUtil.getLocale(request); - RuleAddAndUpdateResponse ruleChangeResponse; - try { - ruleChangeResponse = ruleMgtWrapper - .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest); - log.info("create rule:" + ruleCreateRequest.getRuleName() + " success."); - return ruleChangeResponse; - } catch (CorrelationException e) { - log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e); - throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale, - e.getMessage())); - } - } - - @POST - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class) - @Timed - public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "alarm+ rule update request.
[ruleid]:required", required = true) RuleUpdateRequest ruleUpdateRequest) { - Locale locale = LanguageUtil.getLocale(request); - RuleAddAndUpdateResponse ruleChangeResponse; - try { - ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest); - log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful"); - return ruleChangeResponse; - } catch (CorrelationException e) { - log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e); - throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale, - e.getMessage())); - } - } - - @DELETE - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.") - @Timed - public boolean deleteCorrelationRule(@Context HttpServletRequest request, - @ApiParam(value = "alarm+ rule delete request.
[ruleid]:required", required = true) RuleDeleteRequest ruleDeleteRequest) { - Locale locale = LanguageUtil.getLocale(request); - try { - ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); - log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful"); - return true; - } catch (CorrelationException e) { - log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e); - throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale, - e.getMessage())); - } - } - - @GET - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class) - @Timed - public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request, - @ApiParam(value = "query condition:
" + " [ruleid]:Rule ID;
" - + "[rulename]:Rule name;
" + "[creator]:creator of the rule;
" - + "[modifier]:modifier of the rule;
" - + "[enabled]: 0 is Enabled,1 is disabled;
for example:
{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) { - Locale locale = LanguageUtil.getLocale(request); - RuleQueryListResponse ruleQueryListResponse; - RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request); - try { - ruleQueryListResponse = ruleMgtWrapper - .getCorrelationRuleByCondition(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); - throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale, - e.getMessage())); - } - } - - 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); - } - return ruleQueryCondition; - } catch (IOException e) { - log.warn("queryRequest convert to json failed", e); - throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale, - I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR)); - } - } -} diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java deleted file mode 100644 index 58d461f..0000000 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java +++ /dev/null @@ -1,245 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.wrapper; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import javax.annotation.PostConstruct; -import javax.inject.Inject; -import javax.inject.Singleton; -import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.DbDaoUtil; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest; -import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest; -import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; -import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse; -import org.openo.holmes.rulemgt.bean.response.RuleResult4API; -import org.openo.holmes.rulemgt.bolt.enginebolt.EngineWrapper; -import org.openo.holmes.rulemgt.constant.RuleMgtConstant; -import org.openo.holmes.rulemgt.db.CorrelationRuleDao; -import org.openo.holmes.rulemgt.db.CorrelationRuleQueryDao; - - -@Service -@Singleton -@Slf4j -public class RuleMgtWrapper { - - @Inject - private CorrelationRuleQueryDao correlationRuleQueryDao; - @Inject - private EngineWrapper engineWarpper; - @Inject - private DbDaoUtil daoUtil; - - private CorrelationRuleDao correlationRuleDao; - - @PostConstruct - public void initDaoUtil() { - correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class); - } - - public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest) - throws CorrelationException { - if (ruleCreateRequest == null) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY); - } - CorrelationRule correlationRule = convertCreateRequest2Rule(creator, - ruleCreateRequest); - checkCorrelation(correlationRule); - CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName()); - if (ruleTemp != null) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME); - } - correlationRule.setPackageName(deployRule2Engine(correlationRule)); - CorrelationRule result = correlationRuleDao.saveRule(correlationRule); - RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse(); - ruleAddAndUpdateResponse.setRuleId(result.getRid()); - return ruleAddAndUpdateResponse; - } - - public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest) - throws CorrelationException { - if (ruleUpdateRequest == null) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY); - } - CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId()); - if (oldCorrelationRule == null) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE); - } - CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier, - ruleUpdateRequest, oldCorrelationRule.getName()); - checkCorrelation(newCorrelationRule); - RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse(); - ruleChangeResponse.setRuleId(newCorrelationRule.getRid()); - if (!haveChange(newCorrelationRule, oldCorrelationRule)) { - return ruleChangeResponse; - } - if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) { - engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName()); - } - newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule)); - correlationRuleDao.updateRule(newCorrelationRule); - return ruleChangeResponse; - } - - private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException { - int enabled = correlationRule.getEnabled(); - String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim(); - String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim(); - if ("".equals(content)) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY); - } - if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE - && enabled != RuleMgtConstant.STATUS_RULE_OPEN) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR); - } - if ("".equals(ruleName)) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY); - } - } - - private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) { - String newContent = newCorrelationRule.getContent(); - String oldContent = oldCorrelationRule.getContent(); - int newEnabled = newCorrelationRule.getEnabled(); - int oldEnabled = oldCorrelationRule.getEnabled(); - String newDes = newCorrelationRule.getDescription(); - String oldDes = oldCorrelationRule.getDescription(); - if (newContent.equals(oldContent) && newEnabled == oldEnabled && newDes.equals(oldDes)) { - return false; - } - return true; - } - - public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest) - throws CorrelationException { - if (ruleDeleteRequest == null) { - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY); - } - CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId()); - if (correlationRule == null) { - log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database."); - throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE); - } - if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) { - engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName()); - } - correlationRuleDao.deleteRule(correlationRule); - } - - private CorrelationRule convertCreateRequest2Rule(String userName, - RuleCreateRequest ruleCreateRequest) throws CorrelationException { - String tempContent = ruleCreateRequest.getContent(); - CorrelationRule correlationRule = new CorrelationRule(); - String ruleId = "rule_" + System.currentTimeMillis(); - String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription(); - correlationRule.setRid(ruleId); - if (tempContent != null) { - correlationRule.setContent(tempContent.trim()); - } - correlationRule.setDescription(description); - correlationRule.setCreateTime(new Date()); - correlationRule.setUpdateTime(new Date()); - correlationRule.setName(ruleCreateRequest.getRuleName()); - correlationRule.setEngineID("correlation-d"); - correlationRule.setEngineType(""); - correlationRule.setTemplateID(0); - correlationRule.setVendor(""); - correlationRule.setCreator(userName); - correlationRule.setModifier(userName); - correlationRule.setEnabled(ruleCreateRequest.getEnabled()); - return correlationRule; - } - - private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier, - RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException { - CorrelationRule correlationRule = new CorrelationRule(); - String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription(); - correlationRule.setRid(ruleUpdateRequest.getRuleId()); - correlationRule.setContent(ruleUpdateRequest.getContent()); - correlationRule.setDescription(description); - correlationRule.setEnabled(ruleUpdateRequest.getEnabled()); - correlationRule.setUpdateTime(new Date()); - correlationRule.setModifier(modifier); - correlationRule.setName(ruleName); - return correlationRule; - } - - private String deployRule2Engine(CorrelationRule correlationRule) - throws CorrelationException { - if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && ( - correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) { - return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule)); - } - return ""; - } - - public RuleQueryListResponse getCorrelationRuleByCondition( - RuleQueryCondition ruleQueryCondition) throws CorrelationException { - List correlationRule = correlationRuleQueryDao - .getCorrelationRulesByCondition(ruleQueryCondition); - RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - ruleQueryListResponse.setTotalCount(correlationRule.size()); - ruleQueryListResponse - .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule)); - return ruleQueryListResponse; - } - - private List correlationRules2RuleResult4APIs( - List correlationRules) { - List ruleResult4APIs = new ArrayList(); - for (CorrelationRule correlationRule : correlationRules) { - RuleResult4API ruleResult4API = new RuleResult4API(); - String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription(); - ruleResult4API.setRuleId(correlationRule.getRid()); - ruleResult4API.setRuleName(correlationRule.getName()); - ruleResult4API.setDescription(description); - ruleResult4API.setContent(correlationRule.getContent()); - ruleResult4API.setCreateTime(correlationRule.getCreateTime()); - ruleResult4API.setCreator(correlationRule.getCreator()); - ruleResult4API.setUpdateTime(correlationRule.getUpdateTime()); - ruleResult4API.setModifier(correlationRule.getModifier()); - ruleResult4API.setEnabled(correlationRule.getEnabled()); - ruleResult4APIs.add(ruleResult4API); - } - return ruleResult4APIs; - } - - private CorrelationDeployRule4Engine correlationRules2DeployRule( - CorrelationRule correlationRule) { - CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); - correlationDeployRule4Engine.setContent(correlationRule.getContent()); - correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID()); - return correlationDeployRule4Engine; - } - - private CorrelationCheckRule4Engine correlationRules2CheckRule( - CorrelationRule correlationRule) { - CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine(); - correlationCheckRule4Engine.setContent(correlationRule.getContent()); - return correlationCheckRule4Engine; - } -} diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleActiveAppTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleActiveAppTest.java new file mode 100644 index 0000000..cd79ae8 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleActiveAppTest.java @@ -0,0 +1,36 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsEqual.equalTo; + +import org.junit.Test; + +public class RuleActiveAppTest { + + @Test + public void getName() throws Exception { + RuleActiveApp app = new RuleActiveApp(); + assertThat(app.getName(), equalTo("Holmes Rule Management ActiveApp APP ")); + } + + public static void main(String[] args) throws Exception { + String filePath = "C:\\correlation-rule.yml"; + new RuleActiveApp().run(new String[]{"server", filePath}); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleAppConfigTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleAppConfigTest.java new file mode 100644 index 0000000..d411603 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/RuleAppConfigTest.java @@ -0,0 +1,59 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.Assert.assertThat; + +import io.dropwizard.db.DataSourceFactory; +import org.junit.Before; +import org.junit.Test; + +public class RuleAppConfigTest { + + private RuleAppConfig ruleAppConfig; + + @Before + public void setUp() { + ruleAppConfig = new RuleAppConfig(); + } + + @Test + public void getDataSourceFactory() throws Exception { + assertThat(ruleAppConfig.getDataSourceFactory(), notNullValue()); + } + + @Test + public void setDataSourceFactory() throws Exception { + final DataSourceFactory factory = new DataSourceFactory(); + ruleAppConfig.setDataSourceFactory(factory); + assertThat(ruleAppConfig.getDataSourceFactory(), equalTo(factory)); + } + + @Test + public void getApidescription() throws Exception { + assertThat(ruleAppConfig.getApidescription(), equalTo("Holmes rule management rest API")); + } + + @Test + public void setApidescription() throws Exception { + final String value = "desc"; + ruleAppConfig.setApidescription(value); + assertThat(ruleAppConfig.getApidescription(), equalTo(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java new file mode 100644 index 0000000..e292e16 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class CorrelationCheckRule4EngineTest { + @Test + public void getterAndSetter4Content(){ + final String value = "content"; + CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine(); + correlationCheckRule4Engine.setContent(value); + assertThat(correlationCheckRule4Engine.getContent(), equalTo(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java new file mode 100644 index 0000000..418ff41 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java @@ -0,0 +1,40 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.junit.Assert.*; +import static org.hamcrest.core.IsEqual.equalTo; + +import org.junit.Test; + +public class CorrelationDeployRule4EngineTest { + @Test + public void getterAndSetter4Content(){ + final String value = "content"; + CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); + correlationDeployRule4Engine.setContent(value); + assertThat(correlationDeployRule4Engine.getContent(), equalTo(value)); + } + + @Test + public void getterAndSetter4EngineId(){ + final String value = "engineId"; + CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); + correlationDeployRule4Engine.setEngineId(value); + assertThat(correlationDeployRule4Engine.getEngineId(), equalTo(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java new file mode 100644 index 0000000..e616a71 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java @@ -0,0 +1,34 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class CorrelationRestRequestTest { + + @Test + public void getterAndSetter4RootURL() { + final String rootURL = "rootURL"; + CorrelationRestRequest correlationRestRequest = new CorrelationRestRequest(); + correlationRestRequest.setRootURL(rootURL); + assertThat(correlationRestRequest.getRootURL(), equalTo(rootURL)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequestTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequestTest.java new file mode 100644 index 0000000..54fa132 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequestTest.java @@ -0,0 +1,58 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class RuleCreateRequestTest { + + @Test + public void getterAndSetter4RuleName(){ + final String rulename = "rulename"; + RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + ruleCreateRequest.setRuleName(rulename); + assertThat(ruleCreateRequest.getRuleName(), equalTo(rulename)); + } + + @Test + public void getterAndSetter4Description(){ + final String description = "desc"; + RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + ruleCreateRequest.setDescription(description); + assertThat(ruleCreateRequest.getDescription(), equalTo(description)); + } + + @Test + public void getterAndSetter4Content(){ + final String contents = "contents"; + RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + ruleCreateRequest.setContent(contents); + assertThat(ruleCreateRequest.getContent(), equalTo(contents)); + } + + @Test + public void getterAndSetter4Enabled(){ + final int enabled = 0; + RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + ruleCreateRequest.setEnabled(enabled); + assertThat(ruleCreateRequest.getEnabled(), is(enabled)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java new file mode 100644 index 0000000..3d49b2f --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; + + +public class RuleDeleteRequestTest { + @Test + public void getterAndSetter4RuleId(){ + final String ruleId = "ruleId"; + RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); + ruleDeleteRequest.setRuleId(ruleId); + assertThat(ruleDeleteRequest.getRuleId(), equalTo(ruleId)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleQueryConditionTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleQueryConditionTest.java new file mode 100644 index 0000000..36d5024 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleQueryConditionTest.java @@ -0,0 +1,65 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class RuleQueryConditionTest { + @Test + public void getterAndSetter4RuleId(){ + final String value = "ruleId"; + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setRid(value); + assertThat(ruleQueryCondition.getRid(), equalTo(value)); + } + + @Test + public void getterAndSetter4RuleName(){ + final String value = "ruleName"; + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setName(value); + assertThat(ruleQueryCondition.getName(), equalTo(value)); + } + + @Test + public void getterAndSetter4Creator(){ + final String value = "admin"; + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setCreator(value); + assertThat(ruleQueryCondition.getCreator(), equalTo(value)); + } + + @Test + public void getterAndSetter4Modifier(){ + final String value = "admin"; + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setModifier(value); + assertThat(ruleQueryCondition.getModifier(), equalTo(value)); + } + + @Test + public void getterAndSetter4Enabled(){ + final int value = 0; + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setEnabled(value); + assertThat(ruleQueryCondition.getEnabled(), is(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java new file mode 100644 index 0000000..9f24c0c --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java @@ -0,0 +1,58 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.request; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; + + +public class RuleUpdateRequestTest { + @Test + public void getterAndSetter4RuleId(){ + final String value = "ruleId"; + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setRuleId(value); + assertThat(ruleUpdateRequest.getRuleId(), equalTo(value)); + } + + @Test + public void getterAndSetter4Description(){ + final String value = "desc"; + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setDescription(value); + assertThat(ruleUpdateRequest.getDescription(), equalTo(value)); + } + + @Test + public void getterAndSetter4Content(){ + final String value = "content"; + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setContent(value); + assertThat(ruleUpdateRequest.getContent(), equalTo(value)); + } + + @Test + public void getterAndSetter4Enabled(){ + final int value = 0; + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setEnabled(value); + assertThat(ruleUpdateRequest.getEnabled(), is(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java new file mode 100644 index 0000000..a7db0fe --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import static org.junit.Assert.*; +import static org.hamcrest.core.IsEqual.equalTo; + +import org.junit.Test; + +public class RuleAddAndUpdateResponseTest { + @Test + public void getterAndSetter4RuleId(){ + final String value = "ruleId"; + RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse(); + ruleAddAndUpdateResponse.setRuleId(value); + assertThat(ruleAddAndUpdateResponse.getRuleId(), equalTo(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java new file mode 100644 index 0000000..fa423db --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java @@ -0,0 +1,45 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +public class RuleQueryListResponseTest { + + @Test + public void getterAndSetter4CorrelationRules(){ + final List value = new ArrayList<>(); + RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); + ruleQueryListResponse.setCorrelationRules(value); + assertThat(ruleQueryListResponse.getCorrelationRules(), equalTo(value)); + } + + @Test + public void getterAndSetter4TotalCount(){ + final int value = 0; + RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); + ruleQueryListResponse.setTotalCount(value); + assertThat(ruleQueryListResponse.getTotalCount(), is(value)); + } + +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleResult4APITest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleResult4APITest.java new file mode 100644 index 0000000..2c006ab --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bean/response/RuleResult4APITest.java @@ -0,0 +1,99 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bean.response; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import java.util.Date; +import org.junit.Test; + +public class RuleResult4APITest { + + @Test + public void getterAndSetter4RuleId(){ + final String value = "ruleId"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setRuleId(value); + assertThat(ruleResult4API.getRuleId(), equalTo(value)); + } + + @Test + public void getterAndSetter4RuleName(){ + final String value = "ruleName"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setRuleName(value); + assertThat(ruleResult4API.getRuleName(), equalTo(value)); + } + + @Test + public void getterAndSetter4Description(){ + final String value = "desc"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setDescription(value); + assertThat(ruleResult4API.getDescription(), equalTo(value)); + } + + @Test + public void getterAndSetter4Content(){ + final String value = "content"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setContent(value); + assertThat(ruleResult4API.getContent(), equalTo(value)); + } + + @Test + public void getterAndSetter4CreateTime(){ + final Date value = new Date(); + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setCreateTime(value); + assertThat(ruleResult4API.getCreateTime(), equalTo(value)); + } + + @Test + public void getterAndSetter4Creator(){ + final String value = "admin"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setCreator(value); + assertThat(ruleResult4API.getCreator(), equalTo(value)); + } + + @Test + public void getterAndSetter4UpdateTime(){ + final Date value = new Date(); + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setUpdateTime(value); + assertThat(ruleResult4API.getUpdateTime(), equalTo(value)); + } + + @Test + public void getterAndSetter4Modifier(){ + final String value = "admin"; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setModifier(value); + assertThat(ruleResult4API.getModifier(), equalTo(value)); + } + + @Test + public void getterAndSetter4Enabled(){ + final int value = 0; + RuleResult4API ruleResult4API = new RuleResult4API(); + ruleResult4API.setEnabled(value); + assertThat(ruleResult4API.getEnabled(), is(value)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java new file mode 100644 index 0000000..b3cb93d --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java @@ -0,0 +1,56 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bolt.enginebolt; + + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.rule.PowerMockRule; + +@PrepareForTest({HttpClients.class, CloseableHttpClient.class}) +public class EngineServiceTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + private EngineService engineService; + private HttpResponse httpResponseMock; + private CloseableHttpClient closeableHttpClient; + private CorrelationDeployRule4Engine correlationDeployRule4Engine; + private CloseableHttpResponse closeableHttpResponseMock; + + @Before + public void setUp() { + engineService = new EngineService(); + closeableHttpClient = PowerMock.createMock(CloseableHttpClient.class); + httpResponseMock = PowerMock.createMock(HttpResponse.class); + closeableHttpResponseMock = PowerMock.createMock(CloseableHttpResponse.class); + correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); + correlationDeployRule4Engine.setContent("{\"package\":\"test\"}"); + correlationDeployRule4Engine.setEngineId("engine_id"); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java new file mode 100644 index 0000000..b63b106 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -0,0 +1,183 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.bolt.enginebolt; + + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import javax.ws.rs.core.Response; +import org.apache.http.StatusLine; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.onap.holmes.common.exception.CorrelationException; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +public class EngineWrapperTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + private EngineWrapper engineWrapper = new EngineWrapper(); + private EngineService engineServiceMock; + private Response response; + private StatusLine statusLineMock; + + @Before + public void setUp() throws Exception { + engineServiceMock = PowerMock.createMock(EngineService.class); + response = PowerMock.createMock(Response.class); + statusLineMock = PowerMock.createMock(StatusLine.class); + Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock); + } + + @Test + public void deployEngine_invoke_rule_deploy_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to call the rule deployment RESTful API."); + + EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow( + new RuntimeException("")); + PowerMock.replayAll(); + + engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + + PowerMock.verifyAll(); + } + + @Test + public void deployEngine_http_status_not_ok() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to deploy the rule!"); + + EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(400); + PowerMock.replayAll(); + + engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + + PowerMock.verifyAll(); + } + + @Test + public void deployEngine_parse_content_exception() throws Exception { + String content = ""; + + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to parse the value returned by the engine management service."); + EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(200); + EasyMock.expect(response.readEntity(String.class)).andReturn(content); + PowerMock.replayAll(); + + engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + + PowerMock.verifyAll(); + } + + @Test + public void deployEngine_success() throws Exception { + String content = "{\"package\":\"test\"}"; + EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(200); + EasyMock.expect(response.readEntity(String.class)).andReturn(content); + PowerMock.replayAll(); + + String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + + assertThat(result, equalTo("test")); + + } + + @Test + public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to call the rule deleting RESTful API."); + + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow( + new RuntimeException("")); + PowerMock.replayAll(); + + engineWrapper.deleteRuleFromEngine(""); + + PowerMock.verifyAll(); + } + + @Test + public void deleteRuleFromEngine_http_status_not_ok() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to delete the rule!"); + + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(400); + + PowerMock.replayAll(); + + engineWrapper.deleteRuleFromEngine(""); + + PowerMock.verifyAll(); + } + + @Test + public void deleteRuleFromEngine_success() throws Exception { + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(200); + + PowerMock.replayAll(); + + boolean result = engineWrapper.deleteRuleFromEngine(""); + + assertThat(result, equalTo(true)); + } + + @Test + public void checkRuleFromEngine_rule_delete_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to call the rule verification RESTful API."); + + EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow( + new RuntimeException("")); + PowerMock.replayAll(); + + engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); + + PowerMock.verifyAll(); + } + + @Test + public void checkRuleFromEngine_success() throws Exception { + EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + .andReturn(response); + EasyMock.expect(response.getStatus()).andReturn(200); + + PowerMock.replayAll(); + + boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); + + assertThat(result, equalTo(true)); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java new file mode 100644 index 0000000..3854cdc --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java @@ -0,0 +1,161 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.db; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.powermock.reflect.Whitebox; +import org.skife.jdbi.v2.Handle; +import org.skife.jdbi.v2.Query; + + +public class CorrelationRuleQueryDaoTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + private DbDaoUtil dbDaoUtil; + + private Handle handle; + + private Query query; + + private CorrelationRuleQueryDao correlationRuleQueryDao; + private RuleQueryCondition ruleQueryCondition; + + @Before + public void setUp() throws Exception { + correlationRuleQueryDao = new CorrelationRuleQueryDao(); + + dbDaoUtil = PowerMock.createMock(DbDaoUtil.class); + handle = PowerMock.createMock(Handle.class); + query = PowerMock.createMock(Query.class); + + Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil); + + ruleQueryCondition = createRuleQueryCondition(); + } + + + @Test + public void getCorrelationRulesByCondition_db_exception() throws Exception { + + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to query the rule."); + + EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); + EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); + EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes(); + dbDaoUtil.close(handle); + EasyMock.expectLastCall(); + + PowerMock.replayAll(); + + correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition); + + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRulesByCondition_normal() throws Exception { + EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); + EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); + EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes(); + dbDaoUtil.close(handle); + EasyMock.expectLastCall(); + + PowerMock.replayAll(); + + List result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition); + assertThat(result.size(), is(1)); + + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("An error occurred while building the query SQL."); + + EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); + EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); + EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes(); + dbDaoUtil.close(handle); + EasyMock.expectLastCall(); + + PowerMock.replayAll(); + + correlationRuleQueryDao.getCorrelationRulesByCondition(null); + + PowerMock.verifyAll(); + } + + private List> createQueryResult() { + List> list = new ArrayList<>(); + Map value = new HashMap<>(); + value.put("name", "Rule-001"); + value.put("rid", "rule_" + System.currentTimeMillis()); + value.put("description", "desc"); + value.put("enable", 0); + value.put("templateID", 1); + value.put("engineId", "engine-001"); + value.put("engineType", "engineType-001"); + value.put("creator", "admin"); + value.put("createTime", new Date()); + value.put("updator", "admin"); + value.put("updateTime", new Date()); + value.put("params", new Properties()); + value.put("domain", "Domain"); + value.put("isManual", 0); + value.put("vendor", "Vendor"); + value.put("content", "Contents"); + value.put("package", "package"); + list.add(value); + return list; + } + + private RuleQueryCondition createRuleQueryCondition() { + RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); + ruleQueryCondition.setRid("rule_" + System.currentTimeMillis()); + ruleQueryCondition.setName("Rule-001"); + ruleQueryCondition.setEnabled(0); + ruleQueryCondition.setCreator("admin"); + ruleQueryCondition.setModifier("admin"); + return ruleQueryCondition; + } + +} diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java new file mode 100644 index 0000000..bcf56ca --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java @@ -0,0 +1,52 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.db.mapper; + +import static org.easymock.EasyMock.expect; + +import java.sql.Date; +import java.sql.ResultSet; +import java.util.Properties; +import org.junit.Test; +import org.powermock.api.easymock.PowerMock; + +public class CorrelationRuleMapperTest { + + @Test + public void map() throws Exception { + CorrelationRuleMapper mapper = new CorrelationRuleMapper(); + ResultSet resultSet = PowerMock.createMock(ResultSet.class); + expect(resultSet.getString("name")).andReturn(""); + expect(resultSet.getString("rid")).andReturn(""); + expect(resultSet.getString("description")).andReturn(""); + expect(resultSet.getInt("enable")).andReturn(0); + expect(resultSet.getInt("templateID")).andReturn(1); + expect(resultSet.getString("engineID")).andReturn(""); + expect(resultSet.getString("engineType")).andReturn(""); + expect(resultSet.getString("creator")).andReturn(""); + expect(resultSet.getDate("createTime")).andReturn(new Date(System.currentTimeMillis())); + expect(resultSet.getString("updator")).andReturn(""); + expect(resultSet.getDate("updateTime")).andReturn(new Date(System.currentTimeMillis())); + expect(resultSet.getObject("params")).andReturn(new Properties()); + expect(resultSet.getString("content")).andReturn(""); + expect(resultSet.getString("vendor")).andReturn(""); + expect(resultSet.getString("package")).andReturn(""); + PowerMock.replay(resultSet); + mapper.map(0, resultSet, null); + PowerMock.verify(resultSet); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..dd68ad8 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java @@ -0,0 +1,182 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.resources; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import org.easymock.EasyMock; +import org.eclipse.jetty.server.Request; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; +import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest; +import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest; +import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; +import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +public class RuleMgtResourcesTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private HttpServletRequest request = PowerMock.createMock(HttpServletRequest.class); + + private RuleMgtWrapper ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class); + + private RuleMgtResources ruleMgtResources = new RuleMgtResources(); + + private Request requestMock = PowerMock.createMock(Request.class); + + @Before + public void setUp() throws Exception { + Whitebox.setInternalState(ruleMgtResources, "ruleMgtWrapper", ruleMgtWrapper); + PowerMock.resetAll(); + } + + @Test + public void addCorrelationRule_correlation_exception() throws Exception { + thrown.expect(WebApplicationException.class); + + final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest)) + .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + EasyMock.expect(request.getHeader("username")).andReturn("admin"); + PowerMock.replayAll(); + ruleMgtResources.addCorrelationRule(request, ruleCreateRequest); + PowerMock.verifyAll(); + } + + @Test + public void addCorrelationRule_normal() throws Exception { + StringBuilder stringBuilder = new StringBuilder("http://localhost"); + final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); + EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", + ruleCreateRequest)).andReturn(new RuleAddAndUpdateResponse()); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + EasyMock.expect(request.getHeader("username")).andReturn("admin"); + PowerMock.replayAll(); + ruleMgtResources.addCorrelationRule(request, ruleCreateRequest); + PowerMock.verifyAll(); + } + + @Test + public void updateCorrelationRule_correlation_exception() throws Exception { + thrown.expect(WebApplicationException.class); + + final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest)) + .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + EasyMock.expect(request.getHeader("username")).andReturn("admin"); + PowerMock.replayAll(); + ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest); + PowerMock.verifyAll(); + } + + @Test + public void updateCorrelationRule_normal() throws Exception { + final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", + ruleUpdateRequest)).andReturn(new RuleAddAndUpdateResponse()); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + EasyMock.expect(request.getHeader("username")).andReturn("admin"); + PowerMock.replayAll(); + ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest); + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_correlation_exception() throws Exception { + thrown.expect(WebApplicationException.class); + + final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + EasyMock.expectLastCall().andThrow(new CorrelationException(EasyMock.anyObject(String.class))); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + PowerMock.replayAll(); + ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest); + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_normal() throws Exception { + final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + EasyMock.expectLastCall(); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); + PowerMock.replayAll(); + ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest); + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRules_data_format_exception() throws Exception { + thrown.expect(WebApplicationException.class); + + final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\"," + + "\"enabled\":0,\"creator\":\"admin\"}"; + EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) + .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); + PowerMock.replayAll(); + ruleMgtResources.getCorrelationRules(request, requestStr); + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRules_param_translate_exception() { + thrown.expect(WebApplicationException.class); + + String queryRequest = "this is error param"; + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); + + PowerMock.replayAll(); + ruleMgtResources.getCorrelationRules(request, queryRequest); + PowerMock.verifyAll(); + + } + + @Test + public void getCorrelationRules_normal_request_string_null() throws Exception { + EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) + .andReturn(new RuleQueryListResponse()); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); + PowerMock.replayAll(); + ruleMgtResources.getCorrelationRules(request, null); + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRules_normal_request_string_enabled_missing() throws Exception { + final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\"," + + "\"creator\":\"admin\"}"; + EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) + .andReturn(new RuleQueryListResponse()); + EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); + PowerMock.replayAll(); + ruleMgtResources.getCorrelationRules(request, requestStr); + PowerMock.verifyAll(); + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java new file mode 100644 index 0000000..0c81b98 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java @@ -0,0 +1,411 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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.holmes.rulemgt.wrapper; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; +import org.onap.holmes.rulemgt.db.CorrelationRuleDao; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; +import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; +import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest; +import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; +import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest; +import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; +import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; +import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper; +import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.powermock.reflect.Whitebox; + +public class RuleMgtWrapperTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + + private RuleMgtWrapper ruleMgtWrapper; + + private EngineWrapper engineWrapperMock; + + private DbDaoUtil dbDaoUtilMock; + + private CorrelationRuleQueryDao correlationRuleQueryDaoMock; + + private CorrelationRuleDao correlationRuleDaoMock; + + private static final String USER_NAME = "admin"; + + @Before + public void setUp() throws Exception { + + ruleMgtWrapper = new RuleMgtWrapper(); + + engineWrapperMock = PowerMock.createMock(EngineWrapper.class); + correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class); + dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class); + correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class); + + Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock); + Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock); + Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock); + Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock); + + PowerMock.resetAll(); + } + + @Test + public void initDaoUtil_normal() { + ruleMgtWrapper.initDaoUtil(); + } + + @Test + public void addCorrelationRule_name_is_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The name of the rule can not be empty."); + + ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.", + "Mocked contents.", 0)); + } + + @Test + public void addCorrelationRule_request_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.addCorrelationRule(USER_NAME, null); + } + + @Test + public void addCorrelationRule_name_is_empty() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The name of the rule can not be empty."); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.", + "Mocked contents.", 0)); + } + + @Test + public void addCorrelationRule_content_is_empty() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The contents of the rule can not be empty!"); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", + "", 0)); + } + + @Test + public void addCorrelationRule_enabled_is_off_limit() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed."); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", + "Mocked contents.", 3)); + } + + @Test + public void addCorrelationRule_duplicated_rule() throws Exception { + + final String ruleName = "Rule-001"; + + RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", + "Mocked contents.", 0); + CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest); + + thrown.expect(CorrelationException.class); + thrown.expectMessage("A rule with the same name already exists."); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule); + PowerMock.replayAll(); + + ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); + + PowerMock.verifyAll(); + } + + @Test + public void addCorrelationRule_normal() throws Exception { + final String ruleName = "Rule-001"; + + RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", + "Mocked contents.", 1); + + CorrelationRule correlationRuleRet = new CorrelationRule(); + correlationRuleRet.setRid("rule_" + System.currentTimeMillis()); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null); + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + .andReturn(true); + EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + .andReturn("package-001"); + EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class))) + .andReturn(correlationRuleRet); + + PowerMock.replayAll(); + + RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); + assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid())); + + PowerMock.verifyAll(); + } + + @Test + public void updateCorrelationRule_param_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, null); + } + + @Test + public void updateCorrelationRule_normal() throws Exception { + CorrelationRule oldCorrelationRule = new CorrelationRule(); + oldCorrelationRule.setRid("rule_1"); + oldCorrelationRule.setName("name"); + oldCorrelationRule.setDescription("des1"); + oldCorrelationRule.setContent("content"); + oldCorrelationRule.setPackageName("testName"); + oldCorrelationRule.setEnabled(1); + RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des2", "contetnt2", 1); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName")).andReturn(true); + correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class)); + EasyMock.expectLastCall(); + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + .andReturn(true); + EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + .andReturn("packageName1"); + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); + + PowerMock.verifyAll(); + + assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); + } + + @Test + public void updateCorrelationRule_param_no_change() throws Exception { + CorrelationRule oldCorrelationRule = new CorrelationRule(); + oldCorrelationRule.setRid("rule_1"); + oldCorrelationRule.setName("name"); + oldCorrelationRule.setDescription("des1"); + oldCorrelationRule.setContent("content"); + oldCorrelationRule.setPackageName("testName"); + oldCorrelationRule.setEnabled(1); + RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des1", "content", 1); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); + + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); + + PowerMock.verifyAll(); + + assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); + } + + @Test + public void updateCorrelationRule_rule_not_exist() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("You're trying to update a rule which does not exist in the system."); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null); + + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest()); + + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_request_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.deleteCorrelationRule(null); + } + + @Test + public void deleteCorrelationRule_rule_not_exit() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("You're trying to delete a rule which does not exist in the system."); + + RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); + + EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn( + correlationRuleDaoMock).anyTimes(); + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) + .andReturn(null); + + PowerMock.replayAll(); + + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_normal() throws Exception { + RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setEnabled(1); + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) + .andReturn(correlationRule); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class))).andReturn(true); + correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class)); + EasyMock.expectLastCall(); + PowerMock.replayAll(); + + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRuleByCondition_data_format_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("An error occurred while building the query SQL."); + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))) + .andThrow(new CorrelationException("An error occurred while building the query SQL.")); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + } + + @Test + public void getCorrelationRuleByCondition_db_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to query the rule."); + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))) + .andThrow(new CorrelationException("Failed to query the rule.")); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + } + + @Test + public void getCorrelationRuleByCondition_normal() throws Exception { + List correlationRuleList = new ArrayList(10); + for (int i = 0; i < 10; ++i) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setContent("content" + i); + correlationRule.setName("name" + i); + correlationRule.setRid("rule_" + i); + correlationRule.setEngineType("engineType" + (i % 2 + 1)); + correlationRule.setEngineID("engineId" + i); + correlationRule.setCreateTime(new Date()); + correlationRule.setCreator(USER_NAME); + correlationRule.setDescription("description" + i); + correlationRule.setPackageName("package" + i); + correlationRuleList.add(correlationRule); + } + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))).andReturn(correlationRuleList); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + assertThat(response.getTotalCount(), is(10)); + + for (int i = 0; i < 10; ++i) { + assertThat(response.getCorrelationRules().get(i).getRuleId(), + equalTo(correlationRuleList.get(i).getRid())); + } + } + + private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content, + int enabled) { + RuleCreateRequest rcr; + rcr = new RuleCreateRequest(); + rcr.setRuleName(ruleName); + rcr.setDescription(description); + rcr.setContent(content); + rcr.setEnabled(enabled); + return rcr; + } + + private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String description, String content, int enabled) { + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setRuleId(ruleId); + ruleUpdateRequest.setDescription(description); + ruleUpdateRequest.setContent(content); + ruleUpdateRequest.setEnabled(enabled); + return ruleUpdateRequest; + } + + private RuleDeleteRequest createRuleDeleteRequest(String ruleId) { + RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); + ruleDeleteRequest.setRuleId(ruleId); + return ruleDeleteRequest; + } + + private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setContent(ruleCreateRequest.getContent()); + correlationRule.setDescription(ruleCreateRequest.getDescription()); + correlationRule.setName(ruleCreateRequest.getRuleName()); + correlationRule.setCreator(USER_NAME); + correlationRule.setModifier(USER_NAME); + correlationRule.setEnabled(ruleCreateRequest.getEnabled()); + return correlationRule; + } + + private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setRid(ruleUpdateRequest.getRuleId()); + correlationRule.setContent(ruleUpdateRequest.getContent()); + correlationRule.setDescription(ruleUpdateRequest.getDescription()); + correlationRule.setEnabled(ruleUpdateRequest.getEnabled()); + correlationRule.setUpdateTime(new Date()); + correlationRule.setModifier(USER_NAME); + return correlationRule; + } +} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java deleted file mode 100644 index 4f35b94..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; - -import org.junit.Test; - -public class RuleActiveAppTest { - - @Test - public void getName() throws Exception { - RuleActiveApp app = new RuleActiveApp(); - assertThat(app.getName(), equalTo("Holmes Rule Management ActiveApp APP ")); - } - - public static void main(String[] args) throws Exception { - String filePath = "C:\\correlation-rule.yml"; - new RuleActiveApp().run(new String[]{"server", filePath}); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java deleted file mode 100644 index d14e55e..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.IsNull.notNullValue; -import static org.junit.Assert.assertThat; - -import io.dropwizard.db.DataSourceFactory; -import org.junit.Before; -import org.junit.Test; - -public class RuleAppConfigTest { - - private RuleAppConfig ruleAppConfig; - - @Before - public void setUp() { - ruleAppConfig = new RuleAppConfig(); - } - - @Test - public void getDataSourceFactory() throws Exception { - assertThat(ruleAppConfig.getDataSourceFactory(), notNullValue()); - } - - @Test - public void setDataSourceFactory() throws Exception { - final DataSourceFactory factory = new DataSourceFactory(); - ruleAppConfig.setDataSourceFactory(factory); - assertThat(ruleAppConfig.getDataSourceFactory(), equalTo(factory)); - } - - @Test - public void getApidescription() throws Exception { - assertThat(ruleAppConfig.getApidescription(), equalTo("Holmes rule management rest API")); - } - - @Test - public void setApidescription() throws Exception { - final String value = "desc"; - ruleAppConfig.setApidescription(value); - assertThat(ruleAppConfig.getApidescription(), equalTo(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java deleted file mode 100644 index 59e020c..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationCheckRule4EngineTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class CorrelationCheckRule4EngineTest { - @Test - public void getterAndSetter4Content(){ - final String value = "content"; - CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine(); - correlationCheckRule4Engine.setContent(value); - assertThat(correlationCheckRule4Engine.getContent(), equalTo(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java deleted file mode 100644 index 8a9b8ec..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationDeployRule4EngineTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.junit.Assert.*; -import static org.hamcrest.core.IsEqual.equalTo; - -import org.junit.Test; - -public class CorrelationDeployRule4EngineTest { - @Test - public void getterAndSetter4Content(){ - final String value = "content"; - CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); - correlationDeployRule4Engine.setContent(value); - assertThat(correlationDeployRule4Engine.getContent(), equalTo(value)); - } - - @Test - public void getterAndSetter4EngineId(){ - final String value = "engineId"; - CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); - correlationDeployRule4Engine.setEngineId(value); - assertThat(correlationDeployRule4Engine.getEngineId(), equalTo(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java deleted file mode 100644 index 5f3c5c1..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -import org.junit.Test; - -public class CorrelationRestRequestTest { - - @Test - public void getterAndSetter4RootURL() { - final String rootURL = "rootURL"; - CorrelationRestRequest correlationRestRequest = new CorrelationRestRequest(); - correlationRestRequest.setRootURL(rootURL); - assertThat(correlationRestRequest.getRootURL(), equalTo(rootURL)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequestTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequestTest.java deleted file mode 100644 index 3f5469d..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleCreateRequestTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class RuleCreateRequestTest { - - @Test - public void getterAndSetter4RuleName(){ - final String rulename = "rulename"; - RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - ruleCreateRequest.setRuleName(rulename); - assertThat(ruleCreateRequest.getRuleName(), equalTo(rulename)); - } - - @Test - public void getterAndSetter4Description(){ - final String description = "desc"; - RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - ruleCreateRequest.setDescription(description); - assertThat(ruleCreateRequest.getDescription(), equalTo(description)); - } - - @Test - public void getterAndSetter4Content(){ - final String contents = "contents"; - RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - ruleCreateRequest.setContent(contents); - assertThat(ruleCreateRequest.getContent(), equalTo(contents)); - } - - @Test - public void getterAndSetter4Enabled(){ - final int enabled = 0; - RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - ruleCreateRequest.setEnabled(enabled); - assertThat(ruleCreateRequest.getEnabled(), is(enabled)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java deleted file mode 100644 index f9a09ff..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleDeleteRequestTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import org.junit.Test; - - -public class RuleDeleteRequestTest { - @Test - public void getterAndSetter4RuleId(){ - final String ruleId = "ruleId"; - RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); - ruleDeleteRequest.setRuleId(ruleId); - assertThat(ruleDeleteRequest.getRuleId(), equalTo(ruleId)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleQueryConditionTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleQueryConditionTest.java deleted file mode 100644 index 8bb5b8b..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleQueryConditionTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class RuleQueryConditionTest { - @Test - public void getterAndSetter4RuleId(){ - final String value = "ruleId"; - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setRid(value); - assertThat(ruleQueryCondition.getRid(), equalTo(value)); - } - - @Test - public void getterAndSetter4RuleName(){ - final String value = "ruleName"; - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setName(value); - assertThat(ruleQueryCondition.getName(), equalTo(value)); - } - - @Test - public void getterAndSetter4Creator(){ - final String value = "admin"; - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setCreator(value); - assertThat(ruleQueryCondition.getCreator(), equalTo(value)); - } - - @Test - public void getterAndSetter4Modifier(){ - final String value = "admin"; - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setModifier(value); - assertThat(ruleQueryCondition.getModifier(), equalTo(value)); - } - - @Test - public void getterAndSetter4Enabled(){ - final int value = 0; - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setEnabled(value); - assertThat(ruleQueryCondition.getEnabled(), is(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java deleted file mode 100644 index 4cd4744..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/RuleUpdateRequestTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.request; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import org.junit.Test; - - -public class RuleUpdateRequestTest { - @Test - public void getterAndSetter4RuleId(){ - final String value = "ruleId"; - RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - ruleUpdateRequest.setRuleId(value); - assertThat(ruleUpdateRequest.getRuleId(), equalTo(value)); - } - - @Test - public void getterAndSetter4Description(){ - final String value = "desc"; - RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - ruleUpdateRequest.setDescription(value); - assertThat(ruleUpdateRequest.getDescription(), equalTo(value)); - } - - @Test - public void getterAndSetter4Content(){ - final String value = "content"; - RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - ruleUpdateRequest.setContent(value); - assertThat(ruleUpdateRequest.getContent(), equalTo(value)); - } - - @Test - public void getterAndSetter4Enabled(){ - final int value = 0; - RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - ruleUpdateRequest.setEnabled(value); - assertThat(ruleUpdateRequest.getEnabled(), is(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java deleted file mode 100644 index 82731d6..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleAddAndUpdateResponseTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import static org.junit.Assert.*; -import static org.hamcrest.core.IsEqual.equalTo; - -import org.junit.Test; - -public class RuleAddAndUpdateResponseTest { - @Test - public void getterAndSetter4RuleId(){ - final String value = "ruleId"; - RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse(); - ruleAddAndUpdateResponse.setRuleId(value); - assertThat(ruleAddAndUpdateResponse.getRuleId(), equalTo(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java deleted file mode 100644 index c6e0a7e..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleQueryListResponseTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -import java.util.ArrayList; -import java.util.List; -import org.junit.Test; - -public class RuleQueryListResponseTest { - - @Test - public void getterAndSetter4CorrelationRules(){ - final List value = new ArrayList<>(); - RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - ruleQueryListResponse.setCorrelationRules(value); - assertThat(ruleQueryListResponse.getCorrelationRules(), equalTo(value)); - } - - @Test - public void getterAndSetter4TotalCount(){ - final int value = 0; - RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - ruleQueryListResponse.setTotalCount(value); - assertThat(ruleQueryListResponse.getTotalCount(), is(value)); - } - -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleResult4APITest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleResult4APITest.java deleted file mode 100644 index 3af245e..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/response/RuleResult4APITest.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bean.response; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.*; - -import java.util.Date; -import org.junit.Test; - -public class RuleResult4APITest { - - @Test - public void getterAndSetter4RuleId(){ - final String value = "ruleId"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setRuleId(value); - assertThat(ruleResult4API.getRuleId(), equalTo(value)); - } - - @Test - public void getterAndSetter4RuleName(){ - final String value = "ruleName"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setRuleName(value); - assertThat(ruleResult4API.getRuleName(), equalTo(value)); - } - - @Test - public void getterAndSetter4Description(){ - final String value = "desc"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setDescription(value); - assertThat(ruleResult4API.getDescription(), equalTo(value)); - } - - @Test - public void getterAndSetter4Content(){ - final String value = "content"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setContent(value); - assertThat(ruleResult4API.getContent(), equalTo(value)); - } - - @Test - public void getterAndSetter4CreateTime(){ - final Date value = new Date(); - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setCreateTime(value); - assertThat(ruleResult4API.getCreateTime(), equalTo(value)); - } - - @Test - public void getterAndSetter4Creator(){ - final String value = "admin"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setCreator(value); - assertThat(ruleResult4API.getCreator(), equalTo(value)); - } - - @Test - public void getterAndSetter4UpdateTime(){ - final Date value = new Date(); - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setUpdateTime(value); - assertThat(ruleResult4API.getUpdateTime(), equalTo(value)); - } - - @Test - public void getterAndSetter4Modifier(){ - final String value = "admin"; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setModifier(value); - assertThat(ruleResult4API.getModifier(), equalTo(value)); - } - - @Test - public void getterAndSetter4Enabled(){ - final int value = 0; - RuleResult4API ruleResult4API = new RuleResult4API(); - ruleResult4API.setEnabled(value); - assertThat(ruleResult4API.getEnabled(), is(value)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java deleted file mode 100644 index f4275eb..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bolt.enginebolt; - - -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.junit.Before; -import org.junit.Rule; -import org.junit.rules.ExpectedException; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.rule.PowerMockRule; - -@PrepareForTest({HttpClients.class, CloseableHttpClient.class}) -public class EngineServiceTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule - public PowerMockRule powerMockRule = new PowerMockRule(); - private EngineService engineService; - private HttpResponse httpResponseMock; - private CloseableHttpClient closeableHttpClient; - private CorrelationDeployRule4Engine correlationDeployRule4Engine; - private CloseableHttpResponse closeableHttpResponseMock; - - @Before - public void setUp() { - engineService = new EngineService(); - closeableHttpClient = PowerMock.createMock(CloseableHttpClient.class); - httpResponseMock = PowerMock.createMock(HttpResponse.class); - closeableHttpResponseMock = PowerMock.createMock(CloseableHttpResponse.class); - correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); - correlationDeployRule4Engine.setContent("{\"package\":\"test\"}"); - correlationDeployRule4Engine.setEngineId("engine_id"); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java deleted file mode 100644 index 38afa47..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.bolt.enginebolt; - - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -import javax.ws.rs.core.Response; -import org.apache.http.StatusLine; -import org.easymock.EasyMock; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -public class EngineWrapperTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private EngineWrapper engineWrapper = new EngineWrapper(); - private EngineService engineServiceMock; - private Response response; - private StatusLine statusLineMock; - - @Before - public void setUp() throws Exception { - engineServiceMock = PowerMock.createMock(EngineService.class); - response = PowerMock.createMock(Response.class); - statusLineMock = PowerMock.createMock(StatusLine.class); - Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock); - } - - @Test - public void deployEngine_invoke_rule_deploy_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED); - - EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow( - new RuntimeException("")); - PowerMock.replayAll(); - - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); - - PowerMock.verifyAll(); - } - - @Test - public void deployEngine_http_status_not_ok() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.ENGINE_DEPLOY_RULE_FAILED); - - EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(400); - PowerMock.replayAll(); - - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); - - PowerMock.verifyAll(); - } - - @Test - public void deployEngine_parse_content_exception() throws Exception { - String content = ""; - - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR); - EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(200); - EasyMock.expect(response.readEntity(String.class)).andReturn(content); - PowerMock.replayAll(); - - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); - - PowerMock.verifyAll(); - } - - @Test - public void deployEngine_success() throws Exception { - String content = "{\"package\":\"test\"}"; - EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(200); - EasyMock.expect(response.readEntity(String.class)).andReturn(content); - PowerMock.replayAll(); - - String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); - - assertThat(result, equalTo("test")); - - } - - @Test - public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED); - - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow( - new RuntimeException("")); - PowerMock.replayAll(); - - engineWrapper.deleteRuleFromEngine(""); - - PowerMock.verifyAll(); - } - - @Test - public void deleteRuleFromEngine_http_status_not_ok() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED); - - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(400); - - PowerMock.replayAll(); - - engineWrapper.deleteRuleFromEngine(""); - - PowerMock.verifyAll(); - } - - @Test - public void deleteRuleFromEngine_success() throws Exception { - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(200); - - PowerMock.replayAll(); - - boolean result = engineWrapper.deleteRuleFromEngine(""); - - assertThat(result, equalTo(true)); - } - - @Test - public void checkRuleFromEngine_rule_delete_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED); - - EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow( - new RuntimeException("")); - PowerMock.replayAll(); - - engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); - - PowerMock.verifyAll(); - } - - @Test - public void checkRuleFromEngine_success() throws Exception { - EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) - .andReturn(response); - EasyMock.expect(response.getStatus()).andReturn(200); - - PowerMock.replayAll(); - - boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); - - assertThat(result, equalTo(true)); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java deleted file mode 100644 index f241484..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.db; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import org.easymock.EasyMock; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.DbDaoUtil; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.powermock.api.easymock.PowerMock; -import org.powermock.modules.junit4.rule.PowerMockRule; -import org.powermock.reflect.Whitebox; -import org.skife.jdbi.v2.Handle; -import org.skife.jdbi.v2.Query; - - -public class CorrelationRuleQueryDaoTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public PowerMockRule powerMockRule = new PowerMockRule(); - private DbDaoUtil dbDaoUtil; - - private Handle handle; - - private Query query; - - private CorrelationRuleQueryDao correlationRuleQueryDao; - private RuleQueryCondition ruleQueryCondition; - - @Before - public void setUp() throws Exception { - correlationRuleQueryDao = new CorrelationRuleQueryDao(); - - dbDaoUtil = PowerMock.createMock(DbDaoUtil.class); - handle = PowerMock.createMock(Handle.class); - query = PowerMock.createMock(Query.class); - - Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil); - - ruleQueryCondition = createRuleQueryCondition(); - } - - - @Test - public void getCorrelationRulesByCondition_db_exception() throws Exception { - - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED); - - EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); - EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); - EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes(); - dbDaoUtil.close(handle); - EasyMock.expectLastCall(); - - PowerMock.replayAll(); - - correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition); - - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRulesByCondition_normal() throws Exception { - EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); - EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); - EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes(); - dbDaoUtil.close(handle); - EasyMock.expectLastCall(); - - PowerMock.replayAll(); - - List result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition); - assertThat(result.size(), is(1)); - - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED); - - EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle); - EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query); - EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes(); - dbDaoUtil.close(handle); - EasyMock.expectLastCall(); - - PowerMock.replayAll(); - - correlationRuleQueryDao.getCorrelationRulesByCondition(null); - - PowerMock.verifyAll(); - } - - private List> createQueryResult() { - List> list = new ArrayList<>(); - Map value = new HashMap<>(); - value.put("name", "Rule-001"); - value.put("rid", "rule_" + System.currentTimeMillis()); - value.put("description", "desc"); - value.put("enable", 0); - value.put("templateID", 1); - value.put("engineId", "engine-001"); - value.put("engineType", "engineType-001"); - value.put("creator", "admin"); - value.put("createTime", new Date()); - value.put("updator", "admin"); - value.put("updateTime", new Date()); - value.put("params", new Properties()); - value.put("domain", "Domain"); - value.put("isManual", 0); - value.put("vendor", "Vendor"); - value.put("content", "Contents"); - value.put("package", "package"); - list.add(value); - return list; - } - - private RuleQueryCondition createRuleQueryCondition() { - RuleQueryCondition ruleQueryCondition = new RuleQueryCondition(); - ruleQueryCondition.setRid("rule_" + System.currentTimeMillis()); - ruleQueryCondition.setName("Rule-001"); - ruleQueryCondition.setEnabled(0); - ruleQueryCondition.setCreator("admin"); - ruleQueryCondition.setModifier("admin"); - return ruleQueryCondition; - } - -} diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java deleted file mode 100644 index 65a53dd..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.db.mapper; - -import static org.easymock.EasyMock.expect; - -import java.sql.Date; -import java.sql.ResultSet; -import java.util.Properties; -import org.junit.Test; -import org.powermock.api.easymock.PowerMock; - -public class CorrelationRuleMapperTest { - - @Test - public void map() throws Exception { - CorrelationRuleMapper mapper = new CorrelationRuleMapper(); - ResultSet resultSet = PowerMock.createMock(ResultSet.class); - expect(resultSet.getString("name")).andReturn(""); - expect(resultSet.getString("rid")).andReturn(""); - expect(resultSet.getString("description")).andReturn(""); - expect(resultSet.getInt("enable")).andReturn(0); - expect(resultSet.getInt("templateID")).andReturn(1); - expect(resultSet.getString("engineID")).andReturn(""); - expect(resultSet.getString("engineType")).andReturn(""); - expect(resultSet.getString("creator")).andReturn(""); - expect(resultSet.getDate("createTime")).andReturn(new Date(System.currentTimeMillis())); - expect(resultSet.getString("updator")).andReturn(""); - expect(resultSet.getDate("updateTime")).andReturn(new Date(System.currentTimeMillis())); - expect(resultSet.getObject("params")).andReturn(new Properties()); - expect(resultSet.getString("content")).andReturn(""); - expect(resultSet.getString("vendor")).andReturn(""); - expect(resultSet.getString("package")).andReturn(""); - PowerMock.replay(resultSet); - mapper.map(0, resultSet, null); - PowerMock.verify(resultSet); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java deleted file mode 100644 index 16a547b..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java +++ /dev/null @@ -1,182 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.resources; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.WebApplicationException; -import org.easymock.EasyMock; -import org.eclipse.jetty.server.Request; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest; -import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest; -import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; -import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse; -import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -public class RuleMgtResourcesTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private HttpServletRequest request = PowerMock.createMock(HttpServletRequest.class); - - private RuleMgtWrapper ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class); - - private RuleMgtResources ruleMgtResources = new RuleMgtResources(); - - private Request requestMock = PowerMock.createMock(Request.class); - - @Before - public void setUp() throws Exception { - Whitebox.setInternalState(ruleMgtResources, "ruleMgtWrapper", ruleMgtWrapper); - PowerMock.resetAll(); - } - - @Test - public void addCorrelationRule_correlation_exception() throws Exception { - thrown.expect(WebApplicationException.class); - - final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest)) - .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - EasyMock.expect(request.getHeader("username")).andReturn("admin"); - PowerMock.replayAll(); - ruleMgtResources.addCorrelationRule(request, ruleCreateRequest); - PowerMock.verifyAll(); - } - - @Test - public void addCorrelationRule_normal() throws Exception { - StringBuilder stringBuilder = new StringBuilder("http://localhost"); - final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest(); - EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", - ruleCreateRequest)).andReturn(new RuleAddAndUpdateResponse()); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - EasyMock.expect(request.getHeader("username")).andReturn("admin"); - PowerMock.replayAll(); - ruleMgtResources.addCorrelationRule(request, ruleCreateRequest); - PowerMock.verifyAll(); - } - - @Test - public void updateCorrelationRule_correlation_exception() throws Exception { - thrown.expect(WebApplicationException.class); - - final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest)) - .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - EasyMock.expect(request.getHeader("username")).andReturn("admin"); - PowerMock.replayAll(); - ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest); - PowerMock.verifyAll(); - } - - @Test - public void updateCorrelationRule_normal() throws Exception { - final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", - ruleUpdateRequest)).andReturn(new RuleAddAndUpdateResponse()); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - EasyMock.expect(request.getHeader("username")).andReturn("admin"); - PowerMock.replayAll(); - ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest); - PowerMock.verifyAll(); - } - - @Test - public void deleteCorrelationRule_correlation_exception() throws Exception { - thrown.expect(WebApplicationException.class); - - final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); - ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); - EasyMock.expectLastCall().andThrow(new CorrelationException(EasyMock.anyObject(String.class))); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - PowerMock.replayAll(); - ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest); - PowerMock.verifyAll(); - } - - @Test - public void deleteCorrelationRule_normal() throws Exception { - final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); - ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); - EasyMock.expectLastCall(); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US"); - PowerMock.replayAll(); - ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest); - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRules_data_format_exception() throws Exception { - thrown.expect(WebApplicationException.class); - - final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\"," - + "\"enabled\":0,\"creator\":\"admin\"}"; - EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) - .andThrow(new CorrelationException(EasyMock.anyObject(String.class))); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); - PowerMock.replayAll(); - ruleMgtResources.getCorrelationRules(request, requestStr); - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRules_param_translate_exception() { - thrown.expect(WebApplicationException.class); - - String queryRequest = "this is error param"; - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); - - PowerMock.replayAll(); - ruleMgtResources.getCorrelationRules(request, queryRequest); - PowerMock.verifyAll(); - - } - - @Test - public void getCorrelationRules_normal_request_string_null() throws Exception { - EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) - .andReturn(new RuleQueryListResponse()); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); - PowerMock.replayAll(); - ruleMgtResources.getCorrelationRules(request, null); - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRules_normal_request_string_enabled_missing() throws Exception { - final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\"," - + "\"creator\":\"admin\"}"; - EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class))) - .andReturn(new RuleQueryListResponse()); - EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2); - PowerMock.replayAll(); - ruleMgtResources.getCorrelationRules(request, requestStr); - PowerMock.verifyAll(); - } -} \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java deleted file mode 100644 index f6d0544..0000000 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java +++ /dev/null @@ -1,412 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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.openo.holmes.rulemgt.wrapper; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import org.easymock.EasyMock; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.DbDaoUtil; -import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; -import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest; -import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest; -import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; -import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest; -import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse; -import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse; -import org.openo.holmes.rulemgt.bolt.enginebolt.EngineWrapper; -import org.openo.holmes.rulemgt.db.CorrelationRuleDao; -import org.openo.holmes.rulemgt.db.CorrelationRuleQueryDao; -import org.powermock.api.easymock.PowerMock; -import org.powermock.modules.junit4.rule.PowerMockRule; -import org.powermock.reflect.Whitebox; - -public class RuleMgtWrapperTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule - public PowerMockRule powerMockRule = new PowerMockRule(); - - private RuleMgtWrapper ruleMgtWrapper; - - private EngineWrapper engineWrapperMock; - - private DbDaoUtil dbDaoUtilMock; - - private CorrelationRuleQueryDao correlationRuleQueryDaoMock; - - private CorrelationRuleDao correlationRuleDaoMock; - - private static final String USER_NAME = "admin"; - - @Before - public void setUp() throws Exception { - - ruleMgtWrapper = new RuleMgtWrapper(); - - engineWrapperMock = PowerMock.createMock(EngineWrapper.class); - correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class); - dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class); - correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class); - - Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock); - Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock); - Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock); - Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock); - - PowerMock.resetAll(); - } - - @Test - public void initDaoUtil_normal() { - ruleMgtWrapper.initDaoUtil(); - } - - @Test - public void addCorrelationRule_name_is_null() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY); - - ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.", - "Mocked contents.", 0)); - } - - @Test - public void addCorrelationRule_request_null() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY)); - - ruleMgtWrapper.addCorrelationRule(USER_NAME, null); - } - - @Test - public void addCorrelationRule_name_is_empty() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY); - - ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.", - "Mocked contents.", 0)); - } - - @Test - public void addCorrelationRule_content_is_empty() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY); - - ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", - "", 0)); - } - - @Test - public void addCorrelationRule_enabled_is_off_limit() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR); - - ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", - "Mocked contents.", 3)); - } - - @Test - public void addCorrelationRule_duplicated_rule() throws Exception { - - final String ruleName = "Rule-001"; - - RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", - "Mocked contents.", 0); - CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest); - - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME); - - EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule); - PowerMock.replayAll(); - - ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); - - PowerMock.verifyAll(); - } - - @Test - public void addCorrelationRule_normal() throws Exception { - final String ruleName = "Rule-001"; - - RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", - "Mocked contents.", 1); - - CorrelationRule correlationRuleRet = new CorrelationRule(); - correlationRuleRet.setRid("rule_" + System.currentTimeMillis()); - - EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null); - EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) - .andReturn(true); - EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) - .andReturn("package-001"); - EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class))) - .andReturn(correlationRuleRet); - - PowerMock.replayAll(); - - RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); - assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid())); - - PowerMock.verifyAll(); - } - - @Test - public void updateCorrelationRule_param_null() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY); - - ruleMgtWrapper.updateCorrelationRule(USER_NAME, null); - } - - @Test - public void updateCorrelationRule_normal() throws Exception { - CorrelationRule oldCorrelationRule = new CorrelationRule(); - oldCorrelationRule.setRid("rule_1"); - oldCorrelationRule.setName("name"); - oldCorrelationRule.setDescription("des1"); - oldCorrelationRule.setContent("content"); - oldCorrelationRule.setPackageName("testName"); - oldCorrelationRule.setEnabled(1); - RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des2", "contetnt2", 1); - - EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); - EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName")).andReturn(true); - correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class)); - EasyMock.expectLastCall(); - EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) - .andReturn(true); - EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) - .andReturn("packageName1"); - PowerMock.replayAll(); - - ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); - - PowerMock.verifyAll(); - - assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); - } - - @Test - public void updateCorrelationRule_param_no_change() throws Exception { - CorrelationRule oldCorrelationRule = new CorrelationRule(); - oldCorrelationRule.setRid("rule_1"); - oldCorrelationRule.setName("name"); - oldCorrelationRule.setDescription("des1"); - oldCorrelationRule.setContent("content"); - oldCorrelationRule.setPackageName("testName"); - oldCorrelationRule.setEnabled(1); - RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des1", "content", 1); - - EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); - - PowerMock.replayAll(); - - ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); - - PowerMock.verifyAll(); - - assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); - } - - @Test - public void updateCorrelationRule_rule_not_exist() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE); - - EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null); - - PowerMock.replayAll(); - - ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest()); - - PowerMock.verifyAll(); - } - - @Test - public void deleteCorrelationRule_request_null() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY)); - - ruleMgtWrapper.deleteCorrelationRule(null); - } - - @Test - public void deleteCorrelationRule_rule_not_exit() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE)); - - RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); - - EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn( - correlationRuleDaoMock).anyTimes(); - EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) - .andReturn(null); - - PowerMock.replayAll(); - - ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); - - PowerMock.verifyAll(); - } - - @Test - public void deleteCorrelationRule_normal() throws Exception { - RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setEnabled(1); - EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) - .andReturn(correlationRule); - EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class))).andReturn(true); - correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class)); - EasyMock.expectLastCall(); - PowerMock.replayAll(); - - ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); - - PowerMock.verifyAll(); - } - - @Test - public void getCorrelationRuleByCondition_data_format_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED)); - - EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( - RuleQueryCondition.class))) - .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED)); - - PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - - ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); - - PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - } - - @Test - public void getCorrelationRuleByCondition_db_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED)); - - EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( - RuleQueryCondition.class))) - .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED)); - - PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - - ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); - - PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - } - - @Test - public void getCorrelationRuleByCondition_normal() throws Exception { - List correlationRuleList = new ArrayList(10); - for (int i = 0; i < 10; ++i) { - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setContent("content" + i); - correlationRule.setName("name" + i); - correlationRule.setRid("rule_" + i); - correlationRule.setEngineType("engineType" + (i % 2 + 1)); - correlationRule.setEngineID("engineId" + i); - correlationRule.setCreateTime(new Date()); - correlationRule.setCreator(USER_NAME); - correlationRule.setDescription("description" + i); - correlationRule.setPackageName("package" + i); - correlationRuleList.add(correlationRule); - } - - EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( - RuleQueryCondition.class))).andReturn(correlationRuleList); - - PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - - RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); - - PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); - - assertThat(response.getTotalCount(), is(10)); - - for (int i = 0; i < 10; ++i) { - assertThat(response.getCorrelationRules().get(i).getRuleId(), - equalTo(correlationRuleList.get(i).getRid())); - } - } - - private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content, - int enabled) { - RuleCreateRequest rcr; - rcr = new RuleCreateRequest(); - rcr.setRuleName(ruleName); - rcr.setDescription(description); - rcr.setContent(content); - rcr.setEnabled(enabled); - return rcr; - } - - private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String description, String content, int enabled) { - RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); - ruleUpdateRequest.setRuleId(ruleId); - ruleUpdateRequest.setDescription(description); - ruleUpdateRequest.setContent(content); - ruleUpdateRequest.setEnabled(enabled); - return ruleUpdateRequest; - } - - private RuleDeleteRequest createRuleDeleteRequest(String ruleId) { - RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); - ruleDeleteRequest.setRuleId(ruleId); - return ruleDeleteRequest; - } - - private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) { - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setContent(ruleCreateRequest.getContent()); - correlationRule.setDescription(ruleCreateRequest.getDescription()); - correlationRule.setName(ruleCreateRequest.getRuleName()); - correlationRule.setCreator(USER_NAME); - correlationRule.setModifier(USER_NAME); - correlationRule.setEnabled(ruleCreateRequest.getEnabled()); - return correlationRule; - } - - private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) { - CorrelationRule correlationRule = new CorrelationRule(); - correlationRule.setRid(ruleUpdateRequest.getRuleId()); - correlationRule.setContent(ruleUpdateRequest.getContent()); - correlationRule.setDescription(ruleUpdateRequest.getDescription()); - correlationRule.setEnabled(ruleUpdateRequest.getEnabled()); - correlationRule.setUpdateTime(new Date()); - correlationRule.setModifier(USER_NAME); - return correlationRule; - } -} \ No newline at end of file -- cgit 1.2.3-korg