Fit Tests

Signed-off-by: Christian Amend <chrisam@apache.org>
This commit is contained in:
Christian Holzer 2014-12-09 13:40:57 +01:00 committed by Christian Amend
parent 5f18ea2e09
commit aab7eaf6e3
12 changed files with 248 additions and 66 deletions

View File

@ -191,7 +191,66 @@ public class BatchClientITCase extends AbstractTestITCase {
// Check if third request is available
assertFalse(iter.hasNext());
}
@Test
public void testInvalidAbsoluteUri() throws URISyntaxException {
final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(SERVICE_URI);
request.setAccept(ACCEPT);
final BatchManager payload = request.payloadManager();
final URI uri = new URI(SERVICE_URI + "/../ESAllPrim(32767)");
final ODataEntityRequest<ODataEntity> queryReq = client.getRetrieveRequestFactory().getEntityRequest(uri);
queryReq.setFormat(ODataFormat.JSON);
payload.addRequest(queryReq);
// Fetch result
final ODataBatchResponse response = payload.getResponse();
assertEquals(202, response.getStatusCode());
final Iterator<ODataBatchResponseItem> bodyIterator = response.getBody();
assertTrue(bodyIterator.hasNext());
ODataBatchResponseItem item = bodyIterator.next();
assertFalse(item.isChangeset());
final ODataResponse oDataResponse = item.next();
assertEquals(400, oDataResponse.getStatusCode());
}
@Test
@Ignore
public void testInvalidHost() throws URISyntaxException {
final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(SERVICE_URI);
request.setAccept(ACCEPT);
final BatchManager payload = request.payloadManager();
final URI uri = new URI("http://otherhost/odata/ESAllPrim(32767)");
final ODataEntityRequest<ODataEntity> queryReq = client.getRetrieveRequestFactory().getEntityRequest(uri);
queryReq.setFormat(ODataFormat.JSON);
payload.addRequest(queryReq);
// Fetch result
final ODataBatchResponse response = payload.getResponse();
assertEquals(400, response.getStatusCode());
}
@Test
@Ignore
public void testInvalidAbsoluteRequest() throws URISyntaxException {
final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(SERVICE_URI);
request.setAccept(ACCEPT);
final BatchManager payload = request.payloadManager();
final URI uri = new URI("/ESAllPrim(32767)");
final ODataEntityRequest<ODataEntity> queryReq = client.getRetrieveRequestFactory().getEntityRequest(uri);
queryReq.setFormat(ODataFormat.JSON);
payload.addRequest(queryReq);
// Fetch result
final ODataBatchResponse response = payload.getResponse();
assertEquals(400, response.getStatusCode());
}
@Test
public void testErrorWithContinueOnErrorPreferHeader() throws URISyntaxException {
client.getConfiguration().setContinueOnError(true);

View File

@ -0,0 +1,162 @@
/*
* 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.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.fit.AbstractBaseTestITCase;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Test;
public class BasicBatchITCase extends AbstractBaseTestITCase {
private static final String HEADER_CONTENT_TRANSFER_ENCODING_BINARY = "Content-Transfer-Encoding: binary";
private static final String HEADER_CONTENT_TYPE_HTTP = "Content-Type: application/http";
private static final String SERVICE_URI = TecSvcConst.BASE_URI + "/";
private static final String CONTENT_TYPE_HEADER_VALUE = " multipart/mixed;boundary=batch_123";
private static final String CRLF = "\r\n";
private static final String ACCEPT_HEADER_VALUE = "application/json";
@Test
public void test() throws IOException {
final String content = getRequest("ESAllPrim(32767)");
final HttpURLConnection connection = batch(content);
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
assertTrue(reader.readLine().contains("batch_"));
checkMimeHeader(reader);
blankLine(reader);
assertEquals("HTTP/1.1 200 OK", reader.readLine());
assertEquals("OData-Version: 4.0", reader.readLine());
assertEquals("Content-Type: application/json;odata.metadata=minimal", reader.readLine());
assertEquals("Content-Length: 538", reader.readLine());
blankLine(reader);
reader.close();
}
@Test
public void testInvalidRelativeURI() throws IOException {
final String content = getRequest("/ESAllPrim(32767)");
batchFail(content);
}
@Test
public void testInvalidAbsoluteURI() throws IOException {
final String content = getRequest(SERVICE_URI + "../ESAllPrim(32767)");
HttpURLConnection connection = batch(content);
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
assertTrue(reader.readLine().contains("batch_"));
checkMimeHeader(reader);
blankLine(reader);
assertEquals("HTTP/1.1 400 Bad Request", reader.readLine());
}
@Test
public void testNestedAbsoluteRequest() throws IOException {
final String content = getRequest(SERVICE_URI + SERVICE_URI + "../ESAllPrim(32767)");
HttpURLConnection connection = batch(content);
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
assertTrue(reader.readLine().contains("batch_"));
checkMimeHeader(reader);
blankLine(reader);
assertEquals("HTTP/1.1 400 Bad Request", reader.readLine());
}
@Test
public void testInvalidHost() throws IOException {
final String content = getRequest("http://otherhost/odata/odata.svc/ESAllPrim(32767)");
batchFail(content);
}
private void checkMimeHeader(final BufferedReader reader) throws IOException {
assertEquals(HEADER_CONTENT_TYPE_HTTP, reader.readLine());
assertEquals(HEADER_CONTENT_TRANSFER_ENCODING_BINARY, reader.readLine());
}
private void blankLine(BufferedReader reader) throws IOException {
assertEquals("", reader.readLine()); // CRLF becomes to an empty string
}
private String getRequest(String uri) {
return "--batch_123" + CRLF
+ HEADER_CONTENT_TRANSFER_ENCODING_BINARY + CRLF
+ HEADER_CONTENT_TYPE_HTTP + CRLF
+ CRLF
+ "GET " + uri + " HTTP/1.1" + CRLF
+ CRLF
+ CRLF
+ "--batch_123--";
}
private HttpURLConnection batch(final String content) throws IOException {
final HttpURLConnection connection = getConnection(content);
assertEquals(HttpStatusCode.ACCEPTED.getStatusCode(), connection.getResponseCode());
return connection;
}
private HttpURLConnection batchFail(String content) throws IOException {
final HttpURLConnection connection = getConnection(content);
assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());
return connection; }
private HttpURLConnection getConnection(String content) throws MalformedURLException, IOException, ProtocolException {
final URL url = new URL(SERVICE_URI + "$batch");
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(HttpMethod.POST.toString());
connection.setRequestProperty(HttpHeader.CONTENT_TYPE, CONTENT_TYPE_HEADER_VALUE);
connection.setRequestProperty(HttpHeader.ACCEPT, ACCEPT_HEADER_VALUE);
connection.setDoOutput(true);
final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.append(content);
writer.close();
connection.connect();
return connection;
}
@Override
protected CommonODataClient<?> getClient() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -18,7 +18,9 @@
*/
package org.apache.olingo.server.api.batch.exception;
public class BatchDeserializerException extends BatchException {
import org.apache.olingo.server.api.deserializer.DeserializerException;
public class BatchDeserializerException extends DeserializerException {
public static enum MessageKeys implements MessageKey {
INVALID_BOUNDARY,
INVALID_CHANGESET_METHOD,
@ -60,8 +62,4 @@ public class BatchDeserializerException extends BatchException {
super(developmentMessage, messageKey, parameters);
}
@Override
protected String getBundleName() {
return DEFAULT_SERVER_BUNDLE_NAME;
}
}

View File

@ -1,47 +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.batch.exception;
import org.apache.olingo.server.api.ODataTranslatedException;
public class BatchException extends ODataTranslatedException {
private static final long serialVersionUID = 8747815702545202733L;
public static enum MessageKeys implements MessageKey {
;
@Override
public String getKey() {
return name();
}
}
public BatchException(final String developmentMessage, final MessageKey messageKey, final int lineNumber) {
this(developmentMessage, messageKey, "" + lineNumber);
}
public BatchException(final String developmentMessage, final MessageKey messageKey, final String... parameters) {
super(developmentMessage, messageKey, parameters);
}
@Override
protected String getBundleName() {
return DEFAULT_SERVER_BUNDLE_NAME;
}
}

View File

@ -18,7 +18,9 @@
*/
package org.apache.olingo.server.api.batch.exception;
public class BatchSerializerExecption extends BatchException {
import org.apache.olingo.server.api.serializer.SerializerException;
public class BatchSerializerExecption extends SerializerException {
private static final long serialVersionUID = 2634433974342796905L;

View File

@ -23,14 +23,14 @@ import java.util.List;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.batch.BatchFacade;
import org.apache.olingo.server.api.batch.exception.BatchException;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
import org.apache.olingo.server.api.serializer.SerializerException;
public interface BatchProcessor extends Processor {
// TODO:Check exception signature
void executeBatch(BatchFacade facade, ODataRequest request, ODataResponse response)
throws SerializerException, BatchException;
throws SerializerException, DeserializerException;
ODataResponsePart executeChangeSet(BatchFacade facade, List<ODataRequest> requests);
}

View File

@ -25,6 +25,7 @@ import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.ODataServerError;
import org.apache.olingo.server.api.ODataTranslatedException;
import org.apache.olingo.server.api.ODataTranslatedException.ODataErrorMessage;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.core.uri.parser.UriParserException;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
@ -93,7 +94,13 @@ public class ODataExceptionHelper {
serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode());
return serverError;
}
public static ODataServerError createServerErrorObject(BatchDeserializerException e, Locale requestedLocale) {
ODataServerError serverError = basicTranslatedError(e, requestedLocale);
serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode());
return serverError;
}
public static ODataServerError createServerErrorObject(ODataTranslatedException e, Locale requestedLocale) {
return basicTranslatedError(e, requestedLocale);
}

View File

@ -36,7 +36,7 @@ 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.batch.exception.BatchException;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.processor.BatchProcessor;
import org.apache.olingo.server.api.processor.ComplexCollectionProcessor;
@ -110,6 +110,9 @@ public class ODataHandler {
} catch (SerializerException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError);
} catch (BatchDeserializerException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError);
} catch (DeserializerException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError);
@ -128,7 +131,7 @@ public class ODataHandler {
private void processInternal(final ODataRequest request, final ODataResponse response)
throws ODataHandlerException, UriParserException, UriValidationException, ContentNegotiatorException,
ODataApplicationException, SerializerException, DeserializerException, BatchException {
ODataApplicationException, SerializerException, DeserializerException {
validateODataVersion(request, response);
uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null,

View File

@ -25,7 +25,7 @@ import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.batch.BatchFacade;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException.MessageKeys;
import org.apache.olingo.server.api.batch.exception.BatchException;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.processor.BatchProcessor;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.apache.olingo.server.core.ODataHandler;
@ -42,7 +42,7 @@ public class BatchHandler {
}
public void process(final ODataRequest request, final ODataResponse response, final boolean isStrict)
throws SerializerException, BatchException {
throws DeserializerException, SerializerException {
validateRequest(request);
final BatchFacade operation = new BatchFascadeImpl(oDataHandler, request, batchProcessor, isStrict);

View File

@ -40,7 +40,6 @@ import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.batch.BatchFacade;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException;
import org.apache.olingo.server.api.batch.exception.BatchException;
import org.apache.olingo.server.api.deserializer.batch.BatchOptions;
import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart;
import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
@ -596,7 +595,7 @@ public class MockedBatchHandlerTest {
@Override
public void executeBatch(BatchFacade fascade, ODataRequest request, ODataResponse response)
throws BatchException, SerializerException {
throws SerializerException, BatchDeserializerException {
final String boundary = getBoundary(request.getHeader(HttpHeader.CONTENT_TYPE));
final BatchOptions options = BatchOptions.with().isStrict(true).rawBaseUri(BASE_URI).build();
final List<BatchRequestPart> parts =

View File

@ -44,7 +44,7 @@ public class HttpRequestStatusLineTest {
@Test
public void testAbsoluteWithRelativePath() throws BatchDeserializerException {
final HttpRequestStatusLine line = parse("http://localhost/odata../../Employee?$top=2");
final HttpRequestStatusLine line = parse("http://localhost/odata/../../Employee?$top=2");
assertEquals("/../../Employee", line.getRawODataPath());
assertEquals("$top=2", line.getRawQueryPath());
assertEquals("http://localhost/odata/../../Employee?$top=2", line.getRawRequestUri());

View File

@ -32,7 +32,6 @@ import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.batch.BatchFacade;
import org.apache.olingo.server.api.batch.exception.BatchDeserializerException;
import org.apache.olingo.server.api.batch.exception.BatchException;
import org.apache.olingo.server.api.deserializer.batch.BatchOptions;
import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart;
import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart;
@ -50,7 +49,7 @@ public class TechnicalBatchProcessor extends TechnicalProcessor implements Batch
@Override
public void executeBatch(BatchFacade fascade, ODataRequest request, ODataResponse response)
throws SerializerException, BatchException {
throws SerializerException, BatchDeserializerException {
// TODO refactor isContinueOnError
boolean continueOnError = isContinueOnError(request);