49858abad11fb068d4bce0fb68c493170f6dabf9
[holmes/common.git] / holmes-actions / src / test / java / org / openo / holmes / common / api / stat / AlarmTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.holmes.common.api.stat;
18
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;
23
24 import java.util.HashMap;
25 import java.util.Map;
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;
31
32 public class AlarmTest {
33
34     @Rule
35     public ExpectedException thrown = ExpectedException.none();
36
37     private Alarm alarm;
38
39     @Before
40     public void before() throws Exception {
41         alarm = new Alarm();
42     }
43
44     @After
45     public void after() throws Exception {
46     }
47
48     @Test
49     public void testCompareLinkPosition_isEmpty() throws Exception {
50         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
51         assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
52     }
53
54     @Test
55     public void testCompareLinkPosition_TempLinkId() throws Exception {
56         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
57         otherIdIdx.put(1, 2);
58         alarm.addLinkIdNodeIdx(1, 3);
59         assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
60     }
61
62     @Test
63     public void testContainNode_NoContainLink() throws Exception {
64         alarm.addLinkIdNodeIdx(1, 2);
65         assertThat(false, equalTo(alarm.containNode(2, 2)));
66     }
67
68     @Test
69     public void testContainNode_ContainLinkNoIdx() throws Exception {
70         alarm.addLinkIdNodeIdx(1, 2);
71         assertFalse(alarm.containNode(1, 3));
72     }
73
74     @Test
75     public void testContainNode_ContainLinkAndIdx() throws Exception {
76         alarm.addLinkIdNodeIdx(1, 2);
77         assertTrue(alarm.containNode(1, 2));
78     }
79
80     @Test
81     public void testGetDataType() throws Exception {
82         assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
83     }
84
85     @Test
86     public void testToString() throws Exception {
87         final Alarm alarmTemp = new Alarm();
88         assertThat(alarmTemp.toString(), equalTo(alarm.toString()));
89     }
90
91     @Test
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()));
98     }
99
100     @Test
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));
107     }
108
109     @Test
110     public void testEqualsAndH_isNull() throws Exception {
111         assertFalse(alarm.equals(null));
112     }
113
114     @Test
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);
120     }
121
122     @Test
123     public void testGetObjectId() throws Exception {
124         alarm.setId(11);
125         assertThat("11", equalTo(alarm.getObjectId()));
126     }
127
128     @Test
129     public void testAddLinkIds() throws Exception {
130         final int linkId = 11;
131         alarm.addLinkIds(linkId);
132         assertTrue(alarm.getLinkIds().contains(linkId));
133     }
134
135     @Test
136     public void testContainsPriority_true() throws Exception {
137         String ruleId = "ruleId";
138         alarm.getPriorityMap().put(ruleId, 2);
139         assertTrue(alarm.containsPriority(ruleId));
140     }
141
142     @Test
143     public void testContainsPriority_false() throws Exception {
144         final String ruleId = "ruleId";
145         assertFalse(alarm.containsPriority(ruleId));
146     }
147
148     @Test
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)));
153     }
154
155     @Test
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)));
161     }
162
163     @Test
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)));
168     }
169
170     @Test
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)));
176     }
177
178     @Test
179     public void getterAndSetter4CenterType() throws Exception {
180         final int centerType = 1;
181         alarm.setCenterType(centerType);
182         assertThat(centerType, equalTo(alarm.getCenterType()));
183     }
184
185     @Test
186     public void valueOf_exception() {
187         thrown.expect(Exception.class);
188         String xmlString = "";
189         Alarm.valueOf(xmlString);
190     }
191
192     @Test
193     public void valueOf_normal() {
194         String xmlString = alarm.toString();
195         Alarm alarmValue = Alarm.valueOf(xmlString);
196         assertThat(alarmValue, equalTo(alarm));
197     }
198 }