From 7ab5d3b5150f3941f61f40bc27c94909bcbdfe7d Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Mon, 7 Aug 2017 12:36:54 +0800 Subject: Change the groupid from openo to onap Change the groupid and package paths to onap. Change-Id: Ie2f9926d418fbb434509a0d740efa95148ab58de Issue-ID: HOLMES-8 Signed-off-by: Guangrong Fu --- engine-d-standalone/pom.xml | 10 +- .../src/main/assembly/bin/initDB.bat | 2 +- .../src/main/assembly/bin/initDB.sh | 2 +- engine-d-standalone/src/main/assembly/bin/run.bat | 2 +- engine-d-standalone/src/main/assembly/bin/run.sh | 2 +- engine-d-standalone/src/main/assembly/bin/stop.bat | 2 +- engine-d-standalone/src/main/assembly/bin/stop.sh | 2 +- .../src/main/assembly/conf/engine-d.yml | 2 +- engine-d/pom.xml | 12 +- .../org/onap/holmes/engine/EngineDActiveApp.java | 54 ++++ .../org/onap/holmes/engine/EngineDAppConfig.java | 69 +++++ .../onap/holmes/engine/db/CorrelationRuleDao.java | 37 +++ .../engine/db/mapper/CorrelationRuleMapper.java | 48 ++++ .../onap/holmes/engine/manager/DroolsEngine.java | 285 ++++++++++++++++++ .../holmes/engine/request/CompileRuleRequest.java | 30 ++ .../holmes/engine/request/DeployRuleRequest.java | 33 +++ .../holmes/engine/resources/EngineResources.java | 115 ++++++++ .../engine/response/CorrelationRuleResponse.java | 28 ++ .../org/onap/holmes/engine/utils/AlarmUtil.java | 99 +++++++ .../onap/holmes/engine/wrapper/RuleMgtWrapper.java | 43 +++ .../org/openo/holmes/engine/EngineDActiveApp.java | 54 ---- .../org/openo/holmes/engine/EngineDAppConfig.java | 69 ----- .../openo/holmes/engine/db/CorrelationRuleDao.java | 37 --- .../engine/db/mapper/CorrelationRuleMapper.java | 48 ---- .../openo/holmes/engine/manager/DroolsEngine.java | 301 ------------------- .../holmes/engine/request/CompileRuleRequest.java | 30 -- .../holmes/engine/request/DeployRuleRequest.java | 33 --- .../holmes/engine/resources/EngineResources.java | 115 -------- .../engine/response/CorrelationRuleResponse.java | 28 -- .../org/openo/holmes/engine/utils/AlarmUtil.java | 99 ------- .../holmes/engine/wrapper/RuleMgtWrapper.java | 44 --- .../onap/holmes/engine/EngineDActiveAppTest.java | 27 ++ .../onap/holmes/engine/EnginedAppConfigTest.java | 75 +++++ .../holmes/engine/db/CorrelationRuleDaoTest.java | 48 ++++ .../db/mapper/CorrelationRuleMapperTest.java | 57 ++++ .../holmes/engine/manager/DroolsEngineTest.java | 320 +++++++++++++++++++++ .../engine/request/CompileRuleRequestTest.java | 32 +++ .../engine/request/DeployRuleRequestTest.java | 40 +++ .../engine/resources/EngineResourcesTest.java | 132 +++++++++ .../response/CorrelationRuleResponseTest.java | 32 +++ .../onap/holmes/engine/utils/AlarmUtilTest.java | 136 +++++++++ .../holmes/engine/wrapper/RuleMgtWrapperTest.java | 62 ++++ .../openo/holmes/engine/EngineDActiveAppTest.java | 27 -- .../openo/holmes/engine/EnginedAppConfigTest.java | 75 ----- .../holmes/engine/db/CorrelationRuleDaoTest.java | 48 ---- .../db/mapper/CorrelationRuleMapperTest.java | 57 ---- .../holmes/engine/manager/DroolsEngineTest.java | 320 --------------------- .../engine/request/CompileRuleRequestTest.java | 32 --- .../engine/request/DeployRuleRequestTest.java | 40 --- .../engine/resources/EngineResourcesTest.java | 132 --------- .../response/CorrelationRuleResponseTest.java | 33 --- .../openo/holmes/engine/utils/AlarmUtilTest.java | 136 --------- .../holmes/engine/wrapper/RuleMgtWrapperTest.java | 62 ---- pom.xml | 26 +- 54 files changed, 1825 insertions(+), 1859 deletions(-) create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/EnginedAppConfigTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/db/CorrelationRuleDaoTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapperTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/request/CompileRuleRequestTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/request/DeployRuleRequestTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/response/CorrelationRuleResponseTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/utils/AlarmUtilTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/engine/wrapper/RuleMgtWrapperTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java delete mode 100644 engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java diff --git a/engine-d-standalone/pom.xml b/engine-d-standalone/pom.xml index dc573d1..bb72317 100644 --- a/engine-d-standalone/pom.xml +++ b/engine-d-standalone/pom.xml @@ -20,9 +20,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-parent - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT holmes-engine-d-standalone @@ -96,7 +96,7 @@ - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-d jar true @@ -115,7 +115,7 @@ - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-d jar true @@ -166,7 +166,7 @@ - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-d ${project.version} diff --git a/engine-d-standalone/src/main/assembly/bin/initDB.bat b/engine-d-standalone/src/main/assembly/bin/initDB.bat index 29a795c..84b2e54 100644 --- a/engine-d-standalone/src/main/assembly/bin/initDB.bat +++ b/engine-d-standalone/src/main/assembly/bin/initDB.bat @@ -24,7 +24,7 @@ echo start init holmes engine-d db echo HOME=%HOME% set main_path=%HOME%..\ cd /d %main_path% -mysql -u%user% -p%password% -P%port% -h%host% < %main_path%\dbscripts\mysql\openo-holmes_engine_d-createobj.sql +mysql -u%user% -p%password% -P%port% -h%host% < %main_path%\dbscripts\mysql\onap-holmes_engine_d-createobj.sql set "err=%errorlevel%" if "%err%"=="0" ( echo init engine-d db success diff --git a/engine-d-standalone/src/main/assembly/bin/initDB.sh b/engine-d-standalone/src/main/assembly/bin/initDB.sh index a02391b..97361ea 100644 --- a/engine-d-standalone/src/main/assembly/bin/initDB.sh +++ b/engine-d-standalone/src/main/assembly/bin/initDB.sh @@ -24,7 +24,7 @@ host=$4 echo "start init holmes engine-d db" main_path=$HOME/../ cat $main_path -mysql -u$user -p$password -P$port -h$host <$main_path/dbscripts/mysql/openo-holmes_engine_d-createobj.sql +mysql -u$user -p$password -P$port -h$host <$main_path/dbscripts/mysql/onap-holmes_engine_d-createobj.sql sql_result=$? if [ $sql_result != 0 ] ; then echo "failed to init engine-d database!" diff --git a/engine-d-standalone/src/main/assembly/bin/run.bat b/engine-d-standalone/src/main/assembly/bin/run.bat index 89151e8..e61f25a 100644 --- a/engine-d-standalone/src/main/assembly/bin/run.bat +++ b/engine-d-standalone/src/main/assembly/bin/run.bat @@ -30,7 +30,7 @@ set class_path=%main_path%;%main_path%holmes-engine-d.jar echo ### jvm_opts: %jvm_opts% echo ### class_path: %class_path% -%JAVA% -classpath %class_path% %jvm_opts% org.openo.holmes.engine.EngineDActiveApp server %main_path%conf/engine-d.yml +%JAVA% -classpath %class_path% %jvm_opts% EngineDActiveApp server %main_path%conf/engine-d.yml IF ERRORLEVEL 1 goto showerror exit diff --git a/engine-d-standalone/src/main/assembly/bin/run.sh b/engine-d-standalone/src/main/assembly/bin/run.sh index 4a93b50..a403aeb 100644 --- a/engine-d-standalone/src/main/assembly/bin/run.sh +++ b/engine-d-standalone/src/main/assembly/bin/run.sh @@ -31,5 +31,5 @@ echo @JAVA_OPTS@ $JAVA_OPTS class_path="$main_path/:$main_path/holmes-engine-d.jar" echo @class_path@ $class_path -"$JAVA" $JAVA_OPTS -classpath "$class_path" org.openo.holmes.engine.EngineDActiveApp server "$main_path/conf/engine-d.yml" +"$JAVA" $JAVA_OPTS -classpath "$class_path" EngineDActiveApp server "$main_path/conf/engine-d.yml" diff --git a/engine-d-standalone/src/main/assembly/bin/stop.bat b/engine-d-standalone/src/main/assembly/bin/stop.bat index 22280e7..f4ed01b 100644 --- a/engine-d-standalone/src/main/assembly/bin/stop.bat +++ b/engine-d-standalone/src/main/assembly/bin/stop.bat @@ -18,7 +18,7 @@ title stopping engine-d-service set HOME=%~dp0 -set Main_Class="org.openo.holmes.engine.EngineDActiveApp" +set Main_Class="EngineDActiveApp" echo ================== engine-d-service info ============================================= echo HOME=$HOME diff --git a/engine-d-standalone/src/main/assembly/bin/stop.sh b/engine-d-standalone/src/main/assembly/bin/stop.sh index 8f81bda..5c217f1 100644 --- a/engine-d-standalone/src/main/assembly/bin/stop.sh +++ b/engine-d-standalone/src/main/assembly/bin/stop.sh @@ -17,7 +17,7 @@ DIRNAME=`dirname $0` HOME=`cd $DIRNAME/; pwd` -Main_Class="org.openo.holmes.engine.EngineDActiveApp" +Main_Class="EngineDActiveApp" echo ================== engine-d-service info ============================================= echo HOME=$HOME diff --git a/engine-d-standalone/src/main/assembly/conf/engine-d.yml b/engine-d-standalone/src/main/assembly/conf/engine-d.yml index c16bf71..de129c4 100644 --- a/engine-d-standalone/src/main/assembly/conf/engine-d.yml +++ b/engine-d-standalone/src/main/assembly/conf/engine-d.yml @@ -15,7 +15,7 @@ # server: type: simple - rootPath: '/openoapi/holmes-engine-mgmt/v1/*' + rootPath: '/onapapi/holmes-engine-mgmt/v1/*' applicationContextPath: / adminContextPath: /admin connector: diff --git a/engine-d/pom.xml b/engine-d/pom.xml index b47bf62..c5aa87d 100644 --- a/engine-d/pom.xml +++ b/engine-d/pom.xml @@ -20,9 +20,9 @@ 4.0.0 - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-parent - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT holmes-engine-d @@ -36,7 +36,7 @@ - org.openo.holmes.actions + org.onap.holmes.common holmes-actions @@ -120,10 +120,6 @@ org.slf4j slf4j-api - - org.openo.client.cli - swagger-sdk - io.swagger swagger-jersey2-jaxrs @@ -196,7 +192,7 @@ implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> - org.openo.holmes.engine.EngineDActiveApp + org.onap.holmes.engine.EngineDActiveApp diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java new file mode 100644 index 0000000..ef671e3 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java @@ -0,0 +1,54 @@ +/** + * 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.engine; + +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 EngineDActiveApp extends IOCApplication { + + public static void main(String[] args) throws Exception { + new EngineDActiveApp().run(args); + } + + @Override + public void run(EngineDAppConfig 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-engine-mgmt"); + serviceRegisterEntity.setProtocol("REST"); + serviceRegisterEntity.setVersion("v1"); + serviceRegisterEntity.setUrl("/onapapi/holmes-engine-mgmt/v1"); + serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9102", 0); + serviceRegisterEntity.setVisualRange("1|0"); + return serviceRegisterEntity; + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java new file mode 100644 index 0000000..c42e9d9 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java @@ -0,0 +1,69 @@ +/** + * 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.engine; + +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; +import org.onap.holmes.common.config.MQConfig; + +@Service +public class EngineDAppConfig extends Configuration { + + @NotEmpty + private String defaultName = "Correlation-Rule"; + + @NotEmpty + private String apidescription = "Holmes rule management rest API"; + + @JsonProperty + @NotNull + @Valid + private MQConfig mqConfig; + @Valid + @NotNull + private DataSourceFactory database = new DataSourceFactory(); + + public MQConfig getMqConfig() { + return mqConfig; + } + + public void setMqConfig(MQConfig mqConfig) { + this.mqConfig = mqConfig; + } + + @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/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java new file mode 100644 index 0000000..4e01778 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java @@ -0,0 +1,37 @@ +/** + * 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.engine.db; + + +import java.util.List; +import org.onap.holmes.engine.db.mapper.CorrelationRuleMapper; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.skife.jdbi.v2.sqlobject.Bind; +import org.skife.jdbi.v2.sqlobject.SqlQuery; +import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; + +@RegisterMapper(CorrelationRuleMapper.class) +public abstract class CorrelationRuleDao { + + + @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable") + public abstract List queryRuleByEnable(@Bind("enable") int enable); + + public List queryRuleByRuleEnable(int enable) { + return queryRuleByEnable(enable); + } +} + diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java b/engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java new file mode 100644 index 0000000..a682824 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/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.engine.db.mapper; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.skife.jdbi.v2.StatementContext; +import org.skife.jdbi.v2.tweak.ResultSetMapper; + +public class CorrelationRuleMapper implements ResultSetMapper { + + 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/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java new file mode 100644 index 0000000..c8835cc --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java @@ -0,0 +1,285 @@ +/** + * 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.engine.manager; + + +import java.io.Serializable; +import java.io.StringReader; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; +import lombok.extern.slf4j.Slf4j; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.drools.KnowledgeBase; +import org.drools.KnowledgeBaseConfiguration; +import org.drools.KnowledgeBaseFactory; +import org.drools.builder.KnowledgeBuilder; +import org.drools.builder.KnowledgeBuilderFactory; +import org.drools.builder.ResourceType; +import org.drools.conf.EventProcessingOption; +import org.drools.definition.KnowledgePackage; +import org.drools.io.Resource; +import org.drools.io.ResourceFactory; +import org.drools.runtime.StatefulKnowledgeSession; +import org.drools.runtime.rule.FactHandle; +import org.glassfish.hk2.api.IterableProvider; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.engine.request.DeployRuleRequest; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.api.stat.Alarm; +import org.onap.holmes.common.config.MQConfig; +import org.onap.holmes.common.constant.AlarmConst; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.ExceptionUtil; +import org.onap.holmes.engine.wrapper.RuleMgtWrapper; + +@Slf4j +@Service +public class DroolsEngine { + + private final static int ENABLE = 1; + private final Set packageNames = new HashSet(); + @Inject + private RuleMgtWrapper ruleMgtWrapper; + private KnowledgeBase kbase; + private KnowledgeBaseConfiguration kconf; + private StatefulKnowledgeSession ksession; + @Inject + private IterableProvider mqConfigProvider; + private ConnectionFactory connectionFactory; + + @PostConstruct + private void init() { + try { + // 1. start engine + start(); + // 2. start mq listener + registerAlarmTopicListener(); + } catch (Exception e) { + log.error("Failed to start the service: " + e.getMessage(), e); + throw ExceptionUtil.buildExceptionResponse("Failed to start the drools engine!"); + } + } + + private void registerAlarmTopicListener() { + String brokerURL = + "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort; + connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername, + mqConfigProvider.get().brokerPassword, brokerURL); + + AlarmMqMessageListener listener = new AlarmMqMessageListener(); + listener.receive(); + } + + + private void start() throws CorrelationException { + log.info("Drools Engine Initialize Beginning..."); + + initEngineParameter(); + initDeployRule(); + + log.info("Business Rule Engine Initialize Successfully."); + } + + public void stop() { + this.ksession.dispose(); + } + + private void initEngineParameter() { + this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); + + this.kconf.setOption(EventProcessingOption.STREAM); + + this.kconf.setProperty("drools.assertBehaviour", "equality"); + + this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf); + + this.ksession = kbase.newStatefulKnowledgeSession(); + } + + private void initDeployRule() throws CorrelationException { + List rules = ruleMgtWrapper.queryRuleByEnable(ENABLE); + + if (rules.isEmpty()) { + return; + } + for (CorrelationRule rule : rules) { + if (rule.getContent() != null) { + deployRuleFromDB(rule.getContent()); + } + } + } + + private void deployRuleFromDB(String ruleContent) throws CorrelationException { + StringReader reader = new StringReader(ruleContent); + Resource res = ResourceFactory.newReaderResource(reader); + + KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + + kbuilder.add(res, ResourceType.DRL); + + try { + + kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); + } catch (Exception e) { + throw new CorrelationException(e.getMessage(), e); + } + ksession.fireAllRules(); + } + + public synchronized String deployRule(DeployRuleRequest rule, Locale locale) + throws CorrelationException { + StringReader reader = new StringReader(rule.getContent()); + Resource res = ResourceFactory.newReaderResource(reader); + + KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + + kbuilder.add(res, ResourceType.DRL); + + judgeRuleContent(locale, kbuilder, true); + + String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); + try { + packageNames.add(packageName); + kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); + } catch (Exception e) { + throw new CorrelationException("Failed to deploy the rule.", e); + } + + ksession.fireAllRules(); + return packageName; + } + + public synchronized void undeployRule(String packageName, Locale locale) + throws CorrelationException { + + KnowledgePackage pkg = kbase.getKnowledgePackage(packageName); + + if (null == pkg) { + throw new CorrelationException("The rule " + packageName + " does not exist!"); + } + + try { + kbase.removeKnowledgePackage(pkg.getName()); + } catch (Exception e) { + throw new CorrelationException("Failed to delete the rule: " + packageName, e); + } + } + + public void compileRule(String content, Locale locale) + throws CorrelationException { + StringReader reader = new StringReader(content); + Resource res = ResourceFactory.newReaderResource(reader); + + KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + + kbuilder.add(res, ResourceType.DRL); + + judgeRuleContent(locale, kbuilder, false); + } + + private void judgeRuleContent(Locale locale, KnowledgeBuilder kbuilder, boolean judgePackageName) + throws CorrelationException { + if (kbuilder.hasErrors()) { + String errorMsg = "There are errors in the rule: " + kbuilder.getErrors().toString(); + log.error(errorMsg); + throw new CorrelationException(errorMsg); + } + + String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); + + if (packageNames.contains(packageName) && judgePackageName) { + throw new CorrelationException("The rule " + packageName + " already exists in the drools engine."); + } + } + + public void putRaisedIntoStream(Alarm raiseAlarm) { + FactHandle factHandle = this.ksession.getFactHandle(raiseAlarm); + if (factHandle != null) { + this.ksession.retract(factHandle); + } + this.ksession.insert(raiseAlarm); + this.ksession.fireAllRules(); + } + + class AlarmMqMessageListener implements MessageListener { + + private Connection connection = null; + private Session session = null; + private Destination destination = null; + private MessageConsumer consumer = null; + + private void initialize() throws JMSException { + connection = connectionFactory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM); + consumer = session.createConsumer(destination); + connection.start(); + } + + public void receive() { + try { + initialize(); + consumer.setMessageListener(this); + } catch (JMSException e) { + log.error("Failed to connect to the MQ service : " + e.getMessage(), e); + try { + close(); + } catch (JMSException e1) { + log.error("Failed close connection " + e1.getMessage(), e1); + } + } + } + + public void onMessage(Message arg0) { + ActiveMQObjectMessage objectMessage = (ActiveMQObjectMessage) arg0; + try { + Serializable object = objectMessage.getObject(); + + if (object instanceof Alarm) { + Alarm alarm = (Alarm) object; + putRaisedIntoStream(alarm); + } + } catch (JMSException e) { + log.error("Failed get object : " + e.getMessage(), e); + } + } + + private void close() throws JMSException { + if (consumer != null) { + consumer.close(); + } + if (session != null) { + session.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java new file mode 100644 index 0000000..6e0da48 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java @@ -0,0 +1,30 @@ +/** + * 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.engine.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CompileRuleRequest { + + @JsonProperty(value = "content") + @NotNull + private String content; +} \ No newline at end of file diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java new file mode 100644 index 0000000..de9d773 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.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.engine.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DeployRuleRequest { + + @JsonProperty(value = "content") + @NotNull + private String content; + + @JsonProperty(value = "engineid") + private String engineId; +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java new file mode 100644 index 0000000..3480dbb --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java @@ -0,0 +1,115 @@ +/** + * 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.engine.resources; + + +import com.codahale.metrics.annotation.Timed; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import java.util.Locale; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.ExceptionUtil; +import org.onap.holmes.common.utils.LanguageUtil; +import org.onap.holmes.engine.manager.DroolsEngine; +import org.onap.holmes.engine.request.CompileRuleRequest; +import org.onap.holmes.engine.request.DeployRuleRequest; +import org.onap.holmes.engine.response.CorrelationRuleResponse; + +@Service +@Path("/rule") +@Api(tags = {"Engine Manager"}) +@Produces(MediaType.APPLICATION_JSON) +@Slf4j +public class EngineResources { + + @Inject + DroolsEngine droolsEngine; + + @PUT + @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class) + @Produces(MediaType.APPLICATION_JSON) + @Timed + public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest, + @Context HttpServletRequest httpRequest) { + + CorrelationRuleResponse crResponse = new CorrelationRuleResponse(); + Locale locale = LanguageUtil.getLocale(httpRequest); + try { + + String packageName = droolsEngine.deployRule(deployRuleRequest, locale); + log.info("Rule deployed. Package name: " + packageName); + crResponse.setPackageName(packageName); + + } catch (CorrelationException correlationException) { + log.error(correlationException.getMessage(), correlationException); + throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); + } + + return crResponse; + } + + @DELETE + @ApiOperation(value = "delete rule") + @Produces(MediaType.APPLICATION_JSON) + @Timed + @Path("/{packageName}") + public boolean undeployRule(@PathParam("packageName") String packageName, + @Context HttpServletRequest httpRequest) { + + Locale locale = LanguageUtil.getLocale(httpRequest); + + try { + + droolsEngine.undeployRule(packageName, locale); + + } catch (CorrelationException correlationException) { + log.error(correlationException.getMessage(), correlationException); + throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); + } + return true; + } + + + @POST + @ApiOperation(value = "compile rule") + @Produces(MediaType.APPLICATION_JSON) + @Timed + public boolean compileRule(CompileRuleRequest compileRuleRequest, + @Context HttpServletRequest httpRequest) { + + Locale locale = LanguageUtil.getLocale(httpRequest); + + try { + droolsEngine.compileRule(compileRuleRequest.getContent(), locale); + } catch (CorrelationException correlationException) { + log.error(correlationException.getMessage(), correlationException); + throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); + } + return true; + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java new file mode 100644 index 0000000..b234f91 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.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.engine.response; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CorrelationRuleResponse { + + @JsonProperty(value = "package") + private String packageName; +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java new file mode 100644 index 0000000..ca93066 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.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.engine.utils; + +import java.util.HashMap; +import java.util.Map; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.stat.Alarm; +import org.onap.holmes.common.producer.MQProducer; + +@Service +public class AlarmUtil { + + private final static AlarmUtil alarmUtil = new AlarmUtil(); + /** + * Map> + */ + private final Map> rootPriorityMap = + new HashMap>(); + /** + * Map + */ + private final Map saveRuleMsg = new HashMap(); + + private AlarmUtil() { + } + + public static AlarmUtil getInstance() { + return alarmUtil; + } + + public boolean equipTypeFilter(String probableCauseStr, String equipType, Alarm alarm) { + if ("null".equals(probableCauseStr)) { + return true; + } + String[] equipTypes = equipType.replace(" ", "").split(","); + String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(","); + for (int i = 0; i < probableCauseStrs.length; i++) { + if (alarm.getProbableCause() == Long.parseLong(probableCauseStrs[i]) + && alarm.getEquipType().equals(equipTypes[i])) { + return true; + } + } + return false; + } + + public Integer getPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, + String equipTypeStr, Alarm alarm) { + if (rootPriorityMap.containsKey(ruleId)) { + if (!saveRuleMsg.get(ruleId) + .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) { + setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr); + } + } else { + setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr); + } + + Integer priority = + rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType()); + if (priority == null) { + priority = 0; + } + return priority; + } + + private void setPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, + String equipTypeStr) { + saveRuleMsg.put(ruleId, probableCauseStr + equipTypeStr + rootAlarmFeatureStr); + + Map map = new HashMap(); + String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(","); + String[] rootAlarmFeatureStrs = rootAlarmFeatureStr.replace(" ", "").split(","); + String[] equipTypes = equipTypeStr.replace(" ", "").split(","); + for (int i = 0; i < rootAlarmFeatureStrs.length; i++) { + map.put(probableCauseStrs[i] + "-" + equipTypes[i], + Integer.parseInt(rootAlarmFeatureStrs[i])); + } + + rootPriorityMap.put(ruleId, map); + } + + public MQProducer getMqProducer() { + MQProducer mqProducer = new MQProducer(); + return mqProducer; + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java new file mode 100644 index 0000000..1fbbfe7 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java @@ -0,0 +1,43 @@ +/** + * 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.engine.wrapper; + +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.engine.db.CorrelationRuleDao; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.DbDaoUtil; + + +@Service +@Singleton +@Slf4j +public class RuleMgtWrapper { + + @Inject + private DbDaoUtil daoUtil; + + public List queryRuleByEnable(int enable) throws CorrelationException { + + List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) + .queryRuleByRuleEnable(enable); + return ruleTemp; + } +} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java deleted file mode 100644 index 3169f1b..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java +++ /dev/null @@ -1,54 +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.engine; - -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 EngineDActiveApp extends IOCApplication { - - public static void main(String[] args) throws Exception { - new EngineDActiveApp().run(args); - } - - @Override - public void run(EngineDAppConfig 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-engine-mgmt"); - serviceRegisterEntity.setProtocol("REST"); - serviceRegisterEntity.setVersion("v1"); - serviceRegisterEntity.setUrl("/openoapi/holmes-engine-mgmt/v1"); - serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9102", 0); - serviceRegisterEntity.setVisualRange("1|0"); - return serviceRegisterEntity; - } -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java b/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java deleted file mode 100644 index 66944a1..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java +++ /dev/null @@ -1,69 +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.engine; - -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; -import org.openo.holmes.common.config.MQConfig; - -@Service -public class EngineDAppConfig extends Configuration { - - @NotEmpty - private String defaultName = "Correlation-Rule"; - - @NotEmpty - private String apidescription = "Holmes rule management rest API"; - - @JsonProperty - @NotNull - @Valid - private MQConfig mqConfig; - @Valid - @NotNull - private DataSourceFactory database = new DataSourceFactory(); - - public MQConfig getMqConfig() { - return mqConfig; - } - - public void setMqConfig(MQConfig mqConfig) { - this.mqConfig = mqConfig; - } - - @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/engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java deleted file mode 100644 index 4370054..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java +++ /dev/null @@ -1,37 +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.engine.db; - - -import java.util.List; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.engine.db.mapper.CorrelationRuleMapper; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; - -@RegisterMapper(CorrelationRuleMapper.class) -public abstract class CorrelationRuleDao { - - - @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable") - public abstract List queryRuleByEnable(@Bind("enable") int enable); - - public List queryRuleByRuleEnable(int enable) { - return queryRuleByEnable(enable); - } -} - diff --git a/engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java b/engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java deleted file mode 100644 index 9c46b58..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/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.engine.db.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Properties; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.skife.jdbi.v2.StatementContext; -import org.skife.jdbi.v2.tweak.ResultSetMapper; - -public class CorrelationRuleMapper implements ResultSetMapper { - - 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/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java deleted file mode 100644 index f8d9626..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java +++ /dev/null @@ -1,301 +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.engine.manager; - - -import java.io.Serializable; -import java.io.StringReader; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; -import javax.annotation.PostConstruct; -import javax.inject.Inject; -import javax.jms.Connection; -import javax.jms.ConnectionFactory; -import javax.jms.Destination; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageConsumer; -import javax.jms.MessageListener; -import javax.jms.Session; -import lombok.extern.slf4j.Slf4j; -import org.apache.activemq.ActiveMQConnectionFactory; -import org.apache.activemq.command.ActiveMQObjectMessage; -import org.drools.KnowledgeBase; -import org.drools.KnowledgeBaseConfiguration; -import org.drools.KnowledgeBaseFactory; -import org.drools.builder.KnowledgeBuilder; -import org.drools.builder.KnowledgeBuilderFactory; -import org.drools.builder.ResourceType; -import org.drools.conf.EventProcessingOption; -import org.drools.definition.KnowledgePackage; -import org.drools.io.Resource; -import org.drools.io.ResourceFactory; -import org.drools.runtime.StatefulKnowledgeSession; -import org.drools.runtime.rule.FactHandle; -import org.glassfish.hk2.api.IterableProvider; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.openo.holmes.common.api.stat.Alarm; -import org.openo.holmes.common.config.MQConfig; -import org.openo.holmes.common.constant.AlarmConst; -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.engine.request.DeployRuleRequest; -import org.openo.holmes.engine.wrapper.RuleMgtWrapper; - -@Slf4j -@Service -public class DroolsEngine { - - private final static int ENABLE = 1; - private final Set packageNames = new HashSet(); - @Inject - private RuleMgtWrapper ruleMgtWrapper; - private KnowledgeBase kbase; - private KnowledgeBaseConfiguration kconf; - private StatefulKnowledgeSession ksession; - @Inject - private IterableProvider mqConfigProvider; - private ConnectionFactory connectionFactory; - - @PostConstruct - private void init() { - try { - // 1. start engine - start(); - // 2. start mq listener - registerAlarmTopicListener(); - } catch (Exception e) { - log.error("Start service failed: " + e.getMessage(), e); - throw ExceptionUtil.buildExceptionResponse("Start service failed!"); - } - } - - private void registerAlarmTopicListener() { - String brokerURL = - "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort; - connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername, - mqConfigProvider.get().brokerPassword, brokerURL); - - AlarmMqMessageListener listener = new AlarmMqMessageListener(); - listener.receive(); - } - - - private void start() throws CorrelationException { - log.info("Drools Engine Initialize Beginning..."); - - initEngineParameter(); - initDeployRule(); - - log.info("Business Rule Engine Initialize Successfully."); - } - - public void stop() { - this.ksession.dispose(); - } - - private void initEngineParameter() { - this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); - - this.kconf.setOption(EventProcessingOption.STREAM); - - this.kconf.setProperty("drools.assertBehaviour", "equality"); - - this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf); - - this.ksession = kbase.newStatefulKnowledgeSession(); - } - - private void initDeployRule() throws CorrelationException { - List rules = ruleMgtWrapper.queryRuleByEnable(ENABLE); - - if (rules.isEmpty()) { - return; - } - for (CorrelationRule rule : rules) { - if (rule.getContent() != null) { - deployRuleFromDB(rule.getContent()); - } - } - } - - private void deployRuleFromDB(String ruleContent) throws CorrelationException { - StringReader reader = new StringReader(ruleContent); - Resource res = ResourceFactory.newReaderResource(reader); - - KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add(res, ResourceType.DRL); - - try { - - kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); - } catch (Exception e) { - throw new CorrelationException(e.getMessage(), e); - } - ksession.fireAllRules(); - } - - public synchronized String deployRule(DeployRuleRequest rule, Locale locale) - throws CorrelationException { - StringReader reader = new StringReader(rule.getContent()); - Resource res = ResourceFactory.newReaderResource(reader); - - KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add(res, ResourceType.DRL); - - judgeRuleContent(locale, kbuilder, true); - - String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); - try { - packageNames.add(packageName); - kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); - } catch (Exception e) { - - String errorMsg = - I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED); - throw new CorrelationException(errorMsg, e); - } - - ksession.fireAllRules(); - return packageName; - } - - public synchronized void undeployRule(String packageName, Locale locale) - throws CorrelationException { - - KnowledgePackage pkg = kbase.getKnowledgePackage(packageName); - - if (null == pkg) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_DELETE_RULE_NULL, - new String[]{packageName}); - throw new CorrelationException(errorMsg); - } - - try { - - kbase.removeKnowledgePackage(pkg.getName()); - } catch (Exception e) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName}); - throw new CorrelationException(errorMsg, e); - } - } - - public void compileRule(String content, Locale locale) - throws CorrelationException { - StringReader reader = new StringReader(content); - Resource res = ResourceFactory.newReaderResource(reader); - - KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add(res, ResourceType.DRL); - - judgeRuleContent(locale, kbuilder, false); - } - - private void judgeRuleContent(Locale locale, KnowledgeBuilder kbuilder, boolean judgePackageName) - throws CorrelationException { - if (kbuilder.hasErrors()) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_CONTENT_ILLEGALITY, - new String[]{kbuilder.getErrors().toString()}); - log.error(errorMsg); - throw new CorrelationException(errorMsg); - } - - String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); - - if (packageNames.contains(packageName) && judgePackageName) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_CONTENT_ILLEGALITY, new String[]{ - I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE)}); - - throw new CorrelationException(errorMsg); - } - } - - public void putRaisedIntoStream(Alarm raiseAlarm) { - FactHandle factHandle = this.ksession.getFactHandle(raiseAlarm); - if (factHandle != null) { - this.ksession.retract(factHandle); - } - this.ksession.insert(raiseAlarm); - this.ksession.fireAllRules(); - } - - class AlarmMqMessageListener implements MessageListener { - - private Connection connection = null; - private Session session = null; - private Destination destination = null; - private MessageConsumer consumer = null; - - private void initialize() throws JMSException { - connection = connectionFactory.createConnection(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM); - consumer = session.createConsumer(destination); - connection.start(); - } - - public void receive() { - try { - initialize(); - consumer.setMessageListener(this); - } catch (JMSException e) { - log.error("Failed to connect to the MQ service : " + e.getMessage(), e); - try { - close(); - } catch (JMSException e1) { - log.error("Failed close connection " + e1.getMessage(), e1); - } - } - } - - public void onMessage(Message arg0) { - ActiveMQObjectMessage objectMessage = (ActiveMQObjectMessage) arg0; - try { - Serializable object = objectMessage.getObject(); - - if (object instanceof Alarm) { - Alarm alarm = (Alarm) object; - putRaisedIntoStream(alarm); - } - } catch (JMSException e) { - log.error("Failed get object : " + e.getMessage(), e); - } - } - - private void close() throws JMSException { - if (consumer != null) { - consumer.close(); - } - if (session != null) { - session.close(); - } - if (connection != null) { - connection.close(); - } - } - } -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java deleted file mode 100644 index 692deb5..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java +++ /dev/null @@ -1,30 +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.engine.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import javax.validation.constraints.NotNull; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class CompileRuleRequest { - - @JsonProperty(value = "content") - @NotNull - private String content; -} \ No newline at end of file diff --git a/engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.java deleted file mode 100644 index 979c5bf..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.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.engine.request; - -import com.fasterxml.jackson.annotation.JsonProperty; -import javax.validation.constraints.NotNull; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class DeployRuleRequest { - - @JsonProperty(value = "content") - @NotNull - private String content; - - @JsonProperty(value = "engineid") - private String engineId; -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java deleted file mode 100644 index 5be8eb9..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java +++ /dev/null @@ -1,115 +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.engine.resources; - - -import com.codahale.metrics.annotation.Timed; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import java.util.Locale; -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.common.utils.ExceptionUtil; -import org.openo.holmes.common.utils.LanguageUtil; -import org.openo.holmes.engine.manager.DroolsEngine; -import org.openo.holmes.engine.request.CompileRuleRequest; -import org.openo.holmes.engine.request.DeployRuleRequest; -import org.openo.holmes.engine.response.CorrelationRuleResponse; - -@Service -@Path("/rule") -@Api(tags = {"Engine Manager"}) -@Produces(MediaType.APPLICATION_JSON) -@Slf4j -public class EngineResources { - - @Inject - DroolsEngine droolsEngine; - - @PUT - @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class) - @Produces(MediaType.APPLICATION_JSON) - @Timed - public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest, - @Context HttpServletRequest httpRequest) { - - CorrelationRuleResponse crResponse = new CorrelationRuleResponse(); - Locale locale = LanguageUtil.getLocale(httpRequest); - try { - - String packageName = droolsEngine.deployRule(deployRuleRequest, locale); - log.info("Rule deployed. Package name: " + packageName); - crResponse.setPackageName(packageName); - - } catch (CorrelationException correlationException) { - log.error(correlationException.getMessage(), correlationException); - throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); - } - - return crResponse; - } - - @DELETE - @ApiOperation(value = "delete rule") - @Produces(MediaType.APPLICATION_JSON) - @Timed - @Path("/{packageName}") - public boolean undeployRule(@PathParam("packageName") String packageName, - @Context HttpServletRequest httpRequest) { - - Locale locale = LanguageUtil.getLocale(httpRequest); - - try { - - droolsEngine.undeployRule(packageName, locale); - - } catch (CorrelationException correlationException) { - log.error(correlationException.getMessage(), correlationException); - throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); - } - return true; - } - - - @POST - @ApiOperation(value = "compile rule") - @Produces(MediaType.APPLICATION_JSON) - @Timed - public boolean compileRule(CompileRuleRequest compileRuleRequest, - @Context HttpServletRequest httpRequest) { - - Locale locale = LanguageUtil.getLocale(httpRequest); - - try { - droolsEngine.compileRule(compileRuleRequest.getContent(), locale); - } catch (CorrelationException correlationException) { - log.error(correlationException.getMessage(), correlationException); - throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); - } - return true; - } -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.java deleted file mode 100644 index f033a1b..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.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.engine.response; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class CorrelationRuleResponse { - - @JsonProperty(value = "package") - private String packageName; -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.java b/engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.java deleted file mode 100644 index 558d97b..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.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.engine.utils; - -import java.util.HashMap; -import java.util.Map; -import org.jvnet.hk2.annotations.Service; -import org.openo.holmes.common.api.stat.Alarm; -import org.openo.holmes.common.producer.MQProducer; - -@Service -public class AlarmUtil { - - private final static AlarmUtil alarmUtil = new AlarmUtil(); - /** - * Map> - */ - private final Map> rootPriorityMap = - new HashMap>(); - /** - * Map - */ - private final Map saveRuleMsg = new HashMap(); - - private AlarmUtil() { - } - - public static AlarmUtil getInstance() { - return alarmUtil; - } - - public boolean equipTypeFilter(String probableCauseStr, String equipType, Alarm alarm) { - if ("null".equals(probableCauseStr)) { - return true; - } - String[] equipTypes = equipType.replace(" ", "").split(","); - String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(","); - for (int i = 0; i < probableCauseStrs.length; i++) { - if (alarm.getProbableCause() == Long.parseLong(probableCauseStrs[i]) - && alarm.getEquipType().equals(equipTypes[i])) { - return true; - } - } - return false; - } - - public Integer getPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, - String equipTypeStr, Alarm alarm) { - if (rootPriorityMap.containsKey(ruleId)) { - if (!saveRuleMsg.get(ruleId) - .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) { - setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr); - } - } else { - setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr); - } - - Integer priority = - rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType()); - if (priority == null) { - priority = 0; - } - return priority; - } - - private void setPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, - String equipTypeStr) { - saveRuleMsg.put(ruleId, probableCauseStr + equipTypeStr + rootAlarmFeatureStr); - - Map map = new HashMap(); - String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(","); - String[] rootAlarmFeatureStrs = rootAlarmFeatureStr.replace(" ", "").split(","); - String[] equipTypes = equipTypeStr.replace(" ", "").split(","); - for (int i = 0; i < rootAlarmFeatureStrs.length; i++) { - map.put(probableCauseStrs[i] + "-" + equipTypes[i], - Integer.parseInt(rootAlarmFeatureStrs[i])); - } - - rootPriorityMap.put(ruleId, map); - } - - public MQProducer getMqProducer() { - MQProducer mqProducer = new MQProducer(); - return mqProducer; - } -} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java deleted file mode 100644 index 5a9ce8f..0000000 --- a/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java +++ /dev/null @@ -1,44 +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.engine.wrapper; - -import java.util.List; -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.engine.db.CorrelationRuleDao; - - -@Service -@Singleton -@Slf4j -public class RuleMgtWrapper { - - @Inject - private DbDaoUtil daoUtil; - - public List queryRuleByEnable(int enable) throws CorrelationException { - - List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) - .queryRuleByRuleEnable(enable); - return ruleTemp; - } -} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java b/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java new file mode 100644 index 0000000..362211b --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.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.engine; + +/** + * Created by Administrator on 2017/2/20. + */ + +public class EngineDActiveAppTest { + public static void main(String[] args) throws Exception { + String filePath = "C:\\correlation-engine.yml"; + new EngineDActiveApp().run(new String[]{"server", filePath}); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/EnginedAppConfigTest.java b/engine-d/src/test/java/org/onap/holmes/engine/EnginedAppConfigTest.java new file mode 100644 index 0000000..9642474 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/EnginedAppConfigTest.java @@ -0,0 +1,75 @@ +/** + * 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.engine; + +import io.dropwizard.db.DataSourceFactory; +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.holmes.common.config.MQConfig; +import org.powermock.api.easymock.PowerMock; + +public class EnginedAppConfigTest { + + private EngineDAppConfig engineAppConfig; + + @Before + public void setUp() { + engineAppConfig = new EngineDAppConfig(); + } + + @Test + public void getMqConfig() { + MQConfig mqConfig = PowerMock.createMock(MQConfig.class); + engineAppConfig.setMqConfig(mqConfig); + Assert.assertThat(engineAppConfig.getMqConfig(), IsNull.notNullValue()); + } + + @Test + public void setMqConfig() { + MQConfig mqConfig = PowerMock.createMock(MQConfig.class); + engineAppConfig.setMqConfig(mqConfig); + Assert.assertThat(engineAppConfig.getMqConfig(), IsEqual.equalTo(mqConfig)); + } + + @Test + public void getDataSourceFactory() { + Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsNull.notNullValue()); + } + + @Test + public void setDataSourceFactory() { + DataSourceFactory database = new DataSourceFactory(); + engineAppConfig.setDataSourceFactory(database); + Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsEqual.equalTo(database)); + } + + @Test + public void getApidescription() { + final String apidescription = "Holmes rule management rest API"; + Assert.assertThat(engineAppConfig.getApidescription(), IsEqual.equalTo(apidescription)); + } + + @Test + public void setApidescription() { + final String apidescription = "set api description"; + engineAppConfig.setApidescription(apidescription); + Assert.assertThat(engineAppConfig.getApidescription(), IsEqual.equalTo(apidescription)); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/db/CorrelationRuleDaoTest.java b/engine-d/src/test/java/org/onap/holmes/engine/db/CorrelationRuleDaoTest.java new file mode 100644 index 0000000..128f58a --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/db/CorrelationRuleDaoTest.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.engine.db; + +import org.easymock.EasyMock; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.powermock.api.easymock.PowerMock; + +import java.util.ArrayList; +import java.util.List; + +public class CorrelationRuleDaoTest { + private CorrelationRuleDao correlationRuleDao; + + @Before + public void setUp() { + correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + } + + @Test + public void queryRuleByEnable() { + int enable = 0; + EasyMock.expect(correlationRuleDao.queryRuleByEnable(EasyMock.anyInt())).andReturn(new ArrayList()); + PowerMock.replayAll(); + List correlationRules = correlationRuleDao.queryRuleByEnable(enable); + PowerMock.verifyAll(); + Assert.assertThat(correlationRules, IsNull.>notNullValue()); + } + +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapperTest.java b/engine-d/src/test/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapperTest.java new file mode 100644 index 0000000..acf62c6 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapperTest.java @@ -0,0 +1,57 @@ +/** + * 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.engine.db.mapper; + +import org.junit.Test; +import org.powermock.api.easymock.PowerMock; + +import java.sql.Date; +import java.sql.ResultSet; +import java.util.Properties; + +import static org.easymock.EasyMock.expect; + +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(0); + 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.replayAll(); + + mapper.map(0, resultSet, null); + + PowerMock.verify(); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java new file mode 100644 index 0000000..5487177 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java @@ -0,0 +1,320 @@ +/** + * 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.engine.manager; + +import static org.easymock.EasyMock.anyBoolean; +import static org.easymock.EasyMock.anyInt; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; +import javax.jms.Topic; +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.drools.KnowledgeBase; +import org.drools.KnowledgeBaseConfiguration; +import org.drools.KnowledgeBaseFactory; +import org.drools.conf.EventProcessingOption; +import org.drools.runtime.StatefulKnowledgeSession; +import org.easymock.EasyMock; +import org.glassfish.hk2.api.IterableProvider; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.engine.request.DeployRuleRequest; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.api.stat.Alarm; +import org.onap.holmes.common.config.MQConfig; +import org.onap.holmes.common.constant.AlarmConst; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.engine.wrapper.RuleMgtWrapper; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.powermock.reflect.Whitebox; + +public class DroolsEngineTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + + private RuleMgtWrapper ruleMgtWrapper; + + private KnowledgeBase kbase; + + private KnowledgeBaseConfiguration kconf; + + private StatefulKnowledgeSession ksession; + + private IterableProvider mqConfigProvider; + + private ConnectionFactory connectionFactory; + + private DroolsEngine droolsEngine; + + @Before + public void setUp() { + droolsEngine = new DroolsEngine(); + + this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); + this.kconf.setOption(EventProcessingOption.STREAM); + this.kconf.setProperty("drools.assertBehaviour", "equality"); + this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf); + this.ksession = kbase.newStatefulKnowledgeSession(); + + ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class); + mqConfigProvider = PowerMock.createMock(IterableProvider.class); + connectionFactory = PowerMock.createMock(ConnectionFactory.class); + + Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper); + Whitebox.setInternalState(droolsEngine, "mqConfigProvider", mqConfigProvider); + Whitebox.setInternalState(droolsEngine, "kconf", kconf); + Whitebox.setInternalState(droolsEngine, "kbase", kbase); + Whitebox.setInternalState(droolsEngine, "ksession", ksession); + Whitebox.setInternalState(droolsEngine, "connectionFactory", connectionFactory); + + PowerMock.resetAll(); + } + + @Test + public void init() throws Exception { + MQConfig mqConfig = new MQConfig(); + mqConfig.brokerIp = "127.0.0.1"; + mqConfig.brokerPort = 4567; + mqConfig.brokerUsername = "admin"; + mqConfig.brokerPassword = "admin"; + List rules = new ArrayList(); + CorrelationRule rule = new CorrelationRule(); + rule.setContent("content"); + rules.add(rule); + + expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes(); + expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules); + PowerMock.replayAll(); + + Method method = DroolsEngine.class.getDeclaredMethod("init"); + method.setAccessible(true); + method.invoke(droolsEngine); + + PowerMock.verifyAll(); + } + + @Test + public void deployRule_rule_is_null() throws CorrelationException { + Locale locale = PowerMock.createMock(Locale.class); + + thrown.expect(NullPointerException.class); + + droolsEngine.deployRule(null, locale); + } + + @Test + public void deployRule_kbuilder_has_errors() throws CorrelationException { + DeployRuleRequest rule = new DeployRuleRequest(); + rule.setContent("rule123"); + Locale locale = new Locale(AlarmConst.I18N_EN); + + thrown.expect(CorrelationException.class); + + droolsEngine.deployRule(rule, locale); + } + + @Test + public void deployRule_package_name_repeat() throws CorrelationException { + DeployRuleRequest rule = new DeployRuleRequest(); + rule.setContent("package rule123"); + Locale locale = new Locale(AlarmConst.I18N_EN); + + thrown.expect(CorrelationException.class); + + droolsEngine.deployRule(rule, locale); + droolsEngine.deployRule(rule, locale); + } + + @Test + public void undeployRule_package_name_is_null() throws CorrelationException { + String packageName = null; + Locale locale = new Locale(AlarmConst.I18N_EN); + + thrown.expect(CorrelationException.class); + + droolsEngine.undeployRule(packageName, locale); + } + + @Test + public void undeployRule_normal() throws CorrelationException { + Locale locale = new Locale(AlarmConst.I18N_EN); + + DeployRuleRequest rule = new DeployRuleRequest(); + rule.setContent("package rule123"); + droolsEngine.deployRule(rule, locale); + + String packageName = "rule123"; + + droolsEngine.undeployRule(packageName, locale); + } + + @Test + public void compileRule_kbuilder_has_errors() throws CorrelationException { + String content = "have error content"; + Locale locale = new Locale(AlarmConst.I18N_EN); + + thrown.expect(CorrelationException.class); + + droolsEngine.compileRule(content, locale); + } + + + @Test + public void putRaisedIntoStream_facthandle_is_null() { + Alarm raiseAlarm = new Alarm(); + droolsEngine.putRaisedIntoStream(raiseAlarm); + droolsEngine.putRaisedIntoStream(raiseAlarm); + } + + @Test + public void putRaisedIntoStream_factHandle_is_not_null() { + droolsEngine.putRaisedIntoStream(new Alarm()); + } + + + @Test + public void listener_receive() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); + + Whitebox.setInternalState(listener, "connection", connection); + Whitebox.setInternalState(listener, "session", session); + Whitebox.setInternalState(listener, "destination", destination); + Whitebox.setInternalState(listener, "consumer", consumer); + + PowerMock.reset(); + + expect(connectionFactory.createConnection()).andReturn(connection); + connection.start(); + expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); + expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); + expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); + consumer.setMessageListener(listener); + + PowerMock.replayAll(); + + listener.receive(); + + PowerMock.verifyAll(); + } + + @Test + public void listener_exception() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); + + Whitebox.setInternalState(listener, "connection", connection); + Whitebox.setInternalState(listener, "session", session); + Whitebox.setInternalState(listener, "destination", destination); + Whitebox.setInternalState(listener, "consumer", consumer); + + PowerMock.reset(); + + expect(connectionFactory.createConnection()).andReturn(connection); + connection.start(); + expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); + expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); + expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); + consumer.setMessageListener(listener); + EasyMock.expectLastCall().andThrow(new JMSException("")); + + consumer.close(); + session.close(); + connection.close(); + + PowerMock.replayAll(); + + listener.receive(); + + PowerMock.verifyAll(); + } + + @Test + public void listener_close_exception() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); + + Whitebox.setInternalState(listener, "connection", connection); + Whitebox.setInternalState(listener, "session", session); + Whitebox.setInternalState(listener, "destination", destination); + Whitebox.setInternalState(listener, "consumer", consumer); + + PowerMock.reset(); + + expect(connectionFactory.createConnection()).andReturn(connection); + connection.start(); + expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); + expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); + expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); + consumer.setMessageListener(listener); + EasyMock.expectLastCall().andThrow(new JMSException("")); + + consumer.close(); + EasyMock.expectLastCall().andThrow(new JMSException("")); + + PowerMock.replayAll(); + + listener.receive(); + + PowerMock.verifyAll(); + } + + @Test + public void listener_on_message() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + Alarm alarm = new Alarm(); + alarm.setAlarmKey("alarmKey"); + ActiveMQObjectMessage objectMessage = new ActiveMQObjectMessage(); + objectMessage.setObject(alarm); + + listener.onMessage(objectMessage); + } + + @Test + public void stop() throws Exception { + droolsEngine.stop(); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/request/CompileRuleRequestTest.java b/engine-d/src/test/java/org/onap/holmes/engine/request/CompileRuleRequestTest.java new file mode 100644 index 0000000..3c938fd --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/request/CompileRuleRequestTest.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.engine.request; + +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class CompileRuleRequestTest { + @Test + public void getterAndSetter4Content(){ + final String content = "content"; + CompileRuleRequest request = new CompileRuleRequest(); + request.setContent(content); + assertThat(request.getContent(), equalTo(content)); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/request/DeployRuleRequestTest.java b/engine-d/src/test/java/org/onap/holmes/engine/request/DeployRuleRequestTest.java new file mode 100644 index 0000000..8170668 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/request/DeployRuleRequestTest.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.engine.request; + +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class DeployRuleRequestTest { + @Test + public void getterAndSetter4Content(){ + final String content = "content"; + DeployRuleRequest request = new DeployRuleRequest(); + request.setContent(content); + assertThat(request.getContent(), equalTo(content)); + } + + @Test + public void getterAndSetter4Engineid(){ + final String engineid = "engineid"; + DeployRuleRequest request = new DeployRuleRequest(); + request.setEngineId(engineid); + assertThat(request.getEngineId(), equalTo(engineid)); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java b/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java new file mode 100644 index 0000000..3c68182 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java @@ -0,0 +1,132 @@ +/** + * 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.engine.resources; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.engine.manager.DroolsEngine; +import org.onap.holmes.engine.request.CompileRuleRequest; +import org.onap.holmes.engine.request.DeployRuleRequest; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import java.util.Locale; + +import static org.easymock.EasyMock.*; + +public class EngineResourcesTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + DroolsEngine droolsEngine; + private EngineResources engineResources; + + @Before + public void setUp() { + droolsEngine = PowerMock.createMock(DroolsEngine.class); + engineResources = new EngineResources(); + + Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine); + PowerMock.resetAll(); + } + + @Test + public void deployRule_exception() throws CorrelationException { + DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))). + andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.deployRule(deployRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void deployRule_normal() throws CorrelationException { + DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), + anyObject(Locale.class))).andReturn("packageName"); + PowerMock.replayAll(); + engineResources.deployRule(deployRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void undeployRule_exception() throws CorrelationException { + String packageName = "packageName"; + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); + expectLastCall().andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.undeployRule(packageName, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void undeployRule_normal() throws CorrelationException { + String packageName = "packageName"; + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); + PowerMock.replayAll(); + engineResources.undeployRule(packageName, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void compileRule_exception() throws CorrelationException { + CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); + expectLastCall().andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.compileRule(compileRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void compileRule_normal() throws CorrelationException { + CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); + PowerMock.replayAll(); + engineResources.compileRule(compileRuleRequest, httpRequest); + PowerMock.verifyAll(); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/response/CorrelationRuleResponseTest.java b/engine-d/src/test/java/org/onap/holmes/engine/response/CorrelationRuleResponseTest.java new file mode 100644 index 0000000..85659f2 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/response/CorrelationRuleResponseTest.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.engine.response; + +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class CorrelationRuleResponseTest { + @Test + public void getterAndSetter4RuleId(){ + final String packageName = "package"; + CorrelationRuleResponse request = new CorrelationRuleResponse(); + request.setPackageName(packageName); + assertThat(request.getPackageName(), equalTo(packageName)); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/utils/AlarmUtilTest.java b/engine-d/src/test/java/org/onap/holmes/engine/utils/AlarmUtilTest.java new file mode 100644 index 0000000..97330a7 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/utils/AlarmUtilTest.java @@ -0,0 +1,136 @@ +/** + * 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.engine.utils; + +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.holmes.common.api.stat.Alarm; +import org.onap.holmes.common.producer.MQProducer; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import java.util.HashMap; +import java.util.Map; + +public class AlarmUtilTest { + private AlarmUtil alarmUtil; + private final Map> rootPriorityMap = new HashMap>(); + private final Map saveRuleMsg = new HashMap(); + + @Before + public void setUp() { + alarmUtil = AlarmUtil.getInstance(); + Whitebox.setInternalState(alarmUtil,"rootPriorityMap",rootPriorityMap); + Whitebox.setInternalState(alarmUtil,"saveRuleMsg",saveRuleMsg); + PowerMock.resetAll(); + } + + @Test + public void getInstance() { + AlarmUtil instance = AlarmUtil.getInstance(); + Assert.assertThat(instance,IsNull.notNullValue()); + } + + @Test + public void equipTypeFilter_is_nullstr() { + String probableCauseStr = "null"; + String equipType = "equipType"; + Alarm alarm = new Alarm(); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(true)); + } + + @Test + public void equipTypeFilter_equals_alarm() { + String probableCauseStr = "11,4567"; + String equipType = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(4567); + alarm.setEquipType("equipType"); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(true)); + } + + @Test + public void equipTypeFilter_not_equals_alarm() { + String probableCauseStr = "11,45"; + String equipType = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(4567); + alarm.setEquipType("equipType"); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(false)); + } + + @Test + public void getPriority_rootprioritymap_containskey_ruleid() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "0,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + + Map map = new HashMap(); + map.put("11-ee", 0); + map.put("4567-equipType", 1); + rootPriorityMap.put(ruleId, map); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(0)); + } + + @Test + public void getPriority_rootprioritymap_not_containskey_ruleid() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "0,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(0)); + } + + @Test + public void getPriority_priority_is_not_null() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "1,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(11); + alarm.setEquipType("ee"); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(1)); + } + + @Test + public void getMqProducer() { + MQProducer mqProducer = alarmUtil.getMqProducer(); + Assert.assertThat(mqProducer, IsNull.notNullValue()); + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/engine/wrapper/RuleMgtWrapperTest.java b/engine-d/src/test/java/org/onap/holmes/engine/wrapper/RuleMgtWrapperTest.java new file mode 100644 index 0000000..cd7aff4 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/wrapper/RuleMgtWrapperTest.java @@ -0,0 +1,62 @@ +/** + * 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.engine.wrapper; + +import static org.easymock.EasyMock.anyInt; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; + +import java.util.ArrayList; +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.engine.db.CorrelationRuleDao; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +public class RuleMgtWrapperTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + private DbDaoUtil daoUtil; + private RuleMgtWrapper ruleMgtWrapper; + + @Before + public void setUp() { + daoUtil = PowerMock.createMock(DbDaoUtil.class); + ruleMgtWrapper = new RuleMgtWrapper(); + + Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", daoUtil); + PowerMock.resetAll(); + } + + @Test + public void queryRuleByEnable_normal() throws CorrelationException { + int enable = 3; + + CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao); + expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(new ArrayList()); + PowerMock.replayAll(); + ruleMgtWrapper.queryRuleByEnable(enable); + PowerMock.verifyAll(); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java b/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java deleted file mode 100644 index b69f9db..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.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.engine; - -/** - * Created by Administrator on 2017/2/20. - */ - -public class EngineDActiveAppTest { - public static void main(String[] args) throws Exception { - String filePath = "C:\\correlation-engine.yml"; - new EngineDActiveApp().run(new String[]{"server", filePath}); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java b/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java deleted file mode 100644 index b7bcf34..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java +++ /dev/null @@ -1,75 +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.engine; - -import io.dropwizard.db.DataSourceFactory; -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsNull; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openo.holmes.common.config.MQConfig; -import org.powermock.api.easymock.PowerMock; - -public class EnginedAppConfigTest { - - private EngineDAppConfig engineAppConfig; - - @Before - public void setUp() { - engineAppConfig = new EngineDAppConfig(); - } - - @Test - public void getMqConfig() { - MQConfig mqConfig = PowerMock.createMock(MQConfig.class); - engineAppConfig.setMqConfig(mqConfig); - Assert.assertThat(engineAppConfig.getMqConfig(), IsNull.notNullValue()); - } - - @Test - public void setMqConfig() { - MQConfig mqConfig = PowerMock.createMock(MQConfig.class); - engineAppConfig.setMqConfig(mqConfig); - Assert.assertThat(engineAppConfig.getMqConfig(), IsEqual.equalTo(mqConfig)); - } - - @Test - public void getDataSourceFactory() { - Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsNull.notNullValue()); - } - - @Test - public void setDataSourceFactory() { - DataSourceFactory database = new DataSourceFactory(); - engineAppConfig.setDataSourceFactory(database); - Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsEqual.equalTo(database)); - } - - @Test - public void getApidescription() { - final String apidescription = "Holmes rule management rest API"; - Assert.assertThat(engineAppConfig.getApidescription(), IsEqual.equalTo(apidescription)); - } - - @Test - public void setApidescription() { - final String apidescription = "set api description"; - engineAppConfig.setApidescription(apidescription); - Assert.assertThat(engineAppConfig.getApidescription(), IsEqual.equalTo(apidescription)); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java b/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java deleted file mode 100644 index 073f590..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.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.engine.db; - -import org.easymock.EasyMock; -import org.hamcrest.core.IsNull; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openo.holmes.common.api.entity.CorrelationRule; -import org.powermock.api.easymock.PowerMock; - -import java.util.ArrayList; -import java.util.List; - -public class CorrelationRuleDaoTest { - private CorrelationRuleDao correlationRuleDao; - - @Before - public void setUp() { - correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); - } - - @Test - public void queryRuleByEnable() { - int enable = 0; - EasyMock.expect(correlationRuleDao.queryRuleByEnable(EasyMock.anyInt())).andReturn(new ArrayList()); - PowerMock.replayAll(); - List correlationRules = correlationRuleDao.queryRuleByEnable(enable); - PowerMock.verifyAll(); - Assert.assertThat(correlationRules, IsNull.>notNullValue()); - } - -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java b/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java deleted file mode 100644 index 6e43f86..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java +++ /dev/null @@ -1,57 +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.engine.db.mapper; - -import org.junit.Test; -import org.powermock.api.easymock.PowerMock; - -import java.sql.Date; -import java.sql.ResultSet; -import java.util.Properties; - -import static org.easymock.EasyMock.expect; - -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(0); - 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.replayAll(); - - mapper.map(0, resultSet, null); - - PowerMock.verify(); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java deleted file mode 100644 index 54d9bf5..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java +++ /dev/null @@ -1,320 +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.engine.manager; - -import static org.easymock.EasyMock.anyBoolean; -import static org.easymock.EasyMock.anyInt; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import javax.jms.Connection; -import javax.jms.ConnectionFactory; -import javax.jms.Destination; -import javax.jms.JMSException; -import javax.jms.MessageConsumer; -import javax.jms.Session; -import javax.jms.Topic; -import org.apache.activemq.command.ActiveMQObjectMessage; -import org.drools.KnowledgeBase; -import org.drools.KnowledgeBaseConfiguration; -import org.drools.KnowledgeBaseFactory; -import org.drools.conf.EventProcessingOption; -import org.drools.runtime.StatefulKnowledgeSession; -import org.easymock.EasyMock; -import org.glassfish.hk2.api.IterableProvider; -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.api.stat.Alarm; -import org.openo.holmes.common.config.MQConfig; -import org.openo.holmes.common.constant.AlarmConst; -import org.openo.holmes.common.exception.CorrelationException; -import org.openo.holmes.engine.request.DeployRuleRequest; -import org.openo.holmes.engine.wrapper.RuleMgtWrapper; -import org.powermock.api.easymock.PowerMock; -import org.powermock.modules.junit4.rule.PowerMockRule; -import org.powermock.reflect.Whitebox; - -public class DroolsEngineTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public PowerMockRule powerMockRule = new PowerMockRule(); - - private RuleMgtWrapper ruleMgtWrapper; - - private KnowledgeBase kbase; - - private KnowledgeBaseConfiguration kconf; - - private StatefulKnowledgeSession ksession; - - private IterableProvider mqConfigProvider; - - private ConnectionFactory connectionFactory; - - private DroolsEngine droolsEngine; - - @Before - public void setUp() { - droolsEngine = new DroolsEngine(); - - this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); - this.kconf.setOption(EventProcessingOption.STREAM); - this.kconf.setProperty("drools.assertBehaviour", "equality"); - this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf); - this.ksession = kbase.newStatefulKnowledgeSession(); - - ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class); - mqConfigProvider = PowerMock.createMock(IterableProvider.class); - connectionFactory = PowerMock.createMock(ConnectionFactory.class); - - Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper); - Whitebox.setInternalState(droolsEngine, "mqConfigProvider", mqConfigProvider); - Whitebox.setInternalState(droolsEngine, "kconf", kconf); - Whitebox.setInternalState(droolsEngine, "kbase", kbase); - Whitebox.setInternalState(droolsEngine, "ksession", ksession); - Whitebox.setInternalState(droolsEngine, "connectionFactory", connectionFactory); - - PowerMock.resetAll(); - } - - @Test - public void init() throws Exception { - MQConfig mqConfig = new MQConfig(); - mqConfig.brokerIp = "127.0.0.1"; - mqConfig.brokerPort = 4567; - mqConfig.brokerUsername = "admin"; - mqConfig.brokerPassword = "admin"; - List rules = new ArrayList(); - CorrelationRule rule = new CorrelationRule(); - rule.setContent("content"); - rules.add(rule); - - expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes(); - expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules); - PowerMock.replayAll(); - - Method method = DroolsEngine.class.getDeclaredMethod("init"); - method.setAccessible(true); - method.invoke(droolsEngine); - - PowerMock.verifyAll(); - } - - @Test - public void deployRule_rule_is_null() throws CorrelationException { - Locale locale = PowerMock.createMock(Locale.class); - - thrown.expect(NullPointerException.class); - - droolsEngine.deployRule(null, locale); - } - - @Test - public void deployRule_kbuilder_has_errors() throws CorrelationException { - DeployRuleRequest rule = new DeployRuleRequest(); - rule.setContent("rule123"); - Locale locale = new Locale(AlarmConst.I18N_EN); - - thrown.expect(CorrelationException.class); - - droolsEngine.deployRule(rule, locale); - } - - @Test - public void deployRule_package_name_repeat() throws CorrelationException { - DeployRuleRequest rule = new DeployRuleRequest(); - rule.setContent("package rule123"); - Locale locale = new Locale(AlarmConst.I18N_EN); - - thrown.expect(CorrelationException.class); - - droolsEngine.deployRule(rule, locale); - droolsEngine.deployRule(rule, locale); - } - - @Test - public void undeployRule_package_name_is_null() throws CorrelationException { - String packageName = null; - Locale locale = new Locale(AlarmConst.I18N_EN); - - thrown.expect(CorrelationException.class); - - droolsEngine.undeployRule(packageName, locale); - } - - @Test - public void undeployRule_normal() throws CorrelationException { - Locale locale = new Locale(AlarmConst.I18N_EN); - - DeployRuleRequest rule = new DeployRuleRequest(); - rule.setContent("package rule123"); - droolsEngine.deployRule(rule, locale); - - String packageName = "rule123"; - - droolsEngine.undeployRule(packageName, locale); - } - - @Test - public void compileRule_kbuilder_has_errors() throws CorrelationException { - String content = "have error content"; - Locale locale = new Locale(AlarmConst.I18N_EN); - - thrown.expect(CorrelationException.class); - - droolsEngine.compileRule(content, locale); - } - - - @Test - public void putRaisedIntoStream_facthandle_is_null() { - Alarm raiseAlarm = new Alarm(); - droolsEngine.putRaisedIntoStream(raiseAlarm); - droolsEngine.putRaisedIntoStream(raiseAlarm); - } - - @Test - public void putRaisedIntoStream_factHandle_is_not_null() { - droolsEngine.putRaisedIntoStream(new Alarm()); - } - - - @Test - public void listener_receive() throws JMSException { - DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); - - Connection connection = PowerMock.createMock(Connection.class); - Session session = PowerMock.createMock(Session.class); - Destination destination = PowerMock.createMock(Topic.class); - MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); - - Whitebox.setInternalState(listener, "connection", connection); - Whitebox.setInternalState(listener, "session", session); - Whitebox.setInternalState(listener, "destination", destination); - Whitebox.setInternalState(listener, "consumer", consumer); - - PowerMock.reset(); - - expect(connectionFactory.createConnection()).andReturn(connection); - connection.start(); - expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); - expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); - expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); - consumer.setMessageListener(listener); - - PowerMock.replayAll(); - - listener.receive(); - - PowerMock.verifyAll(); - } - - @Test - public void listener_exception() throws JMSException { - DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); - - Connection connection = PowerMock.createMock(Connection.class); - Session session = PowerMock.createMock(Session.class); - Destination destination = PowerMock.createMock(Topic.class); - MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); - - Whitebox.setInternalState(listener, "connection", connection); - Whitebox.setInternalState(listener, "session", session); - Whitebox.setInternalState(listener, "destination", destination); - Whitebox.setInternalState(listener, "consumer", consumer); - - PowerMock.reset(); - - expect(connectionFactory.createConnection()).andReturn(connection); - connection.start(); - expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); - expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); - expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); - consumer.setMessageListener(listener); - EasyMock.expectLastCall().andThrow(new JMSException("")); - - consumer.close(); - session.close(); - connection.close(); - - PowerMock.replayAll(); - - listener.receive(); - - PowerMock.verifyAll(); - } - - @Test - public void listener_close_exception() throws JMSException { - DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); - - Connection connection = PowerMock.createMock(Connection.class); - Session session = PowerMock.createMock(Session.class); - Destination destination = PowerMock.createMock(Topic.class); - MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class); - - Whitebox.setInternalState(listener, "connection", connection); - Whitebox.setInternalState(listener, "session", session); - Whitebox.setInternalState(listener, "destination", destination); - Whitebox.setInternalState(listener, "consumer", consumer); - - PowerMock.reset(); - - expect(connectionFactory.createConnection()).andReturn(connection); - connection.start(); - expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); - expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); - expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer); - consumer.setMessageListener(listener); - EasyMock.expectLastCall().andThrow(new JMSException("")); - - consumer.close(); - EasyMock.expectLastCall().andThrow(new JMSException("")); - - PowerMock.replayAll(); - - listener.receive(); - - PowerMock.verifyAll(); - } - - @Test - public void listener_on_message() throws JMSException { - DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); - Alarm alarm = new Alarm(); - alarm.setAlarmKey("alarmKey"); - ActiveMQObjectMessage objectMessage = new ActiveMQObjectMessage(); - objectMessage.setObject(alarm); - - listener.onMessage(objectMessage); - } - - @Test - public void stop() throws Exception { - droolsEngine.stop(); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java b/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java deleted file mode 100644 index e1baae7..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.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.engine.request; - -import org.junit.Test; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -public class CompileRuleRequestTest { - @Test - public void getterAndSetter4Content(){ - final String content = "content"; - CompileRuleRequest request = new CompileRuleRequest(); - request.setContent(content); - assertThat(request.getContent(), equalTo(content)); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java b/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java deleted file mode 100644 index 1f9b12f..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.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.engine.request; - -import org.junit.Test; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -public class DeployRuleRequestTest { - @Test - public void getterAndSetter4Content(){ - final String content = "content"; - DeployRuleRequest request = new DeployRuleRequest(); - request.setContent(content); - assertThat(request.getContent(), equalTo(content)); - } - - @Test - public void getterAndSetter4Engineid(){ - final String engineid = "engineid"; - DeployRuleRequest request = new DeployRuleRequest(); - request.setEngineId(engineid); - assertThat(request.getEngineId(), equalTo(engineid)); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java b/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java deleted file mode 100644 index 51f46a6..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java +++ /dev/null @@ -1,132 +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.engine.resources; - -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.engine.manager.DroolsEngine; -import org.openo.holmes.engine.request.CompileRuleRequest; -import org.openo.holmes.engine.request.DeployRuleRequest; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.WebApplicationException; -import java.util.Locale; - -import static org.easymock.EasyMock.*; - -public class EngineResourcesTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - DroolsEngine droolsEngine; - private EngineResources engineResources; - - @Before - public void setUp() { - droolsEngine = PowerMock.createMock(DroolsEngine.class); - engineResources = new EngineResources(); - - Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine); - PowerMock.resetAll(); - } - - @Test - public void deployRule_exception() throws CorrelationException { - DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - thrown.expect(WebApplicationException.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))). - andThrow(new CorrelationException("")); - PowerMock.replayAll(); - engineResources.deployRule(deployRuleRequest, httpRequest); - PowerMock.verifyAll(); - } - - @Test - public void deployRule_normal() throws CorrelationException { - DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), - anyObject(Locale.class))).andReturn("packageName"); - PowerMock.replayAll(); - engineResources.deployRule(deployRuleRequest, httpRequest); - PowerMock.verifyAll(); - } - - @Test - public void undeployRule_exception() throws CorrelationException { - String packageName = "packageName"; - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - thrown.expect(WebApplicationException.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); - expectLastCall().andThrow(new CorrelationException("")); - PowerMock.replayAll(); - engineResources.undeployRule(packageName, httpRequest); - PowerMock.verifyAll(); - } - - @Test - public void undeployRule_normal() throws CorrelationException { - String packageName = "packageName"; - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); - PowerMock.replayAll(); - engineResources.undeployRule(packageName, httpRequest); - PowerMock.verifyAll(); - } - - @Test - public void compileRule_exception() throws CorrelationException { - CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - thrown.expect(WebApplicationException.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); - expectLastCall().andThrow(new CorrelationException("")); - PowerMock.replayAll(); - engineResources.compileRule(compileRuleRequest, httpRequest); - PowerMock.verifyAll(); - } - - @Test - public void compileRule_normal() throws CorrelationException { - CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); - HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); - - expect(httpRequest.getHeader("language-option")).andReturn("en_US"); - droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); - PowerMock.replayAll(); - engineResources.compileRule(compileRuleRequest, httpRequest); - PowerMock.verifyAll(); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java b/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java deleted file mode 100644 index f8899e6..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.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.engine.response; - -import org.junit.Test; -import org.openo.holmes.engine.request.DeployRuleRequest; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -public class CorrelationRuleResponseTest { - @Test - public void getterAndSetter4RuleId(){ - final String packageName = "package"; - CorrelationRuleResponse request = new CorrelationRuleResponse(); - request.setPackageName(packageName); - assertThat(request.getPackageName(), equalTo(packageName)); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java b/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java deleted file mode 100644 index 107d6e9..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java +++ /dev/null @@ -1,136 +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.engine.utils; - -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsNull; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openo.holmes.common.api.stat.Alarm; -import org.openo.holmes.common.producer.MQProducer; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -import java.util.HashMap; -import java.util.Map; - -public class AlarmUtilTest { - private AlarmUtil alarmUtil; - private final Map> rootPriorityMap = new HashMap>(); - private final Map saveRuleMsg = new HashMap(); - - @Before - public void setUp() { - alarmUtil = AlarmUtil.getInstance(); - Whitebox.setInternalState(alarmUtil,"rootPriorityMap",rootPriorityMap); - Whitebox.setInternalState(alarmUtil,"saveRuleMsg",saveRuleMsg); - PowerMock.resetAll(); - } - - @Test - public void getInstance() { - AlarmUtil instance = AlarmUtil.getInstance(); - Assert.assertThat(instance,IsNull.notNullValue()); - } - - @Test - public void equipTypeFilter_is_nullstr() { - String probableCauseStr = "null"; - String equipType = "equipType"; - Alarm alarm = new Alarm(); - boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); - Assert.assertThat(filter, IsEqual.equalTo(true)); - } - - @Test - public void equipTypeFilter_equals_alarm() { - String probableCauseStr = "11,4567"; - String equipType = "ee,equipType"; - Alarm alarm = new Alarm(); - alarm.setProbableCause(4567); - alarm.setEquipType("equipType"); - boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); - Assert.assertThat(filter, IsEqual.equalTo(true)); - } - - @Test - public void equipTypeFilter_not_equals_alarm() { - String probableCauseStr = "11,45"; - String equipType = "ee,equipType"; - Alarm alarm = new Alarm(); - alarm.setProbableCause(4567); - alarm.setEquipType("equipType"); - boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); - Assert.assertThat(filter, IsEqual.equalTo(false)); - } - - @Test - public void getPriority_rootprioritymap_containskey_ruleid() { - String ruleId = "1"; - String probableCauseStr = "11,4567"; - String rootAlarmFeatureStr = "0,1"; - String equipTypeStr = "ee,equipType"; - Alarm alarm = new Alarm(); - - Map map = new HashMap(); - map.put("11-ee", 0); - map.put("4567-equipType", 1); - rootPriorityMap.put(ruleId, map); - - saveRuleMsg.put(ruleId, "11ee0"); - - Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); - Assert.assertThat(priority,IsEqual.equalTo(0)); - } - - @Test - public void getPriority_rootprioritymap_not_containskey_ruleid() { - String ruleId = "1"; - String probableCauseStr = "11,4567"; - String rootAlarmFeatureStr = "0,1"; - String equipTypeStr = "ee,equipType"; - Alarm alarm = new Alarm(); - - saveRuleMsg.put(ruleId, "11ee0"); - - Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); - Assert.assertThat(priority,IsEqual.equalTo(0)); - } - - @Test - public void getPriority_priority_is_not_null() { - String ruleId = "1"; - String probableCauseStr = "11,4567"; - String rootAlarmFeatureStr = "1,1"; - String equipTypeStr = "ee,equipType"; - Alarm alarm = new Alarm(); - alarm.setProbableCause(11); - alarm.setEquipType("ee"); - - saveRuleMsg.put(ruleId, "11ee0"); - - Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); - Assert.assertThat(priority,IsEqual.equalTo(1)); - } - - @Test - public void getMqProducer() { - MQProducer mqProducer = alarmUtil.getMqProducer(); - Assert.assertThat(mqProducer, IsNull.notNullValue()); - } -} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java b/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java deleted file mode 100644 index fab68df..0000000 --- a/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java +++ /dev/null @@ -1,62 +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.engine.wrapper; - -import static org.easymock.EasyMock.anyInt; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; - -import java.util.ArrayList; -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.engine.db.CorrelationRuleDao; -import org.powermock.api.easymock.PowerMock; -import org.powermock.reflect.Whitebox; - -public class RuleMgtWrapperTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private DbDaoUtil daoUtil; - private RuleMgtWrapper ruleMgtWrapper; - - @Before - public void setUp() { - daoUtil = PowerMock.createMock(DbDaoUtil.class); - ruleMgtWrapper = new RuleMgtWrapper(); - - Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", daoUtil); - PowerMock.resetAll(); - } - - @Test - public void queryRuleByEnable_normal() throws CorrelationException { - int enable = 3; - - CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); - expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao); - expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(new ArrayList()); - PowerMock.replayAll(); - ruleMgtWrapper.queryRuleByEnable(enable); - PowerMock.verifyAll(); - } -} diff --git a/pom.xml b/pom.xml index 06c8aa4..b3b0e19 100644 --- a/pom.xml +++ b/pom.xml @@ -19,13 +19,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.openo.oparent - oparent - 1.1.0-SNAPSHOT - ../oparent + org.onap.oparent + version + 1.0.0-SNAPSHOT + ../../oparent - org.openo.holmes.engine-management + org.onap.holmes.engine-management holmes-engine-parent pom holmes-engine-parent @@ -56,7 +56,7 @@ 1.6.1 2.2.1 - openo-holmes-engine-d + onap-holmes-engine-d linux64 win64 target/assembly/${linux64id} @@ -71,7 +71,7 @@ 3.0 - org.openo.holmes.actions + org.onap.holmes.common holmes-actions ${project.version} @@ -117,12 +117,6 @@ ${stringtemplate.version} - - org.openo.common-services.common-utilities - dropwizard-ioc-container - ${project.version} - - org.quartz-scheduler quartz @@ -150,12 +144,6 @@ test - - org.openo.client.cli - swagger-sdk - 1.1.0-SNAPSHOT - - org.apache.httpcomponents httpclient -- cgit 1.2.3-korg