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.onap.holmes.common.api.stat;
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;
30 import java.util.HashMap;
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;
39 public class AlarmTest {
42 public ExpectedException thrown = ExpectedException.none();
47 public void before() throws Exception {
52 public void after() throws Exception {
56 public void testCompareLinkPosition_isEmpty() throws Exception {
57 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
58 assertThat(999, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
62 public void testCompareLinkPosition_TempLinkId() throws Exception {
63 final Map<Integer, Integer> otherIdIdx = new HashMap<>();
65 alarm.addLinkIdNodeIdx(1, 3);
66 assertThat(1, equalTo(alarm.CompareLinkPosition(otherIdIdx)));
70 public void testContainNode_NoContainLink() throws Exception {
71 alarm.addLinkIdNodeIdx(1, 2);
72 assertThat(false, equalTo(alarm.containNode(2, 2)));
76 public void testContainNode_ContainLinkNoIdx() throws Exception {
77 alarm.addLinkIdNodeIdx(1, 2);
78 assertFalse(alarm.containNode(1, 3));
82 public void testContainNode_ContainLinkAndIdx() throws Exception {
83 alarm.addLinkIdNodeIdx(1, 2);
84 assertTrue(alarm.containNode(1, 2));
88 public void testGetDataType() throws Exception {
89 assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
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()));
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()));
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));
125 public void testEqualsAndH_isNull() throws Exception {
126 assertFalse(alarm.equals(null));
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);
138 public void testGetObjectId() throws Exception {
140 assertThat("11", equalTo(alarm.getObjectId()));
144 public void testAddLinkIds() throws Exception {
145 final int linkId = 11;
146 alarm.addLinkIds(linkId);
147 assertTrue(alarm.getLinkIds().contains(linkId));
151 public void testContainsPriority_true() throws Exception {
152 String ruleId = "ruleId";
153 alarm.getPriorityMap().put(ruleId, 2);
154 assertTrue(alarm.containsPriority(ruleId));
158 public void testContainsPriority_false() throws Exception {
159 final String ruleId = "ruleId";
160 assertFalse(alarm.containsPriority(ruleId));
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)));
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)));
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)));
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)));
194 public void getterAndSetter4CenterType() throws Exception {
195 final int centerType = 1;
196 alarm.setCenterType(centerType);
197 assertThat(centerType, equalTo(alarm.getCenterType()));
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}]}";
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"));