You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ad...@apache.org on 2014/10/24 18:08:42 UTC

[08/21] git commit: Cleanup output-only Error object.

Cleanup output-only Error object.


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/7919efab
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/7919efab
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/7919efab

Branch: refs/heads/1.8.x
Commit: 7919efab91ecec6e6278326acd14957e6e2e0dbb
Parents: 34b4fc2
Author: Adrian Cole <ac...@twitter.com>
Authored: Thu Oct 16 19:59:06 2014 -0700
Committer: Adrian Cole <ad...@apache.org>
Committed: Fri Oct 24 08:42:15 2014 -0700

----------------------------------------------------------------------
 .../org/jclouds/azurecompute/domain/Error.java  | 201 +++----------------
 .../jclouds/azurecompute/xml/ErrorHandler.java  |  41 ++--
 .../jclouds/azurecompute/parse/ErrorTest.java   |  50 -----
 .../azurecompute/parse/GetOperationTest.java    |  14 +-
 .../azurecompute/xml/ErrorHandlerTest.java      |  42 ++++
 5 files changed, 96 insertions(+), 252 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/7919efab/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Error.java
----------------------------------------------------------------------
diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Error.java b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Error.java
index d25a046..1b529d0 100644
--- a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Error.java
+++ b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/Error.java
@@ -16,224 +16,77 @@
  */
 package org.jclouds.azurecompute.domain;
 
-import com.google.common.base.CaseFormat;
-import com.google.common.base.Objects;
-
+import static com.google.common.base.Objects.equal;
+import static com.google.common.base.Objects.toStringHelper;
 import static com.google.common.base.Preconditions.checkNotNull;
 
+import com.google.common.base.Objects;
+
 /**
- * additional error information that is defined by the management service. Th
- *
  * @see <a href="http://msdn.microsoft.com/en-us/library/ee460801" >api</a>
  */
-public class Error {
-
+public final class Error {
    public static enum Code {
-
-      /**
-       * Bad Request (400)
-       *
-       * The versioning header is not specified or was specified incorrectly.
-       */
       MISSING_OR_INCORRECT_VERSION_HEADER,
-
-      /**
-       * Bad Request (400)
-       *
-       * The request body’s XML was invalid or not correctly specified.
-       */
       INVALID_XML_REQUEST,
-
-      /**
-       * Bad Request (400)
-       *
-       * A required query parameter was not specified for this request or was specified incorrectly.
-       */
       MISSING_OR_INVALID_REQUIRED_QUERY_PARAMETER,
-
-      /**
-       * Bad Request (400)
-       *
-       * The HTTP verb specified was not recognized by the server or isn’t valid for this resource.
-       */
       INVALID_HTTP_VERB,
-
-      /**
-       * Forbidden (403)
-       *
-       * The server failed to authenticate the request. Verify that the certificate is valid and is
-       * associated with this subscription.
-       */
       AUTHENTICATION_FAILED,
-
-      /**
-       * Not Found (404)
-       *
-       * The specified resource does not exist.
-       */
       RESOURCE_NOT_FOUND,
-
-      /**
-       * Internal Server Error (500)
-       *
-       * The server encountered an internal error. Please retry the request.
-       */
       INTERNAL_ERROR,
-
-      /**
-       * Internal Server Error (500)
-       *
-       * The operation could not be completed within the permitted time.
-       */
       OPERATION_TIMED_OUT,
-
-      /**
-       * Service Unavailable (503)
-       *
-       * The server (or an internal component) is currently unavailable to receive requests. Please
-       * retry your request
-       */
       SERVER_BUSY,
-
-      /**
-       * Forbidden (403)
-       *
-       * The subscription is in a disabled state.
-       */
       SUBSCRIPTION_DISABLED,
-
-      /**
-       * Bad Request (400)
-       *
-       * A parameter was incorrect.
-       */
       BAD_REQUEST,
-
-      /**
-       * Conflict (409)
-       *
-       * A conflict occurred to prevent the operation from completing.
-       */
       CONFLICT_ERROR,
-
       UNRECOGNIZED;
-
-      public String value() {
-         return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name());
-      }
-
-      @Override
-      public String toString() {
-         return value();
-      }
-
-      public static Code fromValue(String code) {
-         try {
-            return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(code, "code")));
-         } catch (IllegalArgumentException e) {
-            return UNRECOGNIZED;
-         }
-      }
    }
 
