public class BatchResponseTest {
@Test
public void testBatchResponse() throws BatchException, IOException {
List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
ODataResponse response = ODataResponse.entity("Walter Winter")
.status(HttpStatusCodes.OK)
.contentHeader("application/json")
.build();
List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
responses.add(response);
parts.add(BatchResponsePart.responses(responses).changeSet(false).build());
ODataResponse changeSetResponse = ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
responses = new ArrayList<ODataResponse>(1);
responses.add(changeSetResponse);
parts.add(BatchResponsePart.responses(responses).changeSet(true).build());
BatchResponseWriter writer = new BatchResponseWriter();
ODataResponse batchResponse = writer.writeResponse(parts);
assertEquals(202, batchResponse.getStatus().getStatusCode());
assertNotNull(batchResponse.getEntity());
String body = (String) batchResponse.getEntity();
assertTrue(body.contains("--batch"));
assertTrue(body.contains("--changeset"));
assertTrue(body.contains("HTTP/1.1 200 OK"));
assertTrue(body.contains("Content-Type: application/http"));
assertTrue(body.contains("Content-Transfer-Encoding: binary"));
assertTrue(body.contains("Walter Winter"));
assertTrue(body.contains("multipart/mixed; boundary=changeset"));
assertTrue(body.contains("HTTP/1.1 204 No Content"));
String contentHeader = batchResponse.getContentHeader();
BatchResponseParser parser = new BatchResponseParser(contentHeader);
List<BatchSingleResponse> result = parser.parse(new ByteArrayInputStream(body.getBytes()));
assertEquals(2, result.size());
}