d701f3d91c8863d7ed25897e6693492175c4bbdc
[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 com.alibaba.fastjson.JSON;
20 import com.alibaba.fastjson.JSONArray;
21 import com.alibaba.fastjson.JSONObject;
22 import com.google.gson.Gson;
23 import com.google.gson.reflect.TypeToken;
24 import java.util.Date;
25 import static org.hamcrest.core.IsEqual.equalTo;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.holmes.common.api.entity.ServiceNode;
38
39 public class AlarmTest {
40
41     @Rule
42     public ExpectedException thrown = ExpectedException.none();
43
44     private Alarm alarm;
45
46     @Before
47     public void before() throws Exception {
48         alarm = new Alarm();
49     }
50
51     @After
52     public void after() throws Exception {
53     }
54
55     @Test
56     public void testCompareLinkPosition_isEmpty() throws Exception {
57         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
58         assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
59     }
60
61     @Test
62     public void testCompareLinkPosition_TempLinkId() throws Exception {
63         final Map<Integer, Integer> otherIdIdx = new HashMap<>();
64         otherIdIdx.put(1, 2);
65         alarm.addLinkIdNodeIdx(1, 3);
66         assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
67     }
68
69     @Test
70     public void testContainNode_NoContainLink() throws Exception {
71         alarm.addLinkIdNodeIdx(1, 2);
72         assertThat(false, equalTo(alarm.containNode(2, 2)));
73     }
74
75     @Test
76     public void testContainNode_ContainLinkNoIdx() throws Exception {
77         alarm.addLinkIdNodeIdx(1, 2);
78         assertFalse(alarm.containNode(1, 3));
79     }
80
81     @Test
82     public void testContainNode_ContainLinkAndIdx() throws Exception {
83         alarm.addLinkIdNodeIdx(1, 2);
84         assertTrue(alarm.containNode(1, 2));
85     }
86
87     @Test
88     public void testGetDataType() throws Exception {
89         assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
90     }
91
92     @Test
93     public void testToString() throws Exception {
94         Alarm alarmTempA = new Alarm();
95         Alarm alarmTempB = new Alarm();
96         Date date = new Date();
97         alarmTempA.setClearedTime(date);
98         alarmTempA.setRaisedTime(date);
99         alarmTempA.setRaisedServerTime(date);
100         alarmTempB.setClearedTime(date);
101         alarmTempB.setRaisedTime(date);
102         alarmTempB.setRaisedServerTime(date);
103         assertThat(alarmTempA.toString(),equalTo(alarmTempB.toString()));
104     }
105
106     @Test
107     public void testHashCode() throws Exception {
108         final Alarm alarmTemp = new Alarm();
109         final String alarmKey = "alarmKey";
110         alarm.setAlarmKey(alarmKey);
111         alarmTemp.setAlarmKey(alarmKey);
112         assertThat(alarm.hashCode(), equalTo(alarmTemp.hashCode()));
113     }
114
115     @Test
116     public void testEqualsAnd_NotNull() throws Exception {
117         final Alarm alarmTemp = new Alarm();
118         final String alarmKey = "alarmKey";
119         alarm.setAlarmKey(alarmKey);
120         alarmTemp.setAlarmKey(alarmKey);
121         assertTrue(alarm.equals(alarmTemp));
122     }
123
124     @Test
125     public void testEqualsAndH_isNull() throws Exception {
126         assertFalse(alarm.equals(null));
127     }
128
129     @Test
130     public void testClone() throws Exception {
131         alarm.setAlarmKey("alarmKey");
132         Alarm alarmTemp = (Alarm) alarm.clone();
133         assertTrue(alarm.equals(alarmTemp));
134         assertFalse(alarm == alarmTemp);
135     }
136
137     @Test
138     public void testGetObjectId() throws Exception {
139         alarm.setId(11);
140         assertThat("11", equalTo(alarm.getObjectId()));
141     }
142
143     @Test
144     public void testAddLinkIds() throws Exception {
145         final int linkId = 11;
146         alarm.addLinkIds(linkId);
147         assertTrue(alarm.getLinkIds().contains(linkId));
148     }
149
150     @Test
151     public void testContainsPriority_true() throws Exception {
152         String ruleId = "ruleId";
153         alarm.getPriorityMap().put(ruleId, 2);
154         assertTrue(alarm.containsPriority(ruleId));
155     }
156
157     @Test
158     public void testContainsPriority_false() throws Exception {
159         final String ruleId = "ruleId";
160         assertFalse(alarm.containsPriority(ruleId));
161     }
162
163     @Test
164     public void testGetPriority_isNull() throws Exception {
165         final String ruleId = "ruleId";
166         alarm.getPriorityMap().put(ruleId, null);
167         assertThat(0, equalTo(alarm.getPriority(ruleId)));
168     }
169
170     @Test
171     public void testGetPriority_notNull() throws Exception {
172         final String ruleId = "ruleId";
173         final int priority = 2;
174         alarm.getPriorityMap().put(ruleId, priority);
175         assertThat(priority, equalTo(alarm.getPriority(ruleId)));
176     }
177
178     @Test
179     public void testGetAlarmTypeRuleId_isNull() throws Exception {
180         final String ruleId = "ruleId";
181         alarm.getRootAlarmTypeMap().put(ruleId, null);
182         assertThat(-1, equalTo(alarm.getRootAlarmType(ruleId)));
183     }
184
185     @Test
186     public void testGetAlarmTypeRuleId_notNull() throws Exception {
187         final String ruleId = "ruleId";
188         final int rootAlarmType = 2;
189         alarm.getRootAlarmTypeMap().put(ruleId, rootAlarmType);
190         assertThat(rootAlarmType, equalTo(alarm.getRootAlarmType(ruleId)));
191     }
192
193     @Test
194     public void getterAndSetter4CenterType() throws Exception {
195         final int centerType = 1;
196         alarm.setCenterType(centerType);
197         assertThat(centerType, equalTo(alarm.getCenterType()));
198     }
199     @Test
200     public void TestJson(){
201         ServiceNode serviceNode = new ServiceNode();
202         serviceNode.setIp("111");
203         String jsonString = "{\"uid\":\"189024\", \"region\":\"SouthChina\", \"order\":123}";
204         String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
205
206         JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
207         JSONArray jsonArray = jsonObject.getJSONArray("students");
208          System.out.printf("jsonObject:"+jsonArray);
209 //     System.out.println("uid:" + retMap.get("uid") + ", " + "region:" + retMap.get("region") + ", " + "order:" + retMap.get("order"));
210     }
211 }