Skip to content

Commit

Permalink
booking: Refactor the json schema selection in the scenario builder
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Nov 20, 2023
1 parent bec6758 commit b705080
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3,957 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public class BookingComponentFactory extends AbstractComponentFactory {
private static final String CARRIER_AUTH_HEADER_VALUE = UUID.randomUUID().toString();
private static final String SHIPPER_AUTH_HEADER_VALUE = UUID.randomUUID().toString();

private static final String UC1_SHIPPER_SUBMIT_BOOKING_REQUEST_ACTION = "UC1_SHIPPER_SUBMIT_BOOKING_REQUEST";

private static final String BOOKING_SCHEMA_NAME = "postBooking";

private static final String BOOKING_NOTIFICATION_SCHEMA_NAME = "BookingNotification";

private final String standardVersion;

public BookingComponentFactory(String standardVersion) {
Expand Down Expand Up @@ -119,29 +113,12 @@ public Set<String> getReportRoleNames(
.collect(Collectors.toSet());
}

public JsonSchemaValidator getMessageSchemaValidator(String apiProviderRole, boolean forRequest, String action) {
String schemaName = getSchemaName(apiProviderRole, forRequest, action);
String schemaSuffix = "api";
if (schemaName.equals(BOOKING_NOTIFICATION_SCHEMA_NAME)) {
schemaSuffix = "notification";
}
String schemaFilePath = "/standards/booking/schemas/booking-%s-%s-%s.json"
.formatted(schemaSuffix,standardVersion.startsWith("2") ? "v20" : "v30", apiProviderRole.toLowerCase());
public JsonSchemaValidator getMessageSchemaValidator(JsonSchema jsonSchema) {
String schemaFilePath = "/standards/booking/schemas/booking-%s-v%s0.json"
.formatted(jsonSchema.apiName(), standardVersion.charAt(0));

return new JsonSchemaValidator(
BookingComponentFactory.class.getResourceAsStream(schemaFilePath), schemaName);
}

private String getSchemaName(String apiProviderRole, boolean forRequest, String action) {
String schemaName = BOOKING_NOTIFICATION_SCHEMA_NAME;
if (BookingRole.isShipper(apiProviderRole)) {
if (forRequest) {
if (UC1_SHIPPER_SUBMIT_BOOKING_REQUEST_ACTION.equals(action)) {
schemaName = BOOKING_SCHEMA_NAME;
}
}
}
return schemaName;
BookingComponentFactory.class.getResourceAsStream(schemaFilePath), jsonSchema.schemaName());
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.dcsa.conformance.standards.booking;

import static org.dcsa.conformance.standards.booking.party.BookingState.*;

import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.dcsa.conformance.core.check.JsonSchemaValidator;
import org.dcsa.conformance.core.scenario.ConformanceAction;
import org.dcsa.conformance.core.scenario.ScenarioListBuilder;
import org.dcsa.conformance.standards.booking.action.*;
import org.dcsa.conformance.standards.booking.party.BookingRole;
import org.dcsa.conformance.standards.booking.party.BookingState;

import java.util.function.Function;

import static org.dcsa.conformance.standards.booking.party.BookingState.*;

@Slf4j
public class BookingScenarioListBuilder extends ScenarioListBuilder<BookingScenarioListBuilder> {

private static final String UC1_SHIPPER_SUBMIT_BOOKING_REQUEST_ACTION = "UC1_SHIPPER_SUBMIT_BOOKING_REQUEST";
private static final ThreadLocal<BookingComponentFactory> threadLocalComponentFactory =
new ThreadLocal<>();
private static final ThreadLocal<String> threadLocalCarrierPartyName = new ThreadLocal<>();
private static final ThreadLocal<String> threadLocalShipperPartyName = new ThreadLocal<>();

private static final String BOOKING_API = "api";
private static final String BOOKING_NOTIFICATIONS_API = "notification";
private static final String POST_SCHEMA_NAME = "postBooking";
private static final String GET_BOOKING_SCHEMA_NAME = "getBooking";

private static final String BOOKING_NOTIFICATION_SCHEMA_NAME = "BookingNotification";



public static BookingScenarioListBuilder buildTree(
BookingComponentFactory componentFactory, String carrierPartyName, String shipperPartyName) {
threadLocalComponentFactory.set(componentFactory);
Expand Down Expand Up @@ -156,7 +164,7 @@ private static BookingScenarioListBuilder shipper_GetBooking(BookingState expect
(BookingAction) previousAction,
expectedState,
componentFactory.getMessageSchemaValidator(
BookingRole.SHIPPER.getConfigName(), false, null)));
new JsonSchema(BOOKING_API, GET_BOOKING_SCHEMA_NAME))));
}

private static BookingScenarioListBuilder uc1_shipper_SubmitBookingRequest() {
Expand All @@ -170,7 +178,7 @@ private static BookingScenarioListBuilder uc1_shipper_SubmitBookingRequest() {
shipperPartyName,
(BookingAction) previousAction,
componentFactory.getMessageSchemaValidator(
BookingRole.SHIPPER.getConfigName(), true, UC1_SHIPPER_SUBMIT_BOOKING_REQUEST_ACTION)));
new JsonSchema(BOOKING_API, POST_SCHEMA_NAME))));
}

private static BookingScenarioListBuilder carrierStateChange(CarrierNotificationUseCase constructor) {
Expand All @@ -184,7 +192,7 @@ private static BookingScenarioListBuilder carrierStateChange(CarrierNotification
shipperPartyName,
(BookingAction) previousAction,
componentFactory.getMessageSchemaValidator(
BookingRole.CARRIER.getConfigName(), true, null))
new JsonSchema(BOOKING_NOTIFICATIONS_API, BOOKING_NOTIFICATION_SCHEMA_NAME)))
);
}

Expand Down Expand Up @@ -280,4 +288,5 @@ BookingAction newInstance(
JsonSchemaValidator requestSchemaValidator
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.dcsa.conformance.standards.booking;

record JsonSchema(
String apiName,
String schemaName
) {
}
Loading

0 comments on commit b705080

Please sign in to comment.