Skip to content

Commit

Permalink
Ebl OverwritingReference of DSP
Browse files Browse the repository at this point in the history
  • Loading branch information
gj0dcsa committed Nov 29, 2023
1 parent b6b2848 commit bfbffad
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package org.dcsa.conformance.standards.ebl.action;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.dcsa.conformance.core.check.*;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.scenario.OverwritingReference;
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.ebl.party.EblRole;
import org.dcsa.conformance.standards.ebl.party.CarrierScenarioParameters;
import org.dcsa.conformance.standards.ebl.party.DynamicScenarioParameters;
import org.dcsa.conformance.standards.ebl.party.*;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

public abstract class EblAction extends ConformanceAction {
protected final int expectedStatus;
private final OverwritingReference<DynamicScenarioParameters> dspReference;

public EblAction(
String sourcePartyName,
Expand All @@ -25,6 +27,37 @@ public EblAction(
int expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle);
this.expectedStatus = expectedStatus;
this.dspReference =
previousAction == null
? new OverwritingReference<>(
null, new DynamicScenarioParameters(null, null, null, null, null))
: new OverwritingReference<>(previousAction.dspReference, null);
}

@Override
public void reset() {
super.reset();
if (previousAction != null) {
this.dspReference.set(null);
}
}

@Override
public ObjectNode exportJsonState() {
ObjectNode jsonState = super.exportJsonState();
if (dspReference.hasCurrentValue()) {
jsonState.set("currentDsp", dspReference.get().toJson());
}
return jsonState;
}

@Override
public void importJsonState(JsonNode jsonState) {
super.importJsonState(jsonState);
JsonNode dspNode = jsonState.get("currentDsp");
if (dspNode != null) {
dspReference.set(DynamicScenarioParameters.fromJson(dspNode));
}
}

protected EblAction getPreviousEblAction() {
Expand All @@ -35,54 +68,100 @@ protected Consumer<CarrierScenarioParameters> getCspConsumer() {
return getPreviousEblAction().getCspConsumer();
}

protected Consumer<DynamicScenarioParameters> getDspConsumer() {
return getPreviousEblAction().getDspConsumer();
}

protected Supplier<CarrierScenarioParameters> getCspSupplier() {
return getPreviousEblAction().getCspSupplier();
}

protected Supplier<DynamicScenarioParameters> getDspSupplier() {
return getPreviousEblAction().getDspSupplier();
return dspReference::get;
}

protected void storeCbrAndCbrrIfPresent(ConformanceExchange exchange) {
protected void updateDSPFromResponsePayload(ConformanceExchange exchange) {
DynamicScenarioParameters dsp = dspReference.get();

JsonNode responseJsonNode = exchange.getResponse().message().body().getJsonBody();
if (getDspSupplier().get().shippingInstructionsReference() == null) {
if (responseJsonNode.has("shippingInstructionsReference")) {
getDspConsumer()
.accept(
new DynamicScenarioParameters(
responseJsonNode.get("shippingInstructionsReference").asText(),
getDspSupplier().get().transportDocumentReference()));
}
var newShippingInstructionsReference =
responseJsonNode.path("shippingInstructionsReference").asText(null);
var newTransportDocumentReference =
responseJsonNode.path("transportDocumentReference").asText(null);
var newShippingInstructionsStatus =
parseShippingInstructionsStatus(
responseJsonNode.path("shippingInstructionsStatus").asText(null));
var newUpdatedShippingInstructionsStatus =
parseShippingInstructionsStatus(
responseJsonNode.path("updatedShippingInstructionsStatus").asText(null));
var newTransportDocumentStatus =
parseTransportDocumentStatus(responseJsonNode.path("transportDocumentStatus").asText(null));

var updatedDsp = dsp;
updatedDsp =
updateIfNotNull(
updatedDsp,
newShippingInstructionsReference,
updatedDsp::withShippingInstructionsReference);
updatedDsp =
updateIfNotNull(
updatedDsp, newTransportDocumentReference, updatedDsp::withTransportDocumentReference);
updatedDsp =
updateIfNotNull(
updatedDsp, newShippingInstructionsStatus, updatedDsp::withShippingInstructionsStatus);
updatedDsp =
updateIfNotNull(
updatedDsp,
newUpdatedShippingInstructionsStatus,
updatedDsp::withUpdatedShippingInstructionsStatus);
updatedDsp =
updateIfNotNull(
updatedDsp, newTransportDocumentStatus, updatedDsp::withTransportDocumentStatus);

if (!dsp.equals(updatedDsp)) {
dspReference.set(updatedDsp);
}
}

private static ShippingInstructionsStatus parseShippingInstructionsStatus(String v) {
if (v == null) {
return null;
}
try {
return ShippingInstructionsStatus.fromWireName(v);
} catch (IllegalArgumentException e) {
// Do not assume conformant payload.
return null;
}
if (getDspSupplier().get().transportDocumentReference() == null) {
if (responseJsonNode.has("transportDocumentReference")) {
getDspConsumer()
.accept(
new DynamicScenarioParameters(
getDspSupplier().get().shippingInstructionsReference(),
responseJsonNode.get("transportDocumentReference").asText()));
}
}

private static TransportDocumentStatus parseTransportDocumentStatus(String v) {
if (v == null) {
return null;
}
try {
return TransportDocumentStatus.fromWireName(v);
} catch (IllegalArgumentException e) {
// Do not assume conformant payload.
return null;
}
}

private <T> DynamicScenarioParameters updateIfNotNull(
DynamicScenarioParameters dsp, T value, Function<T, DynamicScenarioParameters> with) {
if (value == null) {
return dsp;
}
return with.apply(value);
}

protected Stream<ActionCheck> getNotificationChecks(
String expectedApiVersion,
JsonSchemaValidator notificationSchemaValidator) {
String expectedApiVersion, JsonSchemaValidator notificationSchemaValidator) {
String titlePrefix = "[Notification]";
var cbr = getDspSupplier().get().transportDocumentReference();
var cbrr = getDspSupplier().get().shippingInstructionsReference();
return Stream.of(
new HttpMethodCheck(
titlePrefix, EblRole::isCarrier, getMatchedNotificationExchangeUuid(), "POST"),
new UrlPathCheck(
titlePrefix,
EblRole::isCarrier,
getMatchedNotificationExchangeUuid(),
"/v2/ebl-notifications"),
"/v2/shipping-instruction-notifications"),
new ResponseStatusCheck(
titlePrefix, EblRole::isShipper, getMatchedNotificationExchangeUuid(), 204),
new ApiHeaderCheck(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

@With
public record DynamicScenarioParameters(
String shippingInstructionsReference, String transportDocumentReference) {
String shippingInstructionsReference,
String transportDocumentReference,
ShippingInstructionsStatus shippingInstructionsStatus,
ShippingInstructionsStatus updatedShippingInstructionsStatus,
TransportDocumentStatus transportDocumentStatus) {
public ObjectNode toJson() {
ObjectNode dspNode = new ObjectMapper().createObjectNode();
if (shippingInstructionsReference != null) {
Expand All @@ -16,6 +20,16 @@ public ObjectNode toJson() {
if (transportDocumentReference != null) {
dspNode.put("transportDocumentReference", transportDocumentReference);
}
if (shippingInstructionsStatus != null) {
dspNode.put("shippingInstructionsStatus", shippingInstructionsStatus.wireName());
}
if (updatedShippingInstructionsStatus != null) {
dspNode.put(
"updatedShippingInstructionsStatus", updatedShippingInstructionsStatus.wireName());
}
if (transportDocumentStatus != null) {
dspNode.put("transportDocumentStatus", transportDocumentStatus.wireName());
}
return dspNode;
}

Expand All @@ -27,6 +41,17 @@ public static DynamicScenarioParameters fromJson(JsonNode jsonNode) {
: null,
dspNode.has("transportDocumentReference")
? dspNode.get("transportDocumentReference").asText()
: null,
dspNode.has("shippingInstructionsStatus")
? ShippingInstructionsStatus.fromWireName(
dspNode.get("shippingInstructionsStatus").asText())
: null,
dspNode.has("updatedShippingInstructionsStatus")
? ShippingInstructionsStatus.fromWireName(
dspNode.get("updatedShippingInstructionsStatus").asText())
: null,
dspNode.has("transportDocumentStatus")
? TransportDocumentStatus.fromWireName(dspNode.get("transportDocumentStatus").asText())
: null);
}
}

0 comments on commit bfbffad

Please sign in to comment.