Skip to content

Commit

Permalink
Generic improvements
Browse files Browse the repository at this point in the history
* Add useful Javadoc
* Remove redundant code
* Remove double logging of partyInput
* Fix comparison of incompatible types
* Fix Python method naming in TnT
* Apply @ParameterizedTest pattern on TnT
  • Loading branch information
jkosternl committed Nov 26, 2024
1 parent 500f1ca commit 2cc303e
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.dcsa.conformance.standards.adoption.party;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.Getter;

@Getter
Expand All @@ -17,9 +13,4 @@ public enum FilterParameter {
this.queryParamName = queryParamName;
}

public static final Map<String, FilterParameter> byQueryParamName =
Arrays.stream(values())
.collect(
Collectors.toUnmodifiableMap(
FilterParameter::getQueryParamName, Function.identity()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;

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

public class JsonSchemaCheck extends ActionCheck {
private final HttpMessageType httpMessageType;
private final JsonSchemaValidator jsonSchemaValidator;

public JsonSchemaCheck(
Expand All @@ -34,7 +32,6 @@ public JsonSchemaCheck(
isRelevantForRoleName,
matchedExchangeUuid,
httpMessageType);
this.httpMessageType = httpMessageType;
this.jsonSchemaValidator = jsonSchemaValidator;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.dcsa.conformance.core.party;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.Data;

@Getter
@Setter
@ToString
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EndpointUriOverrideConfiguration {
private String method;
private String endpointBaseUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

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

/**
* Interface to support Supplied and Dynamic Scenario Parameters.
*
* <p>SuppliedScenarioParameters (SSP): mechanism that allows an adopter to "parameterize" a
* scenario by providing certain config values without which the synthetic party in the sandbox
* could not send requests that the actual implementation would be able to accept. Usually provided
* by one party, sometimes with separate implementations for both parties.
*
* <p>DynamicScenarioParameters (DSP): parameters that get captured by the orchestrator while the
* scenario unfolds, by analyzing the traffic data. How to use them: when the Orchestrator calls the
* handleExchange() of the Action class, that action probably needs to save these ids in the DSP so
* that they can be picked up and embedded in the JSON description of subsequent actions.
*
* <p>SSP and DSP are both captured by actions and saved as part of the orchestrator's state.
*/
public interface ScenarioParameters {

default ObjectNode toJson() {
return OBJECT_MAPPER.valueToTree(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public boolean isConfirmationRequired() {
return false;
}

/**
* Whether the action requires input from the user. Overwrite this method to return true if required.
*
* @return true if the action requires input.
*/
public boolean isInputRequired() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ConformanceAction peekNextAction() {
public ConformanceAction popNextAction() {
ConformanceAction poppedAction = nextActions.pop();
log.info(
"Popped from scenario '%s' action: %s".formatted(toString(), poppedAction.getActionTitle()));
"Popped from scenario '%s', current action: %s".formatted(toString(), poppedAction.getActionTitle()));
return poppedAction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static JsonNode templateFileToJsonNode(
}
if (replacements != null)
replacements.forEach(
(key, value) -> jsonString.set(jsonString.get().replaceAll(key, value)));
(key, value) -> jsonString.set(jsonString.get().replace(key, value)));
return OBJECT_MAPPER.readTree(jsonString.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,27 +231,22 @@ public JsonNode handleGetPartyPrompt(String partyName) {
public void handlePartyInput(JsonNode partyInput) {
log.info("ConformanceOrchestrator.handlePartyInput(%s)".formatted(partyInput.toPrettyString()));
if (currentScenarioId == null) {
log.info(
"Ignoring party input %s: no scenario is currently active"
.formatted(partyInput.toPrettyString()));
log.info("Ignoring party input: no scenario is currently active");
return;
}

ConformanceScenario currentScenario = _getCurrentScenario();
ConformanceAction nextAction = currentScenario.peekNextAction();
if (nextAction == null) {
log.info(
"Ignoring party input %s: the active scenario has no next action"
.formatted(partyInput.toPrettyString()));
log.info("Ignoring party input: the active scenario has no next action");
return;
}

String actionId = partyInput.get("actionId").asText();
if (!Objects.equals(actionId, nextAction.getId().toString())) {
log.info(
"Ignoring party input %s: the expected next action id is %s in current scenario %s"
.formatted(
partyInput.toPrettyString(), nextAction.getId(), currentScenario.toString()));
"Ignoring party input: the expected next action id is %s in current scenario %s"
.formatted(nextAction.getId(), currentScenario.toString()));
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dcsa.conformance.standards.tnt.action;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
Expand All @@ -8,9 +10,6 @@
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.scenario.OverwritingReference;
Expand All @@ -24,7 +23,7 @@ public abstract class TntAction extends ConformanceAction {
protected final int expectedStatus;
private final OverwritingReference<DynamicScenarioParameters> dsp;

public TntAction(
protected TntAction(
String sourcePartyName,
String targetPartyName,
TntAction previousAction,
Expand Down Expand Up @@ -81,7 +80,7 @@ private void updateCursorFromResponsePayload(ConformanceExchange exchange) {
}
}

if (!dsp.equals(updatedDsp)) dsp.set(updatedDsp);
if (!dsp.get().equals(updatedDsp)) dsp.set(updatedDsp);
}

private <T> DynamicScenarioParameters updateIfNotNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static ActionCheck responseContentChecks(
.forEach(
eventNode -> {
eventIndex.incrementAndGet();
_checkThatEventValuesMatchParamValues(eventNode, filterParametersMap)
checkThatEventValuesMatchParamValues(eventNode, filterParametersMap)
.forEach(
validationError ->
validationErrors.add(
Expand Down Expand Up @@ -103,18 +103,18 @@ public static ActionCheck responseContentChecks(
TntSchemaConformanceCheck.findEventNodes(body).stream()
.map(
eventNode ->
_stringToOffsetDateTime(eventNode.path("eventCreatedDateTime").asText()))
stringToOffsetDateTime(eventNode.path("eventCreatedDateTime").asText()))
.filter(Objects::nonNull)
.forEach(
eventCreatedDateTime -> {
eventIndex.incrementAndGet();
eventCreatedDateTimeParams.forEach(
entry -> {
OffsetDateTime filterParamDateTime =
_stringToOffsetDateTime(entry.getValue());
stringToOffsetDateTime(entry.getValue());
if (filterParamDateTime != null) {
String validationError =
_validateEventCreatedDateTime(
validateEventCreatedDateTime(
eventCreatedDateTime, entry.getKey(), filterParamDateTime);
if (validationError != null) {
validationErrors.add(
Expand Down Expand Up @@ -147,25 +147,25 @@ public static ActionCheck responseContentChecks(
TntRole::isPublisher, matched, HttpMessageType.RESPONSE, standardVersion, checks);
}

private static OffsetDateTime _stringToOffsetDateTime(String dateTimeString) {
private static OffsetDateTime stringToOffsetDateTime(String dateTimeString) {
try {
return OffsetDateTime.parse(dateTimeString);
} catch (DateTimeParseException e) {
return null;
}
}

static TntEventType _getEventType(JsonNode eventNode) {
static TntEventType getEventType(JsonNode eventNode) {
return Arrays.stream(TntEventType.values())
.filter(
eventType -> eventType.name().equalsIgnoreCase(eventNode.path("eventType").asText()))
.findFirst()
.orElse(null);
}

static Set<String> _checkThatEventValuesMatchParamValues(
static Set<String> checkThatEventValuesMatchParamValues(
JsonNode eventNode, Map<TntFilterParameter, String> filterParametersMap) {
TntEventType eventType = _getEventType(eventNode);
TntEventType eventType = getEventType(eventNode);
if (eventType == null) return Set.of();
Set<String> validationErrors = new LinkedHashSet<>();
Arrays.stream(TntFilterParameter.values())
Expand Down Expand Up @@ -246,10 +246,9 @@ static Set<String> _checkThatEventValuesMatchParamValues(
return validationErrors;
}

static String _validateEventCreatedDateTime(OffsetDateTime eventCreatedDateTime, TntFilterParameter parameterKey, OffsetDateTime parameterValue) {
static String validateEventCreatedDateTime(OffsetDateTime eventCreatedDateTime, TntFilterParameter parameterKey, OffsetDateTime parameterValue) {
switch (parameterKey) {
case EVENT_CREATED_DATE_TIME:
case EVENT_CREATED_DATE_TIME_EQ:
case EVENT_CREATED_DATE_TIME, EVENT_CREATED_DATE_TIME_EQ:
if (!eventCreatedDateTime.isEqual(parameterValue))
return "eventCreatedDateTime '%s' does not equal filter parameter date time '%s'"
.formatted(eventCreatedDateTime, parameterValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum SortDirection {
ASC,
DESC;
DESC
}
Loading

0 comments on commit 2cc303e

Please sign in to comment.