Skip to content

Commit

Permalink
Response callback support in asyncCounterpartXyz()
Browse files Browse the repository at this point in the history
  • Loading branch information
gj0dcsa committed Nov 23, 2023
1 parent df5bd39 commit deb9d02
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -15,6 +16,7 @@
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
import org.dcsa.conformance.standards.booking.party.BookingRole;
import org.dcsa.conformance.standards.booking.party.Carrier;
import org.dcsa.conformance.standards.booking.party.Shipper;
Expand All @@ -40,7 +42,7 @@ public List<ConformanceParty> createParties(
PartyConfiguration[] partyConfigurations,
CounterpartConfiguration[] counterpartConfigurations,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
Map<String, PartyConfiguration> partyConfigurationsByRoleName =
Arrays.stream(partyConfigurations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.StreamSupport;
import lombok.Builder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,17 +27,6 @@
import org.dcsa.conformance.core.traffic.ConformanceResponse;
import org.dcsa.conformance.standards.booking.action.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.StreamSupport;

@Slf4j
public class Carrier extends ConformanceParty {
private static final Random RANDOM = new Random();
Expand All @@ -42,7 +40,7 @@ public Carrier(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
super(
apiVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.party.ConformanceParty;
Expand All @@ -26,7 +27,7 @@ public Shipper(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
super(
apiVersion,
Expand Down Expand Up @@ -77,15 +78,27 @@ private void sendBookingRequest(JsonNode actionPrompt) {

JsonNode jsonRequestBody =
JsonToolkit.templateFileToJsonNode(
"/standards/booking/messages/booking-api-v20-request.json",
"/standards/booking/messages/booking-api-v20-request.json",
Map.ofEntries(
Map.entry(
"CARRIER_SERVICE_NAME_PLACEHOLDER",
carrierScenarioParameters.carrierServiceName()),
Map.entry(
"VESSEL_IMO_NUMBER_PLACEHOLDER", carrierScenarioParameters.vesselIMONumber())));

asyncCounterpartPost("/v2/bookings", jsonRequestBody);
asyncCounterpartPost(
"/v2/bookings",
jsonRequestBody,
conformanceResponse -> {
JsonNode jsonBody = conformanceResponse.message().body().getJsonBody();
String cbrr = jsonBody.get("carrierBookingRequestReference").asText();
String bookingStatus = jsonBody.get("bookingStatus").asText();
ObjectNode updatedBooking =
((ObjectNode) jsonRequestBody)
.put("bookingStatus", bookingStatus)
.put("carrierBookingRequestReference", cbrr);
persistentMap.save(cbrr, updatedBooking);
});

addOperatorLogEntry(
"Sent a booking request with the parameters: %s"
Expand All @@ -97,14 +110,12 @@ private void sendCancelEntireBooking(JsonNode actionPrompt) {
String cbrr = actionPrompt.get("cbrr").asText();

asyncCounterpartPatch(
"/v2/bookings/%s?operation=cancelBooking".formatted(cbrr),
new ObjectMapper().createObjectNode()
.put("bookingStatus", BookingState.CANCELLED.wireName())
);
"/v2/bookings/%s?operation=cancelBooking".formatted(cbrr),
new ObjectMapper()
.createObjectNode()
.put("bookingStatus", BookingState.CANCELLED.wireName()));

addOperatorLogEntry(
"Sent a cancel booking request of '%s'"
.formatted(cbrr));
addOperatorLogEntry("Sent a cancel booking request of '%s'".formatted(cbrr));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package org.dcsa.conformance.core;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import org.dcsa.conformance.core.check.JsonSchemaValidator;
import org.dcsa.conformance.core.party.ConformanceParty;
import org.dcsa.conformance.core.party.CounterpartConfiguration;
import org.dcsa.conformance.core.party.PartyConfiguration;
import org.dcsa.conformance.core.scenario.ScenarioListBuilder;
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;

public abstract class AbstractComponentFactory {
public abstract List<ConformanceParty> createParties(
PartyConfiguration[] partyConfigurations,
CounterpartConfiguration[] counterpartConfigurations,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader);

public abstract ScenarioListBuilder<?> createScenarioListBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
import lombok.SneakyThrows;
Expand All @@ -29,15 +30,14 @@ public abstract class ConformanceParty implements StatefulEntity {
protected final CounterpartConfiguration counterpartConfiguration;

/**
* Used to store full documents between steps.
* Unlike the state saved and loaded via exportJsonState and importJsonState,
* which is entirely (the whole map) stored within a single DynamoDB item,
* the items in this map are separately stored each in its own DynamoDB item.
* This is to avoid growing the state map past the size limit of DynamoDB items.
* Used to store full documents between steps. Unlike the state saved and loaded via
* exportJsonState and importJsonState, which is entirely (the whole map) stored within a single
* DynamoDB item, the items in this map are separately stored each in its own DynamoDB item. This
* is to avoid growing the state map past the size limit of DynamoDB items.
*/
protected final JsonNodeMap persistentMap;

private final Consumer<ConformanceRequest> asyncWebClient;
private final BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient;
private final Map<String, ? extends Collection<String>> orchestratorAuthHeader;
private final ActionPromptsQueue actionPromptsQueue = new ActionPromptsQueue();

Expand All @@ -49,7 +49,7 @@ public ConformanceParty(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
this.apiVersion = apiVersion;
this.partyConfiguration = partyConfiguration;
Expand Down Expand Up @@ -140,10 +140,15 @@ protected void asyncOrchestratorPostPartyInput(JsonNode jsonPartyInput) {
"orchestrator",
orchestratorAuthHeader,
new ConformanceMessageBody(jsonPartyInput),
System.currentTimeMillis())));
System.currentTimeMillis())),
conformanceResponse -> {});
}

protected void asyncCounterpartGet(String path) {
asyncCounterpartGet(path, conformanceResponse -> {});
}

protected void asyncCounterpartGet(String path, Consumer<ConformanceResponse> responseCallback) {
asyncWebClient.accept(
new ConformanceRequest(
"GET",
Expand All @@ -162,27 +167,40 @@ protected void asyncCounterpartGet(String path) {
counterpartConfiguration.getAuthHeaderName(),
List.of(counterpartConfiguration.getAuthHeaderValue())),
new ConformanceMessageBody(""),
System.currentTimeMillis())));
System.currentTimeMillis())),
responseCallback);
}

protected void asyncCounterpartPatch(String path, JsonNode jsonBody) {
_asyncCounterpartPatchPostOrPut("PATCH", path, Collections.emptyMap(), jsonBody);
asyncCounterpartPatch(path, jsonBody, conformanceResponse -> {});
}

protected void asyncCounterpartPost(String path, JsonNode jsonBody) {
_asyncCounterpartPatchPostOrPut("POST", path, Collections.emptyMap(), jsonBody);
asyncCounterpartPost(path, jsonBody, conformanceResponse -> {});
}

protected void asyncCounterpartPut(String path, JsonNode jsonBody) {
_asyncCounterpartPatchPostOrPut("PUT", path, Collections.emptyMap(), jsonBody);
asyncCounterpartPut(path, jsonBody, conformanceResponse -> {});
}

protected void asyncCounterpartPatch(String path, JsonNode jsonBody, Consumer<ConformanceResponse> responseCallback) {
_asyncCounterpartPatchPostOrPut("PATCH", path, Collections.emptyMap(), jsonBody, responseCallback);
}

protected void asyncCounterpartPost(String path, JsonNode jsonBody, Consumer<ConformanceResponse> responseCallback) {
_asyncCounterpartPatchPostOrPut("POST", path, Collections.emptyMap(), jsonBody, responseCallback);
}

protected void asyncCounterpartPut(String path, JsonNode jsonBody, Consumer<ConformanceResponse> responseCallback) {
_asyncCounterpartPatchPostOrPut("PUT", path, Collections.emptyMap(), jsonBody, responseCallback);
}

private void _asyncCounterpartPatchPostOrPut(
String method,
String path,
Map<String, ? extends Collection<String>> queryParams,
JsonNode jsonBody
) {
String method,
String path,
Map<String, ? extends Collection<String>> queryParams,
JsonNode jsonBody,
Consumer<ConformanceResponse> responseCallback) {
asyncWebClient.accept(
new ConformanceRequest(
method,
Expand All @@ -201,7 +219,8 @@ private void _asyncCounterpartPatchPostOrPut(
counterpartConfiguration.getAuthHeaderName(),
List.of(counterpartConfiguration.getAuthHeaderValue())),
new ConformanceMessageBody(jsonBody),
System.currentTimeMillis())));
System.currentTimeMillis())),
responseCallback);
}

