kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | package org.onosproject.incubator.net.faultmanagement.alarm; |
| 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.notNullValue; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 21 | import static org.hamcrest.Matchers.greaterThan; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import static org.junit.Assert.*; |
| 24 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | |
| 27 | public 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() { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 39 | final DefaultAlarm a = generate(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 40 | assertThat(a, is(notNullValue())); |
| 41 | final DefaultAlarm b = new DefaultAlarm.Builder(a).build(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 42 | assertEquals(a, b); |
| 43 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 44 | |
| 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 | } |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 92 | } |