aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org')
-rw-r--r--framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java73
-rw-r--r--framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java140
-rw-r--r--framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java135
-rw-r--r--framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java72
4 files changed, 420 insertions, 0 deletions
diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java
new file mode 100644
index 00000000..89886e8a
--- /dev/null
+++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.faultmanagement.web;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+
+/**
+ * Mock codec context for use in codec unit tests.
+ */
+public class AlarmCodecContext implements CodecContext {
+
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
+
+ /**
+ * Constructs a new mock codec context.
+ */
+ public AlarmCodecContext() {
+ codecs.clear();
+ registerCodec(Alarm.class, new AlarmCodec());
+
+ }
+
+ @Override
+ public ObjectMapper mapper() {
+ return mapper;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getService(Class<T> serviceClass) {
+ // TODO
+ return null;
+ }
+
+ /**
+ * Registers the specified JSON codec for the given entity class.
+ *
+ * @param entityClass entity class
+ * @param codec JSON codec
+ * @param <T> entity type
+ */
+ public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
+ codecs.putIfAbsent(entityClass, codec);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> JsonCodec<T> codec(Class<T> entityClass) {
+ return codecs.get(entityClass);
+ }
+}
diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java
new file mode 100644
index 00000000..3009b99a
--- /dev/null
+++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.faultmanagement.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.IOException;
+import java.io.InputStream;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import static org.onosproject.faultmanagement.web.AlarmJsonMatcher.matchesAlarm;
+import org.onosproject.net.DeviceId;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+
+public class AlarmCodecTest {
+
+ private final AlarmCodecContext context = new AlarmCodecContext();
+
+ // Use this to check handling for miminal Alarm
+ private final Alarm alarmMinimumFields = new DefaultAlarm.Builder(
+ new AlarmId(44),
+ DeviceId.deviceId("of:2222000000000000"),
+ "NE unreachable",
+ Alarm.SeverityLevel.CLEARED,
+ 1).
+ build();
+
+ // Use this to check handling for fully populated Alarm
+ private final Alarm alarmWithSource = new DefaultAlarm.Builder(
+ new AlarmId(44),
+ DeviceId.deviceId("of:2222000000000000"),
+ "NE unreachable",
+ Alarm.SeverityLevel.CLEARED, 1).
+ forSource(AlarmEntityId.alarmEntityId("port:1/2/3/4")).
+ withTimeUpdated(2).
+ withTimeCleared(3L).
+ withServiceAffecting(true).
+ withAcknowledged(true).
+ withManuallyClearable(true).
+ withAssignedUser("the assigned user").build();
+
+ @Test
+ public void alarmCodecTestWithOptionalFieldMissing() {
+ //context.registerService(AlarmService.class, new AlarmServiceAdapter());
+ final JsonCodec<Alarm> codec = context.codec(Alarm.class);
+ assertThat(codec, is(notNullValue()));
+
+ final ObjectNode alarmJson = codec.encode(alarmMinimumFields, context);
+ assertThat(alarmJson, notNullValue());
+ assertThat(alarmJson, matchesAlarm(alarmMinimumFields));
+
+ }
+
+ @Test
+ public void alarmCodecTestWithOptionalField() {
+ final JsonCodec<Alarm> codec = context.codec(Alarm.class);
+ assertThat(codec, is(notNullValue()));
+
+ final ObjectNode alarmJson = codec.encode(alarmWithSource, context);
+ assertThat(alarmJson, notNullValue());
+ assertThat(alarmJson, matchesAlarm(alarmWithSource));
+
+ }
+
+ @Test
+ public void verifyMinimalAlarmIsEncoded() throws Exception {
+ final JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
+
+ final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json");
+ assertCommon(alarm);
+
+ assertThat(alarm.timeCleared(), nullValue());
+ assertThat(alarm.assignedUser(), nullValue());
+
+ }
+
+ @Test
+ public void verifyFullyLoadedAlarmIsEncoded() throws Exception {
+ final JsonCodec<Alarm> alarmCodec = context.codec(Alarm.class);
+
+ final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json");
+ assertCommon(alarm);
+
+ assertThat(alarm.timeCleared(), is(2222L));
+ assertThat(alarm.assignedUser(), is("foo"));
+
+ }
+
+ private void assertCommon(final Alarm alarm) {
+ assertThat(alarm.id(), is(new AlarmId(10L)));
+ assertThat(alarm.description(), is("NE is not reachable"));
+ assertThat(alarm.source(), is(AlarmEntityId.NONE));
+ assertThat(alarm.timeRaised(), is(999L));
+ assertThat(alarm.timeUpdated(), is(1111L));
+ assertThat(alarm.severity(), is(Alarm.SeverityLevel.MAJOR));
+ assertThat(alarm.serviceAffecting(), is(true));
+ assertThat(alarm.acknowledged(), is(false));
+ assertThat(alarm.manuallyClearable(), is(true));
+ }
+
+ /**
+ * Reads in a rule from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded flow rule
+ * @throws IOException if processing the resource failsdecode
+ */
+ private Alarm getDecodedAlarm(final JsonCodec<Alarm> codec, final String resourceName) throws IOException {
+ final InputStream jsonStream = AlarmCodecTest.class
+ .getResourceAsStream(resourceName);
+ final JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ final Alarm result = codec.decode((ObjectNode) json, context);
+ assertThat(result, notNullValue());
+ return result;
+ }
+
+
+}
diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java
new file mode 100644
index 00000000..14bb45f3
--- /dev/null
+++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.faultmanagement.web;
+
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Hamcrest matcher for alarms.
+ */
+public final class AlarmJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final Alarm alarm;
+
+ private AlarmJsonMatcher(final Alarm alarm) {
+ this.alarm = alarm;
+ }
+
+ @Override
+ public boolean matchesSafely(final JsonNode jsonAlarm, final Description description) {
+ final String jsonAlarmId = jsonAlarm.get("id").asText();
+ final String alarmId = Long.toString(alarm.id().fingerprint());
+ if (!jsonAlarmId.equals(alarmId)) {
+ description.appendText("alarm id was " + jsonAlarmId);
+ return false;
+ }
+
+ final String jsonDeviceId = jsonAlarm.get("deviceId").asText();
+ final String alarmDeviceId = alarm.deviceId().toString();
+ if (!jsonDeviceId.equals(alarmDeviceId)) {
+ description.appendText("DeviceId was " + jsonDeviceId);
+ return false;
+ }
+
+
+ final String jsonDescription = jsonAlarm.get("description").asText();
+ final String alarmDesc = alarm.description();
+ if (!jsonDescription.equals(alarmDesc)) {
+ description.appendText("description was " + jsonDescription);
+ return false;
+ }
+
+ final long jsonTimeRaised = jsonAlarm.get("timeRaised").asLong();
+ final long timeRaised = alarm.timeRaised();
+ if (timeRaised != jsonTimeRaised) {
+ description.appendText("timeRaised was " + jsonTimeRaised);
+ return false;
+ }
+
+
+ final long jsonTimeUpdated = jsonAlarm.get("timeUpdated").asLong();
+ final long timeUpdated = alarm.timeUpdated();
+ if (timeUpdated != jsonTimeUpdated) {
+ description.appendText("timeUpdated was " + jsonTimeUpdated);
+ return false;
+ }
+
+ final JsonNode jsonTimeClearedNode = jsonAlarm.get("timeCleared");
+
+ if (alarm.timeCleared() != null) {
+ final Long jsonTimeCleared = jsonTimeClearedNode.longValue();
+ final Long timeCleared = alarm.timeCleared();
+
+ if (!timeCleared.equals(jsonTimeCleared)) {
+ description.appendText("Time Cleared was " + jsonTimeCleared);
+ return false;
+ }
+ } else {
+ // No clear time not specified, JSON representation must be empty
+ if (!jsonTimeClearedNode.isNull()) {
+ description.appendText("Time Cleared should be null");
+ return false;
+ }
+ }
+
+ final String jsonSeverity = jsonAlarm.get("severity").asText();
+ final String severity = alarm.severity().toString();
+ if (!severity.equals(jsonSeverity)) {
+ description.appendText("severity was " + jsonSeverity);
+ return false;
+ }
+
+ final JsonNode jsonAlarmNode = jsonAlarm.get("source");
+
+ if (alarm.source() != null) {
+ final String jsonSource = jsonAlarmNode.textValue();
+ final String source = alarm.source().toString();
+
+ if (!source.equals(jsonSource)) {
+ description.appendText("source was " + jsonSource);
+ return false;
+ }
+ } else {
+ // source not specified, JSON representation must be empty
+ if (!jsonAlarmNode.isNull()) {
+ description.appendText("source should be null");
+ return false;
+ }
+ }
+
+ // In progress
+ return true;
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText(alarm.toString());
+ }
+
+ /**
+ * Factory to allocate a alarm matcher.
+ *
+ * @param alarm alarm object we are looking for
+ * @return matcher
+ */
+ public static AlarmJsonMatcher matchesAlarm(final Alarm alarm) {
+ return new AlarmJsonMatcher(alarm);
+ }
+}
diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java
new file mode 100644
index 00000000..0b7d9811
--- /dev/null
+++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.faultmanagement.web;
+
+import com.sun.jersey.api.client.WebResource;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+import org.junit.Ignore;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.rest.ResourceTest;
+
+/**
+ * Test of the Fault Management Web REST API for Alarms.
+ */
+public class AlarmsWebResourceTest extends ResourceTest {
+
+ @Before
+ public void setUp() {
+
+ final CodecManager codecService = new CodecManager();
+ codecService.activate();
+
+ final ServiceDirectory testDirectory
+ = new TestServiceDirectory()
+ // Currently no alarms-service implemented
+ // .add(AlarmsService.class, alarmsService)
+ .add(CodecService.class, codecService);
+ BaseResource.setServiceDirectory(testDirectory);
+ }
+
+ @Test
+ @Ignore
+ public void getAllAlarms() {
+ final WebResource rs = resource();
+ final String response = rs.path("/alarms").get(String.class);
+ // Ensure hard-coded alarms returned okay
+ assertThat(response, containsString("\"NE is not reachable\","));
+ assertThat(response, containsString("\"Equipment Missing\","));
+ }
+
+ @Test
+ @Ignore
+ public void getAlarm() {
+ final WebResource rs = resource();
+ final String response = rs.path("/alarms/1").get(String.class);
+ // Ensure hard-coded alarms returned okay
+ assertThat(response, containsString("\"NE is not reachable\","));
+ assertThat(response, not(containsString("\"Equipment Missing\",")));
+ }
+
+}