Repository: olingo-odata2
Updated Branches:
  refs/heads/master 7961a940c -> a9f2e4156


[OLINGO-1203] Locale information is not set to error context


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

Branch: refs/heads/master
Commit: a9f2e41567c98d9335c38c5ea5e1a2739a4aa787
Parents: 7961a94
Author: ramya vasanth <[email protected]>
Authored: Fri Nov 17 09:59:20 2017 +0530
Committer: ramya vasanth <[email protected]>
Committed: Fri Nov 17 09:59:20 2017 +0530

----------------------------------------------------------------------
 .../odata2/core/rest/ODataExceptionWrapper.java |  1 +
 .../odata2/core/ODataExceptionWrapperTest.java  | 97 ++++++++++++++++++++
 2 files changed, 98 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a9f2e415/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
----------------------------------------------------------------------
diff --git 
a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
 
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
index a942693..11aff62 100644
--- 
a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
+++ 
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
@@ -148,6 +148,7 @@ public class ODataExceptionWrapper {
   private void enhanceContextWithApplicationException(final 
ODataApplicationException toHandleException) {
     errorContext.setHttpStatus(toHandleException.getHttpStatus());
     errorContext.setErrorCode(toHandleException.getCode());
+    errorContext.setLocale(messageLocale);
   }
 
   private void enhanceContextWithMessageException(final ODataMessageException 
toHandleException) {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a9f2e415/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
----------------------------------------------------------------------
diff --git 
a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
 
b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
index 98c6b26..4af0449 100644
--- 
a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
+++ 
b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
@@ -24,12 +24,16 @@ import static org.mockito.Mockito.when;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.olingo.odata2.api.ODataCallback;
 import org.apache.olingo.odata2.api.ODataService;
@@ -85,7 +89,83 @@ public class ODataExceptionWrapperTest extends BaseTest {
     String contentTypeHeader = response.getContentHeader();
     assertEquals("text/html", contentTypeHeader);
   }
+  
+  @Test
+  public void testCallbackWithLocales() throws Exception {
+    ODataContextImpl context = 
getMockedContextWithLocale("http://localhost:80/test";, "ODataServiceRoot");
+    ODataErrorCallback errorCallback = new ODataErrorCallback() {
+      @Override
+      public ODataResponse handleError(final ODataErrorContext context) throws 
ODataApplicationException {
+        PathInfo pathInfo = context.getPathInfo();
+        assertEquals("ODataServiceRoot", pathInfo.getServiceRoot().toString());
+        assertEquals("http://localhost:80/test";, 
pathInfo.getRequestUri().toString());
+        assertEquals("de", context.getLocale().getLanguage());
+        assertEquals("DE", context.getLocale().getCountry());
+        return 
ODataResponse.entity("bla").status(HttpStatusCodes.BAD_REQUEST).contentHeader("text/html").build();
+      }
+    };
+    when(context.getServiceFactory()).thenReturn(new 
MapperServiceFactory(errorCallback));
+
+    //
+    Map<String, String> queryParameters = Collections.emptyMap();
+    List<String> acceptContentTypes = Arrays.asList("text/html");
+    ODataExceptionWrapper exceptionWrapper = createWrapper(context, 
queryParameters, acceptContentTypes);
+    ODataResponse response = exceptionWrapper.wrapInExceptionResponse(
+        new ODataApplicationException("Error",Locale.GERMANY));
+
+    // verify
+    assertNotNull(response);
+    assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), 
response.getStatus().getStatusCode());
+    String errorMessage = (String) response.getEntity();
+    assertEquals("bla", errorMessage);
+    String contentTypeHeader = response.getContentHeader();
+    assertEquals("text/html", contentTypeHeader);
+  }
+  
+  @Test
+  public void testCallbackWithLocales1() throws Exception {
+    UriInfo uriInfo = getMockedUriInfo();
+    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
+    List<Locale> locales = new ArrayList<Locale>();
+    locales.add(Locale.GERMANY);
+    locales.add(Locale.FRANCE);
+    when(httpHeaders.getAcceptableLanguages()).thenReturn(locales);
+    
+    ODataErrorCallback errorCallback = new ODataErrorCallback() {
+      @Override
+      public ODataResponse handleError(final ODataErrorContext context) throws 
ODataApplicationException {
+        assertEquals("de", context.getLocale().getLanguage());
+        assertEquals("DE", context.getLocale().getCountry());
+        return 
ODataResponse.entity("bla").status(HttpStatusCodes.BAD_REQUEST).contentHeader("text/html").build();
+      }
+    };
+
+    ODataExceptionWrapper exceptionWrapper = createWrapper1(uriInfo, 
httpHeaders, errorCallback);
+    ODataResponse response = exceptionWrapper.wrapInExceptionResponse(
+        new ODataApplicationException("Error",Locale.GERMANY));
 
+    // verify
+    assertNotNull(response);
+    assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), 
response.getStatus().getStatusCode());
+    String errorMessage = (String) response.getEntity();
+    assertEquals("bla", errorMessage);
+    String contentTypeHeader = response.getContentHeader();
+    assertEquals("text/html", contentTypeHeader);
+  }
+
+  private UriInfo getMockedUriInfo() {
+    UriInfo uriInfo = Mockito.mock(UriInfo.class);
+    
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:80/test";));
+    return uriInfo;
+  }
+
+  private ODataExceptionWrapper createWrapper1(final UriInfo uriInfo,
+      final HttpHeaders httpHeaders, ODataErrorCallback errorCallback) throws 
URISyntaxException {
+    ODataExceptionWrapper exceptionWrapper = new 
ODataExceptionWrapper(uriInfo, httpHeaders, errorCallback);
+
+    return exceptionWrapper;
+  }
+  
   private ODataExceptionWrapper createWrapper(final ODataContextImpl context,
       final Map<String, String> queryParameters, final List<String> 
acceptContentTypes) throws URISyntaxException {
     ODataExceptionWrapper exceptionWrapper = new 
ODataExceptionWrapper(context, queryParameters, acceptContentTypes);
@@ -103,6 +183,23 @@ public class ODataExceptionWrapperTest extends BaseTest {
     when(context.getRequestHeaders()).thenReturn(new 
MultivaluedHashMap<String, String>());
     return context;
   }
+  
+  private ODataContextImpl getMockedContextWithLocale(final String requestUri, 
+      final String serviceRoot) throws ODataException,
+  URISyntaxException {
+    ODataContextImpl context = Mockito.mock(ODataContextImpl.class);
+    PathInfoImpl pathInfo = new PathInfoImpl();
+    pathInfo.setRequestUri(new URI(requestUri));
+    pathInfo.setServiceRoot(new URI(serviceRoot));
+    when(context.getPathInfo()).thenReturn(pathInfo);
+    MultivaluedHashMap<String,String> headers = new MultivaluedHashMap<String, 
String>();
+    headers.add("Accept-Language","de-DE, de;q=0.7");
+    when(context.getRequestHeaders()).thenReturn(headers);
+    List<Locale> locales = new ArrayList<Locale>();
+    locales.add(Locale.GERMANY);
+    when(context.getAcceptableLanguages()).thenReturn(locales);
+    return context;
+    }
 
   public static final class MapperServiceFactory extends ODataServiceFactory {
     private ODataErrorCallback errorCallback;

Reply via email to