2 * Copyright 2017-2020 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;
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;
29 import java.util.HashMap;
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;
38 public class AlarmTest {
41 public ExpectedException thrown = ExpectedException.none();
46 public void before() throws Exception {
51 public void after() throws Exception {
55 public void testContainNode_NoContainLink() throws Exception {
56 alarm.addLinkIdNodeIdx(1, 2);
57 assertThat(false, equalTo(alarm.containNode(2, 2)));
61 public void testContainNode_ContainLinkNoIdx() throws Exception {
62 alarm.addLinkIdNodeIdx(1, 2);
63 assertFalse(alarm.containNode(1, 3));
67 public void testContainNode_ContainLinkAndIdx() throws Exception {
68 alarm.addLinkIdNodeIdx(1, 2);
69 assertTrue(alarm.containNode(1, 2));
73 public void testGetDataType() throws Exception {
74 assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType()));
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()));
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()));
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));
110 public void testEqualsAndH_isNull() throws Exception {
111 assertFalse(alarm.equals(null));
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);
123 public void testGetObjectId() throws Exception {
125 assertThat("11", equalTo(alarm.getObjectId()));
129 public void testAddLinkIds() throws Exception {
130 final int linkId = 11;
131 alarm.addLinkIds(linkId);
132 assertTrue(alarm.getLinkIds().contains(linkId));
136 public void testContainsPriority_true() throws Exception {
137 String ruleId = "ruleId";
138 alarm.getPriorityMap().put(ruleId, 2);
139 assertTrue(alarm.containsPriority(ruleId));
143 public void testContainsPriority_false() throws Exception {
144 final String ruleId = "ruleId";
145 assertFalse(alarm.containsPriority(ruleId));
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)));
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)));
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)));
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)));
179 public void getterAndSetter4CenterType() throws Exception {
180 final int centerType = 1;
181 alarm.setCenterType(centerType);
182 assertThat(centerType, equalTo(alarm.getCenterType()));
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}]}";
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"));