-   public static Builder builder() {
-      return new Builder();
+   /** Error code */
+   public Code getCode() {
+      return code;
    }
 
-   public Builder toBuilder() {
-      return builder().fromError(this);
+   /** User message */
+   public String message() {
+      return message;
    }
 
-   public static class Builder {
-
-      private String rawCode;
-      private Code code;
-      private String message;
-
-      /**
-       * @see Error#getRawCode()
-       */
-      public Builder rawCode(String rawCode) {
-         this.rawCode = rawCode;
-         return this;
-      }
-
-      /**
-       * @see Error#getCode()
-       */
-      public Builder code(Code code) {
-         this.code = code;
-         return this;
-      }
-
-      /**
-       * @see Error#getMessage()
-       */
-      public Builder message(String message) {
-         this.message = message;
-         return this;
-      }
-
-      public Error build() {
-         return new Error(rawCode, code, message);
-      }
-
-      public Builder fromError(Error in) {
-         return this.rawCode(in.rawCode).code(in.code).message(in.message);
-      }
+   public static Error create(Code code, String message) {
+      return new Error(code, message);
    }
 
-   private final String rawCode;
-   private final Code code;
-   private final String message;
-
-   protected Error(String rawCode, Code code, String message) {
-      this.rawCode = checkNotNull(rawCode, "rawCode for %s", message);
-      this.code = checkNotNull(code, "code for %s", message);
+   // TODO: Remove from here down with @AutoValue.
+   private Error(Code code, String message) {
+      this.code = checkNotNull(code, "code");
       this.message = checkNotNull(message, "message");
    }
 
-   /**
-    * Error code
-    */
-   public Code getCode() {
-      return code;
-   }
-
-   /**
-    * Error code, unparsed
-    */
-   public String getRawCode() {
-      return rawCode;
-   }
-
-   /**
-    * User message
-    */
-   public String getMessage() {
-      return message;
-   }
+   private final Code code;
+   private final String message;
 
    @Override
    public int hashCode() {
-      return Objects.hashCode(rawCode, code, message);
+      return Objects.hashCode(code, message);
    }
 
    @Override
    public boolean equals(Object obj) {
-      if (this == obj)
+      if (this == obj) {
          return true;
-      if (obj == null)
+      }
+      if (obj == null) {
          return false;
-      if (getClass() != obj.getClass())
+      }
+      if (getClass() != obj.getClass()) {
          return false;
+      }
       Error other = (Error) obj;
-      return Objects.equal(this.rawCode, other.rawCode) && Objects.equal(this.code, other.code)
-               && Objects.equal(this.message, other.message);
+      return equal(this.code, other.code) && equal(this.message, other.message);
    }
 
    @Override
    public String toString() {
-      return Objects.toStringHelper(this).omitNullValues().add("code", rawCode).add("message", message).toString();
+      return toStringHelper(this).add("code", code).add("message", message).toString();
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/7919efab/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ErrorHandler.java
----------------------------------------------------------------------
diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ErrorHandler.java b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ErrorHandler.java
index 04dca23..2d49bbc 100644
--- a/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ErrorHandler.java
+++ b/azurecompute/src/main/java/org/jclouds/azurecompute/xml/ErrorHandler.java
@@ -16,43 +16,46 @@
  */
 package org.jclouds.azurecompute.xml;
 
+import static com.google.common.base.CaseFormat.UPPER_CAMEL;
+import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
+import static org.jclouds.util.SaxUtils.currentOrNull;
+
 import org.jclouds.azurecompute.domain.Error;
 import org.jclouds.azurecompute.domain.Error.Code;
 import org.jclouds.http.functions.ParseSax;
-import org.jclouds.util.SaxUtils;
-import org.xml.sax.SAXException;
 
 /**
  * @see <a href="http://msdn.microsoft.com/en-us/library/ee460801" >api</a>
  */
-public class ErrorHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Error> {
+public final class ErrorHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Error> {
+   private Code code;
+   private String message;
 
    private StringBuilder currentText = new StringBuilder();
-   private Error.Builder builder = Error.builder();
 
-   @Override
-   public Error getResult() {
-      try {
-         return builder.build();
-      } finally {
-         builder = Error.builder();
-      }
+   @Override public Error getResult() {
+      return Error.create(code, message);
    }
 
-   @Override
-   public void endElement(String uri, String name, String qName) throws SAXException {
+   @Override public void endElement(String ignoredUri, String ignoredName, String qName) {
       if (qName.equals("Code")) {
-         String rawCode = SaxUtils.currentOrNull(currentText);
-         builder.rawCode(rawCode);
-         builder.code(Code.fromValue(rawCode));
+         String codeText = currentOrNull(currentText);
+         code = parseCode(codeText);
       } else if (qName.equals("Message")) {
-         builder.message(SaxUtils.currentOrNull(currentText));
+         message = currentOrNull(currentText);
       }
       currentText = new StringBuilder();
    }
 
-   @Override
-   public void characters(char ch[], int start, int length) {
+   @Override public void characters(char ch[], int start, int length) {
       currentText.append(ch, start, length);
    }
+
+   private static Code parseCode(String code) {
+      try {
+         return Code.valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, code));
+      } catch (IllegalArgumentException e) {
+         return Code.UNRECOGNIZED;
+      }
+   }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/7919efab/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ErrorTest.java
----------------------------------------------------------------------
diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ErrorTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ErrorTest.java
deleted file mode 100644
index 12d38c6..0000000
--- a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/ErrorTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.jclouds.azurecompute.parse;
-
-import java.io.InputStream;
-import org.jclouds.azurecompute.domain.Error;
-import org.jclouds.azurecompute.domain.Error.Code;
-import org.jclouds.azurecompute.xml.ErrorHandler;
-import org.jclouds.http.functions.BaseHandlerTest;
-import org.testng.annotations.Test;
-
-import static org.testng.Assert.assertEquals;
-
-@Test(groups = "unit", testName = "ErrorTest")
-public class ErrorTest extends BaseHandlerTest {
-
-   public void test() {
-      InputStream is = getClass().getResourceAsStream("/error.xml");
-
-      Error expected = expected();
-
-      ErrorHandler handler = injector.getInstance(ErrorHandler.class);
-      Error result = factory.create(handler).parse(is);
-
-      assertEquals(result.toString(), expected.toString());
-
-   }
-
-   public Error expected() {
-      return Error.builder()
-                  .rawCode("MissingOrInvalidRequiredQueryParameter")
-                  .code(Code.MISSING_OR_INVALID_REQUIRED_QUERY_PARAMETER)
-                  .message("A required query parameter was not specified for this request or was specified incorrectly.")
-                  .build();
-   }
-}

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/7919efab/azurecompute/src/test/java/org/jclouds/azurecompute/parse/GetOperationTest.java
----------------------------------------------------------------------
diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/GetOperationTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/parse/GetOperationTest.java
index ecf7083..8e41f81 100644
--- a/azurecompute/src/test/java/org/jclouds/azurecompute/parse/GetOperationTest.java
+++ b/azurecompute/src/test/java/org/jclouds/azurecompute/parse/GetOperationTest.java
@@ -16,17 +16,17 @@
  */
 package org.jclouds.azurecompute.parse;
 
+import static org.testng.Assert.assertEquals;
+
 import java.io.InputStream;
-import org.jclouds.azurecompute.domain.Error;
-import org.jclouds.azurecompute.domain.Error.Code;
+
 import org.jclouds.azurecompute.domain.Operation;
 import org.jclouds.azurecompute.domain.Operation.Status;
+import org.jclouds.azurecompute.xml.ErrorHandlerTest;
 import org.jclouds.azurecompute.xml.OperationHandler;
 import org.jclouds.http.functions.BaseHandlerTest;
 import org.testng.annotations.Test;
 
-import static org.testng.Assert.assertEquals;
-
 @Test(groups = "unit", testName = "GetOperationTest")
 public class GetOperationTest extends BaseHandlerTest {
 
@@ -47,11 +47,7 @@ public class GetOperationTest extends BaseHandlerTest {
                       .rawStatus("Failed")
                       .status(Status.FAILED)
                       .httpStatusCode(400)
-                      .error(Error.builder()
-                                  .rawCode("MissingOrInvalidRequiredQueryParameter")
-                                  .code(Code.MISSING_OR_INVALID_REQUIRED_QUERY_PARAMETER)
-                                  .message("A required query parameter was not specified for this request or was specified incorrectly.")
-                                  .build())
+                      .error(ErrorHandlerTest.expected())
                       .build();
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/7919efab/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ErrorHandlerTest.java
----------------------------------------------------------------------
diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ErrorHandlerTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ErrorHandlerTest.java
new file mode 100644
index 0000000..3c9a74b
--- /dev/null
+++ b/azurecompute/src/test/java/org/jclouds/azurecompute/xml/ErrorHandlerTest.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jclouds.azurecompute.xml;
+
+import static org.testng.Assert.assertEquals;
+
+import java.io.InputStream;
+
+import org.jclouds.azurecompute.domain.Error;
+import org.jclouds.azurecompute.domain.Error.Code;
+import org.jclouds.http.functions.BaseHandlerTest;
+import org.testng.annotations.Test;
+
+@Test(groups = "unit", testName = "ErrorHandlerTest")
+public class ErrorHandlerTest extends BaseHandlerTest {
+
+   public void test() {
+      InputStream is = getClass().getResourceAsStream("/error.xml");
+      Error result = factory.create(new ErrorHandler()).parse(is);
+
+      assertEquals(result, expected());
+   }
+
+   public static Error expected() {
+      return Error.create(Code.MISSING_OR_INVALID_REQUIRED_QUERY_PARAMETER,
+            "A required query parameter was not specified for this request or was specified incorrectly.");
+   }
+}