Skip to content

Commit

Permalink
APCV-913 added pagination logic. (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
preetamnpr authored Nov 5, 2024
1 parent 0d0df0b commit 8d48ff8
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public static LinkedHashMap<String, TntScenarioListBuilder> createModuleScenario
scenarioWithBadRequestFilterBy(Map.of(DOCUMENT_TYPE_CODE, "INVALID_DOCUMENT_TYPE_CODE")),
scenarioWithBadRequestFilterBy(
Map.of(EVENT_TYPE, "SHIPMENT"
,TRANSPORT_EVENT_TYPE_CODE, "ARRV")))),
,TRANSPORT_EVENT_TYPE_CODE, "ARRV")),
scenarioWithFilterByPagination(
getEvents(),
Map.of(EVENT_TYPE, "SHIPMENT"
,LIMIT, "1")
))),
Map.entry(
"TRANSPORT",
noAction()
Expand Down Expand Up @@ -87,7 +92,12 @@ public static LinkedHashMap<String, TntScenarioListBuilder> createModuleScenario
scenarioWithBadRequestFilterBy(Map.of(EVENT_TYPE, "INVALID_TRANSPORT_EVENT")),
scenarioWithBadRequestFilterBy(
Map.of(EVENT_TYPE, "TRANSPORT"
,SHIPMENT_EVENT_TYPE_CODE, "DRFT")))),
,SHIPMENT_EVENT_TYPE_CODE, "DRFT")),
scenarioWithFilterByPagination(
getEvents(),
Map.of(EVENT_TYPE, "TRANSPORT"
,LIMIT, "1")
))),
Map.entry(
"EQUIPMENT",
noAction()
Expand Down Expand Up @@ -118,8 +128,12 @@ public static LinkedHashMap<String, TntScenarioListBuilder> createModuleScenario
scenarioWithBadRequestFilterBy(Map.of(EQUIPMENT_EVENT_TYPE_CODE, "INVALID_EQUIPMENT_TYPE_CODE")),
scenarioWithBadRequestFilterBy(
Map.of(EVENT_TYPE, "EQUIPMENT"
,SHIPMENT_EVENT_TYPE_CODE, "DRFT"))
)))
,SHIPMENT_EVENT_TYPE_CODE, "DRFT")),
scenarioWithFilterByPagination(
getEvents(),
Map.of(EVENT_TYPE, "EQUIPMENT"
,LIMIT, "1")
))))
.collect(
Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
Expand Down Expand Up @@ -157,6 +171,10 @@ private static TntScenarioListBuilder scenarioWithFilterByEquipmentEvent(Map<Tnt
return supplyScenarioParameters(shipmentParameters).then(getEvents());
}

private static TntScenarioListBuilder scenarioWithFilterByPagination(TntScenarioListBuilder nextEventsAction, Map<TntFilterParameter, String> parameters) {
return supplyScenarioParameters(new LinkedHashMap<>(parameters)).then(nextEventsAction.then(getEvents()));
}


private static TntScenarioListBuilder supplyScenarioParameters(Map<TntFilterParameter, String> parameters) {
String publisherPartyName = threadLocalPublisherPartyName.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import java.util.HexFormat;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
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 @@ -30,37 +31,28 @@ public TntAction(
String actionTitle,
int expectedStatus) {
super(sourcePartyName, targetPartyName, previousAction, actionTitle);
this.sspSupplier = _getSspSupplier(previousAction);
this.sspSupplier = getSspSupplier(previousAction);
this.expectedStatus = expectedStatus;
this.dsp = previousAction == null
? new OverwritingReference<>(null,
new DynamicScenarioParameters(null, null, null,null, null))
new DynamicScenarioParameters(null, null, null))
: new OverwritingReference<>(previousAction.dsp, null);
}

protected TntAction getPreviousTntAction() {
return (TntAction) previousAction;
}


protected Supplier<DynamicScenarioParameters> getDspSupplier() {
return dsp::get;
}

protected Consumer<DynamicScenarioParameters> getDspConsumer() {
return dsp::set;
}

@Override
public void reset() {
super.reset();
if (previousAction != null) {
this.dsp.set(null);
} else {
this.dsp.set(new DynamicScenarioParameters(null, null, null,null, null));
this.dsp.set(new DynamicScenarioParameters(null, null, null));
}
}

protected Supplier<DynamicScenarioParameters> getDspSupplier() {
return dsp::get;
}

@Override
protected void doHandleExchange(ConformanceExchange exchange) {
super.doHandleExchange(exchange);
Expand All @@ -85,7 +77,7 @@ private void updateCursorFromResponsePayload(ConformanceExchange exchange) {
updatedDsp = updateIfNotNull(updatedDsp, firstPageHash, updatedDsp::withFirstPage);
} else {
String secondPageHash = getHashString(jsonResponse);
updatedDsp = updateIfNotNull(updatedDsp, secondPageHash, updatedDsp::withLastPage);
updatedDsp = updateIfNotNull(updatedDsp, secondPageHash, updatedDsp::withSecondPage);
}
}

Expand Down Expand Up @@ -113,11 +105,32 @@ private String getHashString(String actualResponse) {
return responseHash;
}

private Supplier<SuppliedScenarioParameters> _getSspSupplier(ConformanceAction previousAction) {
return previousAction instanceof SupplyScenarioParametersAction supplyAvailableTdrAction
? supplyAvailableTdrAction::getSuppliedScenarioParameters
: previousAction == null
? () -> SuppliedScenarioParameters.fromMap(Map.ofEntries())
: _getSspSupplier(previousAction.getPreviousAction());
private Supplier<SuppliedScenarioParameters> getSspSupplier(ConformanceAction previousAction) {
if (previousAction instanceof SupplyScenarioParametersAction supplyAvailableTdrAction) {
return supplyAvailableTdrAction::getSuppliedScenarioParameters;
} else if (previousAction == null) {
return () -> SuppliedScenarioParameters.fromMap(Map.ofEntries());
} else {
return getSspSupplier(previousAction.getPreviousAction());
}
}

@Override
public ObjectNode exportJsonState() {
ObjectNode jsonState = super.exportJsonState();
if (dsp.hasCurrentValue()) {
jsonState.set("currentDsp", dsp.get().toJson());
}
return jsonState;
}

@Override
public void importJsonState(JsonNode jsonState) {
super.importJsonState(jsonState);
JsonNode dspNode = jsonState.get("currentDsp");
if (dspNode != null) {
dsp.set(DynamicScenarioParameters.fromJson(dspNode));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ public TntGetEventsAction(
String publisherPartyName,
TntAction previousAction,
Map<TntEventType, JsonSchemaValidator> eventSchemaValidators) {
super(subscriberPartyName, publisherPartyName, previousAction, "GetEvents", 200);
super(subscriberPartyName,
publisherPartyName,
previousAction,
(previousAction instanceof TntGetEventsAction)
? "GetEvents (Next page)"
: "GetEvents", 200);
this.eventSchemaValidators = eventSchemaValidators;
}

@Override
public String getHumanReadablePrompt() {
return "Send a GET events request with the following parameters: "
+ sspSupplier.get().toJson().toPrettyString();
return previousAction instanceof TntGetEventsAction
? "Send a GET events request to fetch the next results page, using the cursor retrieved from the headers of the response of the first GET request."
: "Send a GET events request with the following parameters: "
+ sspSupplier.get().toJson().toPrettyString();
}

@Override
Expand Down Expand Up @@ -62,6 +69,12 @@ protected Stream<? extends ConformanceCheck> createSubChecks() {
}

public ObjectNode asJsonNode() {
return super.asJsonNode().set("suppliedScenarioParameters", sspSupplier.get().toJson());
var dsp = getDspSupplier().get();
ObjectNode jsonActionNode = super.asJsonNode().set("suppliedScenarioParameters", sspSupplier.get().toJson());
String cursor = dsp.cursor();
if (cursor != null && !cursor.isEmpty()) {
jsonActionNode.put("cursor", cursor);
}
return jsonActionNode;
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
package org.dcsa.conformance.standards.tnt.party;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.NonNull;
import lombok.With;
import org.dcsa.conformance.core.party.ScenarioParameters;

import static org.dcsa.conformance.core.toolkit.JsonToolkit.OBJECT_MAPPER;

@With
public record DynamicScenarioParameters(
String cursor,
String firstPage,
String lastPage,
String nextPage,
String prevPage) {
@JsonInclude(JsonInclude.Include.NON_NULL)
public record DynamicScenarioParameters(String cursor, String firstPage, String secondPage)
implements ScenarioParameters {

public static DynamicScenarioParameters fromJson(JsonNode jsonNode) {
ObjectNode dspNode = (ObjectNode) jsonNode;
return new DynamicScenarioParameters(
dspNode.path("cursor").asText(),
dspNode.path("firstPage").asText(),
dspNode.path("lastPage").asText(),
dspNode.path("prevPage").asText(),
dspNode.path("nextPage").asText());
}

public ObjectNode toJson() {
ObjectNode dspNode = OBJECT_MAPPER.createObjectNode();
if (cursor != null) {
dspNode.put("cursor", cursor);
}
if (firstPage != null) {
dspNode.put("firstPage", firstPage);
}
if (lastPage != null) {
dspNode.put("lastPage", lastPage);
}
if (nextPage != null) {
dspNode.put("nextPage", nextPage);
}
if (prevPage != null) {
dspNode.put("prevPage", prevPage);
}
return dspNode;
return OBJECT_MAPPER.convertValue(jsonNode, DynamicScenarioParameters.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dcsa.conformance.standards.tnt.party;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -32,7 +33,9 @@ public boolean validate(Map<String, ? extends Collection<String>> queryParams) {
boolean areParamsValid = true;

if (queryParams.containsKey(EVENT_TYPE)) {
Set<String> eventTypes = new HashSet<>(queryParams.get("eventType"));
Set<String> eventTypes = queryParams.get("eventType").stream()
.flatMap(eventType -> Arrays.stream(eventType.split(",")))
.collect(Collectors.toSet());
Set<String> allowedParams = allowedQueryParamForEventTypeMap.entrySet().stream()
.filter(entry -> eventTypes.contains(entry.getKey()))
.flatMap(entry -> entry.getValue().stream())
Expand Down Expand Up @@ -61,17 +64,25 @@ public boolean validate(Map<String, ? extends Collection<String>> queryParams) {


private boolean validateEventType(Collection<String> eventTypes) {
return eventTypes.stream().allMatch(VALID_EVENT_TYPES::contains);
return eventTypes.stream()
.flatMap(eventType -> Arrays.stream(eventType.split(",")))
.allMatch(VALID_EVENT_TYPES::contains);
}

private boolean validateShipmentEventTypeCode(Collection<String> shipmentEventTypeCodes) {
return shipmentEventTypeCodes.stream().allMatch(VALID_SHIPMENT_EVENT_TYPES::contains);
return shipmentEventTypeCodes.stream()
.flatMap(eventType -> Arrays.stream(eventType.split(",")))
.allMatch(VALID_SHIPMENT_EVENT_TYPES::contains);
}
private boolean validateDocumentTypeCode(Collection<String> documentTypeCodes) {
return documentTypeCodes.stream().allMatch(VALID_DOCUMENT_TYPE_CODES::contains);
return documentTypeCodes.stream()
.flatMap(eventType -> Arrays.stream(eventType.split(",")))
.allMatch(VALID_DOCUMENT_TYPE_CODES::contains);
}
private boolean validateEquipmentEventTypeCode(Collection<String> equipmentTypeCodes) {
return equipmentTypeCodes.stream().allMatch(VALID_EQUIPMENT_EVENT_TYPES::contains);
return equipmentTypeCodes.stream()
.flatMap(eventType -> Arrays.stream(eventType.split(",")))
.allMatch(VALID_EQUIPMENT_EVENT_TYPES::contains);
}

}
Expand Down
Loading

0 comments on commit 8d48ff8

Please sign in to comment.