-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra validation for the confirmed booking
Signed-off-by: Niels Thykier <[email protected]>
- Loading branch information
Showing
3 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...org/dcsa/conformance/standards/booking/checks/BookingPayloadResponseConformanceCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.dcsa.conformance.standards.booking.checks; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import org.dcsa.conformance.core.check.ActionCheck; | ||
import org.dcsa.conformance.core.traffic.ConformanceExchange; | ||
import org.dcsa.conformance.core.traffic.HttpMessageType; | ||
import org.dcsa.conformance.standards.booking.party.BookingRole; | ||
import org.dcsa.conformance.standards.booking.party.BookingState; | ||
|
||
import java.util.*; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
public class BookingPayloadResponseConformanceCheck extends ActionCheck { | ||
|
||
private static final Set<BookingState> CONFIRMED_BOOKING_STATES = Set.of( | ||
BookingState.CONFIRMED, | ||
BookingState.COMPLETED | ||
); | ||
|
||
private final BookingState expectedState; | ||
|
||
public BookingPayloadResponseConformanceCheck(UUID matchedExchangeUuid, BookingState bookingState) { | ||
super( | ||
"Validate the carrier response", | ||
BookingRole::isCarrier, | ||
matchedExchangeUuid, | ||
HttpMessageType.RESPONSE | ||
); | ||
this.expectedState = bookingState; | ||
} | ||
|
||
@Override | ||
protected Set<String> checkConformance(Function<UUID, ConformanceExchange> getExchangeByUuid) { | ||
ConformanceExchange exchange = getExchangeByUuid.apply(matchedExchangeUuid); | ||
if (exchange == null) return Collections.emptySet(); | ||
var responsePayload = | ||
exchange | ||
.getResponse() | ||
.message() | ||
.body() | ||
.getJsonBody(); | ||
if (responsePayload == null) { | ||
return Collections.emptySet(); | ||
} | ||
var conformanceIssues = new LinkedHashSet<String>(); | ||
ensureBookingStateIsCorrect(responsePayload, conformanceIssues::add); | ||
if (CONFIRMED_BOOKING_STATES.contains(this.expectedState)) { | ||
nonEmptyList(responsePayload, "transportPlan", conformanceIssues::add); | ||
nonEmptyList(responsePayload, "shipmentCutOffTimes", conformanceIssues::add); | ||
} | ||
return Set.copyOf(conformanceIssues); | ||
} | ||
|
||
private void ensureBookingStateIsCorrect(JsonNode responsePayload, Consumer<String> setAdd) { | ||
String actualState = null; | ||
if (responsePayload.get("bookingStatus") instanceof TextNode statusNode) { | ||
actualState = statusNode.asText(); | ||
if (actualState.equals(this.expectedState.wireName())) { | ||
return; | ||
} | ||
} | ||
setAdd.accept("Expected bookingStatus '%s' but found '%s'" | ||
.formatted(expectedState.wireName(), actualState)); | ||
} | ||
|
||
private void nonEmptyList(JsonNode responsePayload, String key, Consumer<String> setAdd) { | ||
var field = responsePayload.get(key); | ||
if (field instanceof ArrayNode array && !array.isEmpty()) { | ||
return; | ||
} | ||
setAdd.accept("The field " + key + " must be a present and a non-empty list for a confirmed booking"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters