blob: 6818ef9a82da3fce5da195f3f4ee233b28c86c6b [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-present Open Networking Foundation
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
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 *
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08008 * http://www.apache.org/licenses/LICENSE-2.0
kmcpeake4fe18c82015-11-17 20:07:39 +00009 *
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 */
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080016package org.onosproject.alarm;
kmcpeake4fe18c82015-11-17 20:07:39 +000017
kmcpeake4fe18c82015-11-17 20:07:39 +000018import org.junit.Test;
kmcpeake4fe18c82015-11-17 20:07:39 +000019import org.onosproject.net.DeviceId;
20
Andrea Campanella65f9eb92017-05-02 11:36:14 -070021import static org.hamcrest.MatcherAssert.assertThat;
22import static org.hamcrest.Matchers.*;
23import static org.junit.Assert.*;
24import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25
kmcpeake4fe18c82015-11-17 20:07:39 +000026public class DefaultAlarmTest {
27
Andrea Campanella65f9eb92017-05-02 11:36:14 -070028 private static final AlarmEntityId ALARM_ENTITY_ID = AlarmEntityId.alarmEntityId("port:bar");
29 private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar");
30 private static final String UNIQUE_ID_1 = "unique_id_1";
31 private static final AlarmId ALARM_ID = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1);
32 private static final String UNIQUE_ID_2 = "unique_id_1";
33 private static final AlarmId ALARM_ID_2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_2);
34
kmcpeake4fe18c82015-11-17 20:07:39 +000035 @Test
36 public void testImmutability() {
37 assertThatClassIsImmutable(DefaultAlarm.class);
38 }
39
40 /**
41 * Checks the construction of a DefaultAlarm object.
42 */
43 @Test
44 public void testConstruction() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000045 final DefaultAlarm a = generate();
kmcpeake4fe18c82015-11-17 20:07:39 +000046 assertThat(a, is(notNullValue()));
47 final DefaultAlarm b = new DefaultAlarm.Builder(a).build();
kmcpeake4fe18c82015-11-17 20:07:39 +000048 assertEquals(a, b);
49 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000050
51 @Test
52 public void testEquals() {
Andrea Campanella65f9eb92017-05-02 11:36:14 -070053 final DefaultAlarm a = new DefaultAlarm.Builder(ALARM_ID_2,
kmcpeakeb172d5f2015-12-10 11:30:43 +000054 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).build();
Andrea Campanella65f9eb92017-05-02 11:36:14 -070055 final DefaultAlarm b = new DefaultAlarm.Builder(ALARM_ID,
56 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, a.timeRaised() + 1)
57 .withTimeUpdated(a.timeUpdated() + 1).build();
kmcpeakeb172d5f2015-12-10 11:30:43 +000058 assertEquals("id or timeRaised or timeUpdated may differ", a, b);
59
60 assertNotEquals(a, new DefaultAlarm.Builder(a).withAcknowledged(!a.acknowledged()).build());
61 assertNotEquals(a, new DefaultAlarm.Builder(a).withManuallyClearable(!a.manuallyClearable()).build());
62 assertNotEquals(a, new DefaultAlarm.Builder(a).withServiceAffecting(!a.serviceAffecting()).build());
63 assertNotEquals(a, new DefaultAlarm.Builder(a).withAssignedUser("Changed" + a.assignedUser()).build());
64
65 }
66
67 @Test
68 public void testClear() {
69 final DefaultAlarm active = generate();
70 final DefaultAlarm cleared = new DefaultAlarm.Builder(active).clear().build();
71 assertNotEquals(active, cleared);
72 assertThat(cleared.timeRaised(), is(active.timeRaised()));
73 assertThat(cleared.severity(), is(Alarm.SeverityLevel.CLEARED));
74 assertThat(cleared.timeUpdated(), greaterThan(active.timeUpdated()));
75 assertNotNull(cleared.timeCleared());
76
77 }
78
79 @Test
80 public void testId() {
81 final DefaultAlarm a = generate();
Andrea Campanella65f9eb92017-05-02 11:36:14 -070082 final DefaultAlarm b = new DefaultAlarm.Builder(a).build();
kmcpeakeb172d5f2015-12-10 11:30:43 +000083
84 assertEquals("id ignored in equals", a, b);
Andrea Campanella65f9eb92017-05-02 11:36:14 -070085 assertEquals(ALARM_ID, a.id());
kmcpeakeb172d5f2015-12-10 11:30:43 +000086 assertEquals(ALARM_ID, b.id());
87 assertEquals(ALARM_ENTITY_ID, b.source());
88
89 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000090
91 private static DefaultAlarm generate() {
Andrea Campanella65f9eb92017-05-02 11:36:14 -070092 return new DefaultAlarm.Builder(ALARM_ID,
kmcpeakeb172d5f2015-12-10 11:30:43 +000093 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).forSource(ALARM_ENTITY_ID).build();
94 }
kmcpeake4fe18c82015-11-17 20:07:39 +000095}