Skip to content

Commit

Permalink
fix: Improve exception handling in ODataRequestResultMultipartGeneric…
Browse files Browse the repository at this point in the history
… class (#126)

Co-authored-by: Alexander Dümont <[email protected]>
  • Loading branch information
CharlesDuboisSAP and newtork authored Nov 8, 2023
1 parent 1b86d93 commit 1fce72e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public ODataRequestResultGeneric getResult( @Nonnull final ODataRequestGeneric r
}

log.debug("Looking for request {} in batch response at position {}", request, responsePosition);
final List<HttpResponse> subResponses = getBatchedResponses().get(responsePosition._1());
final List<List<HttpResponse>> batchResponseItems = getBatchedResponses();
if( responsePosition._1() >= batchResponseItems.size() ) {
String msg = "Unable to extract batch response item at position %s. The response contains only %s items.";
msg = String.format(msg, responsePosition._1() + 1, batchResponseItems.size());
throw new ODataResponseException(batchRequest, httpResponse, msg, null);
}
final List<HttpResponse> subResponses = batchResponseItems.get(responsePosition._1());

final boolean isSingleResponse = responsePosition._2() == null || responsePosition._2() >= subResponses.size();
final HttpResponse response = subResponses.get(isSingleResponse ? 0 : responsePosition._2());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ public void testBatchWithOnlyReads()
assertThat(resultRead2).isNotNull().hasSize(2).doesNotContainNull().doesNotContainAnyElementsOf(resultRead1);
}

@Test
public void testBatchWithReadsOnMissingResponse()
{
// 2 requests but only 1 response
final String requestBody = readResourceFileCrlf("BatchOnlyReadsMissingRequest.txt");
final String responseBody = readResourceFileCrlf("BatchOnlyReadsMissingResponse.txt");

// Prepare test objects
final ODataEntityKey entityKey1 = new ODataEntityKey(V4).addKeyProperty("key", "one");
final ODataRequestReadByKey readByKey1 = new ODataRequestReadByKey("/", "People", entityKey1, "", V4);
final ODataEntityKey entityKey2 = new ODataEntityKey(V4).addKeyProperty("key", "two");
final ODataRequestReadByKey readByKey2 = new ODataRequestReadByKey("/", "People", entityKey2, "", V4);
final ODataEntityKey entityKey3 = new ODataEntityKey(V4).addKeyProperty("key", "three");
final ODataRequestReadByKey readByKey3 = new ODataRequestReadByKey("/", "People", entityKey3, "", V4);
final ODataRequestBatch batchRequest =
new ODataRequestBatch("/", V4, uuidProvider)
.addReadByKey(readByKey1)
.addReadByKey(readByKey2)
.addReadByKey(readByKey3);

final HttpClient httpClient = MockedHttpClient.of(requestBody, responseBody);
final ODataRequestResultMultipartGeneric batchResponse = batchRequest.execute(httpClient);

// Test assertion: response object not null and healthy
assertThat(batchResponse).isNotNull();
assertThat(batchResponse.getHttpResponse().getStatusLine().getStatusCode()).isEqualTo(200);

// Test assertion:
// response payload1 is 200
assertThat(batchResponse.getResult(readByKey1)).isNotNull();

// response payload2 is 404
assertThatExceptionOfType(ODataServiceErrorException.class)
.isThrownBy(() -> batchResponse.getResult(readByKey2))
.satisfies(e -> assertThat(e.getHttpCode()).isEqualTo(404));

// response payload3 cannot be extracted, response is missing
assertThatExceptionOfType(ODataResponseException.class)
.isThrownBy(() -> batchResponse.getResult(readByKey3))
.withMessage("Unable to extract batch response item at position 3. The response contains only 2 items.");
}

@Test
public void testBatchWithErrorReads()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--batch_00000000-0000-0000-0000-000000000001
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

GET People(%27one%27) HTTP/1.1
Accept: application/json


--batch_00000000-0000-0000-0000-000000000001
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

GET People(%27two%27) HTTP/1.1
Accept: application/json


--batch_00000000-0000-0000-0000-000000000001
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 3

GET People(%27three%27) HTTP/1.1
Accept: application/json


--batch_00000000-0000-0000-0000-000000000001--
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--batchresponse_76ef6b0a-a0e2-4f31-9f70-f5d3f73a6bef
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
OData-Version: 4.0

{"@odata.context":"https://services.odata.org/TripPinRESTierService/(S(w3zgpoiit3rigb4hkixmletd))/$metadata#People","value":[{"UserName":"one"}]}
--batchresponse_76ef6b0a-a0e2-4f31-9f70-f5d3f73a6bef
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 404 Not Found
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
OData-Version: 4.0

{"error":{"code":"","message":"The request resource is not found."}}
--batchresponse_76ef6b0a-a0e2-4f31-9f70-f5d3f73a6bef--

0 comments on commit 1fce72e

Please sign in to comment.