diff options
author | Guangrong Fu <fu.guangrong@zte.com.cn> | 2017-08-07 12:36:54 +0800 |
---|---|---|
committer | Guangrong Fu <fu.guangrong@zte.com.cn> | 2017-08-07 12:42:24 +0800 |
commit | 7ab5d3b5150f3941f61f40bc27c94909bcbdfe7d (patch) | |
tree | 63a3f115c7023a07f7f092986f666704ba9a9b21 /engine-d/src/test/java/org/onap | |
parent | 4ced1931f4e031761d6d16484bb59a975dfcdc68 (diff) |
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 <fu.guangrong@zte.com.cn>
Diffstat (limited to 'engine-d/src/test/java/org/onap')
11 files changed, 961 insertions, 0 deletions
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.<DataSourceFactory>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<CorrelationRule>());
+ PowerMock.replayAll();
+ List<CorrelationRule> correlationRules = correlationRuleDao.queryRuleByEnable(enable);
+ PowerMock.verifyAll();
+ Assert.assertThat(correlationRules, IsNull.<List<CorrelationRule>>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<MQConfig> 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<CorrelationRule> rules = new ArrayList<CorrelationRule>();
+ 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<String, Map<String, Integer>> rootPriorityMap = new HashMap<String, Map<String, Integer>>();
+ private final Map<String, String> saveRuleMsg = new HashMap<String, String>();
+
+ @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.<AlarmUtil>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<String, Integer> map = new HashMap<String, Integer>();
+ 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.<MQProducer>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<CorrelationRule>());
+ PowerMock.replayAll();
+ ruleMgtWrapper.queryRuleByEnable(enable);
+ PowerMock.verifyAll();
+ }
+}
|