2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openo.holmes.common.api.stat;
19 import java.util.Date;
20 import static org.hamcrest.core.IsEqual.equalTo;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertThat;
23 import static org.junit.Assert.assertTrue;
25 import java.util.HashMap;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
33 public class AlarmTest {
36 public ExpectedException thrown = ExpectedException.none();
41 public void before() throws Exception {
46 public void after() throws Exception {
50 public void testCompareLinkPosition_isEmpty() throws Exception {
51 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
52 assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
56 public void testCompareLinkPosition_TempLinkId() throws Exception {
57 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
59 alarm.addLinkIdNodeIdx(1, 3);
60 assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
64 public void testContainNode_NoContainLink() throws Exception {
65 alarm.addLinkIdNodeIdx(1, 2);
66 assertThat(false, equalTo(alarm.containNode(2, 2)));
70 public void testContainNode_ContainLinkNoIdx() throws Exception {
71 alarm.addLinkIdNodeIdx(1, 2);
72 assertFalse(alarm.containNode(1, 3));
76 public void testContainNode_ContainLinkAndIdx() throws Exception {
77 alarm.addLinkIdNodeIdx(1, 2);
78 assertTrue(alarm.containNode(1, 2));
82 public void testGetDataType() throws Exception {
83 assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
87 public void testToString() throws Exception {
88 Alarm alarmTempA = new Alarm();
89 Alarm alarmTempB = new Alarm();
90 Date date = new Date();
91 alarmTempA.setClearedTime(date);
92 alarmTempA.setRaisedTime(date);
93 alarmTempA.setRaisedServerTime(date);
94 alarmTempB.setClearedTime(date);
95 alarmTempB.setRaisedTime(date);
96 alarmTempB.setRaisedServerTime(date);
97 assertThat(alarmTempA.toString(),equalTo(alarmTempB.toString()));
101 public void testHashCode() throws Exception {
102 final Alarm alarmTemp = new Alarm();
103 final String alarmKey = "alarmKey";
104 alarm.setAlarmKey(alarmKey);
105 alarmTemp.setAlarmKey(alarmKey);
106 assertThat(alarm.hashCode(), equalTo(alarmTemp.hashCode()));
110 public void testEqualsAnd_NotNull() throws Exception {
111 final Alarm alarmTemp = new Alarm();
112 final String alarmKey = "alarmKey";
113 alarm.setAlarmKey(alarmKey);
114 alarmTemp.setAlarmKey(alarmKey);
115 assertTrue(alarm.equals(alarmTemp));
119 public void testEqualsAndH_isNull() throws Exception {
120 assertFalse(alarm.equals(null));
124 public void testClone() throws Exception {
125 alarm.setAlarmKey("alarmKey");
126 Alarm alarmTemp = (Alarm) alarm.clone();
127 assertTrue(alarm.equals(alarmTemp));
128 assertFalse(alarm == alarmTemp);
132 public void testGetObjectId() throws Exception {
134 assertThat("11", equalTo(alarm.getObjectId()));
138 public void testAddLinkIds() throws Exception {
139 final int linkId = 11;
140 alarm.addLinkIds(linkId);
141 assertTrue(alarm.getLinkIds().contains(linkId));
145 public void testContainsPriority_true() throws Exception {
146 String ruleId = "ruleId";
147 alarm.getPriorityMap().put(ruleId, 2);
148 assertTrue(alarm.containsPriority(ruleId));
152 public void testContainsPriority_false() throws Exception {
153 final String ruleId = "ruleId";
154 assertFalse(alarm.containsPriority(ruleId));
158 public void testGetPriority_isNull() throws Exception {
159 final String ruleId = "ruleId";
160 alarm.getPriorityMap().put(ruleId, null);
161 assertThat(0, equalTo(alarm.getPriority(ruleId)));
165 public void testGetPriority_notNull() throws Exception {
166 final String ruleId = "ruleId";
167 final int priority = 2;
168 alarm.getPriorityMap().put(ruleId, priority);
169 assertThat(priority, equalTo(alarm.getPriority(ruleId)));
173 public void testGetAlarmTypeRuleId_isNull() throws Exception {
174 final String ruleId = "ruleId";
175 alarm.getRootAlarmTypeMap().put(ruleId, null);
176 assertThat(-1, equalTo(alarm.getRootAlarmType(ruleId)));
180 public void testGetAlarmTypeRuleId_notNull() throws Exception {
181 final String ruleId = "ruleId";
182 final int rootAlarmType = 2;
183 alarm.getRootAlarmTypeMap().put(ruleId, rootAlarmType);
184 assertThat(rootAlarmType, equalTo(alarm.getRootAlarmType(ruleId)));
188 public void getterAndSetter4CenterType() throws Exception {
189 final int centerType = 1;
190 alarm.setCenterType(centerType);
191 assertThat(centerType, equalTo(alarm.getCenterType()));
195 public void valueOf_exception() {
196 thrown.expect(Exception.class);
197 String xmlString = "";
198 Alarm.valueOf(xmlString);
202 public void valueOf_normal() {
203 String xmlString = alarm.toString();
204 Alarm alarmValue = Alarm.valueOf(xmlString);
205 assertThat(alarmValue, equalTo(alarm));