blob: 60bb33097867b44ffcc526f05d2b2a9c18eff6a8 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 *
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 */
16package org.onosproject.incubator.net.faultmanagement.alarm;
17
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.notNullValue;
kmcpeakeb172d5f2015-12-10 11:30:43 +000021import static org.hamcrest.Matchers.greaterThan;
kmcpeake4fe18c82015-11-17 20:07:39 +000022import org.junit.Test;
23import static org.junit.Assert.*;
24import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25import org.onosproject.net.DeviceId;
26
27public class DefaultAlarmTest {
28
29 @Test
30 public void testImmutability() {
31 assertThatClassIsImmutable(DefaultAlarm.class);
32 }
33
34 /**
35 * Checks the construction of a DefaultAlarm object.
36 */
37 @Test
38 public void testConstruction() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000039 final DefaultAlarm a = generate();
kmcpeake4fe18c82015-11-17 20:07:39 +000040 assertThat(a, is(notNullValue()));
41 final DefaultAlarm b = new DefaultAlarm.Builder(a).build();
kmcpeake4fe18c82015-11-17 20:07:39 +000042 assertEquals(a, b);
43 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000044
45 @Test
46 public void testEquals() {
47 final DefaultAlarm a = new DefaultAlarm.Builder(
48 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).build();
49 final DefaultAlarm b = new DefaultAlarm.Builder(
50 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, a.timeRaised() + 1).
51 withId(ALARM_ID).withTimeUpdated(a.timeUpdated() + 1).build();
52 assertEquals("id or timeRaised or timeUpdated may differ", a, b);
53
54 assertNotEquals(a, new DefaultAlarm.Builder(a).withAcknowledged(!a.acknowledged()).build());
55 assertNotEquals(a, new DefaultAlarm.Builder(a).withManuallyClearable(!a.manuallyClearable()).build());
56 assertNotEquals(a, new DefaultAlarm.Builder(a).withServiceAffecting(!a.serviceAffecting()).build());
57 assertNotEquals(a, new DefaultAlarm.Builder(a).withAssignedUser("Changed" + a.assignedUser()).build());
58
59 }
60
61 @Test
62 public void testClear() {
63 final DefaultAlarm active = generate();
64 final DefaultAlarm cleared = new DefaultAlarm.Builder(active).clear().build();
65 assertNotEquals(active, cleared);
66 assertThat(cleared.timeRaised(), is(active.timeRaised()));
67 assertThat(cleared.severity(), is(Alarm.SeverityLevel.CLEARED));
68 assertThat(cleared.timeUpdated(), greaterThan(active.timeUpdated()));
69 assertNotNull(cleared.timeCleared());
70
71 }
72
73 @Test
74 public void testId() {
75 final DefaultAlarm a = generate();
76 assertThat(a.id(), is(AlarmId.NONE));
77 final DefaultAlarm b = new DefaultAlarm.Builder(a).withId(ALARM_ID).build();
78
79 assertEquals("id ignored in equals", a, b);
80 assertNotEquals(ALARM_ID, a.id());
81 assertEquals(ALARM_ID, b.id());
82 assertEquals(ALARM_ENTITY_ID, b.source());
83
84 }
85 private static final AlarmEntityId ALARM_ENTITY_ID = AlarmEntityId.alarmEntityId("port:bar");
86 private static final AlarmId ALARM_ID = AlarmId.alarmId(888L);
87
88 private static DefaultAlarm generate() {
89 return new DefaultAlarm.Builder(
90 DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).forSource(ALARM_ENTITY_ID).build();
91 }
kmcpeake4fe18c82015-11-17 20:07:39 +000092}