Add Unit Tests
[holmes/common.git] / holmes-actions / src / test / java / org / onap / 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.onap.holmes.common.api.stat;
18
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;
24
25 import java.util.HashMap;
26 import java.util.Map;
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;
32
33 public class AlarmTest {
34
35     @Rule
36     public ExpectedException thrown = ExpectedException.none();
37
38     private Alarm alarm;
39
40     @Before
41     public void before() throws Exception {
42         alarm = new Alarm();
43     }
44
45     @After
46     public void after() throws Exception {
47     }
48
49     @Test
50     public void testCompareLinkPosition_isEmpty() throws Exception {
51         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
52         assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
53     }
54
55     @Test
56     public void testCompareLinkPosition_TempLinkId() throws Exception {
57         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
58         otherIdIdx.put(1, 2);
59         alarm.addLinkIdNodeIdx(1, 3);
60         assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
61     }
62
63     @Test
64     public void testContainNode_NoContainLink() throws Exception {
65         alarm.addLinkIdNodeIdx(1, 2);
66         assertThat(false, equalTo(alarm.containNode(2, 2)));
67     }
68
69     @Test
70     public void testContainNode_ContainLinkNoIdx() throws Exception {
71         alarm.addLinkIdNodeIdx(1, 2);
72         assertFalse(alarm.containNode(1, 3));
73     }
74
75     @Test
76     public void testContainNode_ContainLinkAndIdx() throws Exception {
77         alarm.addLinkIdNodeIdx(1, 2);
78         assertTrue(alarm.containNode(1, 2));
79     }
80
81     @Test
82     public void testGetDataType() throws Exception {
83         assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
84     }
85
86     @Test
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()));
98     }
99
100     @Test
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()));
107     }
108
109     @Test
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));
116     }
117
118     @Test
119     public void testEqualsAndH_isNull() throws Exception {
120         assertFalse(alarm.equals(null));
121     }
122
123     @Test
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);
129     }
130
131     @Test
132     public void testGetObjectId() throws Exception {
133         alarm.setId(11);
134         assertThat("11", equalTo(alarm.getObjectId()));
135     }
136
137     @Test
138     public void testAddLinkIds() throws Exception {
139         final int linkId = 11;
140         alarm.addLinkIds(linkId);
141         assertTrue(alarm.getLinkIds().contains(linkId));
142     }
143
144     @Test
145     public void testContainsPriority_true() throws Exception {
146         String ruleId = "ruleId";
147         alarm.getPriorityMap().put(ruleId, 2);
148         assertTrue(alarm.containsPriority(ruleId));
149     }
150
151     @Test
152     public void testContainsPriority_false() throws Exception {
153         final String ruleId = "ruleId";
154         assertFalse(alarm.containsPriority(ruleId));
155     }
156
157     @Test
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)));
162     }
163
164     @Test
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)));
170     }
171
172     @Test
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)));
177     }
178
179     @Test
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)));
185     }
186
187     @Test
188     public void getterAndSetter4CenterType() throws Exception {
189         final int centerType = 1;
190         alarm.setCenterType(centerType);
191         assertThat(centerType, equalTo(alarm.getCenterType()));
192     }
193 }