Skip to content

Commit

Permalink
Add content check on Planned needs to match Required. Adopted some co…
Browse files Browse the repository at this point in the history
…de for that.

* Also update to latest JIT schema version
  • Loading branch information
jkosternl committed Nov 28, 2024
1 parent 4e1f8f4 commit e4ce8ad
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -25,7 +26,7 @@ protected ConformanceCheck(String title) {

private synchronized List<ConformanceCheck> getSubChecks() {
if (_subChecks == null) {
this._subChecks = this.createSubChecks().collect(Collectors.toList());
_subChecks = createSubChecks().filter(Objects::nonNull).collect(Collectors.toList());
}
return _subChecks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static LinkedHashMap<String, JitScenarioListBuilder> createModuleScenario
.then(
sendTimestamp(context, JitTimestampType.ESTIMATED)
.then(
sendTimestampConsumer(context, JitTimestampType.REQUESTED)
sendRequestedTimestamp(context)
.then(
sendTimestamp(context, JitTimestampType.PLANNED)
.then(
Expand All @@ -52,10 +52,10 @@ private static JitScenarioListBuilder sendTimestamp(
previousAction -> new JitTimestampAction(context, previousAction, timestampType, true));
}

private static JitScenarioListBuilder sendTimestampConsumer(
JitScenarioContext context, JitTimestampType timestampType) {
private static JitScenarioListBuilder sendRequestedTimestamp(
JitScenarioContext context) {
return new JitScenarioListBuilder(
previousAction -> new JitTimestampAction(context, previousAction, timestampType, false));
previousAction -> new JitTimestampAction(context, previousAction, JitTimestampType.REQUESTED, false));
}

private JitScenarioListBuilder(Function<ConformanceAction, ConformanceAction> actionBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public void reset() {
public ObjectNode exportJsonState() {
ObjectNode jsonState = super.exportJsonState();
if (dsp != null) {
jsonState.set("currentDsp", dsp.toJson());
jsonState.set("dsp", dsp.toJson());
}
return jsonState;
}

@Override
public void importJsonState(JsonNode jsonState) {
super.importJsonState(jsonState);
JsonNode dspNode = jsonState.get("currentDsp");
JsonNode dspNode = jsonState.get("dsp");
if (dspNode != null) {
dsp = DynamicScenarioParameters.fromJson(dspNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected void doHandleExchange(ConformanceExchange exchange) {
// Update DSP with the Port Call Service response from the provider
dsp =
new DynamicScenarioParameters(
null,
null,
null,
PortCallServiceType.fromName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.check.ActionCheck;
import org.dcsa.conformance.core.check.ConformanceCheck;
import org.dcsa.conformance.core.check.HttpMethodCheck;
import org.dcsa.conformance.core.check.JsonSchemaCheck;
Expand All @@ -13,6 +14,7 @@
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.jit.JitScenarioContext;
import org.dcsa.conformance.standards.jit.checks.JitChecks;
import org.dcsa.conformance.standards.jit.model.JitTimestamp;
import org.dcsa.conformance.standards.jit.model.JitTimestampType;
import org.dcsa.conformance.standards.jit.party.JitRole;
Expand Down Expand Up @@ -51,7 +53,8 @@ protected void doHandleExchange(ConformanceExchange exchange) {

JitTimestamp receivedTimestamp = JitTimestamp.fromJson(requestJsonNode);
dsp =
dsp.withPreviousTimestamp(receivedTimestamp)
dsp.withPreviousTimestamp(dsp.currentTimestamp())
.withCurrentTimestamp(receivedTimestamp)
.withTimestampDateTime(receivedTimestamp.dateTime())
.withTimestampType(
JitTimestampType.fromClassifierCode(receivedTimestamp.classifierCode()));
Expand All @@ -76,9 +79,8 @@ public ConformanceCheck createCheck(String expectedApiVersion) {
return new ConformanceCheck(getActionTitle()) {
@Override
protected Stream<? extends ConformanceCheck> createSubChecks() {
// ActionCheck checksForTimestamp =
// JitChecks.createChecksForTimestamp(
// JitRole::isProvider, getMatchedExchangeUuid(), expectedApiVersion, dsp);
ActionCheck checksForTimestamp =
JitChecks.createChecksForTimestamp(getMatchedExchangeUuid(), expectedApiVersion, dsp);
if (sendByProvider) {
return Stream.of(
new HttpMethodCheck(JitRole::isProvider, getMatchedExchangeUuid(), "PUT"),
Expand All @@ -87,9 +89,8 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
JitRole::isProvider,
getMatchedExchangeUuid(),
HttpMessageType.REQUEST,
responseSchemaValidator)
// checksForTimestamp
);
responseSchemaValidator),
checksForTimestamp);
}
// Consumer sends requested timestamp
return Stream.of(
Expand All @@ -99,9 +100,8 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
JitRole::isConsumer,
getMatchedExchangeUuid(),
HttpMessageType.REQUEST,
responseSchemaValidator)
// checksForTimestamp
);
responseSchemaValidator),
checksForTimestamp);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dcsa.conformance.standards.jit.checks;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -10,6 +11,8 @@
import org.dcsa.conformance.core.check.JsonAttribute;
import org.dcsa.conformance.core.check.JsonContentCheck;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.jit.model.JitClassifierCode;
import org.dcsa.conformance.standards.jit.model.JitTimestamp;
import org.dcsa.conformance.standards.jit.model.PortCallServiceType;
import org.dcsa.conformance.standards.jit.party.DynamicScenarioParameters;

Expand Down Expand Up @@ -46,20 +49,35 @@ private static JsonContentCheck checkPortCallService(PortCallServiceType service
}

public static ActionCheck createChecksForTimestamp(
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
String expectedApiVersion,
DynamicScenarioParameters dsp) {
UUID matchedExchangeUuid,
String expectedApiVersion,
DynamicScenarioParameters dsp) {
List<JsonContentCheck> checks = new ArrayList<>();
if (dsp != null && dsp.currentTimestamp().classifierCode().equals(JitClassifierCode.PLN)) {
checks.add(checkPlannedMatchesRequestedTimestamp(dsp));
}
if (checks.isEmpty()) return null;
return JsonAttribute.contentChecks(
isRelevantForRoleName,
matchedExchangeUuid,
HttpMessageType.REQUEST,
expectedApiVersion,
List.of(checkPlannedMatchesRequestedTimestamp(dsp)));
x -> true,
matchedExchangeUuid,
HttpMessageType.REQUEST,
expectedApiVersion,
checks);
}

private static JsonContentCheck checkPlannedMatchesRequestedTimestamp(DynamicScenarioParameters dsp) {
// TODO: Implement this check
return null;
private static JsonContentCheck checkPlannedMatchesRequestedTimestamp(
DynamicScenarioParameters dsp) {
return JsonAttribute.customValidator(
"Check if the Planned timestamp matches the Requested timestamp.",
body -> {
JitTimestamp receivedTimestamp = JitTimestamp.fromJson(body);
if (dsp.previousTimestamp().classifierCode().equals(JitClassifierCode.REQ)
&& !dsp.previousTimestamp().dateTime().equals(receivedTimestamp.dateTime())) {
return Set.of(
"Expected matching timestamp: '%s' but got Planned timestamp: '%s'"
.formatted(dsp.previousTimestamp().dateTime(), receivedTimestamp.dateTime()));
}
return Collections.emptySet();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.dcsa.conformance.standards.jit.model;

import lombok.Getter;

@Getter
public enum JitSchema {
PORT_CALL_SERVICE("PortCallService"),
ESTIMATED_TIMESTAMP(Constants.TIMESTAMP),
Expand All @@ -15,10 +18,6 @@ public enum JitSchema {
this.schemaName = schemaName;
}

public String getSchemaName() {
return schemaName;
}

private static class Constants {
public static final String TIMESTAMP = "Timestamp";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public record DynamicScenarioParameters(
JitTimestampType timestampType,
JitTimestamp previousTimestamp,
JitTimestamp currentTimestamp,
PortCallServiceType portCallServiceType,
String timestampDateTime,
String portCallID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void timestampRequest(JsonNode actionPrompt) {

DynamicScenarioParameters dsp = DynamicScenarioParameters.fromJson(actionPrompt.path("dsp"));
JitTimestamp timestamp =
JitProvider.getTimestampForType(timestampType, dsp.previousTimestamp());
JitProvider.getTimestampForType(timestampType, dsp.currentTimestamp());

syncCounterpartPut(
JitStandard.PORT_CALL_SERVICES_URL + timestamp.portCallServiceID() + "/timestamp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ private void timestampRequest(JsonNode actionPrompt) {
JitTimestampType.valueOf(actionPrompt.required("timestampType").asText());

DynamicScenarioParameters dsp = DynamicScenarioParameters.fromJson(actionPrompt.path("dsp"));
JitTimestamp previousTimestamp = dsp.previousTimestamp();
JitTimestamp previousTimestamp =
dsp.currentTimestamp(); // currentTimestamp is still the value from the previous action.

// Create values for the first timestamp in the sequence.
if (previousTimestamp == null) {
previousTimestamp = getTimestampForType(JitTimestampType.ESTIMATED, null);
if (dsp.portCallServiceID() != null) {
previousTimestamp = previousTimestamp.withPortCallServiceID(dsp.portCallServiceID());
}
}
JitTimestamp timestamp = getTimestampForType(timestampType, previousTimestamp);
sendTimestampPutRequest(timestampType, timestamp);
Expand All @@ -110,8 +108,7 @@ static JitTimestamp getTimestampForType(
false,
"Port closed due to strike");
case PLANNED, ACTUAL ->
previousTimestamp
.withClassifierCode(timestampType.getClassifierCode());
previousTimestamp.withClassifierCode(timestampType.getClassifierCode());
case REQUESTED ->
previousTimestamp
.withClassifierCode(timestampType.getClassifierCode())
Expand All @@ -131,8 +128,7 @@ private void sendTimestampPutRequest(
timestamp.toJson());

addOperatorLogEntry(
"Submitted %s timestamp for: %s"
.formatted(timestampType, timestamp.dateTime()));
"Submitted %s timestamp for: %s".formatted(timestampType, timestamp.dateTime()));
}

private JsonNode replacePlaceHolders(
Expand All @@ -158,7 +154,8 @@ public ConformanceResponse handleRequest(ConformanceRequest request) {
"Handled %s timestamp accepted for date/time: %s and remark: %s"
.formatted(
JitTimestampType.fromClassifierCode(timestamp.classifierCode()),
timestamp.dateTime(), timestamp.remark()));
timestamp.dateTime(),
timestamp.remark()));

return request.createResponse(
204,
Expand Down
Loading

0 comments on commit e4ce8ad

Please sign in to comment.