blob: 3009b99aaa2c30de16ab15fe4149a0cd2849d2f7 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
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 */
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();
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}