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 static org.hamcrest.core.IsEqual.equalTo;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
24 import java.util.HashMap;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
32 public class AlarmTest {
35 public ExpectedException thrown = ExpectedException.none();
40 public void before() throws Exception {
45 public void after() throws Exception {
49 public void testCompareLinkPosition_isEmpty() throws Exception {
50 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
51 assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
55 public void testCompareLinkPosition_TempLinkId() throws Exception {
56 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
58 alarm.addLinkIdNodeIdx(1, 3);
59 assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
63 public void testContainNode_NoContainLink() throws Exception {
64 alarm.addLinkIdNodeIdx(1, 2);
65 assertThat(false, equalTo(alarm.containNode(2, 2)));
69 public void testContainNode_ContainLinkNoIdx() throws Exception {
70 alarm.addLinkIdNodeIdx(1, 2);
71 assertFalse(alarm.containNode(1, 3));
75 public void testContainNode_ContainLinkAndIdx() throws Exception {
76 alarm.addLinkIdNodeIdx(1, 2);
77 assertTrue(alarm.containNode(1, 2));
81 public void testGetDataType() throws Exception {
82 assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
86 public void testToString() throws Exception {
87 final Alarm alarmTemp = new Alarm();
88 assertThat(alarmTemp.toString(), equalTo(alarm.toString()));
92 public void testHashCode() throws Exception {
93 final Alarm alarmTemp = new Alarm();
94 final String alarmKey = "alarmKey";
95 alarm.setAlarmKey(alarmKey);
96 alarmTemp.setAlarmKey(alarmKey);
97 assertThat(alarm.hashCode(), equalTo(alarmTemp.hashCode()));
101 public void testEqualsAnd_NotNull() throws Exception {
102 final Alarm alarmTemp = new Alarm();
103 final String alarmKey = "alarmKey";
104 alarm.setAlarmKey(alarmKey);
105 alarmTemp.setAlarmKey(alarmKey);
106 assertTrue(alarm.equals(alarmTemp));
110 public void testEqualsAndH_isNull() throws Exception {
111 assertFalse(alarm.equals(null));
115 public void testClone() throws Exception {
116 alarm.setAlarmKey("alarmKey");
117 Alarm alarmTemp = (Alarm) alarm.clone();
118 assertTrue(alarm.equals(alarmTemp));
119 assertFalse(alarm == alarmTemp);
123 public void testGetObjectId() throws Exception {
125 assertThat("11", equalTo(alarm.getObjectId()));
129 public void testAddLinkIds() throws Exception {
130 final int linkId = 11;
131 alarm.addLinkIds(linkId);
132 assertTrue(alarm.getLinkIds().contains(linkId));
136 public void testContainsPriority_true() throws Exception {
137 String ruleId = "ruleId";
138 alarm.getPriorityMap().put(ruleId, 2);
139 assertTrue(alarm.containsPriority(ruleId));
143 public void testContainsPriority_false() throws Exception {
144 final String ruleId = "ruleId";
145 assertFalse(alarm.containsPriority(ruleId));
149 public void testGetPriority_isNull() throws Exception {
150 final String ruleId = "ruleId";
151 alarm.getPriorityMap().put(ruleId, null);
152 assertThat(0, equalTo(alarm.getPriority(ruleId)));
156 public void testGetPriority_notNull() throws Exception {
157 final String ruleId = "ruleId";
158 final int priority = 2;
159 alarm.getPriorityMap().put(ruleId, priority);
160 assertThat(priority, equalTo(alarm.getPriority(ruleId)));
164 public void testGetAlarmTypeRuleId_isNull() throws Exception {
165 final String ruleId = "ruleId";
166 alarm.getRootAlarmTypeMap().put(ruleId, null);
167 assertThat(-1, equalTo(alarm.getRootAlarmType(ruleId)));
171 public void testGetAlarmTypeRuleId_notNull() throws Exception {
172 final String ruleId = "ruleId";
173 final int rootAlarmType = 2;
174 alarm.getRootAlarmTypeMap().put(ruleId, rootAlarmType);
175 assertThat(rootAlarmType, equalTo(alarm.getRootAlarmType(ruleId)));
179 public void getterAndSetter4CenterType() throws Exception {
180 final int centerType = 1;
181 alarm.setCenterType(centerType);
182 assertThat(centerType, equalTo(alarm.getCenterType()));
186 public void valueOf_exception() {
187 thrown.expect(Exception.class);
188 String xmlString = "";
189 Alarm.valueOf(xmlString);
193 public void valueOf_normal() {
194 String xmlString = alarm.toString();
195 Alarm alarmValue = Alarm.valueOf(xmlString);
196 assertThat(alarmValue, equalTo(alarm));