From c096bbf15c678abce336604abbcbc61d1c8e01a6 Mon Sep 17 00:00:00 2001 From: Christian Holzer Date: Mon, 12 Jan 2015 15:00:21 +0100 Subject: [PATCH] [OLINGO472] BatchResponseWriter tests improved Signed-off-by: Christian Amend --- .../serializer/BatchResponseSerializer.java | 1 - .../batchhandler/MockedBatchHandlerTest.java | 17 ++---- .../BatchResponseSerializerTest.java | 60 +++++++++++++++++-- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java index d8db126ec..a8d789863 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java @@ -80,7 +80,6 @@ public class BatchResponseSerializer { } builder.append(getCloseDelimiter(changeSetBoundary)); - builder.append(CRLF); } private void appendBodyPart(ODataResponse response, StringBuilder builder, boolean isChangeSet) diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java index cad50b2b4..86c336e85 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java @@ -153,7 +153,7 @@ public class MockedBatchHandlerTest { reader.close(); int line = 0; - assertEquals(63, responseContent.size()); + assertEquals(62, responseContent.size()); // Check change set assertTrue(responseContent.get(line++).contains("--batch_")); @@ -187,7 +187,6 @@ public class MockedBatchHandlerTest { } else { fail(); } - assertEquals(CRLF, responseContent.get(line++)); } @@ -196,9 +195,8 @@ public class MockedBatchHandlerTest { assertTrue(responseContent.get(line++).contains("--changeset_")); // Close batch - assertEquals(CRLF, responseContent.get(line++)); assertTrue(responseContent.get(line++).contains("--batch_")); - assertEquals(63, line); + assertEquals(62, line); } @Test @@ -305,7 +303,7 @@ public class MockedBatchHandlerTest { reader.close(); int line = 0; - assertEquals(49, responseContent.size()); + assertEquals(47, responseContent.size()); // Check first change set assertTrue(responseContent.get(line++).contains("--batch_")); @@ -333,7 +331,6 @@ public class MockedBatchHandlerTest { assertTrue(responseContent.get(line++).contains("--changeset_")); // Check second change set - assertEquals(CRLF, responseContent.get(line++)); assertTrue(responseContent.get(line++).contains("--batch_")); assertTrue(responseContent.get(line++).contains("Content-Type: multipart/mixed; boundary=changeset_")); @@ -359,10 +356,9 @@ public class MockedBatchHandlerTest { assertTrue(responseContent.get(line++).contains("--changeset_")); // Close batch - assertEquals(CRLF, responseContent.get(line++)); assertTrue(responseContent.get(line++).contains("--batch_")); - assertEquals(49, line); + assertEquals(47, line); } @Test @@ -425,7 +421,7 @@ public class MockedBatchHandlerTest { reader.close(); int line = 0; - assertEquals(45, responseContent.size()); + assertEquals(44, responseContent.size()); // Check change set assertTrue(responseContent.get(line++).contains("--batch_")); @@ -462,9 +458,8 @@ public class MockedBatchHandlerTest { assertTrue(responseContent.get(line++).contains("--changeset_")); // Close batch - assertEquals(CRLF, responseContent.get(line++)); assertTrue(responseContent.get(line++).contains("--batch_")); - assertEquals(45, line); + assertEquals(44, line); } @Test(expected = BatchDeserializerException.class) diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java index ce88c9280..85a30acce 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java @@ -70,7 +70,7 @@ public class BatchResponseSerializerTest { reader.close(); int line = 0; - assertEquals(25, body.size()); + assertEquals(24, body.size()); assertTrue(body.get(line++).contains("--batch_")); assertEquals("Content-Type: application/http" + CRLF, body.get(line++)); assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++)); @@ -94,10 +94,63 @@ public class BatchResponseSerializerTest { assertEquals(CRLF, body.get(line++)); assertEquals(CRLF, body.get(line++)); assertTrue(body.get(line++).contains("--changeset_")); - assertEquals(CRLF, body.get(line++)); assertTrue(body.get(line++).contains("--batch_")); } + @Test + public void testBatchResponseWithEndingCRLF() throws Exception { + final List parts = new ArrayList(); + ODataResponse response = new ODataResponse(); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, "application/json"); + response.setContent(IOUtils.toInputStream("Walter Winter")); + + List responses = new ArrayList(1); + responses.add(response); + parts.add(new ODataResponsePart(responses, false)); + + ODataResponse changeSetResponse = new ODataResponse(); + changeSetResponse.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); + changeSetResponse.setHeader(BatchParserCommon.HTTP_CONTENT_ID, "1"); + responses = new ArrayList(1); + responses.add(changeSetResponse); + parts.add(new ODataResponsePart(responses, true)); + + BatchResponseSerializer serializer = new BatchResponseSerializer(); + final InputStream content = serializer.serialize(parts, BOUNDARY); + assertNotNull(content); + final BufferedReaderIncludingLineEndings reader = + new BufferedReaderIncludingLineEndings(new InputStreamReader(content)); + final List body = reader.toList(); + reader.close(); + + int line = 0; + assertEquals(23, body.size()); + assertTrue(body.get(line++).contains("--batch_")); + assertEquals("Content-Type: application/http" + CRLF, body.get(line++)); + assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("HTTP/1.1 200 OK" + CRLF, body.get(line++)); + assertEquals("Content-Type: application/json" + CRLF, body.get(line++)); + assertEquals("Content-Length: 13" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("Walter Winter" + CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--batch_")); + assertTrue(body.get(line++).contains("Content-Type: multipart/mixed; boundary=changeset_")); + assertEquals(CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--changeset_")); + assertEquals("Content-Type: application/http" + CRLF, body.get(line++)); + assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++)); + assertEquals("Content-Id: 1" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("HTTP/1.1 204 No Content" + CRLF, body.get(line++)); + assertEquals("Content-Length: 0" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--changeset_")); + assertTrue(body.get(line++).contains("--batch_")); + } + @Test public void testResponse() throws Exception { List parts = new ArrayList(); @@ -155,7 +208,7 @@ public class BatchResponseSerializerTest { reader.close(); int line = 0; - assertEquals(15, body.size()); + assertEquals(14, body.size()); assertTrue(body.get(line++).contains("--batch_")); assertTrue(body.get(line++).contains("Content-Type: multipart/mixed; boundary=changeset_")); assertEquals(CRLF, body.get(line++)); @@ -169,7 +222,6 @@ public class BatchResponseSerializerTest { assertEquals(CRLF, body.get(line++)); assertEquals(CRLF, body.get(line++)); assertTrue(body.get(line++).contains("--changeset_")); - assertEquals(CRLF, body.get(line++)); assertTrue(body.get(line++).contains("--batch_")); } }