Fix PMD errors in REST API framework
Also refactored error catalog to get rid of a useless wrapper
class.
Change-Id: I0d4b190703b85cdea91da00e7f238538e7c115ec
diff --git a/src/main/java/net/onrc/onos/api/rest/RestError.java b/src/main/java/net/onrc/onos/api/rest/RestError.java
index dd61d26..548e3e5 100644
--- a/src/main/java/net/onrc/onos/api/rest/RestError.java
+++ b/src/main/java/net/onrc/onos/api/rest/RestError.java
@@ -8,7 +8,7 @@
*/
public final class RestError {
- private final RestErrorCodes.RestErrorCode code;
+ private final RestErrorCode code;
private final String summary;
private final String formattedDescription;
@@ -18,7 +18,7 @@
private RestError() {
// This is never called, but Java requires these initializations
// because the members are final.
- code = RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS;
+ code = RestErrorCode.INTENT_ALREADY_EXISTS;
summary = "";
formattedDescription = "";
}
@@ -30,7 +30,7 @@
* @param summary summary string for the new error
* @param formattedDescription formatted full description of the error
*/
- private RestError(final RestErrorCodes.RestErrorCode code,
+ private RestError(final RestErrorCode code,
final String summary,
final String formattedDescription) {
this.code = code;
@@ -43,7 +43,7 @@
*
* @return error code
*/
- public RestErrorCodes.RestErrorCode getCode() {
+ public RestErrorCode getCode() {
return code;
}
@@ -74,7 +74,7 @@
* the description
* @return new RestError representing this intance
*/
- public static RestError createRestError(final RestErrorCodes.RestErrorCode code,
+ public static RestError createRestError(final RestErrorCode code,
final Object... parameters) {
final RestErrorCatalogEntry error = RestErrorCatalog.getRestError(code);
final String formattedDescription =
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 fde6133..e1418fc 100644
--- a/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java
+++ b/src/main/java/net/onrc/onos/api/rest/RestErrorCatalog.java
@@ -11,37 +11,42 @@
public final class RestErrorCatalog {
/**
- * Hide the default constructor of a utility class.
- */
- private RestErrorCatalog() { }
-
- /**
* Static list of known errors. Someday this will be read in from an
* external file.
*/
private static final RestErrorCatalogEntry[] ERROR_LIST = {
- new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND,
+ new RestErrorCatalogEntry(RestErrorCode.INTENT_NOT_FOUND,
"Intent not found",
"An intent with the identifier {} was not found."),
- new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS,
+ new RestErrorCatalogEntry(RestErrorCode.INTENT_ALREADY_EXISTS,
"Intent already exists",
"An intent with the identifier {} could not be created " +
"because one already exists."),
- new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NO_PATH,
+ new RestErrorCatalogEntry(RestErrorCode.INTENT_NO_PATH,
"No path found",
"No path found between {} and {}"),
- new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_INVALID,
+ new RestErrorCatalogEntry(RestErrorCode.INTENT_INVALID,
"Intent invalid",
"The intent provided is empty or invalid"),
};
/**
+ * Hide the default constructor of a utility class.
+ */
+ private RestErrorCatalog() { }
+
+ /**
* Singleton implementation using the demand holder idiom.
*/
- private static class RestErrorMapHolder {
+ private static final class RestErrorMapHolder {
private static Map<Integer, RestErrorCatalogEntry> restErrorMap = initializeRestErrorMap();
/**
+ * Hide the default constructor.
+ */
+ private RestErrorMapHolder() {}
+
+ /**
* Load up the error map.
*
* @return REST error map
@@ -82,7 +87,7 @@
* @return the REST error for the code if one exists, null if it does not
* exist.
*/
- public static RestErrorCatalogEntry getRestError(final RestErrorCodes.RestErrorCode code) {
+ public static RestErrorCatalogEntry getRestError(final RestErrorCode code) {
return getRestErrorMap().get(code.ordinal());
}
diff --git a/src/main/java/net/onrc/onos/api/rest/RestErrorCatalogEntry.java b/src/main/java/net/onrc/onos/api/rest/RestErrorCatalogEntry.java
index 96b3eea..f4900a3 100644
--- a/src/main/java/net/onrc/onos/api/rest/RestErrorCatalogEntry.java
+++ b/src/main/java/net/onrc/onos/api/rest/RestErrorCatalogEntry.java
@@ -12,7 +12,7 @@
public final class RestErrorCatalogEntry {
- private final RestErrorCodes.RestErrorCode code;
+ private final RestErrorCode code;
private final String summary;
private final String descriptionFormatString;
@@ -23,7 +23,7 @@
* @param newSummary short summary for the new error
* @param newDescriptionFormatString formatable description for the new error
*/
- public RestErrorCatalogEntry(final RestErrorCodes.RestErrorCode newCode,
+ public RestErrorCatalogEntry(final RestErrorCode newCode,
final String newSummary,
final String newDescriptionFormatString) {
code = newCode;
@@ -45,7 +45,7 @@
*
* @return unique code
*/
- public RestErrorCodes.RestErrorCode getCode() {
+ public RestErrorCode getCode() {
return code;
}
diff --git a/src/main/java/net/onrc/onos/api/rest/RestErrorCode.java b/src/main/java/net/onrc/onos/api/rest/RestErrorCode.java
new file mode 100644
index 0000000..1ff9186
--- /dev/null
+++ b/src/main/java/net/onrc/onos/api/rest/RestErrorCode.java
@@ -0,0 +1,21 @@
+package net.onrc.onos.api.rest;
+
+/**
+ * Enumeration of the ONOS defined error codes.
+ */
+public enum RestErrorCode {
+
+ // TODO: These are made up just for testing purposes.
+
+ /** Intent not found. */
+ INTENT_NOT_FOUND,
+
+ /** Intent already exists. */
+ INTENT_ALREADY_EXISTS,
+
+ /** No path available for the Intent. */
+ INTENT_NO_PATH,
+
+ /** An object specified for an intent is invalid (parsing error). */
+ INTENT_INVALID
+}
diff --git a/src/main/java/net/onrc/onos/api/rest/RestErrorCodes.java b/src/main/java/net/onrc/onos/api/rest/RestErrorCodes.java
deleted file mode 100644
index 09626d2..0000000
--- a/src/main/java/net/onrc/onos/api/rest/RestErrorCodes.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package net.onrc.onos.api.rest;
-
-/**
- * Holder class for constants that describe the REST error codes.
- */
-public final class RestErrorCodes {
-
- /**
- * Hidden default constructor for utility class.
- */
- private RestErrorCodes() { }
-
- /**
- * Enumeration of the ONOS defined error codes.
- */
- public enum RestErrorCode {
-
- // TODO: These are made up just for testing purposes.
-
- /** Intent not found. */
- INTENT_NOT_FOUND,
-
- /** Intent already exists. */
- INTENT_ALREADY_EXISTS,
-
- /** No path available for the Intent. */
- INTENT_NO_PATH,
-
- /** An object specified for an intent is invalid (parsing error). */
- INTENT_INVALID
- }
-
-}
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
index 6f1c098..f5239c1 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
@@ -4,7 +4,7 @@
import java.util.List;
import net.onrc.onos.api.rest.RestError;
-import net.onrc.onos.api.rest.RestErrorCodes;
+import net.onrc.onos.api.rest.RestErrorCode;
import net.onrc.onos.core.intent.Intent;
import net.onrc.onos.core.intent.IntentMap;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
@@ -49,7 +49,7 @@
} else {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
final RestError notFound =
- RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND,
+ RestError.createRestError(RestErrorCode.INTENT_NOT_FOUND,
applnIntentId);
result = toRepresentation(notFound, null);
}
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
index 2406674..e097311 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
@@ -7,7 +7,7 @@
import net.onrc.onos.api.intent.ApplicationIntent;
import net.onrc.onos.api.rest.RestError;
-import net.onrc.onos.api.rest.RestErrorCodes;
+import net.onrc.onos.api.rest.RestErrorCode;
import net.onrc.onos.core.intent.Intent;
import net.onrc.onos.core.intent.IntentMap;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
@@ -75,7 +75,7 @@
if (addOperations == null) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
final RestError error =
- RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_INVALID);
+ RestError.createRestError(RestErrorCode.INTENT_INVALID);
return toRepresentation(error, null);
}
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
index 031acf1..b6f56f3 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
@@ -1,7 +1,7 @@
package net.onrc.onos.core.intent.runtime.web;
import net.onrc.onos.api.rest.RestError;
-import net.onrc.onos.api.rest.RestErrorCodes;
+import net.onrc.onos.api.rest.RestErrorCode;
import net.onrc.onos.core.intent.Intent;
import net.onrc.onos.core.intent.IntentMap;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
@@ -45,7 +45,7 @@
} else {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
final RestError notFound =
- RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND,
+ RestError.createRestError(RestErrorCode.INTENT_NOT_FOUND,
applnIntentId);
result = toRepresentation(notFound, null);
}
diff --git a/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogEntryTest.java b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogEntryTest.java
index 4141b78..c435668 100644
--- a/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogEntryTest.java
+++ b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogEntryTest.java
@@ -22,11 +22,11 @@
@Test
public void testRestErrorFormatting1Parameter() {
final RestErrorCatalogEntry restErrorCatalogEntry =
- RestErrorCatalog.getRestError(RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS);
+ RestErrorCatalog.getRestError(RestErrorCode.INTENT_ALREADY_EXISTS);
assertThat(restErrorCatalogEntry, is(notNullValue()));
final RestError formattedError =
- RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS,
+ RestError.createRestError(RestErrorCode.INTENT_ALREADY_EXISTS,
"INTENT-ID");
final String formattedErrorString =
formattedError.getFormattedDescription();
@@ -45,11 +45,11 @@
@Test
public void testRestErrorFormatting2Parameters() {
final RestErrorCatalogEntry restErrorCatalogEntry =
- RestErrorCatalog.getRestError(RestErrorCodes.RestErrorCode.INTENT_NO_PATH);
+ RestErrorCatalog.getRestError(RestErrorCode.INTENT_NO_PATH);
assertThat(restErrorCatalogEntry, is(notNullValue()));
final RestError formattedError =
- RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NO_PATH,
+ RestError.createRestError(RestErrorCode.INTENT_NO_PATH,
"Switch1", "Switch2");
final String formattedErrorString =
formattedError.getFormattedDescription();
@@ -78,14 +78,6 @@
}
/**
- * Make sure that the error codes is a utility class.
- */
- @Test
- public void assureThatErrorCodesIsUtility() {
- assertThatClassIsUtility(RestErrorCodes.class);
- }
-
- /**
* Make sure that the RestErrorCatalogEntry class is immutable.
*/
@Test
diff --git a/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java
index c97eba2..851ee3d 100644
--- a/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java
+++ b/src/test/java/net/onrc/onos/api/rest/RestErrorCatalogTest.java
@@ -20,7 +20,7 @@
*/
@Test
public void testNoDuplicatesInCatalog() {
- final Set<RestErrorCodes.RestErrorCode> entriesSeen = new HashSet<>();
+ final Set<RestErrorCode> entriesSeen = new HashSet<>();
final RestErrorCatalogEntry[] rawEntries = RestErrorCatalog.getCatalogEntries();
for (final RestErrorCatalogEntry entry : rawEntries) {
@@ -40,7 +40,7 @@
final Map<Integer, RestErrorCatalogEntry> catalogEntryMap =
RestErrorCatalog.getRestErrorMap();
- for (final RestErrorCodes.RestErrorCode code : RestErrorCodes.RestErrorCode.values()) {
+ for (final RestErrorCode code : RestErrorCode.values()) {
// There should be a RestErrorCatalogEntry for every code.
assertThat(catalogEntryMap.keySet(), hasItem(code.ordinal()));
}