Skip to content

Commit

Permalink
TNT 220 showing only the validations for the right event type
Browse files Browse the repository at this point in the history
  • Loading branch information
gj0dcsa committed Mar 4, 2024
1 parent 89d96a8 commit 37672e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import lombok.SneakyThrows;

Expand Down Expand Up @@ -59,6 +60,6 @@ public Set<String> validate(JsonNode jsonNode) {
Set<ValidationMessage> validationMessageSet = jsonSchema.validate(jsonNode);
return validationMessageSet.stream()
.map(ValidationMessage::toString)
.collect(Collectors.toSet());
.collect(Collectors.toCollection(TreeSet::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.dcsa.conformance.core.scenario.ScenarioListBuilder;
import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.core.toolkit.JsonToolkit;
import org.dcsa.conformance.standards.tnt.action.TntEventType;
import org.dcsa.conformance.standards.tnt.party.TntPublisher;
import org.dcsa.conformance.standards.tnt.party.TntRole;
import org.dcsa.conformance.standards.tnt.party.TntSubscriber;
Expand Down Expand Up @@ -112,11 +113,18 @@ public Set<String> getReportRoleNames(
.collect(Collectors.toSet());
}

public JsonSchemaValidator getMessageSchemaValidator(String apiProviderRole, boolean forRequest) {
public Map<TntEventType, JsonSchemaValidator> getEventSchemaValidators() {
String schemaFilePath = "/standards/tnt/schemas/tnt-220-publisher.json";
String schemaName =
TntRole.isPublisher(apiProviderRole) ? (forRequest ? null : "events") : null;
return JsonSchemaValidator.getInstance(schemaFilePath, schemaName);
return Map.ofEntries(
Map.entry(
TntEventType.EQUIPMENT,
JsonSchemaValidator.getInstance(schemaFilePath, "equipmentEvent")),
Map.entry(
TntEventType.SHIPMENT,
JsonSchemaValidator.getInstance(schemaFilePath, "shipmentEvent")),
Map.entry(
TntEventType.TRANSPORT,
JsonSchemaValidator.getInstance(schemaFilePath, "transportEvent")));
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.dcsa.conformance.standards.tnt.action.SupplyScenarioParametersAction;
import org.dcsa.conformance.standards.tnt.action.TntGetEventsAction;
import org.dcsa.conformance.standards.tnt.party.TntFilterParameter;
import org.dcsa.conformance.standards.tnt.party.TntRole;

@Slf4j
public class TntScenarioListBuilder extends ScenarioListBuilder<TntScenarioListBuilder> {
Expand Down Expand Up @@ -116,7 +115,6 @@ private static TntScenarioListBuilder getEvents() {
subscriberPartyName,
publisherPartyName,
previousAction,
componentFactory.getMessageSchemaValidator(
TntRole.PUBLISHER.getConfigName(), false)));
componentFactory.getEventSchemaValidators()));
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package org.dcsa.conformance.standards.tnt.action;

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

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.check.*;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;
import org.dcsa.conformance.standards.tnt.party.TntRole;

@Getter
@Slf4j
public class TntGetEventsAction extends TntAction {
private final JsonSchemaValidator responseSchemaValidator;
private final Map<TntEventType, JsonSchemaValidator> eventSchemaValidators;

public TntGetEventsAction(
String subscriberPartyName,
String publisherPartyName,
ConformanceAction previousAction,
JsonSchemaValidator responseSchemaValidator) {
Map<TntEventType, JsonSchemaValidator> eventSchemaValidators) {
super(subscriberPartyName, publisherPartyName, previousAction, "GetEvents", 200);
this.responseSchemaValidator = responseSchemaValidator;
this.eventSchemaValidators = eventSchemaValidators;
}

@Override
Expand All @@ -37,11 +42,64 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
return Stream.of(
new UrlPathCheck(TntRole::isSubscriber, getMatchedExchangeUuid(), "/events"),
new ResponseStatusCheck(TntRole::isPublisher, getMatchedExchangeUuid(), expectedStatus),
new JsonSchemaCheck(
new ActionCheck(
"The HTTP %s matches the standard JSON schema"
.formatted(HttpMessageType.RESPONSE.name().toLowerCase()),
TntRole::isPublisher,
getMatchedExchangeUuid(),
HttpMessageType.RESPONSE,
responseSchemaValidator));
HttpMessageType.RESPONSE) {

private boolean isEventNode(JsonNode jsonNode) {
return jsonNode.isObject() && !jsonNode.path("eventType").isMissingNode();
}

private ArrayList<JsonNode> _findEventNodes(
ArrayList<JsonNode> foundEventNodes, JsonNode searchInJsonNode) {
if (isEventNode(searchInJsonNode)) {
foundEventNodes.add(searchInJsonNode);
} else {
searchInJsonNode.forEach(
elementNode -> _findEventNodes(foundEventNodes, elementNode));
}
return foundEventNodes;
}

@Override
protected Set<String> checkConformance(
Function<UUID, ConformanceExchange> getExchangeByUuid) {
JsonNode jsonResponse =
getExchangeByUuid
.apply(getMatchedExchangeUuid())
.getMessage(httpMessageType)
.body()
.getJsonBody();
LinkedHashSet<String> validationErrors = new LinkedHashSet<>();
if (!jsonResponse.isArray()) {
validationErrors.add("The root JSON response must be an array of events");
}
ArrayList<JsonNode> eventNodes = _findEventNodes(new ArrayList<>(), jsonResponse);
int eventCount = eventNodes.size();
for (int eventIndex = 0; eventIndex < eventCount; ++eventIndex) {
JsonNode eventNode = eventNodes.get(eventIndex);
JsonNode eventTypeNode = eventNode.path("eventType");
TntEventType eventType;
String eventTypeText = eventTypeNode.asText().toUpperCase();
try {
eventType = TntEventType.valueOf(eventTypeText);
} catch (RuntimeException e) {
validationErrors.add(
"Event #%d: incorrect eventType attribute: %s"
.formatted(eventIndex, eventTypeNode));
continue;
}
JsonSchemaValidator eventSchemaValidator = eventSchemaValidators.get(eventType);
for (String validationError : eventSchemaValidator.validate(eventNode)) {
validationErrors.add("Event #%d: %s".formatted(eventIndex, validationError));
}
}
return validationErrors;
}
});
}
};
}
Expand Down

0 comments on commit 37672e4

Please sign in to comment.