Java Code Examples for org.apache.olingo.odata2.api.ep.EntityProvider#parseBatchResponse()

The following examples show how to use org.apache.olingo.odata2.api.ep.EntityProvider#parseBatchResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatchResponseParserTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void tooBigContentLegthDoesNotResultInException() throws BatchException {
  String getResponse = "--batch_123" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-ID: 1" + CRLF
      + CRLF
      + "HTTP/1.1 200 OK" + CRLF
      + "DataServiceVersion: 2.0" + CRLF
      + "Content-Type: text/plain;charset=utf-8" + CRLF
      + "Content-Length: 100" + CRLF
      + CRLF
      + "Frederic Fall" + CRLF
      + "--batch_123--";

  InputStream in = new ByteArrayInputStream(getResponse.getBytes());
  List<BatchSingleResponse> batchResponse =
      EntityProvider.parseBatchResponse(in, "multipart/mixed;boundary=batch_123");
  BatchSingleResponse response = batchResponse.get(0);
  assertEquals("100", response.getHeader("Content-Length"));
  assertEquals("Frederic Fall", response.getBody());
}
 
Example 2
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleBatch() throws Exception {
  List<BatchPart> batch = new ArrayList<BatchPart>();
  BatchPart request = BatchQueryPart.method(ODataHttpMethod.GET.name()).uri("$metadata").build();
  batch.add(request);

  InputStream body = EntityProvider.writeBatchRequest(batch, BOUNDARY);
  String batchRequestBody = StringHelper.inputStreamToStringCRLFLineBreaks(body);
  checkMimeHeaders(batchRequestBody);
  checkBoundaryDelimiters(batchRequestBody);
  assertTrue(batchRequestBody.contains("GET $metadata HTTP/1.1"));

  HttpResponse batchResponse = execute(batchRequestBody);
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  for (BatchSingleResponse response : responses) {
    assertEquals("200", response.getStatusCode());
    assertEquals("OK", response.getStatusInfo());
    assertTrue(response.getBody().contains("<edmx:Edmx"));
    assertEquals("application/xml;charset=utf-8", response.getHeader(HttpHeaders.CONTENT_TYPE));
    assertNotNull(response.getHeader(HttpHeaders.CONTENT_LENGTH));
  }
}
 
Example 3
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleBatchWithAbsoluteUri() throws Exception {
  final String batchRequestBody = StringHelper.inputStreamToStringCRLFLineBreaks(
      EntityProvider.writeBatchRequest(
          Collections.<BatchPart> singletonList(
              BatchQueryPart
                  .method(ODataHttpMethod.GET.name())
                  .uri(getEndpoint().getPath() + "Employees('2')/EmployeeName/$value")
                  .build()),
          BOUNDARY));
  final HttpResponse batchResponse = execute(batchRequestBody);
  final List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(
      batchResponse.getEntity().getContent(),
      batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
  assertEquals(1, responses.size());
  final BatchSingleResponse response = responses.get(0);
  assertEquals(Integer.toString(HttpStatusCodes.OK.getStatusCode()), response.getStatusCode());
  assertEquals(HttpStatusCodes.OK.getInfo(), response.getStatusInfo());
  assertEquals(EMPLOYEE_2_NAME, response.getBody());
  assertEquals(HttpContentType.TEXT_PLAIN_UTF8, response.getHeader(HttpHeaders.CONTENT_TYPE));
  assertNotNull(response.getHeader(HttpHeaders.CONTENT_LENGTH));
}
 
Example 4
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void errorBatch() throws Exception {
  List<BatchPart> batch = new ArrayList<BatchPart>();
  BatchPart request = BatchQueryPart.method(ODataHttpMethod.GET.name())
      .uri("nonsense")
      .build();
  batch.add(request);

  InputStream body = EntityProvider.writeBatchRequest(batch, BOUNDARY);
  String bodyAsString = StringHelper.inputStreamToStringCRLFLineBreaks(body);
  checkMimeHeaders(bodyAsString);
  checkBoundaryDelimiters(bodyAsString);

  assertTrue(bodyAsString.contains("GET nonsense HTTP/1.1"));
  HttpResponse batchResponse = execute(bodyAsString);
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  for (BatchSingleResponse response : responses) {
    assertEquals("404", response.getStatusCode());
    assertEquals("Not Found", response.getStatusInfo());
  }
}
 
Example 5
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testContentIDReferencing() throws Exception {
  final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  post.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);

  String body = StringHelper.inputStreamToStringCRLFLineBreaks(
      this.getClass().getResourceAsStream("/basicBatchWithContentIdReferencing.batch"));
  HttpEntity entity = new StringEntity(body);
  post.setEntity(entity);
  HttpResponse batchResponse = getHttpClient().execute(post);

  assertNotNull(batchResponse);
  assertEquals(202, batchResponse.getStatusLine().getStatusCode());
  
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  assertEquals("201", responses.get(0).getStatusCode());
  assertEquals("Created", responses.get(0).getStatusInfo());
  assertEquals("201", responses.get(1).getStatusCode());
  assertEquals("Created", responses.get(1).getStatusInfo());
  assertEquals("200", responses.get(2).getStatusCode());
  assertEquals("OK", responses.get(2).getStatusInfo());
}
 
