Skip to content

Commit

Permalink
DT-625: UC2 - Carrier - Request update to shipping instructions
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Dec 4, 2023
1 parent c3a411b commit aad5591
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ private EblScenarioListBuilder thenAllPathsFrom(
shipper_GetShippingInstructions(SI_RECEIVED, TD_START)
.thenAllPathsFrom(SI_RECEIVED)));
case SI_RECEIVED -> thenEither(
uc2_carrier_requestUpdateToShippingInstruction()
.then(shipper_GetShippingInstructions(SI_PENDING_UPDATE, TD_START)
.thenHappyPathFrom(SI_PENDING_UPDATE)),
uc3_shipper_submitUpdatedShippingInstructions()
.then(
shipper_GetShippingInstructions(SI_RECEIVED, SI_UPDATE_RECEIVED, TD_START)
.then(uc6_carrier_publishDraftTransportDocument()
.thenEither(uc6_carrier_publishDraftTransportDocument()
.then(
shipper_GetShippingInstructions(SI_RECEIVED, TD_DRAFT)
.thenAllPathsFrom(TD_DRAFT)
))),
),
uc2_carrier_requestUpdateToShippingInstruction()
.then(shipper_GetShippingInstructions(SI_PENDING_UPDATE, TD_START)
.thenHappyPathFrom(SI_PENDING_UPDATE)))),
uc6_carrier_publishDraftTransportDocument()
.then(
shipper_GetShippingInstructions(SI_RECEIVED, TD_DRAFT)
Expand Down Expand Up @@ -150,6 +156,20 @@ private static EblScenarioListBuilder uc3_shipper_submitUpdatedShippingInstructi
EBL_NOTIFICATIONS_API, EBL_SI_NOTIFICATION_SCHEMA_NAME)));
}

private static EblScenarioListBuilder uc2_carrier_requestUpdateToShippingInstruction() {
EblComponentFactory componentFactory = threadLocalComponentFactory.get();
String carrierPartyName = threadLocalCarrierPartyName.get();
String shipperPartyName = threadLocalShipperPartyName.get();
return new EblScenarioListBuilder(
previousAction ->
new UC2_Carrier_RequestUpdateToShippingInstructionsAction(
carrierPartyName,
shipperPartyName,
(EblAction) previousAction,
componentFactory.getMessageSchemaValidator(
EBL_NOTIFICATIONS_API, EBL_SI_NOTIFICATION_SCHEMA_NAME)));
}

