Skip to content

Commit

Permalink
Implement TD checks in the PINT API
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Mar 4, 2024
1 parent 85e9ad8 commit 9fa1e97
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 195 deletions.
384 changes: 250 additions & 134 deletions core/src/main/java/org/dcsa/conformance/core/check/JsonAttribute.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.dcsa.conformance.core.check;

import java.util.List;

import static org.dcsa.conformance.core.check.JsonAttribute.rebaserFor;

public interface JsonContentCheckRebaser {


default JsonRebaseableContentCheck offset(JsonRebaseableContentCheck jsonRebaseableContentCheck) {
JsonContentMatchedValidation m = offset(jsonRebaseableContentCheck::validate);
return new JsonAttribute.JsonRebaseableCheckImpl(
jsonRebaseableContentCheck.description(),
m::validate
);
}

JsonContentMatchedValidation offset(JsonContentMatchedValidation jsonContentMatchedValidation);

static JsonContentCheckRebaser of(String path) {
return rebaserFor(List.of(path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Set;

@FunctionalInterface
public interface JsonContentMatchedValidation {
/**
* @param nodeToValidate The node to validate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.dcsa.conformance.core.check;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import lombok.NonNull;
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;

class JsonRebaseableAttributeBasedCheck extends ActionCheck {

private final JsonContentCheckRebaser rebaser;
private final List<JsonRebaseableContentCheck> validators;

JsonRebaseableAttributeBasedCheck(
String titlePrefix,
String title,
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
HttpMessageType httpMessageType,
@NonNull JsonContentCheckRebaser rebaser,
@NonNull
List<@NonNull JsonRebaseableContentCheck> validators) {
super(titlePrefix, title, isRelevantForRoleName, matchedExchangeUuid, httpMessageType);
if (validators.isEmpty()) {
throw new IllegalArgumentException("Must have at least one subcheck (validators must be non-empty)");
}
this.rebaser = rebaser;
this.validators = validators;
}

@Override
protected final Set<String> checkConformance(Function<UUID, ConformanceExchange> getExchangeByUuid) {
// All checks are delegated to sub-checks; nothing to do in here.
return Collections.emptySet();
}

@Override
protected Stream<? extends ConformanceCheck> createSubChecks() {
return this.validators.stream()
.map(validator -> new SingleValidatorCheck(this::isRelevantForRole, matchedExchangeUuid, httpMessageType, rebaser.offset(validator)));
}


private static class SingleValidatorCheck extends ActionCheck {

private final JsonRebaseableContentCheck validator;

public SingleValidatorCheck(Predicate<String> isRelevantForRoleName, UUID matchedExchangeUuid, HttpMessageType httpMessageType, @NonNull JsonRebaseableContentCheck validator) {
super(validator.description(), isRelevantForRoleName, matchedExchangeUuid, httpMessageType);
this.validator = validator;
}

@Override
protected Set<String> checkConformance(Function<UUID, ConformanceExchange> getExchangeByUuid) {
ConformanceExchange exchange = getExchangeByUuid.apply(matchedExchangeUuid);
if (exchange == null) return Collections.emptySet();
JsonNode jsonBody = exchange.getMessage(httpMessageType).body().getJsonBody();
return this.validator.validate(jsonBody);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.dcsa.conformance.core.check;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Set;

public interface JsonRebaseableContentCheck extends JsonContentCheck {
/**
* @param nodeToValidate The node to validate
* @param contextPath The path to this node, which should be included in any validation errors
* to describe where in the Json tree the error applies.
* @return A set of validation errors (returns the empty set if everything is ok)
*/
Set<String> validate(JsonNode nodeToValidate, String contextPath);

@Override
default Set<String> validate(JsonNode body) {
return validate(body, "");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dcsa.conformance.core.check;

import static org.dcsa.conformance.core.check.JsonAttribute.concatContextPath;
import static org.dcsa.conformance.core.check.JsonAttribute.renderJsonPointer;

import com.fasterxml.jackson.core.JsonPointer;
Expand All @@ -16,6 +17,7 @@
@RequiredArgsConstructor
class MultiAttributeValidatorImpl implements MultiAttributeValidator {

private final String contextPath;
private final JsonNode body;
private final JsonContentMatchedValidation validation;

Expand Down Expand Up @@ -47,7 +49,7 @@ public AttributePathBuilder path(String path) {
}

private void validateAll(List<Match> matches) {
matches.stream().map(m -> validation.validate(m.node, m.render()))
matches.stream().map(m -> validation.validate(m.node, concatContextPath(contextPath, m.render())))
.filter(s -> !s.isEmpty())
.forEach(validationIssues::addAll);
}
Expand Down
6 changes: 5 additions & 1 deletion ebl-issuance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.dcsa.conformance</groupId>
<artifactId>ebl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"document": {
"transportDocumentReference": "TRANSPORT_DOCUMENT_REFERENCE_PLACEHOLDER",
"shippingInstructionsReference": "SHIPPING_INSTRUCTION_REFERENCE_PLACEHOLDER",
"transportDocumentTypeCode": "SWB",
"transportDocumentTypeCode": "BOL",
"freightPaymentTermCode": "PRE",
"isElectronic": true,
"isToOrder": true,
"isToOrder": false,
"invoicePayableAt": {
"locationType": "UNCO",
"UNLocationCode": "DKAAR"
Expand All @@ -29,6 +29,26 @@
},
"partyFunction": "OS",
"isToBeNotified": false
},
{
"party": {
"partyName": "CONSIGNEE_NAME_PLACEHOLDER",
"partyCodes": [
{
"partyCode": "CONSIGNEE_PARTY_CODE_PLACEHOLDER",
"codeListProvider": "EPUI",
"codeListName": "CONSIGNEE_CODE_LIST_NAME_PLACEHOLDER"
}
],
"partyContactDetails": [
{
"name": "DCSA test person",
"email": "[email protected]"
}
]
},
"partyFunction": "CN",
"isToBeNotified": false
}
],
"consignmentItems": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
getMatchedExchangeUuid(),
HttpMessageType.RESPONSE,
responseSchemaValidator),
EBLChecks.tdContentChecks(getMatchedExchangeUuid(), expectedTdStatus, getCspSupplier(), getDspSupplier()));
EBLChecks.tdPlusScenarioContentChecks(getMatchedExchangeUuid(), expectedTdStatus, getCspSupplier(), getDspSupplier()));
}
};
}
Expand Down
Loading

0 comments on commit 9fa1e97

Please sign in to comment.