Skip to content

Commit

Permalink
Merge branch 'dev' into Dt-620
Browse files Browse the repository at this point in the history
  • Loading branch information
preetamnpr committed Nov 29, 2023
2 parents 292eb8a + b6b2848 commit 2a3808f
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -33,7 +34,7 @@ public BookingAction(
this.expectedStatus = expectedStatus;
this.dspReference =
previousAction == null
? new OverwritingReference<>(null, new DynamicScenarioParameters(null, null))
? new OverwritingReference<>(null, new DynamicScenarioParameters(null, null, null, null))
: new OverwritingReference<>(previousAction.dspReference, null);
}

Expand Down Expand Up @@ -79,24 +80,43 @@ protected Supplier<DynamicScenarioParameters> getDspSupplier() {
return dspReference::get;
}

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

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

protected void updateDSPFromResponsePayload(ConformanceExchange exchange) {
DynamicScenarioParameters dsp = dspReference.get();
String oldCbrr = dsp.carrierBookingRequestReference();
String oldCbr = dsp.carrierBookingReference();

JsonNode responseJsonNode = exchange.getResponse().message().body().getJsonBody();
String newCbrr =
responseJsonNode.has("carrierBookingRequestReference")
? responseJsonNode.get("carrierBookingRequestReference").asText()
: oldCbrr;
String newCbr =
responseJsonNode.has("carrierBookingReference")
? responseJsonNode.get("carrierBookingReference").asText()
: oldCbr;

if ((newCbrr != null && !newCbrr.equals(oldCbrr))
|| (newCbr != null && !newCbr.equals(oldCbr))) {
dspReference.set(new DynamicScenarioParameters(newCbrr, newCbr));
var newCbrr = responseJsonNode.path("carrierBookingRequestReference").asText(null);
var newCbr = responseJsonNode.path("carrierBookingReference").asText(null);
var newBookingStatus = parseBookingState(responseJsonNode.path("bookingStatus").asText(null));
var newAmendedBookingStatus = parseBookingState(responseJsonNode.path("amendedBookingStatus").asText(null));

var updatedDsp = dsp;
updatedDsp = updateIfNotNull(updatedDsp, newCbrr, updatedDsp::withCarrierBookingRequestReference);
updatedDsp = updateIfNotNull(updatedDsp, newCbr, updatedDsp::withCarrierBookingReference);
updatedDsp = updateIfNotNull(updatedDsp, newBookingStatus, updatedDsp::withBookingStatus);
updatedDsp = updateIfNotNull(updatedDsp, newAmendedBookingStatus, updatedDsp::withAmendedBookingStatus);


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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.dcsa.conformance.standards.booking.action;

import org.dcsa.conformance.core.traffic.ConformanceExchange;

public abstract class StateChangingBookingAction extends BookingAction {
public StateChangingBookingAction(String sourcePartyName, String targetPartyName, BookingAction previousAction, String actionTitle, int expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle, expectedStatus);
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
super.doHandleExchange(exchange);
updateDSPFromResponsePayload(exchange);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.stream.Stream;

@Getter
public class UC10_Carrier_DeclineBookingAction extends BookingAction {
public class UC10_Carrier_DeclineBookingAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC10_Carrier_DeclineBookingAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.dcsa.conformance.standards.booking.party.BookingState;

@Getter
public class UC11_Carrier_ConfirmBookingCompletedAction extends BookingAction {
public class UC11_Carrier_ConfirmBookingCompletedAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC11_Carrier_ConfirmBookingCompletedAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Getter
@Slf4j
public class UC12_Shipper_CancelEntireBookingAction extends BookingAction {
public class UC12_Shipper_CancelEntireBookingAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;
private final JsonSchemaValidator responseSchemaValidator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Getter
@Slf4j
public class UC1_Shipper_SubmitBookingRequestAction extends BookingAction {
public class UC1_Shipper_SubmitBookingRequestAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;
private final JsonSchemaValidator responseSchemaValidator;
private final JsonSchemaValidator notificationSchemaValidator;
Expand Down Expand Up @@ -44,11 +44,6 @@ public ObjectNode asJsonNode() {
return jsonNode;
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
storeCbrAndCbrrIfPresent(exchange);
}

@Override
protected boolean expectsNotificationExchange() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.stream.Stream;

@Getter
public class UC2_Carrier_RequestUpdateToBookingRequestAction extends BookingAction {
public class UC2_Carrier_RequestUpdateToBookingRequestAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC2_Carrier_RequestUpdateToBookingRequestAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Getter
@Slf4j
public class UC3_Shipper_SubmitUpdatedBookingRequestAction extends BookingAction {
public class UC3_Shipper_SubmitUpdatedBookingRequestAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;
private final JsonSchemaValidator responseSchemaValidator;
private final JsonSchemaValidator notificationSchemaValidator;
Expand Down Expand Up @@ -42,11 +42,6 @@ public ObjectNode asJsonNode() {
return jsonNode;
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
storeCbrAndCbrrIfPresent(exchange);
}

@Override
protected boolean expectsNotificationExchange() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.stream.Stream;

@Getter
public class UC4_Carrier_RejectBookingRequestAction extends BookingAction {
public class UC4_Carrier_RejectBookingRequestAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC4_Carrier_RejectBookingRequestAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.dcsa.conformance.standards.booking.party.BookingState;

@Getter
public class UC5_Carrier_ConfirmBookingRequestAction extends BookingAction {
public class UC5_Carrier_ConfirmBookingRequestAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC5_Carrier_ConfirmBookingRequestAction(
Expand Down Expand Up @@ -41,11 +41,6 @@ public ObjectNode asJsonNode() {
.put("cbr", getDspSupplier().get().carrierBookingReference());
}

@Override
public void doHandleExchange(ConformanceExchange exchange) {
storeCbrAndCbrrIfPresent(exchange);
}

@Override
public ConformanceCheck createCheck(String expectedApiVersion) {
return new ConformanceCheck(getActionTitle()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.stream.Stream;

@Getter
public class UC6_Carrier_RequestUpdateToConfirmedBookingAction extends BookingAction {
public class UC6_Carrier_RequestUpdateToConfirmedBookingAction extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;

public UC6_Carrier_RequestUpdateToConfirmedBookingAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.check.*;
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.booking.checks.CarrierBookingRefStatusPayloadResponseConformanceCheck;
import org.dcsa.conformance.standards.booking.party.BookingRole;
Expand All @@ -14,7 +13,7 @@

@Getter
@Slf4j
public class UC7_Shipper_SubmitBookingAmendment extends BookingAction {
public class UC7_Shipper_SubmitBookingAmendment extends StateChangingBookingAction {
private final JsonSchemaValidator requestSchemaValidator;
private final JsonSchemaValidator responseSchemaValidator;
private final JsonSchemaValidator notificationSchemaValidator;
Expand Down Expand Up @@ -44,11 +43,6 @@ public ObjectNode asJsonNode() {
return jsonNode;
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
storeCbrAndCbrrIfPresent(exchange);
}

@Override
protected boolean expectsNotificationExchange() {
return true;
Expand All @@ -59,7 +53,9 @@ public ConformanceCheck createCheck(String expectedApiVersion) {
return new ConformanceCheck(getActionTitle()) {
@Override
protected Stream<? extends ConformanceCheck> createSubChecks() {
var cbrr = getDspSupplier().get().carrierBookingRequestReference();
var dsp = getDspSupplier().get();
var cbrr = dsp.carrierBookingRequestReference();
var expectedBookingStatus = dsp.bookingStatus();
Stream<ActionCheck> primaryExchangeChecks =
Stream.of(
new HttpMethodCheck(BookingRole::isShipper, getMatchedExchangeUuid(), "PUT"),
Expand Down Expand Up @@ -89,30 +85,17 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
getMatchedExchangeUuid(),
HttpMessageType.RESPONSE,
responseSchemaValidator));
BookingState notificationBookingStatus = null;
for (BookingAction bookingAction = getPreviousBookingAction();
bookingAction != null;
bookingAction = bookingAction.getPreviousBookingAction()) {
if (bookingAction instanceof UC5_Carrier_ConfirmBookingRequestAction) { // TODO or UC8 when created
notificationBookingStatus = BookingState.CONFIRMED;
break;
}
if (bookingAction instanceof UC6_Carrier_RequestUpdateToConfirmedBookingAction) {
notificationBookingStatus = BookingState.PENDING_AMENDMENT;
break;
}
}
return Stream.concat(
Stream.concat(primaryExchangeChecks,
Stream.of(new CarrierBookingRefStatusPayloadResponseConformanceCheck(
getMatchedExchangeUuid(),
notificationBookingStatus,
expectedBookingStatus,
BookingState.AMENDMENT_RECEIVED
))),
getNotificationChecks(
expectedApiVersion,
notificationSchemaValidator,
notificationBookingStatus,
expectedBookingStatus,
BookingState.AMENDMENT_RECEIVED));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

@With
public record DynamicScenarioParameters(
String carrierBookingRequestReference, String carrierBookingReference) {
String carrierBookingRequestReference,
String carrierBookingReference,
BookingState bookingStatus,
BookingState amendedBookingStatus) {
public ObjectNode toJson() {
ObjectNode dspNode = new ObjectMapper().createObjectNode();
if (carrierBookingRequestReference != null) {
Expand All @@ -16,17 +19,29 @@ public ObjectNode toJson() {
if (carrierBookingReference != null) {
dspNode.put("carrierBookingReference", carrierBookingReference);
}
if (bookingStatus != null) {
dspNode.put("bookingStatus", bookingStatus.wireName());
}
if (amendedBookingStatus != null) {
dspNode.put("amendedBookingStatus", amendedBookingStatus.wireName());
}
return dspNode;
}

private static BookingState bookingState(String value) {
if (value == null) {
return null;
}
return BookingState.fromWireName(value);
}

public static DynamicScenarioParameters fromJson(JsonNode jsonNode) {
ObjectNode dspNode = (ObjectNode) jsonNode;
return new DynamicScenarioParameters(
dspNode.has("carrierBookingRequestReference")
? dspNode.get("carrierBookingRequestReference").asText()
: null,
dspNode.has("carrierBookingReference")
? dspNode.get("carrierBookingReference").asText()
: null);
dspNode.path("carrierBookingRequestReference").asText(null),
dspNode.path("carrierBookingReference").asText(null),
bookingState(dspNode.path("bookingStatus").asText(null)),
bookingState(dspNode.path("amendedBookingStatus").asText(null))
);
}
}

0 comments on commit 2a3808f

Please sign in to comment.