Skip to content

Commit

Permalink
fix: Retain insertion order of batch request steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Aug 20, 2024
1 parent 9ad2008 commit 68c43c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* A class representing the content of a batch request.
*/
public class BatchRequestContent {
private HashMap<String, BatchRequestStep> batchRequestSteps;
private LinkedHashMap<String, BatchRequestStep> batchRequestSteps;
private RequestAdapter requestAdapter;
private final String maxStepsExceededMessage = String.format(Locale.US,ErrorConstants.Messages.MAXIMUM_VALUE_EXCEEDED, "Number of request steps", CoreConstants.BatchRequest.MAX_REQUESTS);

Expand Down Expand Up @@ -56,7 +56,7 @@ public BatchRequestContent(@Nonnull RequestAdapter requestAdapter, @Nonnull List
throw new IllegalArgumentException(maxStepsExceededMessage);
}

this.batchRequestSteps = new HashMap<>();
this.batchRequestSteps = new LinkedHashMap<>();
for (BatchRequestStep requestStep : batchRequestSteps) {
addBatchRequestStep(requestStep);
}
Expand All @@ -68,7 +68,7 @@ public BatchRequestContent(@Nonnull RequestAdapter requestAdapter, @Nonnull List
@Nonnull
public Map<String, BatchRequestStep> getBatchRequestSteps() {

return new HashMap<>(batchRequestSteps);
return new LinkedHashMap<>(batchRequestSteps);
}
/**
* Adds a batch request step to the batch request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.microsoft.graph.core.CoreConstants;
import com.microsoft.graph.core.ErrorConstants;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.microsoft.graph.core.BaseClient;
import com.microsoft.graph.core.models.BatchRequestStep;
import com.microsoft.graph.core.requests.IBaseClient;
Expand Down Expand Up @@ -62,6 +66,29 @@ void BatchRequestContent_InitializeWithInvalidDependsOnIds() {
assertEquals(ErrorConstants.Messages.INVALID_DEPENDS_ON_REQUEST_ID, ex.getMessage());
}
}

@Test
void BatchRequestContent_RetainsOrderOfBatchRequestSteps() throws Exception {
BatchRequestStep requestStep = new BatchRequestStep("1", defaultTestRequest);
BatchRequestStep requestStep2 = new BatchRequestStep("ab23", defaultTestRequest);
BatchRequestStep requestStep3 = new BatchRequestStep("b", defaultTestRequest, Arrays.asList("1"));

BatchRequestContent batchRequestContent = new BatchRequestContent(client, Arrays.asList(requestStep, requestStep2, requestStep3));
InputStream batchRequestPayload = batchRequestContent.getBatchRequestContent();
String requestContentString = readInputStream(batchRequestPayload);
JsonElement jsonElement = JsonParser.parseString(requestContentString);
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("requests")) {
JsonElement requests = jsonObject.get("requests");
assertTrue(requests.isJsonArray());
JsonArray jsonArray = requests.getAsJsonArray();
assertEquals(3, jsonArray.size());
assertEquals("1", jsonArray.get(0).getAsJsonObject().get("id").getAsString());
assertEquals("ab23", jsonArray.get(1).getAsJsonObject().get("id").getAsString());
assertEquals("b", jsonArray.get(2).getAsJsonObject().get("id").getAsString());
}
}

@Test
void BatchRequestContent_AddBatchRequestStepWithNewRequestStep() {
BatchRequestStep batchRequestStep = new BatchRequestStep("1", defaultTestRequest);
Expand Down

0 comments on commit 68c43c1

Please sign in to comment.