kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
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.faultmanagement.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.onosproject.codec.JsonCodec; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 22 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 23 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.*; |
| 33 | import static org.onosproject.faultmanagement.web.AlarmJsonMatcher.matchesAlarm; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 34 | |
| 35 | public class AlarmCodecTest { |
| 36 | |
| 37 | private final AlarmCodecContext context = new AlarmCodecContext(); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 38 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar"); |
| 39 | private static final String UNIQUE_ID_1 = "unique_id_1"; |
| 40 | private static final AlarmId ALARM_ID = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 41 | |
| 42 | // Use this to check handling for miminal Alarm |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 43 | private final Alarm alarmMinimumFields = new DefaultAlarm.Builder(ALARM_ID, |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 44 | DeviceId.deviceId("of:2222000000000000"), "NE unreachable", Alarm.SeverityLevel.CLEARED, 1 |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 45 | ).build(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 46 | |
| 47 | // Use this to check handling for fully populated Alarm |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 48 | private final Alarm alarmWithSource = new DefaultAlarm.Builder(ALARM_ID, |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 49 | DeviceId.deviceId("of:2222000000000000"), "NE unreachable", Alarm.SeverityLevel.CLEARED, 1 |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 50 | ).forSource(AlarmEntityId.alarmEntityId("port:1/2/3/4")).withTimeUpdated(2).withTimeCleared(3L). |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 51 | withServiceAffecting(true).withAcknowledged(true).withManuallyClearable(true). |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 52 | withAssignedUser("the assigned user").build(); |
| 53 | |
| 54 | @Test |
| 55 | public void alarmCodecTestWithOptionalFieldMissing() { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 56 | JsonCodec<Alarm> codec = context.codec(Alarm.class); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 57 | assertThat(codec, is(notNullValue())); |
| 58 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 59 | ObjectNode alarmJson = codec.encode(alarmMinimumFields, context); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 60 | assertThat(alarmJson, notNullValue()); |
| 61 | assertThat(alarmJson, matchesAlarm(alarmMinimumFields)); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void alarmCodecTestWithOptionalField() { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 67 | JsonCodec<Alarm> codec = context.codec(Alarm.class); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 68 | assertThat(codec, is(notNullValue())); |
| 69 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 70 | ObjectNode alarmJson = codec.encode(alarmWithSource, context); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 71 | assertThat(alarmJson, notNullValue()); |
| 72 | assertThat(alarmJson, matchesAlarm(alarmWithSource)); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void verifyMinimalAlarmIsEncoded() throws Exception { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 78 | JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 79 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 80 | Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json"); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 81 | assertCommon(alarm); |
| 82 | |
| 83 | assertThat(alarm.timeCleared(), nullValue()); |
| 84 | assertThat(alarm.assignedUser(), nullValue()); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void verifyFullyLoadedAlarmIsEncoded() throws Exception { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 90 | JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 91 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 92 | Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json"); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 93 | assertCommon(alarm); |
| 94 | |
| 95 | assertThat(alarm.timeCleared(), is(2222L)); |
| 96 | assertThat(alarm.assignedUser(), is("foo")); |
| 97 | |
| 98 | } |
| 99 | |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 100 | private void assertCommon(Alarm alarm) { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 101 | assertThat(alarm.id(), is(AlarmId.alarmId(DeviceId.deviceId("of:123"), |
| 102 | String.valueOf(10)))); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 103 | assertThat(alarm.description(), is("NE is not reachable")); |
| 104 | assertThat(alarm.source(), is(AlarmEntityId.NONE)); |
| 105 | assertThat(alarm.timeRaised(), is(999L)); |
| 106 | assertThat(alarm.timeUpdated(), is(1111L)); |
| 107 | assertThat(alarm.severity(), is(Alarm.SeverityLevel.MAJOR)); |
| 108 | assertThat(alarm.serviceAffecting(), is(true)); |
| 109 | assertThat(alarm.acknowledged(), is(false)); |
| 110 | assertThat(alarm.manuallyClearable(), is(true)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Reads in a rule from the given resource and decodes it. |
| 115 | * |
| 116 | * @param resourceName resource to use to read the JSON for the rule |
| 117 | * @return decoded flow rule |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 118 | * @throws IOException if processing the resource fails to decode |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 119 | */ |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 120 | private Alarm getDecodedAlarm(JsonCodec<Alarm> codec, String resourceName) throws IOException { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 121 | try (InputStream jsonStream = AlarmCodecTest.class |
| 122 | .getResourceAsStream(resourceName)) { |
| 123 | JsonNode json = context.mapper().readTree(jsonStream); |
| 124 | assertThat(json, notNullValue()); |
| 125 | Alarm result = codec.decode((ObjectNode) json, context); |
| 126 | assertThat(result, notNullValue()); |
| 127 | return result; |
| 128 | } |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 129 | } |
| 130 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 131 | } |