From af116170a2fe89dbaef3f859e65d596f79206c4e Mon Sep 17 00:00:00 2001 From: Archana Rai Date: Tue, 11 Apr 2017 13:52:29 +0530 Subject: [PATCH] [OLINGO-1106] JUnit for Custom Query Options in batch Signed-off-by: Christian Amend --- .../fit/tecsvc/http/BasicBatchITCase.java | 105 +++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java index 1fb31a208..fa8cb1b4a 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java @@ -70,6 +70,55 @@ public class BasicBatchITCase extends AbstractBaseTestITCase { reader.close(); } + + /* Tests for custom query options. Services may support additional custom query options + * not defined in the OData specification, but they MUST NOT begin with the "$" .*/ + + @Test + public void testCustomQuery1() throws IOException { + final String content = getRequest("ESAllPrim(32767)"); + final HttpURLConnection connection = batchWithCustomQuery(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: 605", reader.readLine()); + blankLine(reader); + + reader.close(); + } + + @Test + public void testCustomQuery2() throws IOException { + final String content = getRequest("ESAllPrim(32767)"); + final HttpURLConnection connection = batchWithCustomQuery(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: 605", reader.readLine()); + blankLine(reader); + + reader.close(); + } + + @Test + public void testCustomQuery3() throws IOException { + final String content = getRequest("ESAllPrim(32767)"); + batchFailWithCustomQuery(content, "$"); + } + + @Test public void testInvalidRelativeURI() throws IOException { @@ -144,10 +193,26 @@ public class BasicBatchITCase extends AbstractBaseTestITCase { return connection; } + + private HttpURLConnection batchWithCustomQuery(final String content, final String query) + throws IOException { + HttpURLConnection connection = getConnectionForCustomQuery(content, query); + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + return connection; + } + + private HttpURLConnection batchFailWithCustomQuery(final String content, final String query) + throws IOException { + final HttpURLConnection connection = getConnectionForCustomQuery(content, query); + + assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); + + return connection; + } private HttpURLConnection getConnection(final String content) throws MalformedURLException, IOException, ProtocolException { - final URL url = new URL(SERVICE_URI + "$batch"); + final URL url = getUrl(); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.POST.toString()); connection.setRequestProperty(HttpHeader.CONTENT_TYPE, CONTENT_TYPE_HEADER_VALUE); @@ -160,6 +225,44 @@ public class BasicBatchITCase extends AbstractBaseTestITCase { return connection; } + private HttpURLConnection getConnectionForCustomQuery(final String content, final String query) + throws MalformedURLException, IOException, ProtocolException { + URL url = null; + if(query.equals("$")){ + url = getUrlForCustomQueryWith$(); + }else if(query.equals("#")){ + url = getUrlWithSpecialCharacters(); + }else{ + url = getUrlForCustomQuery(); + } + 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; + } + + private URL getUrlWithSpecialCharacters() throws MalformedURLException { + return new URL(SERVICE_URI + "$batch" + "?#language=de"); + } + + private URL getUrlForCustomQuery() throws MalformedURLException { + return new URL(SERVICE_URI + "$batch" + "?language=de"); + } + + private URL getUrlForCustomQueryWith$() throws MalformedURLException { + return new URL(SERVICE_URI + "$batch" + "?$language=de"); + } + + private URL getUrl() throws MalformedURLException { + return new URL(SERVICE_URI + "$batch"); + } + @Override protected ODataClient getClient() { return null;