blob: f922fb0c3ca78a7c762be21e4914b56b9e60b3f5 [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.faultmanagement.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import java.io.IOException;
21import java.io.InputStream;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.hamcrest.Matchers.nullValue;
26
27import org.junit.Test;
28import org.onosproject.codec.JsonCodec;
29import static org.onosproject.faultmanagement.web.AlarmJsonMatcher.matchesAlarm;
30import org.onosproject.net.DeviceId;
31import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
32import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
33import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
34import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
35
36public class AlarmCodecTest {
37
38 private final AlarmCodecContext context = new AlarmCodecContext();
kmcpeakeb172d5f2015-12-10 11:30:43 +000039 private static final AlarmId ALARM_ID = AlarmId.alarmId(44);
kmcpeake4fe18c82015-11-17 20:07:39 +000040
41 // Use this to check handling for miminal Alarm
42 private final Alarm alarmMinimumFields = new DefaultAlarm.Builder(
kmcpeakeb172d5f2015-12-10 11:30:43 +000043 DeviceId.deviceId("of:2222000000000000"), "NE unreachable", Alarm.SeverityLevel.CLEARED, 1
44 ).withId(ALARM_ID).build();
kmcpeake4fe18c82015-11-17 20:07:39 +000045
46 // Use this to check handling for fully populated Alarm
47 private final Alarm alarmWithSource = new DefaultAlarm.Builder(
kmcpeakeb172d5f2015-12-10 11:30:43 +000048 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).
kmcpeake4fe18c82015-11-17 20:07:39 +000051 withAssignedUser("the assigned user").build();
52
53 @Test
54 public void alarmCodecTestWithOptionalFieldMissing() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000055 JsonCodec<Alarm> codec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000056 assertThat(codec, is(notNullValue()));
57
kmcpeakeb172d5f2015-12-10 11:30:43 +000058 ObjectNode alarmJson = codec.encode(alarmMinimumFields, context);
kmcpeake4fe18c82015-11-17 20:07:39 +000059 assertThat(alarmJson, notNullValue());
60 assertThat(alarmJson, matchesAlarm(alarmMinimumFields));
61
62 }
63
64 @Test
65 public void alarmCodecTestWithOptionalField() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000066 JsonCodec<Alarm> codec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000067 assertThat(codec, is(notNullValue()));
68
kmcpeakeb172d5f2015-12-10 11:30:43 +000069 ObjectNode alarmJson = codec.encode(alarmWithSource, context);
kmcpeake4fe18c82015-11-17 20:07:39 +000070 assertThat(alarmJson, notNullValue());
71 assertThat(alarmJson, matchesAlarm(alarmWithSource));
72
73 }
74
75 @Test
76 public void verifyMinimalAlarmIsEncoded() throws Exception {
kmcpeakeb172d5f2015-12-10 11:30:43 +000077 JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000078
kmcpeakeb172d5f2015-12-10 11:30:43 +000079 Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json");
kmcpeake4fe18c82015-11-17 20:07:39 +000080 assertCommon(alarm);
81
82 assertThat(alarm.timeCleared(), nullValue());
83 assertThat(alarm.assignedUser(), nullValue());
84
85 }
86
87 @Test
88 public void verifyFullyLoadedAlarmIsEncoded() throws Exception {
kmcpeakeb172d5f2015-12-10 11:30:43 +000089 JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000090
kmcpeakeb172d5f2015-12-10 11:30:43 +000091 Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json");
kmcpeake4fe18c82015-11-17 20:07:39 +000092 assertCommon(alarm);
93
94 assertThat(alarm.timeCleared(), is(2222L));
95 assertThat(alarm.assignedUser(), is("foo"));
96
97 }
98
kmcpeakea5404812015-12-08 11:52:50 +000099 private void assertCommon(Alarm alarm) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000100 assertThat(alarm.id(), is(AlarmId.alarmId(10L)));
kmcpeake4fe18c82015-11-17 20:07:39 +0000101 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
kmcpeakea5404812015-12-08 11:52:50 +0000116 * @throws IOException if processing the resource fails to decode
kmcpeake4fe18c82015-11-17 20:07:39 +0000117 */
kmcpeakea5404812015-12-08 11:52:50 +0000118 private Alarm getDecodedAlarm(JsonCodec<Alarm> codec, String resourceName) throws IOException {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000119 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 }
kmcpeake4fe18c82015-11-17 20:07:39 +0000127 }
128
kmcpeake4fe18c82015-11-17 20:07:39 +0000129}