Example 6
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testContentIDReferencingfail() throws Exception {
  final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  post.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);

  String body = StringHelper.inputStreamToStringCRLFLineBreaks(
      this.getClass().getResourceAsStream("/basicBatchWithContentIdReferencingFail.batch"));
  HttpEntity entity = new StringEntity(body);
  post.setEntity(entity);
  HttpResponse batchResponse = getHttpClient().execute(post);

  assertNotNull(batchResponse);
  assertEquals(202, batchResponse.getStatusLine().getStatusCode());
  
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  assertEquals("400", responses.get(0).getStatusCode());
  assertEquals("Bad Request", responses.get(0).getStatusInfo());
  assertEquals("201", responses.get(1).getStatusCode());
  assertEquals("Created", responses.get(1).getStatusInfo());
  assertEquals("404", responses.get(2).getStatusCode());
  assertEquals("Not Found", responses.get(2).getStatusInfo());
  assertEquals("200", responses.get(3).getStatusCode());
  assertEquals("OK", responses.get(3).getStatusInfo());
}
 
Example 7
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testContentIDReferencingWithNav() throws Exception {
  final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  post.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);

  String body = StringHelper.inputStreamToStringCRLFLineBreaks(
      this.getClass().getResourceAsStream("/basicBatchWithContentIdWithNav.batch"));
  HttpEntity entity = new StringEntity(body);
  post.setEntity(entity);
  HttpResponse batchResponse = getHttpClient().execute(post);

  assertNotNull(batchResponse);
  assertEquals(202, batchResponse.getStatusLine().getStatusCode());
  
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  assertEquals("201", responses.get(0).getStatusCode());
  assertEquals("Created", responses.get(0).getStatusInfo());
  assertEquals("200", responses.get(1).getStatusCode());
  assertEquals("OK", responses.get(1).getStatusInfo());
  assertTrue(responses.get(1).getBody().contains("Building 100"));
}
 
Example 8
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testContentIDReferencingWithNavFail() throws Exception {
  final HttpPost post = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  post.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);

  String body = StringHelper.inputStreamToStringCRLFLineBreaks(
      this.getClass().getResourceAsStream("/basicBatchWithContentIdWithNavFail.batch"));
  HttpEntity entity = new StringEntity(body);
  post.setEntity(entity);
  HttpResponse batchResponse = getHttpClient().execute(post);

  assertNotNull(batchResponse);
  assertEquals(202, batchResponse.getStatusLine().getStatusCode());
  
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  assertEquals("400", responses.get(0).getStatusCode());
  assertEquals("Bad Request", responses.get(0).getStatusInfo());
  assertTrue(responses.get(0).getBody().contains("The request body is malformed."));
  assertEquals("404", responses.get(1).getStatusCode());
  assertEquals("Not Found", responses.get(1).getStatusInfo());
  assertTrue(responses.get(1).getBody().contains(
      "Could not find an entity set or function import for '$2'."));
}
 
