Skip to content

Commit

Permalink
DT-1217: Guess of which API calls will allow 202
Browse files Browse the repository at this point in the history
This will not close DT-1217 since the API specs have not been updated,
so it might be wrong.
  • Loading branch information
nt-gt committed Aug 2, 2024
1 parent 15af3ff commit 89b59a3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

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

public class ResponseStatusCheck extends ActionCheck {
private final int expectedResponseStatus;
private final Set<Integer> expectedResponseStatus;

public ResponseStatusCheck(
Predicate<String> isRelevantForRoleName,
Expand All @@ -18,29 +20,61 @@ public ResponseStatusCheck(
this("", isRelevantForRoleName, matchedExchangeUuid, expectedResponseStatus);
}

public ResponseStatusCheck(
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
Set<Integer> expectedResponseStatus) {
this("", isRelevantForRoleName, matchedExchangeUuid, expectedResponseStatus);
}

public ResponseStatusCheck(
String titlePrefix,
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
int expectedResponseStatus) {
Set<Integer> expectedResponseStatus) {
super(
titlePrefix,
"The HTTP response status is correct",
isRelevantForRoleName,
matchedExchangeUuid,
HttpMessageType.RESPONSE);
this.expectedResponseStatus = expectedResponseStatus;
this.expectedResponseStatus = Set.copyOf(expectedResponseStatus);
}

public ResponseStatusCheck(
String titlePrefix,
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
int expectedResponseStatus) {
super(
titlePrefix,
"The HTTP response status is correct",
isRelevantForRoleName,
matchedExchangeUuid,
HttpMessageType.RESPONSE);
this.expectedResponseStatus = Set.of(expectedResponseStatus);
}

private String validationErrorMessage(int actualResponseStatus) {
if (expectedResponseStatus.size() == 1) {
var expectedResponseStatus = this.expectedResponseStatus.iterator().next();
return "Response status '%d' does not match the expected value '%d'"
.formatted(actualResponseStatus, expectedResponseStatus);
}
var values = expectedResponseStatus.stream().sorted()
.map(String::valueOf)
.collect(Collectors.joining(", ", "'", "'"));
return "Response status '%d' does not match one of the expected values: %s"
.formatted(actualResponseStatus, values);
}

@Override
protected Set<String> checkConformance(Function<UUID, ConformanceExchange> getExchangeByUuid) {
ConformanceExchange exchange = getExchangeByUuid.apply(matchedExchangeUuid);
if (exchange == null) return Collections.emptySet();
int responseStatus = exchange.getResponse().statusCode();
return responseStatus == expectedResponseStatus
return expectedResponseStatus.contains(responseStatus)
? Collections.emptySet()
: Set.of(
"Response status '%d' does not match the expected value '%d'"
.formatted(responseStatus, expectedResponseStatus));
: Set.of(validationErrorMessage(responseStatus));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -19,7 +21,7 @@
import static org.dcsa.conformance.core.toolkit.JsonToolkit.OBJECT_MAPPER;

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

public EblAction(
Expand All @@ -28,13 +30,22 @@ public EblAction(
EblAction previousAction,
String actionTitle,
int expectedStatus) {
this(sourcePartyName, targetPartyName, previousAction, actionTitle, Set.of(expectedStatus));
}

public EblAction(
String sourcePartyName,
String targetPartyName,
EblAction previousAction,
String actionTitle,
Set<Integer> expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle);
this.expectedStatus = expectedStatus;
this.dspReference =
previousAction == null
? new OverwritingReference<>(
null, new DynamicScenarioParameters(ScenarioType.REGULAR_SWB, null, null, null, null, null, null, null, false, OBJECT_MAPPER.createObjectNode(), OBJECT_MAPPER.createObjectNode()))
: new OverwritingReference<>(previousAction.dspReference, null);
previousAction == null
? new OverwritingReference<>(
null, new DynamicScenarioParameters(ScenarioType.REGULAR_SWB, null, null, null, null, null, null, null, false, OBJECT_MAPPER.createObjectNode(), OBJECT_MAPPER.createObjectNode()))
: new OverwritingReference<>(previousAction.dspReference, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

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

import java.util.Set;

public abstract class StateChangingSIAction extends EblAction {
protected StateChangingSIAction(String sourcePartyName, String targetPartyName, EblAction previousAction, String actionTitle, int expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle, expectedStatus);
}

protected StateChangingSIAction(String sourcePartyName, String targetPartyName, EblAction previousAction, String actionTitle, Set<Integer> expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle, expectedStatus);
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
super.doHandleExchange(exchange);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.dcsa.conformance.standards.ebl.action;

import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Set;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,7 +28,7 @@ public UC1_Shipper_SubmitShippingInstructionsAction(
JsonSchemaValidator requestSchemaValidator,
JsonSchemaValidator responseSchemaValidator,
JsonSchemaValidator notificationSchemaValidator) {
super(shipperPartyName, carrierPartyName, previousAction, "UC1", 201);
super(shipperPartyName, carrierPartyName, previousAction, "UC1", Set.of(201, 202));
this.requestSchemaValidator = requestSchemaValidator;
this.responseSchemaValidator = responseSchemaValidator;
this.notificationSchemaValidator = notificationSchemaValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -29,7 +30,7 @@ public UC3_Shipper_SubmitUpdatedShippingInstructionsAction(
JsonSchemaValidator requestSchemaValidator,
JsonSchemaValidator responseSchemaValidator,
JsonSchemaValidator notificationSchemaValidator) {
super(shipperPartyName, carrierPartyName, previousAction, "UC3" + (useTDRef ? " [TDR]" : ""), 200);
super(shipperPartyName, carrierPartyName, previousAction, "UC3" + (useTDRef ? " [TDR]" : ""), Set.of(200, 202));
this.useTDRef = useTDRef;
this.requestSchemaValidator = requestSchemaValidator;
this.responseSchemaValidator = responseSchemaValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,7 +29,7 @@ public UC5_Shipper_CancelUpdateToShippingInstructionsAction(
JsonSchemaValidator requestSchemaValidator,
JsonSchemaValidator responseSchemaValidator,
JsonSchemaValidator notificationSchemaValidator) {
super(shipperPartyName, carrierPartyName, previousAction, useTDRef ? "UC5 [TDR]" : "UC5", 200);
super(shipperPartyName, carrierPartyName, previousAction, useTDRef ? "UC5 [TDR]" : "UC5", Set.of(200));
this.useTDRef = useTDRef;
this.requestSchemaValidator = requestSchemaValidator;
this.responseSchemaValidator = responseSchemaValidator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.dcsa.conformance.standards.ebl.action;

import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Set;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,7 +27,7 @@ public UC7_Shipper_ApproveDraftTransportDocumentAction(
JsonSchemaValidator requestSchemaValidator,
JsonSchemaValidator responseSchemaValidator,
JsonSchemaValidator notificationSchemaValidator) {
super(shipperPartyName, carrierPartyName, previousAction, "UC7", 200);
super(shipperPartyName, carrierPartyName, previousAction, "UC7", Set.of(200, 202));
this.requestSchemaValidator = requestSchemaValidator;
this.responseSchemaValidator = responseSchemaValidator;
this.notificationSchemaValidator = notificationSchemaValidator;
Expand Down

0 comments on commit 89b59a3

Please sign in to comment.