olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [21/26] olingo-odata4 git commit: [OLINGO-652] FIx error message issue
Date Wed, 10 Jun 2015 20:16:35 GMT
[OLINGO-652] FIx error message issue


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/60f896a9
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/60f896a9
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/60f896a9

Branch: refs/heads/OLINGO-642_OSGi-Sample
Commit: 60f896a9335601debef5ff3071c22565b09a4ed0
Parents: c35076f
Author: Christian Amend <christian.amend@sap.com>
Authored: Mon Jun 8 15:34:25 2015 +0200
Committer: Christian Amend <christian.amend@sap.com>
Committed: Mon Jun 8 15:34:25 2015 +0200

----------------------------------------------------------------------
 .../server/core/ODataExceptionHelper.java       |  13 +-
 .../olingo/server/core/ExceptionHelperTest.java | 137 +++++++++++++++++++
 2 files changed, 145 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/60f896a9/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
index 417dde2..d6ed039 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
@@ -54,8 +54,8 @@ public class ODataExceptionHelper {
     return serverError;
   }
 
-  public static ODataServerError
-      createServerErrorObject(final UriParserSyntaxException e, final Locale requestedLocale)
{
+  public static ODataServerError createServerErrorObject(final UriParserSyntaxException e,
+      final Locale requestedLocale) {
     ODataServerError serverError = basicTranslatedError(e, requestedLocale);
     serverError.setStatusCode(
         UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT.equals(e.getMessageKey())
?
@@ -115,8 +115,7 @@ public class ODataExceptionHelper {
     return serverError;
   }
 
-  public static ODataServerError
-      createServerErrorObject(final ODataLibraryException e, final Locale requestedLocale)
{
+  public static ODataServerError createServerErrorObject(final ODataLibraryException e, final
Locale requestedLocale) {
     return basicTranslatedError(e, requestedLocale);
   }
 
@@ -136,7 +135,11 @@ public class ODataExceptionHelper {
   }
 
   private static ODataServerError basicServerError(final Exception e) {
-    return new ODataServerError().setException(e).setMessage(e.getMessage());
+    ODataServerError serverError = new ODataServerError().setException(e).setMessage(e.getMessage());
+    if (serverError.getMessage() == null) {
+      serverError.setMessage("OData Library: An exception without message text was thrown.");
+    }
+    return serverError;
   }
 
   private static ODataServerError basicTranslatedError(final ODataLibraryException e,

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/60f896a9/lib/server-core/src/test/java/org/apache/olingo/server/core/ExceptionHelperTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ExceptionHelperTest.java
b/lib/server-core/src/test/java/org/apache/olingo/server/core/ExceptionHelperTest.java
new file mode 100644
index 0000000..8b3a853
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ExceptionHelperTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.apache.olingo.server.core;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.apache.olingo.commons.api.http.HttpStatusCode;
+import org.apache.olingo.server.api.ODataLibraryException.MessageKey;
+import org.apache.olingo.server.api.ODataServerError;
+import org.apache.olingo.server.api.deserializer.DeserializerException;
+import org.apache.olingo.server.api.etag.PreconditionException;
+import org.apache.olingo.server.api.serializer.SerializerException;
+import org.apache.olingo.server.core.uri.validator.UriValidationException;
+import org.junit.Test;
+
+public class ExceptionHelperTest {
+
+  private static final String DEV_MSG = "devMsg";
+
+  @Test
+  public void withRuntimeException() {
+    try {
+      throw new NullPointerException();
+    } catch (NullPointerException e) {
+      ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e);
+      assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), serverError.getStatusCode());
+      assertEquals("OData Library: An exception without message text was thrown.", serverError.getMessage());
+      assertEquals(e, serverError.getException());
+    }
+  }
+
+  @Test
+  public void withRuntimeExceptionAndText() {
+    try {
+      throw new NullPointerException("Text");
+    } catch (NullPointerException e) {
+      ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e);
+      assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), serverError.getStatusCode());
+      assertEquals("Text", serverError.getMessage());
+      assertEquals(e, serverError.getException());
+    }
+  }
+
+  @Test
+  public void uriValidatorExceptionMustLeadTo400() {
+    for (MessageKey key : UriValidationException.MessageKeys.values()) {
+      try {
+        throw new UriValidationException(DEV_MSG, key);
+      } catch (UriValidationException e) {
+        ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
+        assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.BAD_REQUEST.getStatusCode(),
+            serverError.getStatusCode());
+      }
+    }
+  }
+
+  @Test
+  public void deserializerExceptionMustLeadTo400() {
+    for (MessageKey key : DeserializerException.MessageKeys.values()) {
+      try {
+        throw new DeserializerException(DEV_MSG, key);
+      } catch (DeserializerException e) {
+        ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
+        assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.BAD_REQUEST.getStatusCode(),
+            serverError.getStatusCode());
+      }
+    }
+  }
+
+  @Test
+  public void serializerExceptionMustLeadTo400() {
+    for (MessageKey key : SerializerException.MessageKeys.values()) {
+      try {
+        throw new SerializerException(DEV_MSG, key);
+      } catch (SerializerException e) {
+        ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
+        assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.BAD_REQUEST.getStatusCode(),
+            serverError.getStatusCode());
+      }
+    }
+  }
+
+  @Test
+  public void contentNegotiatorExceptionMustLeadTo406() {
+    for (MessageKey key : ContentNegotiatorException.MessageKeys.values()) {
+      try {
+        throw new ContentNegotiatorException(DEV_MSG, key);
+      } catch (ContentNegotiatorException e) {
+        ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
+        assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(),
+            serverError.getStatusCode());
+      }
+    }
+  }
+
+  @Test
+  public void preconditionRequiredTesting() {
+    for (MessageKey key : PreconditionException.MessageKeys.values()) {
+      try {
+        throw new PreconditionException(DEV_MSG, key);
+      } catch (PreconditionException e) {
+        if (e.getMessageKey().equals(PreconditionException.MessageKeys.FAILED)) {
+          ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e,
null);
+          assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.PRECONDITION_FAILED.getStatusCode(),
+              serverError.getStatusCode());
+        } else if (e.getMessageKey().equals(PreconditionException.MessageKeys.MISSING_HEADER))
{
+          ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e,
null);
+          assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.PRECONDITION_REQUIRED.getStatusCode(),
+              serverError.getStatusCode());
+        } else if (e.getMessageKey().equals(PreconditionException.MessageKeys.INVALID_URI))
{
+          ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e,
null);
+          assertEquals("FailedKey: " + e.getMessageKey().getKey(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
+              serverError.getStatusCode());
+        } else {
+          fail("Unexpected message key for: " + e.getClass().getName());
+        }
+      }
+    }
+  }
+}


Mime
View raw message