Example 9
Source File: BatchResponseParserTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void boundaryInBodyMustBeIgnored() throws BatchException {
  String getResponse = "--batch_123" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + CRLF
      + "HTTP/1.1 200 OK" + CRLF
      + "Content-Type: text/plain;charset=utf-8" + CRLF
      + "Content-Length: 13" + CRLF
      + CRLF
      + "Frederic Fall" + CRLF
      + CRLF
      + "batch_123" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + CRLF
      + "HTTP/1.1 200 OK" + CRLF
      + "Content-Type: text/plain;charset=utf-8" + CRLF
      + CRLF
      + "Walter Winter" + CRLF
      + CRLF
      + "--batch_123--";
  InputStream in = new ByteArrayInputStream(getResponse.getBytes());
  List<BatchSingleResponse> batchResponse =
      EntityProvider.parseBatchResponse(in, "multipart/mixed;boundary=batch_123");
  BatchSingleResponse response = batchResponse.get(0);
  assertEquals("13", response.getHeader("Content-Length"));
  assertEquals("Frederic Fall", response.getBody());
}
 
Example 10
Source File: BatchTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchWithChangesetWithRawBytesInPutOperation() throws Exception {
  InputStream requestPayload = createBatchRequestWithRawBytes(PUT);
  final HttpPost put = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  put.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);
  HttpEntity entity = new InputStreamEntity(requestPayload, -1);
  put.setEntity(entity);
  HttpResponse response = getHttpClient().execute(put);
  byte[] actualData = Util.getInstance().getBinaryContent();
  byte[] expectedData = rawBytes();
  // Comparing data stored in the data source and the data sent in the request
  assertArrayEquals(actualData, expectedData);
  
  assertNotNull(response);
  assertEquals(202, response.getStatusLine().getStatusCode());
  String responseBody = StringHelper.inputStreamToStringCRLFLineBreaks(response.getEntity().getContent());
  assertTrue(responseBody.contains("204 No Content"));
  
  HttpResponse resp = execute("/simpleGet.batch", BOUNDARY);
  InputStream in = resp.getEntity().getContent();
  StringHelper.Stream batchRequestStream = StringHelper.toStream(in);
  String requestBody = batchRequestStream.asString();
  
  String contentType = resp.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(
      new ByteArrayInputStream(requestBody.getBytes("iso-8859-1")), contentType);
  for (BatchSingleResponse batchResp : responses) {
    assertEquals("200", batchResp.getStatusCode());
    assertEquals("OK", batchResp.getStatusInfo());
    assertArrayEquals(batchResp.getBody().getBytes("iso-8859-1"), actualData);
  }
}
 
Example 11
Source File: BatchTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchWithChangesetWithRawBytesInPOSTOperation() throws Exception {
  InputStream requestPayload = createBatchRequestWithRawBytes(POST);
  final HttpPost put = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  put.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);
  HttpEntity entity = new InputStreamEntity(requestPayload, -1);
  put.setEntity(entity);
  HttpResponse response = getHttpClient().execute(put);
  byte[] actualData = Util.getInstance().getBinaryContent();
  byte[] expectedData = rawBytes();
  // Comparing data stored in the data source and the data sent in the request
  assertArrayEquals(actualData, expectedData);
  
  assertNotNull(response);
  assertEquals(202, response.getStatusLine().getStatusCode());
  String responseBody = StringHelper.inputStreamToStringCRLFLineBreaks(response.getEntity().getContent());
  assertTrue(responseBody.contains("201 Created"));
  
  HttpResponse resp = execute("/simpleGet1.batch", BOUNDARY);
  InputStream in = resp.getEntity().getContent();
  StringHelper.Stream batchRequestStream = StringHelper.toStream(in);
  String requestBody = batchRequestStream.asString();
  
  String contentType = resp.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(
      new ByteArrayInputStream(requestBody.getBytes("iso-8859-1")), contentType);
  for (BatchSingleResponse batchResp : responses) {
    assertEquals("200", batchResp.getStatusCode());
    assertEquals("OK", batchResp.getStatusInfo());
    assertArrayEquals(batchResp.getBody().getBytes("iso-8859-1"), expectedData);
  }
}
 
