Skip to content

Commit

Permalink
DT-1504 merged dev
Browse files Browse the repository at this point in the history
  • Loading branch information
preetamnpr committed Sep 27, 2024
2 parents faa49cf + 53f00cd commit c7f6b33
Show file tree
Hide file tree
Showing 61 changed files with 15,462 additions and 16,260 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.dcsa.conformance.standards.booking.model;

import static org.dcsa.conformance.core.toolkit.JsonToolkit.OBJECT_MAPPER;
import static org.dcsa.conformance.standards.booking.party.BookingCancellationState.*;
import static org.dcsa.conformance.standards.booking.party.BookingState.*;

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

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.*;
import java.util.stream.StreamSupport;

import org.dcsa.conformance.core.state.JsonNodeMap;
import org.dcsa.conformance.standards.booking.party.BookingCancellationState;
import org.dcsa.conformance.standards.booking.party.BookingState;
Expand Down Expand Up @@ -79,7 +77,6 @@ public class PersistableCarrierBooking {
private static final String BOOKING_DATA_FIELD = "booking";
private static final String AMENDED_BOOKING_DATA_FIELD = "amendedBooking";

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final ObjectNode state;

private PersistableCarrierBooking(ObjectNode state) {
Expand Down Expand Up @@ -191,9 +188,7 @@ public void rejectBooking(String reference, String rejectReason) {
rejectReason = "default message of rejection(reason not provided by carrier)";
}
final var reason = rejectReason;
mutateBookingAndAmendment((bookingContent, isAmendedContent) -> {
bookingContent.put("reason", reason);
});
mutateBookingAndAmendment((bookingContent, isAmendedContent) -> bookingContent.put("reason", reason));
}

public void confirmBookingCompleted(String reference, boolean resetAmendedBooking) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public BookingCarrier(

@Override
protected void exportPartyJsonState(ObjectNode targetObjectNode) {
targetObjectNode.set("cbrrToCbr", StateManagementUtil.storeMap(OBJECT_MAPPER, cbrrToCbr));
targetObjectNode.set("cbrToCbrr", StateManagementUtil.storeMap(OBJECT_MAPPER, cbrToCbrr));
targetObjectNode.set("cbrrToCbr", StateManagementUtil.storeMap(cbrrToCbr));
targetObjectNode.set("cbrToCbrr", StateManagementUtil.storeMap(cbrToCbrr));
}

@Override
Expand Down Expand Up @@ -389,7 +389,7 @@ private ConformanceResponse return405(ConformanceRequest request, String... allo
private ConformanceResponse return400(ConformanceRequest request, String message) {
return request.createResponse(
400,
Map.of("Api-Version", List.of(apiVersion)),
Map.of(API_VERSION, List.of(apiVersion)),
new ConformanceMessageBody(OBJECT_MAPPER.createObjectNode().put("message", message)));
}

Expand All @@ -400,14 +400,14 @@ private ConformanceResponse return404(ConformanceRequest request) {
private ConformanceResponse return404(ConformanceRequest request, String message) {
return request.createResponse(
404,
Map.of("Api-Version", List.of(apiVersion)),
Map.of(API_VERSION, List.of(apiVersion)),
new ConformanceMessageBody(OBJECT_MAPPER.createObjectNode().put("message", message)));
}

private ConformanceResponse return409(ConformanceRequest request, String message) {
return request.createResponse(
409,
Map.of("Api-Version", List.of(apiVersion)),
Map.of(API_VERSION, List.of(apiVersion)),
new ConformanceMessageBody(OBJECT_MAPPER.createObjectNode().put("message", message)));
}

Expand Down Expand Up @@ -622,7 +622,7 @@ private ConformanceResponse _handleGetBookingRequest(ConformanceRequest request)
}
ConformanceResponse response =
request.createResponse(
200, Map.of("Api-Version", List.of(apiVersion)), new ConformanceMessageBody(body));
200, Map.of(API_VERSION, List.of(apiVersion)), new ConformanceMessageBody(body));
addOperatorLogEntry(
"Responded to GET booking request '%s' (in state '%s')"
.formatted(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.dcsa.conformance.standards.booking.party;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -20,8 +21,6 @@
import org.dcsa.conformance.standards.booking.checks.ScenarioType;
import org.dcsa.conformance.standards.booking.model.InvalidBookingMessageType;

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

@Slf4j
public class BookingShipper extends ConformanceParty {

Expand Down Expand Up @@ -230,7 +229,7 @@ public ConformanceResponse handleRequest(ConformanceRequest request) {
ConformanceResponse response =
request.createResponse(
204,
Map.of("Api-Version", List.of(apiVersion)),
Map.of(API_VERSION, List.of(apiVersion)),
new ConformanceMessageBody(OBJECT_MAPPER.createObjectNode()));

addOperatorLogEntry(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.dcsa.conformance.standards.booking.party;

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

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

public record CarrierScenarioParameters(
Expand All @@ -17,7 +18,7 @@ public record CarrierScenarioParameters(
String podUNLocationCode
) {
public ObjectNode toJson() {
return new ObjectMapper()
return OBJECT_MAPPER
.createObjectNode()
.put("serviceContractReference", serviceContractReference())
.put("contractQuotationReference", contractQuotationReference())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.dcsa.conformance.standards.booking.party;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.function.Function;
import lombok.NonNull;
import lombok.With;
import org.dcsa.conformance.standards.booking.checks.ScenarioType;

import java.util.function.Function;

@With
public record DynamicScenarioParameters(
@NonNull
Expand All @@ -19,7 +19,7 @@ public record DynamicScenarioParameters(
JsonNode updatedBooking
) {
public ObjectNode toJson() {
ObjectNode dspNode = new ObjectMapper().createObjectNode();
ObjectNode dspNode = OBJECT_MAPPER.createObjectNode();
dspNode.put("scenarioType", scenarioType.name());
if (carrierBookingRequestReference != null) {
dspNode.put("carrierBookingRequestReference", carrierBookingRequestReference);
Expand Down
6 changes: 0 additions & 6 deletions cdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,5 @@
<version>2.159.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>10.3.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class CsAction extends ConformanceAction {
protected final int expectedStatus;
private final OverwritingReference<DynamicScenarioParameters> dsp;

public CsAction(
protected CsAction(
String sourcePartyName,
String targetPartyName,
CsAction previousAction,
Expand Down Expand Up @@ -100,7 +100,7 @@ private String getHashString(String actualResponse) {
byte[] hashBytes = digest.digest(actualResponse.getBytes());
responseHash = HexFormat.of().formatHex(hashBytes);
} catch (NoSuchAlgorithmException e) {
log.error("Hashing of the response failed." + e);
log.error("Hashing of the response failed.", e);
}
return responseHash;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.dcsa.conformance.standards.cs.party;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
import java.util.function.Consumer;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected void importPartyJsonState(ObjectNode sourceObjectNode) {
public ConformanceResponse handleRequest(ConformanceRequest request) {
log.info("CsPublisher.handleRequest(%s)".formatted(request));
String filePath;
Map<String, List<String>> initialIMap = Map.of("Api-Version", List.of(apiVersion));
Map<String, List<String>> initialIMap = Map.of(API_VERSION, List.of(apiVersion));
Map<String, Collection<String>> headers = new HashMap<>(initialIMap);
if (request.queryParams().containsKey("limit")
&& !request.queryParams().containsKey("cursor")) {
Expand Down Expand Up @@ -139,7 +140,7 @@ private void supplyScenarioParameters(JsonNode actionPrompt) {
})));

asyncOrchestratorPostPartyInput(
new ObjectMapper()
OBJECT_MAPPER
.createObjectNode()
.put("actionId", actionPrompt.required("actionId").asText())
.set("input", responseSsp.toJson()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.dcsa.conformance.standards.cs.party;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.With;

Expand All @@ -17,7 +18,7 @@ public static DynamicScenarioParameters fromJson(JsonNode jsonNode) {
}

public ObjectNode toJson() {
ObjectNode dspNode = new ObjectMapper().createObjectNode();
ObjectNode dspNode = OBJECT_MAPPER.createObjectNode();
if (cursor != null) {
dspNode.put("cursor", cursor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.dcsa.conformance.standards.cs.party;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -35,7 +36,7 @@ public static SuppliedScenarioParameters fromJson(JsonNode jsonNode) {
}

public ObjectNode toJson() {
ObjectNode objectNode = new ObjectMapper().createObjectNode();
ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
map.forEach(
(csFilterParameter, value) -> objectNode.put(csFilterParameter.getQueryParamName(), value));
return objectNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import org.dcsa.conformance.core.party.ConformanceParty;
import org.dcsa.conformance.core.traffic.ConformanceExchange;
import org.dcsa.conformance.core.traffic.HttpMessageType;

Expand Down Expand Up @@ -47,21 +48,6 @@ public ApiHeaderCheck(
this("", isRelevantForRoleName, matchedExchangeUuid, httpMessageType, expectedVersion, false);
}

public ApiHeaderCheck(
String titlePrefix,
Predicate<String> isRelevantForRoleName,
UUID matchedExchangeUuid,
HttpMessageType httpMessageType,
String expectedVersion) {
this(
titlePrefix,
isRelevantForRoleName,
matchedExchangeUuid,
httpMessageType,
expectedVersion,
false);
}

private ApiHeaderCheck(
String titlePrefix,
Predicate<String> isRelevantForRoleName,
Expand All @@ -71,8 +57,8 @@ private ApiHeaderCheck(
boolean isNotification) {
super(
titlePrefix,
"The HTTP %s has a correct Api-Version header"
.formatted(httpMessageType.name().toLowerCase()),
"The HTTP %s has a correct %s header"
.formatted(httpMessageType.name().toLowerCase(), ConformanceParty.API_VERSION),
isRelevantForRoleName,
matchedExchangeUuid,
httpMessageType);
Expand All @@ -88,16 +74,16 @@ protected Set<String> checkConformance(Function<UUID, ConformanceExchange> getEx
exchange.getMessage(httpMessageType).headers();
String headerName =
headers.keySet().stream()
.filter(key -> key.equalsIgnoreCase("api-version"))
.filter(key -> key.equalsIgnoreCase(ConformanceParty.API_VERSION))
.findFirst()
.orElse("api-version");
.orElse(ConformanceParty.API_VERSION);
Collection<String> headerValues = headers.get(headerName);
if (headerValues == null || headerValues.isEmpty()) {
return httpMessageType.equals(HttpMessageType.RESPONSE) && !isNotification
? Set.of("Missing Api-Version header")
? Set.of("Missing %s header".formatted(ConformanceParty.API_VERSION))
: Collections.emptySet();
}
if (headerValues.size() > 1) return Set.of("Duplicate Api-Version headers");
if (headerValues.size() > 1) return Set.of("Duplicate %s headers".formatted(ConformanceParty.API_VERSION));
String exchangeApiVersion = headerValues.stream().findFirst().orElseThrow();
if (exchangeApiVersion.contains("-")) {
exchangeApiVersion = exchangeApiVersion.substring(0, exchangeApiVersion.indexOf("-"));
Expand Down Expand Up @@ -132,6 +118,8 @@ private Set<String> _checkResponseApiVersionHeader(
private Set<String> _checkApiVersionHeaderValue(String expectedValue, String actualValue) {
return Objects.equals(expectedValue, actualValue)
? Set.of()
: Set.of("Expected Api-Version '%s' but found '%s'".formatted(expectedValue, actualValue));
: Set.of(
"Expected %s '%s' but found '%s'"
.formatted(ConformanceParty.API_VERSION, expectedValue, actualValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.*;

import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -34,16 +33,26 @@ public static synchronized JsonSchemaValidator getInstance(String filePath, Stri

@SneakyThrows
private JsonSchemaValidator(String filePath, String schemaName) {
log.info("Loading schema: {} with schemaName: {}", filePath, schemaName);
// https://github.com/networknt/json-schema-validator/issues/579#issuecomment-1488269135
InputStream schemaFileInputStream = JsonSchemaValidator.class.getResourceAsStream(filePath);

JsonSchemaFactory jsonSchemaFactory =
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7))
.objectMapper(JSON_FACTORY_OBJECT_MAPPER)
.build();
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
schemaValidatorsConfig.setTypeLoose(false);
JsonSchema rootJsonSchema =
jsonSchemaFactory.getSchema(schemaFileInputStream, schemaValidatorsConfig);

JsonSchema rootJsonSchema;
try (InputStream schemaFileInputStream =
JsonSchemaValidator.class.getResourceAsStream(filePath)) {

if (schemaFileInputStream == null || schemaFileInputStream.available() == 0) {
throw new IllegalArgumentException("Schema file not found: " + filePath);
}
rootJsonSchema = jsonSchemaFactory.getSchema(schemaFileInputStream, schemaValidatorsConfig);
}

ValidationContext validationContext =
new ValidationContext(
jsonSchemaFactory.getUriFactory(),
Expand All @@ -53,15 +62,12 @@ private JsonSchemaValidator(String filePath, String schemaName) {
schemaValidatorsConfig);
// Prevent warnings on unknown keywords
Map<String, Keyword> keywords = validationContext.getMetaSchema().getKeywords();
Arrays.asList(
"example",
"discriminator",
"exclusiveMinimum")
Arrays.asList("example", "discriminator", "exclusiveMinimum")
.forEach(keyword -> keywords.put(keyword, new NonValidationKeyword(keyword)));
jsonSchema =
jsonSchemaFactory.create(
validationContext,
"#/components/schemas/" + schemaName,
"#/components/schemas/" + schemaName,
rootJsonSchema.getSchemaNode().get("components").get("schemas").get(schemaName),
rootJsonSchema);
jsonSchema.initializeValidators();
Expand Down
Loading

0 comments on commit c7f6b33

Please sign in to comment.