[OLINGO-337] more fit tests

This commit is contained in:
Stephan Klevenz 2014-07-08 14:49:05 +02:00
parent 1d31f596a8
commit 51e8d3e8d8
8 changed files with 164 additions and 26 deletions

View File

@ -20,6 +20,6 @@ package org.apache.olingo.fit.tecsvc;
public class TecSvcConst {
public final static String BASE_URL = "http://localhost:9080/olingo-server-tecsvc/odata.svc";
public final static String BASE_URI = "http://localhost:9080/olingo-server-tecsvc/odata.svc";
}

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.fit.tecsvc;
package org.apache.olingo.fit.tecsvc.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -31,12 +31,13 @@ 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.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Before;
import org.junit.Test;
public class BasicITCase {
private static final String REF_SERVICE = TecSvcConst.BASE_URL;
private static final String SERVICE_URI = TecSvcConst.BASE_URI;
private ODataClient odata;
@ -49,7 +50,7 @@ public class BasicITCase {
@Test
public void readServiceDocument() {
ODataServiceDocumentRequest request =
odata.getRetrieveRequestFactory().getServiceDocumentRequest(REF_SERVICE);
odata.getRetrieveRequestFactory().getServiceDocumentRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<ODataServiceDocument> response = request.execute();
@ -66,7 +67,7 @@ public class BasicITCase {
@Test
public void readMetadata() {
EdmMetadataRequest request = odata.getRetrieveRequestFactory().getMetadataRequest(REF_SERVICE);
EdmMetadataRequest request = odata.getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<Edm> response = request.execute();

View File

@ -0,0 +1,118 @@
/*
* 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.fit.tecsvc.http;
import static org.junit.Assert.assertEquals;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Test;
public class BasicHttpITCase {
private static final String SERVICE_URI = TecSvcConst.BASE_URI + "/";
@Test
public void testFormat() throws Exception {
URL url = new URL(SERVICE_URI + "?$format=json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
assertEquals(200, code);
String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
assertEquals("application/json;odata.metadata=minimal", ct);
}
@Test
public void testAccept() throws Exception {
URL url = new URL(SERVICE_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;q=0.2;odata.metadata=minimal");
connection.connect();
int code = connection.getResponseCode();
assertEquals(200, code);
String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
assertEquals("application/json;odata.metadata=minimal", ct);
}
@Test
public void testAcceptSimple() throws Exception {
URL url = new URL(SERVICE_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty(HttpHeader.ACCEPT, "application/json");
connection.connect();
int code = connection.getResponseCode();
assertEquals(200, code);
String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
assertEquals("application/json;odata.metadata=minimal", ct);
}
@Test
public void testAcceptCharset() throws Exception {
URL url = new URL(SERVICE_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;q=0.2;odata.metadata=minimal;charset=utf-8");
connection.connect();
int code = connection.getResponseCode();
assertEquals(200, code);
String ct = connection.getHeaderField(HttpHeader.CONTENT_TYPE);
assertEquals("application/json;odata.metadata=minimal;charset=UTF-8", ct);
}
@Test
public void testODataMaxVersion() throws Exception {
URL url = new URL(SERVICE_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty(HttpHeader.ODATA_MAX_VERSION, "4.0");
connection.connect();
int code = connection.getResponseCode();
assertEquals(200, code);
String v = connection.getHeaderField(HttpHeader.ODATA_VERSION);
assertEquals("4.0", v);
}
}

View File

@ -16,13 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.fit.tecsvc;
package org.apache.olingo.fit.tecsvc.http;
import static org.junit.Assert.assertEquals;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,14 +32,14 @@ public class PingITCase {
private static final Logger LOG = LoggerFactory.getLogger(PingITCase.class);
private static final String REF_SERVICE = TecSvcConst.BASE_URL + "/";
private static final String REDIRECT_URL = TecSvcConst.BASE_URL;
private static final String SERVICE_URI = TecSvcConst.BASE_URI + "/";
private static final String REDIRECT_URI = TecSvcConst.BASE_URI;
@Test
public void ping() throws Exception {
URL url = new URL(REF_SERVICE);
URL url = new URL(SERVICE_URI);
LOG.debug("ping request: " + REF_SERVICE);
LOG.debug("ping request: " + SERVICE_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
@ -51,9 +52,9 @@ public class PingITCase {
@Test
public void redirect() throws Exception {
URL url = new URL(REDIRECT_URL);
URL url = new URL(REDIRECT_URI);
LOG.debug("redirect request: " + REDIRECT_URL);
LOG.debug("redirect request: " + REDIRECT_URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

View File

@ -61,10 +61,16 @@ public class ContentType {
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);
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);
public static final ContentType APPLICATION_JSON_MIN = create("application/json;odata.metadata=minimal");
public static final ContentType APPLICATION_JSON_MIN_CS_UTF_8 =
create("application/json;odata.metadata=minimal;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 =

View File

@ -33,6 +33,8 @@ import org.apache.olingo.server.api.uri.queryoption.FormatOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.dataformat.xml.util.TypeUtil;
public class ContentNegotiator {
private final static Logger LOG = LoggerFactory.getLogger(ContentNegotiator.class);
@ -46,7 +48,7 @@ public class ContentNegotiator {
if (processorClass == MetadataProcessor.class) {
defaults.add(new FormatContentTypeMapping("xml", ContentType.APPLICATION_XML.toContentTypeString()));
} else {
defaults.add(new FormatContentTypeMapping("json", ContentType.APPLICATION_JSON.toContentTypeString()));
// defaults.add(new FormatContentTypeMapping("json", ContentType.APPLICATION_JSON.toContentTypeString()));
defaults.add(new FormatContentTypeMapping("json", ContentType.APPLICATION_JSON.toContentTypeString()
+ ";odata.metadata=minimal"));
}
@ -81,7 +83,7 @@ public class ContentNegotiator {
if (formatOption != null) {
if ("json".equalsIgnoreCase(formatOption.getText().trim())) {
requestedContentType = ContentType.APPLICATION_JSON;
requestedContentType = ContentType.APPLICATION_JSON_MIN;
for (FormatContentTypeMapping entry : supportedContentTypes) {
if (requestedContentType.isCompatible(ContentType.create(entry.getContentType().trim()))) {
supported = true;
@ -115,7 +117,15 @@ public class ContentNegotiator {
for (AcceptType acceptedType : acceptedContentTypes) {
for (FormatContentTypeMapping supportedType : supportedContentTypes) {
ContentType ct = ContentType.create(supportedType.getContentType());
if (acceptedType.getParameters().containsKey("charset")) {
String value = acceptedType.getParameters().get("charset");
if ("utf8".equalsIgnoreCase(value) || "utf-8".equalsIgnoreCase(value)) {
ct = ContentType.create(ct, "charset", "UTF-8");
}
}
if (acceptedType.matches(ct)) {
requestedContentType = ct;
supported = true;
@ -136,7 +146,7 @@ public class ContentNegotiator {
if (processorClass == MetadataProcessor.class) {
requestedContentType = ContentType.APPLICATION_XML;
} else {
requestedContentType = ContentType.APPLICATION_JSON;
requestedContentType = ContentType.APPLICATION_JSON_MIN;
}
for (FormatContentTypeMapping entry : supportedContentTypes) {

View File

@ -51,8 +51,9 @@ import org.slf4j.LoggerFactory;
public class ContentNegotiatorTest {
static final private String ACCEPT_CASE_MIN = "application/json;odata.metadata=minimal";
static final private String ACCEPT_CASE_MIN_UTF8 = "application/json;charset=UTF-8;odata.metadata=minimal";
static final private String ACCEPT_CASE_FULL = "application/json;odata.metadata=full";
static final private String ACCEPT_CASE_JSON = "application/json;q=0.2";
static final private String ACCEPT_CASE_JSONQ = "application/json;q=0.2";
static final private String ACCEPT_CASE_XML = "application/xml";
static final private String ACCEPT_CASE_WILDCARD1 = "*/*";
static final private String ACCEPT_CASE_WILDCARD2 = "application/*";
@ -62,18 +63,19 @@ public class ContentNegotiatorTest {
String[][] casesServiceDocument = {
/* expected $format accept alias ct mapping */
{ "application/json", null, null, null ,null },
{ "application/json", "json", null, null ,null },
{ "application/json", "json", ACCEPT_CASE_JSON, null ,null },
{ 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" },
{ "application/json", null, ACCEPT_CASE_JSON, null ,null },
{ "application/json", null, ACCEPT_CASE_WILDCARD1, null ,null },
{ "application/json", null, ACCEPT_CASE_WILDCARD2, null ,null },
{ 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" },
{ "application/json", "json", ACCEPT_CASE_MIN, null ,null },
{ 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 },
};
String[][] casesMetadata = {
@ -94,7 +96,7 @@ public class ContentNegotiatorTest {
/* expected $format accept alias ct mapping */
{ "application/xml", "xxx", null, null ,null },
{ "a/a", "a", null, "b" ,"b/b" },
{ "application/xml", null, ACCEPT_CASE_JSON, null ,null },
{ "application/xml", null, ACCEPT_CASE_JSONQ, null ,null },
{ "application/json", null, ACCEPT_CASE_FULL, null ,null }, // not jet supported
};
//CHECKSTYLE:ON
@ -104,7 +106,7 @@ public class ContentNegotiatorTest {
@Test
public void testServiceDocumentSingleCase() {
String[] useCase = { "application/json", null, ACCEPT_CASE_JSON, null, null };
String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null, null };
testContentNegotiation(useCase, ServiceDocumentProcessor.class);
}