Fixed Bugs Detected by SonarCloud
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / api / stat / AlarmTest.java
1 /**
2  * Copyright 2017-2020 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
23 import java.util.Date;
24 import static org.hamcrest.core.IsEqual.equalTo;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.onap.holmes.common.api.entity.ServiceNode;
37
38 public class AlarmTest {
39
40     @Rule
41     public ExpectedException thrown = ExpectedException.none();
42
43     private Alarm alarm;
44
45     @Before
46     public void before() throws Exception {
47         alarm = new Alarm();
48     }
49
50     @After
51     public void after() throws Exception {
52     }
53
54     @Test
55     public void testContainNode_NoContainLink() throws Exception {
56         alarm.addLinkIdNodeIdx(1, 2);
57         assertThat(false, equalTo(alarm.containNode(2, 2)));
58     }
59
60     @Test
61     public void testContainNode_ContainLinkNoIdx() throws Exception {
62         alarm.addLinkIdNodeIdx(1, 2);
63         assertFalse(alarm.containNode(1, 3));
64     }
65
66     @Test
67     public void testContainNode_ContainLinkAndIdx() throws Exception {
68         alarm.addLinkIdNodeIdx(1, 2);
69         assertTrue(alarm.containNode(1, 2));
70     }
71
72     @Test
73     public void testGetDataType() throws Exception {
74         assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
75     }
76
77     @Test
78     public void testToString() throws Exception {
79         Alarm alarmTempA = new Alarm();
80         Alarm alarmTempB = new Alarm();
81         Date date = new Date();
82         alarmTempA.setClearedTime(date);
83         alarmTempA.setRaisedTime(date);
84         alarmTempA.setRaisedServerTime(date);
85         alarmTempB.setClearedTime(date);
86         alarmTempB.setRaisedTime(date);
87         alarmTempB.setRaisedServerTime(date);
88         assertThat(alarmTempA.toString(),equalTo(alarmTempB.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     @Test
185     public void TestJson(){
186         ServiceNode serviceNode = new ServiceNode();
187         serviceNode.setIp("111");
188         String jsonString = "{\"uid\":\"189024\", \"region\":\"SouthChina\", \"order\":123}";
189         String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
190
191         JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
192         JSONArray jsonArray = jsonObject.getJSONArray("students");
193          System.out.printf("jsonObject:"+jsonArray);
194 //     System.out.println("uid:" + retMap.get("uid") + ", " + "region:" + retMap.get("region") + ", " + "order:" + retMap.get("order"));
195     }
196 }