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