private static EblScenarioListBuilder uc6_carrier_publishDraftTransportDocument() {
return noAction();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.dcsa.conformance.standards.ebl.action;

import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.stream.Stream;
import lombok.Getter;
import org.dcsa.conformance.core.check.*;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.ebl.party.EblRole;

@Getter
public class UC2_Carrier_RequestUpdateToShippingInstructionsAction extends StateChangingSIAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC2_Carrier_RequestUpdateToShippingInstructionsAction(
String carrierPartyName,
String shipperPartyName,
EblAction previousAction,
JsonSchemaValidator requestSchemaValidator) {
super(carrierPartyName, shipperPartyName, previousAction, "UC2", 204);
this.requestSchemaValidator = requestSchemaValidator;
}

@Override
public String getHumanReadablePrompt() {
return ("UC2: Request update to the shipping instructions with shipping instructions reference %s"
.formatted(getDspSupplier().get().shippingInstructionsReference()));
}

@Override
public ObjectNode asJsonNode() {
return super.asJsonNode()
.put("documentReference", getDspSupplier().get().shippingInstructionsReference());
}

@Override
public ConformanceCheck createCheck(String expectedApiVersion) {
return new ConformanceCheck(getActionTitle()) {
@Override
protected Stream<? extends ConformanceCheck> createSubChecks() {
return Stream.of(
new HttpMethodCheck(EblRole::isCarrier, getMatchedExchangeUuid(), "POST"),
new UrlPathCheck(
EblRole::isCarrier, getMatchedExchangeUuid(), "/v3/shipping-instructions-notifications"),
new ResponseStatusCheck(
EblRole::isShipper, getMatchedExchangeUuid(), expectedStatus),
// TODO: Notification payload check
new ApiHeaderCheck(
EblRole::isCarrier,
getMatchedExchangeUuid(),
HttpMessageType.REQUEST,
expectedApiVersion),
new ApiHeaderCheck(
EblRole::isShipper,
getMatchedExchangeUuid(),
HttpMessageType.RESPONSE,
expectedApiVersion),
new JsonSchemaCheck(
EblRole::isCarrier,
getMatchedExchangeUuid(),
HttpMessageType.REQUEST,
requestSchemaValidator));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.*;
Expand All @@ -12,10 +13,9 @@

public class CarrierShippingInstructions {



private static final Map<ShippingInstructionsStatus, Predicate<ShippingInstructionsStatus>> PREREQUISITE_STATE_FOR_TARGET_STATE = Map.ofEntries(

private static final Set<ShippingInstructionsStatus> PENDING_UPDATE_PREREQUISITE_STATES = Set.of(
SI_RECEIVED,
SI_UPDATE_RECEIVED
);

private static final String SI_STATUS = "shippingInstructionsStatus";
Expand Down Expand Up @@ -66,6 +66,11 @@ private void setUpdatedShippingInstructions(ObjectNode node) {
state.set(UPDATED_SI_DATA_FIELD, node);
}

private void clearUpdatedShippingInstructions() {
state.remove(UPDATED_SI_DATA_FIELD);
mutateShippingInstructionsAndUpdate(siData -> siData.remove(UPDATED_SI_STATUS));
}


private void setReason(String reason) {
if (reason != null) {
Expand All @@ -84,6 +89,14 @@ public void cancelShippingInstructionsUpdate(String shippingInstructionsReferenc
setReason(reason);
}

public void requestChangesToShippingInstructions(String documentReference, Consumer<ArrayNode> requestedChangesGenerator) {
checkState(documentReference, getShippingInstructionsState(), PENDING_UPDATE_PREREQUISITE_STATES::contains);
clearUpdatedShippingInstructions();
changeState(SI_STATUS, SI_PENDING_UPDATE);
setReason(null);
mutateShippingInstructionsAndUpdate(siData -> requestedChangesGenerator.accept(siData.putArray("requestedChanges")));
}

private void changeState(String attributeName, ShippingInstructionsStatus newState) {
mutateShippingInstructionsAndUpdate(b -> b.put(attributeName, newState.wireName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.dcsa.conformance.core.traffic.ConformanceRequest;
import org.dcsa.conformance.core.traffic.ConformanceResponse;
import org.dcsa.conformance.standards.ebl.action.Carrier_SupplyScenarioParametersAction;
import org.dcsa.conformance.standards.ebl.action.UC2_Carrier_RequestUpdateToShippingInstructionsAction;
import org.dcsa.conformance.standards.ebl.models.CarrierShippingInstructions;

@Slf4j
Expand Down Expand Up @@ -68,7 +69,8 @@ protected void doReset() {
@Override
protected Map<Class<? extends ConformanceAction>, Consumer<JsonNode>> getActionPromptHandlers() {
return Map.ofEntries(
Map.entry(Carrier_SupplyScenarioParametersAction.class, this::supplyScenarioParameters)
Map.entry(Carrier_SupplyScenarioParametersAction.class, this::supplyScenarioParameters),
Map.entry(UC2_Carrier_RequestUpdateToShippingInstructionsAction.class, this::requestUpdateToShippingInstructions)
);
}

Expand All @@ -92,6 +94,38 @@ private void supplyScenarioParameters(JsonNode actionPrompt) {
"Provided CarrierScenarioParameters: %s".formatted(carrierScenarioParameters));
}

private void requestUpdateToShippingInstructions(JsonNode actionPrompt) {
log.info("Carrier.requestUpdateToShippingInstructions(%s)".formatted(actionPrompt.toPrettyString()));

var documentReference = actionPrompt.get("documentReference").asText();
var sir = tdrToSir.getOrDefault(documentReference, documentReference);

var si = CarrierShippingInstructions.fromPersistentStore(persistentMap, sir);
si.requestChangesToShippingInstructions(documentReference, requestedChanges -> requestedChanges.addObject()
.put("message", "Please perform the changes requested by the Conformance orchestrator")
);
si.save(persistentMap);
generateAndEmitNotificationFromShippingInstructions(actionPrompt, si, true);

addOperatorLogEntry("Requested update to the shipping instructions with document reference '%s'".formatted(documentReference));
}

private void generateAndEmitNotificationFromShippingInstructions(JsonNode actionPrompt, CarrierShippingInstructions shippingInstructions, boolean includeShippingInstructionsReference) {
var notification =
ShippingInstructionsNotification.builder()
.apiVersion(apiVersion)
.shippingInstructions(shippingInstructions.getShippingInstructions())
.includeShippingInstructionsReference(includeShippingInstructionsReference)
.build()
.asJsonNode();
if (isShipperNotificationEnabled) {
asyncCounterpartPost("/v3/shipping-instructions-notifications", notification);
} else {
asyncOrchestratorPostPartyInput(
OBJECT_MAPPER.createObjectNode().put("actionId", actionPrompt.required("actionId").asText()));
}
}


private ConformanceResponse return405(ConformanceRequest request, String... allowedMethods) {
return request.createResponse(
Expand Down

0 comments on commit aad5591

Please sign in to comment.