[OLINGO-356] Content negotiation change for custom format
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/266c7b4a Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/266c7b4a Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/266c7b4a Branch: refs/heads/master Commit: 266c7b4ae8e5ce75c14854fe70ac181e4819e02e Parents: 9048d72 Author: Michael Bolz <[email protected]> Authored: Thu Aug 7 13:33:13 2014 +0200 Committer: Michael Bolz <[email protected]> Committed: Thu Aug 7 13:49:43 2014 +0200 ---------------------------------------------------------------------- .../olingo/commons/api/format/AcceptType.java | 15 +-- .../olingo/commons/api/format/ContentType.java | 19 +-- .../CustomContentTypeSupportProcessor.java | 6 +- .../api/processor/FormatContentTypeMapping.java | 57 -------- .../olingo/server/core/ContentNegotiator.java | 131 ++++++++---------- .../server/core/ContentNegotiatorTest.java | 133 +++++++++---------- .../olingo/server/core/ODataHandlerTest.java | 24 ++-- 7 files changed, 141 insertions(+), 244 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java index 180583c..379eb95 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptType.java @@ -45,9 +45,6 @@ import java.util.TreeMap; */ public class AcceptType { -// public static final AcceptType WILDCARD = new AcceptType(TypeUtil.MEDIA_TYPE_WILDCARD, TypeUtil.MEDIA_TYPE_WILDCARD, -// createParameterMap(), 1.0F); - private final String type; private final String subtype; private final Map<String, String> parameters; @@ -98,8 +95,8 @@ public class AcceptType { } } - private static void - parse(final String format, final List<String> typeSubtype, final Map<String, String> parameters) { + private static void parse(final String format, final List<String> typeSubtype, + final Map<String, String> parameters) { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; @@ -127,9 +124,9 @@ public class AcceptType { } /** - * Create an {@link AcceptType} based on given input string (<code>format</code>). - * @param format - * @return a new <code>AcceptType</code> object + * Create a list of {@link AcceptType} objects based on given input string (<code>format</code>). + * @param format accept types, comma-separated, as specified for the HTTP header <code>Accept</code> + * @return a list of <code>AcceptType</code> objects * @throws IllegalArgumentException if input string is not parseable */ public static List<AcceptType> create(final String format) { @@ -216,7 +213,7 @@ public class AcceptType { * as defined in RFC 7231, chapters 3.1.1.1, 5.3.1, and 5.3.2. * @param toSort list which is sorted and hence re-arranged */ - private static void sort(final List<AcceptType> toSort) { + private static void sort(List<AcceptType> toSort) { Collections.sort(toSort, new Comparator<AcceptType>() { @Override http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java index 5fa44ce..20620fc 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java @@ -302,22 +302,15 @@ public final class ContentType { } /** - * {@link ContentType}s are <b>compatible</b> - * <ul> - * <li>if <code>type</code>, <code>subtype</code> have the same value.</li> - * <li>if <code>type</code> and/or <code>subtype</code> is set to "*"</li> - * </ul> - * The set <code>parameters</code> are <b>always</b> ignored (for compare with parameters see {@link #equals(Object)} - * ). - * + * <p>{@link ContentType}s are <b>compatible</b> + * if <code>type</code> and <code>subtype</code> have the same value.</p> + * <p>The set <code>parameters</code> are <b>always</b> ignored + * (for compare with parameters see {@link #equals(Object)}).</p> * @return <code>true</code> if both instances are equal (see definition above), otherwise <code>false</code>. */ public boolean isCompatible(final ContentType obj) { - Boolean compatible = isEqualWithoutParameters(obj); - if (compatible == null) { - return true; - } - return compatible.booleanValue(); + final Boolean compatible = isEqualWithoutParameters(obj); + return compatible == null || compatible.booleanValue(); } /** http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java index edcad72..0d94a52 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CustomContentTypeSupportProcessor.java @@ -20,6 +20,8 @@ package org.apache.olingo.server.api.processor; import java.util.List; +import org.apache.olingo.commons.api.format.ContentType; + /** * A processor which supports custom content types can implement this interface. The processor can also remove default * content types if the default serializer of Olingo are not used. By default this interface is not implemented and @@ -35,7 +37,7 @@ public interface CustomContentTypeSupportProcessor { * @return modified list of supported content types * */ - public List<FormatContentTypeMapping> modifySupportedContentTypes( - List<FormatContentTypeMapping> defaultContentTypes, Class<? extends Processor> processorClass); + public List<ContentType> modifySupportedContentTypes( + List<ContentType> defaultContentTypes, Class<? extends Processor> processorClass); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java deleted file mode 100644 index 38ce7d6..0000000 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/FormatContentTypeMapping.java +++ /dev/null @@ -1,57 +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.apache.olingo.server.api.processor; - -/** - * Mapping between an uri $format option value and a content types. For instance the $format option "xml" maps to - * content type "application/xml". - */ -public class FormatContentTypeMapping { - - private String formatOptionValue; - private String contentTypeValue; - - public FormatContentTypeMapping(final String formatOptionValue, final String contentTypeValue) { - super(); - this.formatOptionValue = formatOptionValue; - this.contentTypeValue = contentTypeValue; - } - - public String getFormatAlias() { - return formatOptionValue; - } - - public String getContentType() { - return contentTypeValue; - } - - public void setFormatAlias(final String formatOptionValue) { - this.formatOptionValue = formatOptionValue; - } - - public void setContentType(final String contentTypeValue) { - this.contentTypeValue = contentTypeValue; - } - - @Override - public String toString() { - return "('" + formatOptionValue + "', '" + contentTypeValue + "')"; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java index e762464..1adabf7 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java @@ -28,7 +28,6 @@ import org.apache.olingo.commons.api.format.ODataFormat; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor; -import org.apache.olingo.server.api.processor.FormatContentTypeMapping; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.api.uri.queryoption.FormatOption; @@ -37,29 +36,28 @@ public class ContentNegotiator { private ContentNegotiator() {} - private static List<FormatContentTypeMapping> + private static List<ContentType> getDefaultSupportedContentTypes(final Class<? extends Processor> processorClass) { - List<FormatContentTypeMapping> defaults = new ArrayList<FormatContentTypeMapping>(); + List<ContentType> defaults = new ArrayList<ContentType>(); if (processorClass == MetadataProcessor.class) { - defaults.add(new FormatContentTypeMapping("xml", ContentType.APPLICATION_XML.toContentTypeString())); + defaults.add(ODataFormat.XML.getContentType(ODataServiceVersion.V40)); } else { - defaults.add(new FormatContentTypeMapping("json", - ODataFormat.JSON.getContentType(ODataServiceVersion.V40).toContentTypeString())); + defaults.add(ODataFormat.JSON.getContentType(ODataServiceVersion.V40)); + defaults.add(ODataFormat.JSON_NO_METADATA.getContentType(ODataServiceVersion.V40)); } return defaults; } - private static List<FormatContentTypeMapping> getSupportedContentTypes(final Processor processor, + private static List<ContentType> getSupportedContentTypes(final Processor processor, final Class<? extends Processor> processorClass) { - List<FormatContentTypeMapping> supportedContentTypes = getDefaultSupportedContentTypes(processorClass); + List<ContentType> supportedContentTypes = getDefaultSupportedContentTypes(processorClass); if (processor instanceof CustomContentTypeSupportProcessor) { - supportedContentTypes = - ((CustomContentTypeSupportProcessor) processor).modifySupportedContentTypes(supportedContentTypes, - processorClass); + supportedContentTypes = ((CustomContentTypeSupportProcessor) processor) + .modifySupportedContentTypes(supportedContentTypes, processorClass); } return supportedContentTypes; @@ -67,98 +65,73 @@ public class ContentNegotiator { public static ContentType doContentNegotiation(final FormatOption formatOption, final ODataRequest request, final Processor processor, final Class<? extends Processor> processorClass) throws ContentNegotiatorException { - ContentType requestedContentType = null; - - List<FormatContentTypeMapping> supportedContentTypes = getSupportedContentTypes(processor, processorClass); - - String acceptHeaderValue = request.getHeader(HttpHeader.ACCEPT); - - boolean supported = false; - - if (formatOption != null) { - if ("json".equalsIgnoreCase(formatOption.getText().trim())) { - requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40); - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } - } - } else if ("xml".equalsIgnoreCase(formatOption.getText().trim())) { - requestedContentType = ContentType.APPLICATION_XML; - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } - } - } else { - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (formatOption.getText().equalsIgnoreCase(entry.getFormatAlias().trim())) { - requestedContentType = ContentType.create(entry.getContentType().trim()); - supported = true; - break; - } - } - } - if (!supported) { - throw new ContentNegotiatorException("Unsupported $format = " + formatOption.getText(), - ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatOption.getText()); + final List<ContentType> supportedContentTypes = getSupportedContentTypes(processor, processorClass); + final String acceptHeaderValue = request.getHeader(HttpHeader.ACCEPT); + ContentType result = null; + + if (formatOption != null && formatOption.getFormat() != null) { + final String formatString = formatOption.getFormat().trim(); + final ODataFormat format = + ODataFormat.JSON.name().equalsIgnoreCase(formatString) ? ODataFormat.JSON : + ODataFormat.XML.name().equalsIgnoreCase(formatString) ? ODataFormat.XML : + ODataFormat.ATOM.name().equalsIgnoreCase(formatString) ? ODataFormat.ATOM : null; + result = getSupportedContentType(format == null ? + ContentType.create(formatOption.getFormat()) : format.getContentType(ODataServiceVersion.V40), + supportedContentTypes); + if (result == null) { + throw new ContentNegotiatorException("Unsupported $format = " + formatString, + ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString); } } else if (acceptHeaderValue != null) { - List<AcceptType> acceptedContentTypes = AcceptType.create(acceptHeaderValue); - + final List<AcceptType> acceptedContentTypes = AcceptType.create(acceptHeaderValue); for (AcceptType acceptedType : acceptedContentTypes) { - for (FormatContentTypeMapping supportedType : supportedContentTypes) { - - ContentType ct = ContentType.create(supportedType.getContentType()); + for (final ContentType supportedContentType : supportedContentTypes) { + ContentType contentType = supportedContentType; if (acceptedType.getParameters().containsKey("charset")) { - String value = acceptedType.getParameters().get("charset"); + final String value = acceptedType.getParameters().get("charset"); if ("utf8".equalsIgnoreCase(value) || "utf-8".equalsIgnoreCase(value)) { - ct = ContentType.create(ct, ContentType.PARAMETER_CHARSET_UTF8); + contentType = ContentType.create(contentType, ContentType.PARAMETER_CHARSET_UTF8); } else { throw new ContentNegotiatorException("charset in accept header not supported: " + acceptHeaderValue, ContentNegotiatorException.MessageKeys.WRONG_CHARSET_IN_HEADER, HttpHeader.ACCEPT, acceptHeaderValue); } } - - if (acceptedType.matches(ct)) { - requestedContentType = ct; - supported = true; + if (acceptedType.matches(contentType)) { + result = contentType; break; } } - if (supported) { + if (result != null) { break; } } - - if (!supported) { + if (result == null) { throw new ContentNegotiatorException( "unsupported accept content type: " + acceptedContentTypes + " != " + supportedContentTypes, ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPES, acceptedContentTypes.toString()); } } else { - if (processorClass == MetadataProcessor.class) { - requestedContentType = ContentType.APPLICATION_XML; - } else { - requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40); - } - - for (FormatContentTypeMapping entry : supportedContentTypes) { - if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) { - supported = true; - break; - } + final ContentType requestedContentType = processorClass == MetadataProcessor.class ? + ODataFormat.XML.getContentType(ODataServiceVersion.V40) : + ODataFormat.JSON.getContentType(ODataServiceVersion.V40); + result = getSupportedContentType(requestedContentType, supportedContentTypes); + if (result == null) { + throw new ContentNegotiatorException( + "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes, + ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, + requestedContentType.toContentTypeString()); } } + return result; + } - if (!supported) { - throw new ContentNegotiatorException( - "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes, - ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, requestedContentType.toContentTypeString()); + private static ContentType getSupportedContentType(final ContentType requestedContentType, + final List<ContentType> supportedContentTypes) { + for (final ContentType supportedContentType : supportedContentTypes) { + if (requestedContentType.isCompatible(supportedContentType)) { + return supportedContentType; + } } - - return requestedContentType; + return null; } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java index f858aa0..61d0bc8 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java @@ -20,7 +20,6 @@ package org.apache.olingo.server.core; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -36,9 +35,8 @@ import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor; -import org.apache.olingo.server.api.processor.FormatContentTypeMapping; +import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.MetadataProcessor; import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; @@ -60,49 +58,49 @@ public class ContentNegotiatorTest { //CHECKSTYLE:OFF (Maven checkstyle) String[][] casesServiceDocument = { - /* expected $format accept alias ct mapping */ - { ACCEPT_CASE_MIN, null, null, null ,null }, - { ACCEPT_CASE_MIN, "json", null, null ,null }, - { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_JSONQ, null ,null }, - { "a/a", "a", null, "a" ,"a/a" }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_JSONQ, null ,null }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD1, null ,null }, - { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD2, null ,null }, - { "a/a", "a", null, "a, b" ,"a/a,b/b" }, - { "a/a", " a ", null, " a , b" ," a/a , b/b " }, - { "a/a;x=y", "a", ACCEPT_CASE_WILDCARD1, "a" ,"a/a;x=y" }, - { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_MIN, null ,null }, - { ACCEPT_CASE_FULL, null, ACCEPT_CASE_FULL, "dummy" ,ACCEPT_CASE_FULL }, - { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null ,null }, + /* expected $format accept additional content types */ + { ACCEPT_CASE_MIN, null, null, null }, + { ACCEPT_CASE_MIN, "json", null, null }, + { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_JSONQ, null }, + { "a/a", "a/a", null, "a/a" }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_JSONQ, null }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD1, null }, + { ACCEPT_CASE_MIN, null, ACCEPT_CASE_WILDCARD2, null }, + { "a/a", "a/a", null, "a/a,b/b" }, + { "a/a", " a/a ", null, " a/a , b/b " }, + { "a/a;x=y", "a/a", ACCEPT_CASE_WILDCARD1, "a/a;x=y" }, + { ACCEPT_CASE_MIN, "json", ACCEPT_CASE_MIN, null }, + { ACCEPT_CASE_FULL, null, ACCEPT_CASE_FULL, ACCEPT_CASE_FULL }, + { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null }, }; String[][] casesMetadata = { - /* expected $format accept alias ct mapping */ - { "application/xml", null, null, null ,null }, - { "application/xml", "xml", null, null ,null }, - { "application/xml", "xml", ACCEPT_CASE_XML, null ,null }, - { "a/a", "a", null, "a" ,"a/a" }, - { "application/xml", null, ACCEPT_CASE_XML, null ,null }, - { "application/xml", null, ACCEPT_CASE_WILDCARD1, null ,null }, - { "application/xml", null, ACCEPT_CASE_WILDCARD2, null ,null }, - { "a/a", "a", null, "a, b" ,"a/a,b/b" }, - { "a/a", " a ", null, " a , b" ," a/a , b/b " }, - { "a/a;x=y", "a", ACCEPT_CASE_WILDCARD1, "a" ,"a/a;x=y" }, + /* expected $format accept additional content types */ + { ACCEPT_CASE_XML, null, null, null }, + { ACCEPT_CASE_XML, "xml", null, null }, + { ACCEPT_CASE_XML, "xml", ACCEPT_CASE_XML, null }, + { "a/a", "a/a", null, "a/a" }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_XML, null }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_WILDCARD1, null }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_WILDCARD2, null }, + { "a/a", "a/a", null, "a/a,b/b" }, + { "a/a", " a/a ", null, " a/a , b/b " }, + { "a/a;x=y", "a/a", ACCEPT_CASE_WILDCARD1, "a/a;x=y" }, }; String[][] casesFail = { - /* expected $format accept alias ct mapping */ - { "application/xml", "xxx", null, null ,null }, - { "a/a", "a", null, "b" ,"b/b" }, - { "application/xml", null, ACCEPT_CASE_JSONQ, null ,null }, - { "application/json", null, ACCEPT_CASE_FULL, null ,null }, // not jet supported + /* expected $format accept additional content types */ + { ACCEPT_CASE_XML, "xxx/yyy", null, null }, + { "a/a", "a/a", null, "b/b" }, + { ACCEPT_CASE_XML, null, ACCEPT_CASE_JSONQ, null }, + { "application/json", null, ACCEPT_CASE_FULL, null }, // not yet supported }; //CHECKSTYLE:ON //@formatter:on @Test public void testServiceDocumentSingleCase() throws Exception { - String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null, null }; + String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null }; testContentNegotiation(useCase, ServiceDocumentProcessor.class); } @@ -116,7 +114,7 @@ public class ContentNegotiatorTest { @Test public void testMetadataSingleCase() throws Exception { - String[] useCase = { "application/xml", null, null, null, null }; + String[] useCase = { ACCEPT_CASE_XML, null, null, null }; testContentNegotiation(useCase, MetadataProcessor.class); } @@ -129,86 +127,77 @@ public class ContentNegotiatorTest { } @Test - public void testMetadataFail() { + public void testMetadataFail() throws Exception { for (String[] useCase : casesFail) { try { testContentNegotiation(useCase, MetadataProcessor.class); - fail("Exeption expected!"); - } catch (ContentNegotiatorException e) { - - }catch (Exception e) { - e.printStackTrace(); - fail("Wrong Exception: " + e.getClass().getName()); - } + fail("Exception expected!"); + } catch (final ContentNegotiatorException e) {} } } - public void testContentNegotiation(final String[] useCase, final Class<? extends Processor> processorClass) - throws Exception { + private void testContentNegotiation(final String[] useCase, final Class<? extends Processor> processorClass) + throws ContentNegotiatorException { ODataRequest request = new ODataRequest(); request.setMethod(HttpMethod.GET); request.setRawODataPath("/" + (useCase[1] == null ? "" : "?$format=" + useCase[1])); - ProcessorStub p = new ProcessorStub(createCustomContentTypeMapping(useCase[3], useCase[4])); + ProcessorStub p = new ProcessorStub(createCustomContentTypes(useCase[3])); FormatOption fo = null; if (useCase[1] != null) { fo = mock(FormatOption.class); - when(fo.getText()).thenReturn(useCase[1].trim()); + when(fo.getFormat()).thenReturn(useCase[1].trim()); } if (useCase[2] != null) { request.addHeader(HttpHeader.ACCEPT, Arrays.asList(useCase[2])); } - ContentType requestedContentType = ContentNegotiator.doContentNegotiation(fo, request, p, processorClass); + final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(fo, request, p, processorClass); assertNotNull(requestedContentType); assertEquals(ContentType.create(useCase[0]), requestedContentType); } - private List<FormatContentTypeMapping> createCustomContentTypeMapping(final String formatString, - final String contentTypeString) { - List<FormatContentTypeMapping> map = null; + private List<ContentType> createCustomContentTypes(final String contentTypeString) { - assertTrue(!(formatString == null ^ contentTypeString == null)); - - if (formatString != null) { - String[] formats = formatString.split(","); - String[] contentTypes = contentTypeString.split(","); + if (contentTypeString == null) { + return null; + } - assertEquals(formats.length, contentTypes.length); + String[] contentTypes = contentTypeString.split(","); - map = new ArrayList<FormatContentTypeMapping>(); - for (int i = 0; i < formats.length; i++) { - map.add(new FormatContentTypeMapping(formats[i], contentTypes[i])); - } + List<ContentType> types = new ArrayList<ContentType>(); + for (int i = 0; i < contentTypes.length; i++) { + types.add(ContentType.create(contentTypes[i].trim())); } - return map; + return types; } private class ProcessorStub implements ServiceDocumentProcessor, MetadataProcessor, - EntityCollectionProcessor, - CustomContentTypeSupportProcessor { + EntityCollectionProcessor, CustomContentTypeSupportProcessor { - List<FormatContentTypeMapping> customMapping; + List<ContentType> customTypes; - ProcessorStub(final List<FormatContentTypeMapping> mapping) { - customMapping = mapping; + ProcessorStub(final List<ContentType> types) { + customTypes = types; } @Override public void init(final OData odata, final Edm edm) {} @Override - public List<FormatContentTypeMapping> modifySupportedContentTypes( - final List<FormatContentTypeMapping> supportedContentTypes, + public List<ContentType> modifySupportedContentTypes(final List<ContentType> supportedContentTypes, final Class<? extends Processor> processorClass) { - if (customMapping != null) { - supportedContentTypes.addAll(customMapping); + if (customTypes == null) { + return supportedContentTypes; + } else { + List<ContentType> modifiedTypes = new ArrayList<ContentType>(supportedContentTypes); + modifiedTypes.addAll(customTypes); + return modifiedTypes; } - return supportedContentTypes; } @Override http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/266c7b4a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java ---------------------------------------------------------------------- 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 7e55ab8..3ac7208 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 @@ -78,7 +78,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); } @Test @@ -92,7 +92,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); String ct = response.getHeaders().get(HttpHeader.CONTENT_TYPE); assertTrue(ct.contains("application/json")); @@ -134,7 +134,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); } @Test @@ -147,7 +147,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); assertEquals(HttpContentType.APPLICATION_XML, response.getHeaders().get(HttpHeader.CONTENT_TYPE)); assertNotNull(response.getContent()); @@ -195,7 +195,7 @@ public class ODataHandlerTest { assertNotNull(response); assertEquals(ODataServiceVersion.V40.toString(), response.getHeaders().get(HttpHeader.ODATA_VERSION)); - assertEquals(400, response.getStatusCode()); + assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode()); } @Test @@ -208,7 +208,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(200, response.getStatusCode()); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); } @Test @@ -217,11 +217,11 @@ public class ODataHandlerTest { request.setMethod(HttpMethod.GET); request.setRawODataPath("$metadata"); - request.setRawQueryPath("$format=notSupported"); + request.setRawQueryPath("$format=not/Supported"); ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(406, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), response.getStatusCode()); } @Test @@ -233,7 +233,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(501, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), response.getStatusCode()); } @Test @@ -265,7 +265,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(404, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); } //TODO: Use this test @@ -279,7 +279,7 @@ public class ODataHandlerTest { ODataResponse response = handler.process(request); assertNotNull(response); - assertEquals(404, response.getStatusCode()); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); } @Test @@ -301,7 +301,7 @@ public class ODataHandlerTest { ODataResponse response = localHandler.process(request); assertNotNull(response); - assertEquals(500, response.getStatusCode()); + assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode()); // TODO: Check for message in case of EdmException // System.out.println(IOUtils.toString(response.getContent())); }
