Unit test to check that the REST error codes and catalog are properly defined
Change-Id: Ia5792c568944a3e2969de3f38ccbdcb948177b76
diff --git a/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java b/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java
index 4a8a765..fde6133 100644
--- a/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java
+++ b/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java
@@ -1,5 +1,6 @@
package net.onrc.onos.api.rest;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -32,7 +33,6 @@
new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_INVALID,
"Intent invalid",
"The intent provided is empty or invalid"),
-
};
/**
@@ -85,4 +85,13 @@
public static RestErrorCatalogEntry getRestError(final RestErrorCodes.RestErrorCode code) {
return getRestErrorMap().get(code.ordinal());
}
+
+ /**
+ * Fetch the array of catalog entries.
+ *
+ * @return array of REST error catalog entries currently in use
+ */
+ public static RestErrorCatalogEntry[] getCatalogEntries() {
+ return Arrays.copyOf(ERROR_LIST, ERROR_LIST.length);
+ }
}
diff --git a/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java
new file mode 100644
index 0000000..c97eba2
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java
@@ -0,0 +1,48 @@
+package net.onrc.onos.api.rest;
+
+import org.junit.Test;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * Tests to make sure that the error catalog is consistent.
+ */
+public class RestErrorCatalogTest {
+
+ /**
+ * Make sure that there are no duplicate entries in the catalog.
+ */
+ @Test
+ public void testNoDuplicatesInCatalog() {
+ final Set<RestErrorCodes.RestErrorCode> entriesSeen = new HashSet<>();
+ final RestErrorCatalogEntry[] rawEntries = RestErrorCatalog.getCatalogEntries();
+
+ for (final RestErrorCatalogEntry entry : rawEntries) {
+ // There should only be one entry for this code, if we have seen it
+ // before that's an error.
+ assertThat(entriesSeen, not(hasItem(entry.getCode())));
+
+ entriesSeen.add(entry.getCode());
+ }
+ }
+
+ /**
+ * Make sure that each REST error code has an entry in the catalog.
+ */
+ @Test
+ public void testAllCodesInCatalog() {
+
+ final Map<Integer, RestErrorCatalogEntry> catalogEntryMap =
+ RestErrorCatalog.getRestErrorMap();
+ for (final RestErrorCodes.RestErrorCode code : RestErrorCodes.RestErrorCode.values()) {
+ // There should be a RestErrorCatalogEntry for every code.
+ assertThat(catalogEntryMap.keySet(), hasItem(code.ordinal()));
+ }
+ }
+}