From 4043a64dabad0f27afcb67e9b62272c4e44a4214 Mon Sep 17 00:00:00 2001 From: 6092002067 Date: Tue, 28 Feb 2017 17:22:05 +0800 Subject: Add action module unit tests Issue-ID:HOLMES-51 Change-Id: Iaab72cb42d7af3636736c505f62db295c20ba3c7 Signed-off-by: youbowu --- .../common/api/entity/AlarmsCorrelationTest.java | 133 ++++++++++++++ .../common/api/entity/CorrelationResultTest.java | 78 ++++++++ .../common/api/entity/CorrelationRuleTest.java | 146 +++++++++++++++ .../openo/holmes/common/api/stat/AlarmTest.java | 198 +++++++++++++++++++++ .../openo/holmes/common/config/MQConfigTest.java | 28 +++ .../common/exception/CorrelationExceptionTest.java | 29 +++ .../holmes/common/producer/MQProducerTest.java | 157 ++++++++++++++++ .../openo/holmes/common/utils/DbDaoUtilTest.java | 144 +++++++++++++++ .../holmes/common/utils/ExceptionUtilTest.java | 31 ++++ .../openo/holmes/common/utils/I18nProxyTest.java | 41 +++++ .../openo/holmes/common/utils/JacksonUtilTest.java | 49 +++++ .../holmes/common/utils/JudgeNullUtilTest.java | 44 +++++ .../holmes/common/utils/LanguageUtilTest.java | 118 ++++++++++++ .../openo/holmes/common/utils/UserUtilTest.java | 67 +++++++ .../openo/holmes/common/utils/bean/TestBean.java | 32 ++++ 15 files changed, 1295 insertions(+) create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/api/entity/AlarmsCorrelationTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationResultTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationRuleTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/api/stat/AlarmTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/config/MQConfigTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/exception/CorrelationExceptionTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/producer/MQProducerTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/DbDaoUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/ExceptionUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/I18nProxyTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/JacksonUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/JudgeNullUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/LanguageUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/UserUtilTest.java create mode 100644 holmes-actions/src/test/java/org/openo/holmes/common/utils/bean/TestBean.java (limited to 'holmes-actions/src/test/java/org') diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/AlarmsCorrelationTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/AlarmsCorrelationTest.java new file mode 100644 index 0000000..7dc1fab --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/AlarmsCorrelationTest.java @@ -0,0 +1,133 @@ +/** + * 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.common.api.entity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Date; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import sun.util.resources.cldr.ga.LocaleNames_ga; + +public class AlarmsCorrelationTest { + + private AlarmsCorrelation alarmsCorrelation; + + @Before + public void before() throws Exception { + alarmsCorrelation = new AlarmsCorrelation(); + } + + @After + public void after() throws Exception { + + } + + + @Test + public void getterAndSetter4RuleId() throws Exception { + final String ruleId = "ruleId"; + alarmsCorrelation.setRuleId(ruleId); + assertThat(alarmsCorrelation.getRuleId(), equalTo(ruleId)); + } + + + @Test + public void getterAndSetter4RuleInfo() throws Exception { + final String ruleInfo = "ruleInfo"; + alarmsCorrelation.setRuleInfo(ruleInfo); + assertThat(alarmsCorrelation.getRuleInfo(), equalTo(ruleInfo)); + } + + + @Test + public void getterAndSetter4ResultType() throws Exception { + final byte resultType = 1; + alarmsCorrelation.setResultType(resultType); + assertThat(alarmsCorrelation.getResultType(), equalTo(resultType)); + } + + + @Test + public void getterAndSetter4CreateTime() throws Exception { + final Date createTime = new Date(); + alarmsCorrelation.setCreateTime(createTime); + assertThat(alarmsCorrelation.getCreateTime(), equalTo(createTime)); + } + + + @Test + public void getterAndSetter4ParentAlarmId() throws Exception { + final long pad = 11L; + alarmsCorrelation.setParentAlarmId(pad); + assertThat(alarmsCorrelation.getParentAlarmId(), equalTo(pad)); + } + + + @Test + public void getterAndSetter4ChildAlarmId() throws Exception { + final long childAlarmId = 11L; + alarmsCorrelation.setChildAlarmId(childAlarmId); + assertThat(alarmsCorrelation.getChildAlarmId(), equalTo(childAlarmId)); + } + + + @Test + public void getterAndSetter4Reserve1() throws Exception { + final long reserve1 = 11L; + alarmsCorrelation.setReserve1(reserve1); + assertThat(alarmsCorrelation.getReserve1(), equalTo(reserve1)); + } + + + @Test + public void getterAndSetter4Reserve2() throws Exception { + final long reserve2 = 11L; + alarmsCorrelation.setReserve2(reserve2); + assertThat(alarmsCorrelation.getReserve2(), equalTo(reserve2)); + } + + + @Test + public void getterAndSetter4Reserve3() throws Exception { + final long reserve3 = 11L; + alarmsCorrelation.setReserve3(reserve3); + assertThat(alarmsCorrelation.getReserve3(), equalTo(reserve3)); + } + + + @Test + public void testToString() throws Exception { + final AlarmsCorrelation alarmsCorrelationTemp = new AlarmsCorrelation(); + String ruleId = "ruleId"; + alarmsCorrelationTemp.setRuleId(ruleId); + alarmsCorrelation.setRuleId(ruleId); + assertThat(alarmsCorrelation.toString(), equalTo(alarmsCorrelationTemp.toString())); + } + + + @Test + public void testEqualsAndHashCode() throws Exception { + final AlarmsCorrelation alarmsCorrelationTemp = new AlarmsCorrelation(); + String ruleId = "ruleId"; + alarmsCorrelationTemp.setRuleId(ruleId); + alarmsCorrelation.setRuleId(ruleId); + assertThat(alarmsCorrelation, equalTo(alarmsCorrelationTemp)); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationResultTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationResultTest.java new file mode 100644 index 0000000..14a52c0 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationResultTest.java @@ -0,0 +1,78 @@ +/** + * 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.common.api.entity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Date; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openo.holmes.common.api.stat.Alarm; + +public class CorrelationResultTest { + + private CorrelationResult correlationResult; + + @Before + public void before() throws Exception { + correlationResult = new CorrelationResult(); + } + + @After + public void after() throws Exception { + } + + @Test + public void getterAndSetter4RuleId() throws Exception { + final String ruleId = "ruleId"; + correlationResult.setRuleId(ruleId); + assertThat(correlationResult.getRuleId(), equalTo(ruleId)); + } + + @Test + public void getterAndSetter4CreateTimeL() throws Exception { + final long createTimeL = new Date().getTime(); + correlationResult.setCreateTimeL( + createTimeL); + assertThat(correlationResult.getCreateTimeL(), equalTo(createTimeL)); + } + + @Test + public void getterAndSetter4GetResultType() throws Exception { + final byte resultType = 2; + correlationResult.setResultType(resultType); + assertThat(correlationResult.getResultType(), equalTo(resultType)); + } + + @Test + public void getterAndSetter4AffectedAlarms() throws Exception { + final Alarm alarm[] = new Alarm[2]; + correlationResult.setAffectedAlarms(alarm); + assertThat(correlationResult.getAffectedAlarms(), equalTo(alarm)); + } + + @Test + public void testToString() throws Exception { + CorrelationResult resultTemp = new CorrelationResult(); + final String tempStr = "aa"; + resultTemp.setRuleId(tempStr); + correlationResult.setRuleId(tempStr); + assertThat(correlationResult.toString(), equalTo(resultTemp.toString())); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationRuleTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationRuleTest.java new file mode 100644 index 0000000..8b6268c --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/api/entity/CorrelationRuleTest.java @@ -0,0 +1,146 @@ +/** + * 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.common.api.entity; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Date; +import java.util.Properties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class CorrelationRuleTest { + + private CorrelationRule correlationRule; + + @Before + public void before() throws Exception { + correlationRule = new CorrelationRule(); + } + + @After + public void after() throws Exception { + + } + + @Test + public void getterAndSetter4Rid() throws Exception { + final String rid = "rid"; + correlationRule.setRid(rid); + assertThat(correlationRule.getRid(), equalTo(rid)); + } + + @Test + public void getterAndSetter4Name() throws Exception { + final String name = "name"; + correlationRule.setName(name); + assertThat(correlationRule.getName(), equalTo(name)); + } + + @Test + public void getterAndSetter4Description() throws Exception { + final String description = "description"; + correlationRule.setDescription(description); + assertThat(correlationRule.getDescription(), equalTo(description)); + } + + @Test + public void getterAndSetter4Enabled() throws Exception { + final int enabled = 1; + correlationRule.setEnabled(enabled); + assertThat(correlationRule.getEnabled(), equalTo(enabled)); + } + + @Test + public void getterAndSetter4TemplateID() throws Exception { + final int templateId = 1; + correlationRule.setTemplateID(templateId); + assertThat(correlationRule.getTemplateID(), equalTo(templateId)); + } + + @Test + public void getterAndSetter4EngineId() throws Exception { + final String engineId = "engineId"; + correlationRule.setEngineID(engineId); + assertThat(correlationRule.getEngineID(), equalTo(engineId)); + } + + @Test + public void getterAndSetter4EngineType() throws Exception { + final String engineType = "engineType"; + correlationRule.setEngineType(engineType); + assertThat(correlationRule.getEngineType(), equalTo(engineType)); + } + + @Test + public void getterAndSetter4Creator() throws Exception { + final String creator = "creator"; + correlationRule.setCreator(creator); + assertThat(correlationRule.getCreator(), equalTo(creator)); + } + + @Test + public void getterAndSetter4Modifier() throws Exception { + final String modifier = "modifier"; + correlationRule.setModifier(modifier); + assertThat(correlationRule.getModifier(), equalTo(modifier)); + } + + @Test + public void getterAndSetter4Params() throws Exception { + final Properties params = new Properties(); + correlationRule.setParams(params); + assertThat(correlationRule.getParams(), equalTo(params)); + } + + @Test + public void getterAndSetter4Content() throws Exception { + final String content = "content"; + correlationRule.setContent(content); + assertThat(correlationRule.getContent(), equalTo(content)); + } + + @Test + public void getterAndSetter4Vendor() throws Exception { + final String vendor = "vendor"; + correlationRule.setVendor(vendor); + assertThat(correlationRule.getVendor(), equalTo(vendor)); + } + + @Test + public void getterAndSetter4CreateTime() throws Exception { + Date createTime = new Date(); + correlationRule.setCreateTime(createTime); + assertThat(correlationRule.getCreateTime(), equalTo(createTime)); + } + + @Test + public void getterAndSetter4UpdateTime() throws Exception { + final Date updateTime = new Date(); + correlationRule.setUpdateTime(updateTime); + assertThat(correlationRule.getUpdateTime(), equalTo(updateTime)); + } + + @Test + public void getterAndSetter4PackageName() throws Exception { + final String packageName = "packageName"; + correlationRule.setPackageName(packageName); + assertThat(correlationRule.getPackageName(), equalTo(packageName)); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/api/stat/AlarmTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/api/stat/AlarmTest.java new file mode 100644 index 0000000..b740d87 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/api/stat/AlarmTest.java @@ -0,0 +1,198 @@ +/** + * 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.common.api.stat; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class AlarmTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private Alarm alarm; + + @Before + public void before() throws Exception { + alarm = new Alarm(); + } + + @After + public void after() throws Exception { + } + + @Test + public void testCompareLinkPosition_isEmpty() throws Exception { + final Map otherIdIdx = new HashMap<>(); + assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx))); + } + + @Test + public void testCompareLinkPosition_TempLinkId() throws Exception { + final Map otherIdIdx = new HashMap<>(); + otherIdIdx.put(1, 2); + alarm.addLinkIdNodeIdx(1, 3); + assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx))); + } + + @Test + public void testContainNode_NoContainLink() throws Exception { + alarm.addLinkIdNodeIdx(1, 2); + assertThat(false, equalTo(alarm.containNode(2, 2))); + } + + @Test + public void testContainNode_ContainLinkNoIdx() throws Exception { + alarm.addLinkIdNodeIdx(1, 2); + assertFalse(alarm.containNode(1, 3)); + } + + @Test + public void testContainNode_ContainLinkAndIdx() throws Exception { + alarm.addLinkIdNodeIdx(1, 2); + assertTrue(alarm.containNode(1, 2)); + } + + @Test + public void testGetDataType() throws Exception { + assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType())); + } + + @Test + public void testToString() throws Exception { + final Alarm alarmTemp = new Alarm(); + assertThat(alarmTemp.toString(), equalTo(alarm.toString())); + } + + @Test + public void testHashCode() throws Exception { + final Alarm alarmTemp = new Alarm(); + final String alarmKey = "alarmKey"; + alarm.setAlarmKey(alarmKey); + alarmTemp.setAlarmKey(alarmKey); + assertThat(alarm.hashCode(), equalTo(alarmTemp.hashCode())); + } + + @Test + public void testEqualsAnd_NotNull() throws Exception { + final Alarm alarmTemp = new Alarm(); + final String alarmKey = "alarmKey"; + alarm.setAlarmKey(alarmKey); + alarmTemp.setAlarmKey(alarmKey); + assertTrue(alarm.equals(alarmTemp)); + } + + @Test + public void testEqualsAndH_isNull() throws Exception { + assertFalse(alarm.equals(null)); + } + + @Test + public void testClone() throws Exception { + alarm.setAlarmKey("alarmKey"); + Alarm alarmTemp = (Alarm) alarm.clone(); + assertTrue(alarm.equals(alarmTemp)); + assertFalse(alarm == alarmTemp); + } + + @Test + public void testGetObjectId() throws Exception { + alarm.setId(11); + assertThat("11", equalTo(alarm.getObjectId())); + } + + @Test + public void testAddLinkIds() throws Exception { + final int linkId = 11; + alarm.addLinkIds(linkId); + assertTrue(alarm.getLinkIds().contains(linkId)); + } + + @Test + public void testContainsPriority_true() throws Exception { + String ruleId = "ruleId"; + alarm.getPriorityMap().put(ruleId, 2); + assertTrue(alarm.containsPriority(ruleId)); + } + + @Test + public void testContainsPriority_false() throws Exception { + final String ruleId = "ruleId"; + assertFalse(alarm.containsPriority(ruleId)); + } + + @Test + public void testGetPriority_isNull() throws Exception { + final String ruleId = "ruleId"; + alarm.getPriorityMap().put(ruleId, null); + assertThat(0, equalTo(alarm.getPriority(ruleId))); + } + + @Test + public void testGetPriority_notNull() throws Exception { + final String ruleId = "ruleId"; + final int priority = 2; + alarm.getPriorityMap().put(ruleId, priority); + assertThat(priority, equalTo(alarm.getPriority(ruleId))); + } + + @Test + public void testGetAlarmTypeRuleId_isNull() throws Exception { + final String ruleId = "ruleId"; + alarm.getRootAlarmTypeMap().put(ruleId, null); + assertThat(-1, equalTo(alarm.getRootAlarmType(ruleId))); + } + + @Test + public void testGetAlarmTypeRuleId_notNull() throws Exception { + final String ruleId = "ruleId"; + final int rootAlarmTyep = 2; + alarm.getRootAlarmTypeMap().put(ruleId, rootAlarmTyep); + assertThat(rootAlarmTyep, equalTo(alarm.getRootAlarmType(ruleId))); + } + + @Test + public void getterAndSetter4CenterType() throws Exception { + final int centerType = 1; + alarm.setCenterType(centerType); + assertThat(centerType, equalTo(alarm.getCenterType())); + } + + @Test + public void valueOf_exception() { + thrown.expect(Exception.class); + String xmlString = ""; + Alarm.valueOf(xmlString); + } + + @Test + public void valueOf_normal() { + String xmlString = alarm.toString(); + Alarm alarmValue = Alarm.valueOf(xmlString); + assertThat(alarmValue, equalTo(alarm)); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/config/MQConfigTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/config/MQConfigTest.java new file mode 100644 index 0000000..bb79c8a --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/config/MQConfigTest.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.openo.holmes.common.config; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +public class MQConfigTest { + @Test + public void testNewClass() { + MQConfig mqConfig= new MQConfig(); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/exception/CorrelationExceptionTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/exception/CorrelationExceptionTest.java new file mode 100644 index 0000000..bca63b2 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/exception/CorrelationExceptionTest.java @@ -0,0 +1,29 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.holmes.common.exception; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +public class CorrelationExceptionTest { + @Test + public void testConstructors() throws Exception { + CorrelationException ce1 = new CorrelationException("msg", new Exception()); + CorrelationException ce2 = new CorrelationException("msg"); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/producer/MQProducerTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/producer/MQProducerTest.java new file mode 100644 index 0000000..58ea684 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/producer/MQProducerTest.java @@ -0,0 +1,157 @@ +/** + * 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.common.producer; + +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 javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import javax.jms.Topic; +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.CorrelationResult; +import org.openo.holmes.common.api.stat.Alarm; +import org.openo.holmes.common.config.MQConfig; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.powermock.reflect.Whitebox; + +public class MQProducerTest { + + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private IterableProvider mqConfigProvider; + + private ConnectionFactory connectionFactory; + + private MQProducer mqProducer; + + @Before + public void before() throws Exception { + mqProducer = new MQProducer(); + + mqConfigProvider = PowerMock.createMock(IterableProvider.class); + connectionFactory = PowerMock.createMock(ConnectionFactory.class); + + Whitebox.setInternalState(mqProducer, "mqConfigProvider", mqConfigProvider); + Whitebox.setInternalState(mqProducer, "connectionFactory", connectionFactory); + PowerMock.resetAll(); + } + + @Test + public void sendAlarmMQTopicMsg() throws Exception { + Alarm alarm = new Alarm(); + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class); + ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class); + + 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.createProducer(anyObject(Destination.class))).andReturn(messageProducer); + + expect(session.createObjectMessage(anyObject(Alarm.class))).andReturn(objMessage); + messageProducer.send(objMessage); + session.commit(); + connection.close(); + + PowerMock.replayAll(); + + mqProducer.sendAlarmMQTopicMsg(alarm); + + PowerMock.verifyAll(); + + } + + @Test + public void sendAlarmMQTopicMsg_exception() throws Exception { + thrown.expect(JMSException.class); + Alarm alarm = new Alarm(); + + expect(connectionFactory.createConnection()).andThrow(new JMSException("")); + + PowerMock.replayAll(); + + mqProducer.sendAlarmMQTopicMsg(alarm); + + PowerMock.verifyAll(); + } + + @Test + public void sendCorrelationMQTopicMsg() throws Exception { + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class); + ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class); + + 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.createProducer(anyObject(Destination.class))).andReturn(messageProducer); + + expect(session.createObjectMessage(anyObject(CorrelationResult.class))) + .andReturn(objMessage); + messageProducer.send(objMessage); + session.commit(); + connection.close(); + + PowerMock.replayAll(); + + Alarm parentAlarm = new Alarm(); + Alarm childAlarm = new Alarm(); + mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm); + + PowerMock.verifyAll(); + + } + + @Test + public void sendCorrelationMQTopicMsg_exception() throws Exception { + thrown.expect(JMSException.class); + + expect(connectionFactory.createConnection()).andThrow(new JMSException("")); + + PowerMock.replayAll(); + + Alarm parentAlarm = new Alarm(); + Alarm childAlarm = new Alarm(); + mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm); + + PowerMock.verifyAll(); + + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/DbDaoUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/DbDaoUtilTest.java new file mode 100644 index 0000000..b56e4ee --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/DbDaoUtilTest.java @@ -0,0 +1,144 @@ +/** + * 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.common.utils; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; + +import io.dropwizard.db.DataSourceFactory; +import io.dropwizard.jdbi.DBIFactory; +import io.dropwizard.setup.Environment; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.powermock.reflect.Whitebox; +import org.skife.jdbi.v2.DBI; +import org.skife.jdbi.v2.Handle; + +public class DbDaoUtilTest { + + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private DBI jdbi; + + private Environment environmentProvider; + + private DataSourceFactory dataSourceFactoryProvider; + + private DbDaoUtil dbDaoUtil; + + private DBIFactory factory; + + @Before + public void before() throws Exception { + dbDaoUtil = new DbDaoUtil(); + + jdbi = PowerMock.createMock(DBI.class); + environmentProvider = PowerMock.createMock(Environment.class); + dataSourceFactoryProvider = PowerMock.createMock(DataSourceFactory.class); + factory = PowerMock.createMock(DBIFactory.class); + + Whitebox.setInternalState(dbDaoUtil, "environmentProvider", environmentProvider); + Whitebox.setInternalState(dbDaoUtil, "dataSourceFactoryProvider", + dataSourceFactoryProvider); + Whitebox.setInternalState(dbDaoUtil, "factory", factory); + + PowerMock.resetAll(); + } + + @Test + public void init() throws Exception { + DBI jdbi = PowerMock.createMock(DBI.class); + + expect(factory.build(anyObject(Environment.class), anyObject(DataSourceFactory.class), + anyObject(String.class))).andReturn(jdbi); + + PowerMock.replayAll(); + + dbDaoUtil.init(); + + PowerMock.verifyAll(); + } + + @Test + public void getDao() throws Exception { + Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi); + expect(jdbi.open(anyObject(Class.class))).andReturn(Class.class); + + PowerMock.replayAll(); + + dbDaoUtil.getDao(String.class); + + PowerMock.verifyAll(); + } + + @Test + public void testGetHandle() throws Exception { + Handle handle = PowerMock.createMock(Handle.class); + + Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi); + expect(jdbi.open()).andReturn(handle); + + PowerMock.replayAll(); + + dbDaoUtil.getHandle(); + + PowerMock.verifyAll(); + } + + @Test + public void testClose() throws Exception { + Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi); + jdbi.close(anyObject()); + + PowerMock.replayAll(); + + dbDaoUtil.close(new Object()); + + PowerMock.verifyAll(); + } + + @Test + public void testGetJdbiDaoByOnDemand() throws Exception { + Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi); + expect(jdbi.onDemand(anyObject(Class.class))).andReturn(Class.class); + + PowerMock.replayAll(); + + dbDaoUtil.getJdbiDaoByOnDemand(String.class); + + PowerMock.verifyAll(); + } + + @Test + public void testGetJdbiDaoByOpen() throws Exception { + Whitebox.setInternalState(dbDaoUtil, "jdbi", jdbi); + expect(jdbi.open(anyObject(Class.class))).andReturn(Class.class); + + PowerMock.replayAll(); + + dbDaoUtil.getJdbiDaoByOpen(String.class); + + PowerMock.verifyAll(); + } +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/ExceptionUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/ExceptionUtilTest.java new file mode 100644 index 0000000..caa45e3 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/ExceptionUtilTest.java @@ -0,0 +1,31 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.holmes.common.utils; + +import static org.openo.holmes.common.utils.ExceptionUtil.buildExceptionResponse; + +import org.junit.Test; + +public class ExceptionUtilTest { + + @Test + public void testBuildExceptionResponse() throws Exception { + buildExceptionResponse("test"); + } + + +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/I18nProxyTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/I18nProxyTest.java new file mode 100644 index 0000000..64b31f4 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/I18nProxyTest.java @@ -0,0 +1,41 @@ +/** + * Copyright 2016 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.common.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Locale; +import org.junit.Before; +import org.junit.Test; + +public class I18nProxyTest { + + private I18nProxy i18nProxy; + + @Before + public void setUp() { + i18nProxy = I18nProxy.getInstance(); + } + + @Test + public void testGetValue() { + String result = i18nProxy.getValue(new Locale("zh"), "test"); + assertThat(result, equalTo(null)); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/JacksonUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/JacksonUtilTest.java new file mode 100644 index 0000000..22d628e --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/JacksonUtilTest.java @@ -0,0 +1,49 @@ +/** + * Copyright 2016 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.common.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.openo.holmes.common.utils.bean.TestBean; + +public class JacksonUtilTest { + + @Test + public void testBeanToJson() throws Exception { + TestBean o = new TestBean(); + o.setId("id"); + String result = JacksonUtil.beanToJson(o); + assertThat("{\"id\":\"id\"}", equalTo(result)); + } + + @Test + public void jsonToBean_json_null() throws Exception { + String jsonNull = null; + TestBean testBean = JacksonUtil.jsonToBean(jsonNull, TestBean.class); + assertThat(testBean.getId(), equalTo(null)); + } + + @Test + public void jsonToBean_json_normal() throws Exception { + String json = "{\"id\":\"id\"}"; + TestBean testBean = JacksonUtil.jsonToBean(json, TestBean.class); + assertThat(testBean.getId(), equalTo("id")); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/JudgeNullUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/JudgeNullUtilTest.java new file mode 100644 index 0000000..25da9ac --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/JudgeNullUtilTest.java @@ -0,0 +1,44 @@ +/** + * Copyright 2016 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.common.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.List; +import org.junit.Test; + +public class JudgeNullUtilTest { + + @Test + public void isEmpty_array() { + boolean resultShorts = JudgeNullUtil.isEmpty(new short[]{}); + boolean resultInts = JudgeNullUtil.isEmpty(new int[]{}); + boolean resultLongs = JudgeNullUtil.isEmpty(new long[]{}); + boolean resultObjects = JudgeNullUtil.isEmpty(new Object[]{}); + boolean resultStrings = JudgeNullUtil.isEmpty(new String[]{}); + boolean resultLists = JudgeNullUtil.isEmpty(new List[]{}); + assertThat(true, equalTo(resultShorts)); + assertThat(true, equalTo(resultInts)); + assertThat(true, equalTo(resultLongs)); + assertThat(true, equalTo(resultObjects)); + assertThat(true, equalTo(resultStrings)); + assertThat(true, equalTo(resultLists)); + } + +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/LanguageUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/LanguageUtilTest.java new file mode 100644 index 0000000..d688a35 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/LanguageUtilTest.java @@ -0,0 +1,118 @@ +/** + * 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.common.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Locale; +import javax.servlet.http.HttpServletRequest; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; +import org.openo.holmes.common.constant.AlarmConst; +import org.powermock.api.easymock.PowerMock; + + +public class LanguageUtilTest { + + private HttpServletRequest request; + + @Before + public void setUp() { + request = PowerMock.createMock(HttpServletRequest.class); + } + + @Test + public void getLanguage_header_language_empty() { + EasyMock.expect(request.getHeader("language-option")).andReturn(""); + + PowerMock.replayAll(); + + String language = LanguageUtil.getLanguage(request); + + PowerMock.verifyAll(); + + assertThat(AlarmConst.I18N_EN, equalTo(language)); + } + + @Test + public void getLanguage_zh() { + EasyMock.expect(request.getHeader("language-option")).andReturn(AlarmConst.I18N_ZH); + + PowerMock.replayAll(); + + String language = LanguageUtil.getLanguage(request); + + PowerMock.verifyAll(); + + assertThat(AlarmConst.I18N_ZH, equalTo(language)); + } + + @Test + public void getLanguage_en() { + EasyMock.expect(request.getHeader("language-option")).andReturn(AlarmConst.I18N_EN); + + PowerMock.replayAll(); + + String language = LanguageUtil.getLanguage(request); + + PowerMock.verifyAll(); + + assertThat(AlarmConst.I18N_EN, equalTo(language)); + } + + @Test + public void getLocale_header_language_empty() { + EasyMock.expect(request.getHeader("language-option")).andReturn(""); + + PowerMock.replayAll(); + + Locale locale = LanguageUtil.getLocale(request); + + PowerMock.verifyAll(); + + assertThat(new Locale(AlarmConst.I18N_EN), equalTo(locale)); + } + + @Test + public void getLocale_zh() { + EasyMock.expect(request.getHeader("language-option")).andReturn(AlarmConst.I18N_ZH); + + PowerMock.replayAll(); + + Locale locale = LanguageUtil.getLocale(request); + + PowerMock.verifyAll(); + + assertThat(new Locale(AlarmConst.I18N_ZH), equalTo(locale)); + } + + @Test + public void getLocale_en() { + EasyMock.expect(request.getHeader("language-option")).andReturn(AlarmConst.I18N_EN); + + PowerMock.replayAll(); + + Locale locale = LanguageUtil.getLocale(request); + + PowerMock.verifyAll(); + + assertThat(new Locale(AlarmConst.I18N_EN), equalTo(locale)); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/UserUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/UserUtilTest.java new file mode 100644 index 0000000..d81c919 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/UserUtilTest.java @@ -0,0 +1,67 @@ +/** + * 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.common.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import javax.servlet.http.HttpServletRequest; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.powermock.api.easymock.PowerMock; +public class UserUtilTest { + + private HttpServletRequest request; + + @Before + public void before() throws Exception { + request = PowerMock.createMock(HttpServletRequest.class); + } + + @After + public void after() throws Exception { + } + + @Test + public void getUserName_header_name_empty() throws Exception { + EasyMock.expect(request.getHeader("username")).andReturn(null); + + PowerMock.replayAll(); + + String userName = UserUtil.getUserName(request); + + PowerMock.verifyAll(); + + assertThat("admin", equalTo(userName)); + } + + @Test + public void getUserName_normal() throws Exception { + EasyMock.expect(request.getHeader("username")).andReturn("name1"); + + PowerMock.replayAll(); + + String userName = UserUtil.getUserName(request); + + PowerMock.verifyAll(); + + assertThat("name1", equalTo(userName)); + } + +} diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/bean/TestBean.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/bean/TestBean.java new file mode 100644 index 0000000..fab78c2 --- /dev/null +++ b/holmes-actions/src/test/java/org/openo/holmes/common/utils/bean/TestBean.java @@ -0,0 +1,32 @@ +/** + * Copyright 2016 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.common.utils.bean; + +import java.io.Serializable; + +public class TestBean implements Serializable { + String id; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} \ No newline at end of file -- cgit 1.2.3-korg