Example 12
Source File: BatchTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchWithChangesetWithImageObjectInPutOperation() throws Exception {
  InputStream requestPayload = createBatchRequestWithImage("/Employee_1.png", PUT);
  
  final HttpPost put = new HttpPost(URI.create(getEndpoint().toString() + "$batch"));
  put.setHeader("Content-Type", "multipart/mixed;boundary=" + BOUNDARY);
  HttpEntity entity = new InputStreamEntity(requestPayload, -1);
  put.setEntity(entity);
  HttpResponse response = getHttpClient().execute(put);
  byte[] actualData = Util.getInstance().getBinaryContent();
  byte[] expectedData = getImageData("/Employee_1.png");
  // Comparing data stored in the data source and the data sent in the request
  assertArrayEquals(actualData, expectedData);
  
  assertNotNull(response);
  assertEquals(202, response.getStatusLine().getStatusCode());
  String responseBody = StringHelper.inputStreamToStringCRLFLineBreaks(response.getEntity().getContent());
  assertTrue(responseBody.contains("204 No Content"));
  
  HttpResponse resp = execute("/simpleGet.batch", BOUNDARY);
  InputStream in = resp.getEntity().getContent();
  StringHelper.Stream batchRequestStream = StringHelper.toStream(in);
  String requestBody = batchRequestStream.asString();
  
  String contentType = resp.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(
      new ByteArrayInputStream(requestBody.getBytes("iso-8859-1")), contentType);
  for (BatchSingleResponse batchResp : responses) {
    assertEquals("200", batchResp.getStatusCode());
    assertEquals("OK", batchResp.getStatusInfo());
    assertArrayEquals(batchResp.getBody().getBytes("iso-8859-1"), actualData);
  }
}
 
Example 13
Source File: ClientBatchTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testContentFormatErrorBatch() throws Exception {
  List<BatchPart> batch = new ArrayList<BatchPart>();
  Map<String, String> headers = new HashMap<String, String>();
  headers.put("DataServiceVersion", "2.0");
  headers.put("MaxDataServiceVersion", "3.0");
  headers.put("Accept", "application/json;odata=verbose");
  BatchPart request = BatchQueryPart.method(ODataHttpMethod.GET.name())
      .uri("nonsense")
      .headers(headers)
      .build();
  batch.add(request);

  InputStream body = EntityProvider.writeBatchRequest(batch, BOUNDARY);
  String bodyAsString = StringHelper.inputStreamToStringCRLFLineBreaks(body);
  checkMimeHeaders(bodyAsString);
  checkBoundaryDelimiters(bodyAsString);

  assertTrue(bodyAsString.contains("GET nonsense HTTP/1.1"));
  HttpResponse batchResponse = execute(bodyAsString);
  InputStream responseBody = batchResponse.getEntity().getContent();
  String contentType = batchResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
  List<BatchSingleResponse> responses = EntityProvider.parseBatchResponse(responseBody, contentType);
  for (BatchSingleResponse response : responses) {
    assertEquals("404", response.getStatusCode());
    assertEquals("Not Found", response.getStatusInfo());
    assertEquals("application/json", response.getHeaders().get("Content-Type"));
    assertEquals("{\"error\":{\"code\":null,\"message\":{\"lang\":\"en\",\"value\":"
        + "\"Could not find an entity set or function import for 'nonsense'.\"}}}", response.getBody());
  }
}