public abstract ConformanceResponse handleRequest(ConformanceRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -15,6 +16,7 @@
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
import org.dcsa.conformance.standards.eblissuance.party.EblIssuanceCarrier;
import org.dcsa.conformance.standards.eblissuance.party.EblIssuancePlatform;
import org.dcsa.conformance.standards.eblissuance.party.EblIssuanceRole;
Expand All @@ -40,7 +42,7 @@ public List<ConformanceParty> createParties(
PartyConfiguration[] partyConfigurations,
CounterpartConfiguration[] counterpartConfigurations,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
Map<String, PartyConfiguration> partyConfigurationsByRoleName =
Arrays.stream(partyConfigurations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.party.ConformanceParty;
Expand Down Expand Up @@ -31,7 +32,7 @@ public EblIssuanceCarrier(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
super(
apiVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -13,7 +14,6 @@
import org.dcsa.conformance.core.party.PartyConfiguration;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.state.StateManagementUtil;
import org.dcsa.conformance.core.traffic.ConformanceMessageBody;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
Expand All @@ -30,7 +30,7 @@ public EblIssuancePlatform(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
super(
apiVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -15,6 +16,7 @@
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
import org.dcsa.conformance.standards.eblsurrender.party.EblSurrenderCarrier;
import org.dcsa.conformance.standards.eblsurrender.party.EblSurrenderPlatform;
import org.dcsa.conformance.standards.eblsurrender.party.EblSurrenderRole;
Expand All @@ -40,7 +42,7 @@ public List<ConformanceParty> createParties(
PartyConfiguration[] partyConfigurations,
CounterpartConfiguration[] counterpartConfigurations,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
Map<String, PartyConfiguration> partyConfigurationsByRoleName =
Arrays.stream(partyConfigurations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -13,7 +14,6 @@
import org.dcsa.conformance.core.party.PartyConfiguration;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.state.StateManagementUtil;
import org.dcsa.conformance.core.traffic.ConformanceMessageBody;
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
Expand All @@ -31,7 +31,7 @@ public EblSurrenderCarrier(
PartyConfiguration partyConfiguration,
CounterpartConfiguration counterpartConfiguration,
JsonNodeMap persistentMap,
Consumer<ConformanceRequest> asyncWebClient,
BiConsumer<ConformanceRequest, Consumer<ConformanceResponse>> asyncWebClient,
Map<String, ? extends Collection<String>> orchestratorAuthHeader) {
super(
apiVersion,
Expand Down
Loading

0 comments on commit deb9d02

Please sign in to comment.