blob: 6f126f4e53053d6a07bfe4494c45b0bbca230ccf [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
kmcpeake4fe18c82015-11-17 20:07:39 +000020import org.junit.Test;
21import org.onosproject.codec.JsonCodec;
kmcpeake4fe18c82015-11-17 20:07:39 +000022import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
23import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
24import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
25import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070026import org.onosproject.net.DeviceId;
27
28import java.io.IOException;
29import java.io.InputStream;
30
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.*;
33import static org.onosproject.faultmanagement.web.AlarmJsonMatcher.matchesAlarm;
kmcpeake4fe18c82015-11-17 20:07:39 +000034
35public class AlarmCodecTest {
36
37 private final AlarmCodecContext context = new AlarmCodecContext();
Andrea Campanella65f9eb92017-05-02 11:36:14 -070038 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);
kmcpeake4fe18c82015-11-17 20:07:39 +000041
42 // Use this to check handling for miminal Alarm
Andrea Campanella65f9eb92017-05-02 11:36:14 -070043 private final Alarm alarmMinimumFields = new DefaultAlarm.Builder(ALARM_ID,
kmcpeakeb172d5f2015-12-10 11:30:43 +000044 DeviceId.deviceId("of:2222000000000000"), "NE unreachable", Alarm.SeverityLevel.CLEARED, 1
Andrea Campanella65f9eb92017-05-02 11:36:14 -070045 ).build();
kmcpeake4fe18c82015-11-17 20:07:39 +000046
47 // Use this to check handling for fully populated Alarm
Andrea Campanella65f9eb92017-05-02 11:36:14 -070048 private final Alarm alarmWithSource = new DefaultAlarm.Builder(ALARM_ID,
kmcpeakeb172d5f2015-12-10 11:30:43 +000049 DeviceId.deviceId("of:2222000000000000"), "NE unreachable", Alarm.SeverityLevel.CLEARED, 1
Andrea Campanella65f9eb92017-05-02 11:36:14 -070050 ).forSource(AlarmEntityId.alarmEntityId("port:1/2/3/4")).withTimeUpdated(2).withTimeCleared(3L).
kmcpeakeb172d5f2015-12-10 11:30:43 +000051 withServiceAffecting(true).withAcknowledged(true).withManuallyClearable(true).
kmcpeake4fe18c82015-11-17 20:07:39 +000052 withAssignedUser("the assigned user").build();
53
54 @Test
55 public void alarmCodecTestWithOptionalFieldMissing() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000056 JsonCodec<Alarm> codec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000057 assertThat(codec, is(notNullValue()));
58
kmcpeakeb172d5f2015-12-10 11:30:43 +000059 ObjectNode alarmJson = codec.encode(alarmMinimumFields, context);
kmcpeake4fe18c82015-11-17 20:07:39 +000060 assertThat(alarmJson, notNullValue());
61 assertThat(alarmJson, matchesAlarm(alarmMinimumFields));
62
63 }
64
65 @Test
66 public void alarmCodecTestWithOptionalField() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 JsonCodec<Alarm> codec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000068 assertThat(codec, is(notNullValue()));
69
kmcpeakeb172d5f2015-12-10 11:30:43 +000070 ObjectNode alarmJson = codec.encode(alarmWithSource, context);
kmcpeake4fe18c82015-11-17 20:07:39 +000071 assertThat(alarmJson, notNullValue());
72 assertThat(alarmJson, matchesAlarm(alarmWithSource));
73
74 }
75
76 @Test
77 public void verifyMinimalAlarmIsEncoded() throws Exception {
kmcpeakeb172d5f2015-12-10 11:30:43 +000078 JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000079
kmcpeakeb172d5f2015-12-10 11:30:43 +000080 Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json");
kmcpeake4fe18c82015-11-17 20:07:39 +000081 assertCommon(alarm);
82
83 assertThat(alarm.timeCleared(), nullValue());
84 assertThat(alarm.assignedUser(), nullValue());
85
86 }
87
88 @Test
89 public void verifyFullyLoadedAlarmIsEncoded() throws Exception {
kmcpeakeb172d5f2015-12-10 11:30:43 +000090 JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000091
kmcpeakeb172d5f2015-12-10 11:30:43 +000092 Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json");
kmcpeake4fe18c82015-11-17 20:07:39 +000093 assertCommon(alarm);
94
95 assertThat(alarm.timeCleared(), is(2222L));
96 assertThat(alarm.assignedUser(), is("foo"));
97
98 }
99
kmcpeakea5404812015-12-08 11:52:50 +0000100 private void assertCommon(Alarm alarm) {
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700101 assertThat(alarm.id(), is(AlarmId.alarmId(DeviceId.deviceId("of:123"),
102 String.valueOf(10))));
kmcpeake4fe18c82015-11-17 20:07:39 +0000103 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
kmcpeakea5404812015-12-08 11:52:50 +0000118 * @throws IOException if processing the resource fails to decode
kmcpeake4fe18c82015-11-17 20:07:39 +0000119 */
kmcpeakea5404812015-12-08 11:52:50 +0000120 private Alarm getDecodedAlarm(JsonCodec<Alarm> codec, String resourceName) throws IOException {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000121 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 }
kmcpeake4fe18c82015-11-17 20:07:39 +0000129 }
130
kmcpeake4fe18c82015-11-17 20:07:39 +0000131}