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(); |
| 39 | |
| 40 | // Use this to check handling for miminal Alarm |
| 41 | private final Alarm alarmMinimumFields = new DefaultAlarm.Builder( |
| 42 | new AlarmId(44), |
| 43 | DeviceId.deviceId("of:2222000000000000"), |
| 44 | "NE unreachable", |
| 45 | Alarm.SeverityLevel.CLEARED, |
| 46 | 1). |
| 47 | build(); |
| 48 | |
| 49 | // Use this to check handling for fully populated Alarm |
| 50 | private final Alarm alarmWithSource = new DefaultAlarm.Builder( |
| 51 | new AlarmId(44), |
| 52 | DeviceId.deviceId("of:2222000000000000"), |
| 53 | "NE unreachable", |
| 54 | Alarm.SeverityLevel.CLEARED, 1). |
| 55 | forSource(AlarmEntityId.alarmEntityId("port:1/2/3/4")). |
| 56 | withTimeUpdated(2). |
| 57 | withTimeCleared(3L). |
| 58 | withServiceAffecting(true). |
| 59 | withAcknowledged(true). |
| 60 | withManuallyClearable(true). |
| 61 | withAssignedUser("the assigned user").build(); |
| 62 | |
| 63 | @Test |
| 64 | public void alarmCodecTestWithOptionalFieldMissing() { |
| 65 | //context.registerService(AlarmService.class, new AlarmServiceAdapter()); |
| 66 | final JsonCodec<Alarm> codec = context.codec(Alarm.class); |
| 67 | assertThat(codec, is(notNullValue())); |
| 68 | |
| 69 | final ObjectNode alarmJson = codec.encode(alarmMinimumFields, context); |
| 70 | assertThat(alarmJson, notNullValue()); |
| 71 | assertThat(alarmJson, matchesAlarm(alarmMinimumFields)); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void alarmCodecTestWithOptionalField() { |
| 77 | final JsonCodec<Alarm> codec = context.codec(Alarm.class); |
| 78 | assertThat(codec, is(notNullValue())); |
| 79 | |
| 80 | final ObjectNode alarmJson = codec.encode(alarmWithSource, context); |
| 81 | assertThat(alarmJson, notNullValue()); |
| 82 | assertThat(alarmJson, matchesAlarm(alarmWithSource)); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void verifyMinimalAlarmIsEncoded() throws Exception { |
| 88 | final JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class); |
| 89 | |
| 90 | final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json"); |
| 91 | assertCommon(alarm); |
| 92 | |
| 93 | assertThat(alarm.timeCleared(), nullValue()); |
| 94 | assertThat(alarm.assignedUser(), nullValue()); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void verifyFullyLoadedAlarmIsEncoded() throws Exception { |
| 100 | final JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class); |
| 101 | |
| 102 | final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json"); |
| 103 | assertCommon(alarm); |
| 104 | |
| 105 | assertThat(alarm.timeCleared(), is(2222L)); |
| 106 | assertThat(alarm.assignedUser(), is("foo")); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | private void assertCommon(final Alarm alarm) { |
| 111 | assertThat(alarm.id(), is(new AlarmId(10L))); |
| 112 | assertThat(alarm.description(), is("NE is not reachable")); |
| 113 | assertThat(alarm.source(), is(AlarmEntityId.NONE)); |
| 114 | assertThat(alarm.timeRaised(), is(999L)); |
| 115 | assertThat(alarm.timeUpdated(), is(1111L)); |
| 116 | assertThat(alarm.severity(), is(Alarm.SeverityLevel.MAJOR)); |
| 117 | assertThat(alarm.serviceAffecting(), is(true)); |
| 118 | assertThat(alarm.acknowledged(), is(false)); |
| 119 | assertThat(alarm.manuallyClearable(), is(true)); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reads in a rule from the given resource and decodes it. |
| 124 | * |
| 125 | * @param resourceName resource to use to read the JSON for the rule |
| 126 | * @return decoded flow rule |
| 127 | * @throws IOException if processing the resource failsdecode |
| 128 | */ |
| 129 | private Alarm getDecodedAlarm(final JsonCodec<Alarm> codec, final String resourceName) throws IOException { |
| 130 | final InputStream jsonStream = AlarmCodecTest.class |
| 131 | .getResourceAsStream(resourceName); |
| 132 | final JsonNode json = context.mapper().readTree(jsonStream); |
| 133 | assertThat(json, notNullValue()); |
| 134 | final Alarm result = codec.decode((ObjectNode) json, context); |
| 135 | assertThat(result, notNullValue()); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | } |