From aff42fcee5e189d5cc506ff1cdc05421528fbeb5 Mon Sep 17 00:00:00 2001 From: Stephan Klevenz Date: Tue, 8 Jul 2014 10:07:16 +0200 Subject: [PATCH] [OLINGO-337] more tests and fixes --- .../apache/olingo/fit/tecsvc/BasicITCase.java | 3 ++- .../olingo/commons/api/format/AcceptType.java | 8 +++--- .../commons/api/format/ContentType.java | 27 +++++++++---------- .../commons/api/format/AcceptTypeTest.java | 23 ++++++++++++++-- .../server/core/ContentNegotiatorTest.java | 2 +- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/BasicITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/BasicITCase.java index c18856822..da2f71144 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/BasicITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/BasicITCase.java @@ -30,6 +30,7 @@ import org.apache.olingo.client.core.ODataClientFactory; import org.apache.olingo.commons.api.domain.ODataServiceDocument; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.format.ODataFormat; import org.junit.Before; import org.junit.Test; @@ -49,7 +50,6 @@ public class BasicITCase { public void readServiceDocument() { ODataServiceDocumentRequest request = odata.getRetrieveRequestFactory().getServiceDocumentRequest(REF_SERVICE); - request.setAccept("application/json;odata.metadata=minimal"); assertNotNull(request); ODataRetrieveResponse response = request.execute(); @@ -73,6 +73,7 @@ public class BasicITCase { assertEquals(200, response.getStatusCode()); Edm edm = response.getBody(); + assertNotNull(edm); assertEquals("com.sap.odata.test1", edm.getSchema("com.sap.odata.test1").getNamespace()); assertEquals("Namespace1_Alias", edm.getSchema("com.sap.odata.test1").getAlias()); 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 736b7471d..12ebf9a4f 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 @@ -193,9 +193,6 @@ public class AcceptType { for (final String key : parameters.keySet()) { result.append(';').append(key).append('=').append(parameters.get(key)); } - if (quality < 1F) { - result.append(';').append(TypeUtil.PARAMETER_Q).append('=').append(quality); - } return result.toString(); } @@ -225,8 +222,9 @@ public class AcceptType { } Map compareParameters = contentType.getParameters(); for (final String key : parameters.keySet()) { - if (compareParameters.containsKey(key)) { - if (!parameters.get(key).equalsIgnoreCase(compareParameters.get(key))) { + if (compareParameters.containsKey(key) || TypeUtil.PARAMETER_Q.equalsIgnoreCase(key)) { + if (!parameters.get(key).equalsIgnoreCase(compareParameters.get(key)) + && !TypeUtil.PARAMETER_Q.equalsIgnoreCase(key)) { return false; } } else { 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 a686880f2..4aee30eda 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 @@ -45,31 +45,30 @@ import java.util.TreeMap; */ public class ContentType { - public static final ContentType APPLICATION_XML = create("application", "xml"); public static final ContentType APPLICATION_XML_CS_UTF_8 = create(APPLICATION_XML, TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); public static final ContentType APPLICATION_ATOM_XML = create("application", "atom+xml"); public static final ContentType APPLICATION_ATOM_XML_CS_UTF_8 = create(APPLICATION_ATOM_XML, TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); - public static final ContentType APPLICATION_ATOM_XML_ENTRY = - create(APPLICATION_ATOM_XML,TypeUtil. PARAMETER_TYPE, "entry"); + public static final ContentType APPLICATION_ATOM_XML_ENTRY = + create(APPLICATION_ATOM_XML, TypeUtil.PARAMETER_TYPE, "entry"); public static final ContentType APPLICATION_ATOM_XML_ENTRY_CS_UTF_8 = create(APPLICATION_ATOM_XML_ENTRY, - TypeUtil. PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); - public static final ContentType APPLICATION_ATOM_XML_FEED = - create(APPLICATION_ATOM_XML,TypeUtil. PARAMETER_TYPE, "feed"); + TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); + public static final ContentType APPLICATION_ATOM_XML_FEED = + create(APPLICATION_ATOM_XML, TypeUtil.PARAMETER_TYPE, "feed"); public static final ContentType APPLICATION_ATOM_XML_FEED_CS_UTF_8 = create(APPLICATION_ATOM_XML_FEED, - TypeUtil. PARAMETER_CHARSET,TypeUtil.CHARSET_UTF_8); + TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); public static final ContentType APPLICATION_ATOM_SVC = create("application", "atomsvc+xml"); public static final ContentType APPLICATION_ATOM_SVC_CS_UTF_8 = create(APPLICATION_ATOM_SVC, - TypeUtil. PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); + TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); public static final ContentType APPLICATION_JSON = create("application", "json"); public static final ContentType APPLICATION_JSON_CS_UTF_8 = create(APPLICATION_JSON, - TypeUtil. PARAMETER_CHARSET,TypeUtil. CHARSET_UTF_8); + TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); public static final ContentType APPLICATION_OCTET_STREAM = create("application", "octet-stream"); public static final ContentType TEXT_PLAIN = create("text", "plain"); - public static final ContentType TEXT_PLAIN_CS_UTF_8 = - create(TEXT_PLAIN, TypeUtil.PARAMETER_CHARSET,TypeUtil. CHARSET_UTF_8); + public static final ContentType TEXT_PLAIN_CS_UTF_8 = + create(TEXT_PLAIN, TypeUtil.PARAMETER_CHARSET, TypeUtil.CHARSET_UTF_8); public static final ContentType MULTIPART_MIXED = create("multipart", "mixed"); public static final ContentType APPLICATION_XHTML_XML = create("application", "xhtml+xml"); @@ -209,10 +208,10 @@ public class ContentType { } } else { throw new IllegalArgumentException( - "Too many '" +TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' in format '" + format + "'."); + "Too many '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' in format '" + format + "'."); } } else { - throw new IllegalArgumentException("No separator '" +TypeUtil.TYPE_SUBTYPE_SEPARATOR + throw new IllegalArgumentException("No separator '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' was found in format '" + format + "'."); } @@ -305,7 +304,7 @@ public class ContentType { } return compatible.booleanValue(); } - + /** * Check equal without parameters. * It is possible that no decision about equal/none equal can be determined a NULL is diff --git a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java index 4427bf039..935b001ad 100644 --- a/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java +++ b/lib/commons-api/src/test/java/org/apache/olingo/commons/api/format/AcceptTypeTest.java @@ -20,6 +20,7 @@ package org.apache.olingo.commons.api.format; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.util.List; @@ -45,15 +46,33 @@ public class AcceptTypeTest { assertEquals("a/a", atl.get(0).toString()); } + @Test + public void testWithQParameter() { + List atl = AcceptType.create("application/json;q=0.2"); + + assertEquals(1, atl.size()); + assertEquals("application", atl.get(0).getType()); + assertEquals("json", atl.get(0).getSubtype()); + assertEquals("0.2", atl.get(0).getParameters().get("q")); + assertEquals("application/json;q=0.2", atl.get(0).toString()); + } + + @Test + public void testMatchWithQParameter() { + List atl = AcceptType.create("application/json;q=0.2"); + assertEquals(1, atl.size()); + assertTrue(atl.get(0).matches(ContentType.APPLICATION_JSON)); + } + @Test(expected = IllegalArgumentException.class) public void testWrongQParameter() { AcceptType.create(" a/a;q=z "); } - + @Test public void testWildcard() { List atl = AcceptType.create("*; q=.2"); - + assertNotNull(atl); assertEquals(1, atl.size()); assertEquals("*", atl.get(0).getType()); 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 88896dab5..6c1d67f4b 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 @@ -104,7 +104,7 @@ public class ContentNegotiatorTest { @Test public void testServiceDocumentSingleCase() { - String[] useCase = { ACCEPT_CASE_FULL, null, ACCEPT_CASE_FULL, "dummy", ACCEPT_CASE_FULL }; + String[] useCase = { "application/json", null, ACCEPT_CASE_JSON, null, null }; testContentNegotiation(useCase, ServiceDocumentProcessor.class); }