From ab39763f3e811faf87d6a09e972dd6df0e4723a6 Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Wed, 14 Jan 2015 09:14:36 +0100 Subject: [PATCH] [OLINGO-482] Added test case --- .../olingo/server/core/ODataHandlerTest.java | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java index 72d29568c..b62371ec0 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java @@ -27,9 +27,13 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; import java.util.Collections; +import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; import org.apache.commons.io.IOUtils; import org.apache.olingo.commons.api.ODataException; @@ -45,6 +49,7 @@ import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.ODataServerError; import org.apache.olingo.server.api.ServiceMetadata; import org.apache.olingo.server.api.edm.provider.EdmProvider; import org.apache.olingo.server.api.edm.provider.EntitySet; @@ -63,6 +68,7 @@ import org.apache.olingo.server.api.processor.CountEntityCollectionProcessor; import org.apache.olingo.server.api.processor.CountPrimitiveCollectionProcessor; import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.EntityProcessor; +import org.apache.olingo.server.api.processor.ErrorProcessor; import org.apache.olingo.server.api.processor.MediaEntityProcessor; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.PrimitiveCollectionProcessor; @@ -618,8 +624,32 @@ public class ODataHandlerTest { dispatchMethodNotAllowed(HttpMethod.DELETE, uri, processor); } + @Test + public void unsupportedRequestContentType() throws Exception { + EntityProcessor processor = mock(EntityProcessor.class); + ErrorProcessor errorProcessor = mock(ErrorProcessor.class); + dispatch(HttpMethod.POST, "ESAllPrim", "", HttpHeader.CONTENT_TYPE, "some/unsupported", errorProcessor); + verifyZeroInteractions(processor); + verify(errorProcessor).processError(any(ODataRequest.class), any(ODataResponse.class), any(ODataServerError.class), + any(ContentType.class)); + } + private ODataResponse dispatch(final HttpMethod method, final String path, final String query, final String headerName, final String headerValue, final Processor processor) { + Map> headers = null; + if(headerName != null) { + headers = Collections.singletonMap(headerName, Collections.singletonList(headerValue)); + } + List processors = null; + if(processor != null) { + processors = Collections.singletonList(processor); + } + return dispatch(method, path, query, headers, processors); + } + + + private ODataResponse dispatch(final HttpMethod method, final String path, final String query, + final Map> headers, final List processors) { ODataRequest request = new ODataRequest(); request.setMethod(method); request.setRawBaseUri(BASE_URI); @@ -629,11 +659,17 @@ public class ODataHandlerTest { request.setRawODataPath(path); request.setRawQueryPath(query); - if (headerName != null) { - request.addHeader(headerName, Collections.singletonList(headerValue)); + if (headers != null) { + Set>> headerSet = headers.entrySet(); + for (Map.Entry> headerItem : headerSet) { + request.addHeader(headerItem.getKey(), headerItem.getValue()); + } + } + + if(request.getHeaders(HttpHeader.CONTENT_TYPE) == null) { + request.addHeader(HttpHeader.CONTENT_TYPE, Collections.singletonList( + ODataFormat.JSON.getContentType(ODataServiceVersion.V40).toContentTypeString())); } - request.addHeader(HttpHeader.CONTENT_TYPE, Collections.singletonList( - ODataFormat.JSON.getContentType(ODataServiceVersion.V40).toContentTypeString())); final OData odata = OData.newInstance(); final ServiceMetadata metadata = odata.createServiceMetadata( @@ -641,8 +677,10 @@ public class ODataHandlerTest { ODataHandler handler = new ODataHandler(odata, metadata); - if (processor != null) { - handler.register(processor); + if (processors != null && !processors.isEmpty()) { + for (Processor p : processors) { + handler.register(p); + } } final ODataResponse response = handler.process(request);