Skip to content

Commit

Permalink
[WIP] Test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ksclarke committed Jul 18, 2024
1 parent d5ebdc4 commit 64212b4
Show file tree
Hide file tree
Showing 35 changed files with 1,604 additions and 194 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<jackson.version>2.17.1</jackson.version>
<jackson.databind.version>2.17.1</jackson.databind.version>
<throwing.function.version>1.5.1</throwing.function.version>
<freelib.utils.version>5.0.2</freelib.utils.version>
<freelib.utils.version>5.0.3</freelib.utils.version>
<jsoup.version>1.17.2</jsoup.version>
<combinatorics.version>3.3.0</combinatorics.version>
<media.fragments.uri.version>2.4</media.fragments.uri.version>
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/info/freelibrary/iiif/presentation/v3/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public final Canvas supplementWith(final boolean aChoice, final ContentResource.

@Override
public final Canvas supplementWith(final boolean aChoice, final List<ContentResource> aContentList) {
return super.supplement(this, aChoice, aContentList.toArray(new ContentResource[0]));
return super.supplement(this, aChoice, aContentList);
}

@Override
Expand All @@ -218,7 +218,7 @@ public final Canvas supplementWith(final ContentResource... aContentArray) {

@Override
public final Canvas supplementWith(final List<ContentResource> aContentList) {
return super.supplement(this, false, aContentList.toArray(new ContentResource[0]));
return super.supplement(this, false, aContentList);
}

@Override
Expand All @@ -231,7 +231,7 @@ public final Canvas supplementWith(final MediaFragmentSelector aCanvasRegion, fi
@Override
public final Canvas supplementWith(final MediaFragmentSelector aCanvasRegion, final boolean aChoice,
final List<ContentResource> aContentList) {
return super.supplement(this, aCanvasRegion, aChoice, aContentList.toArray(new ContentResource[0]));
return super.supplement(this, aCanvasRegion, aChoice, aContentList);
}

@Override
Expand All @@ -244,7 +244,7 @@ public final Canvas supplementWith(final MediaFragmentSelector aCanvasRegion,
@Override
public final Canvas supplementWith(final MediaFragmentSelector aCanvasRegion,
final List<ContentResource> aContentList) {
return super.supplement(this, aCanvasRegion, false, aContentList.toArray(new ContentResource[0]));
return super.supplement(this, aCanvasRegion, false, aContentList);
}

@Override
Expand All @@ -257,8 +257,7 @@ public final Canvas supplementWith(final String aCanvasRegion, final boolean aCh
@Override
public final Canvas supplementWith(final String aCanvasRegion, final boolean aChoice,
final List<ContentResource> aContentList) {
return super.supplement(this, new MediaFragmentSelector(aCanvasRegion), aChoice,
aContentList.toArray(new ContentResource[0]));
return super.supplement(this, new MediaFragmentSelector(aCanvasRegion), aChoice, aContentList);
}

@Override
Expand All @@ -269,8 +268,7 @@ public final Canvas supplementWith(final String aCanvasRegion, final ContentReso

@Override
public final Canvas supplementWith(final String aCanvasRegion, final List<ContentResource> aContentList) {
return super.supplement(this, new MediaFragmentSelector(aCanvasRegion), false,
aContentList.toArray(new ContentResource[0]));
return super.supplement(this, new MediaFragmentSelector(aCanvasRegion), false, aContentList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
public interface Service {

/**
* Gets the service ID.
* Gets the service ID. Most services require an ID, but not all do so this is optional.
*
* @return The ID
* @return The service ID
*/
String getID();
Optional<String> getID();

/**
* Gets an optional service profile.
Expand All @@ -48,7 +48,7 @@ public interface Service {
*
* @return The service type
*/
String getType();
Optional<String> getType();

/**
* Sets the service ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public Service deserialize(final JsonParser aParser, final DeserializationContex
return deserializeServiceNode(aParser, aParser.getCodec().readTree(aParser));
}

/**
* Checks that a required ID is found; else, a {@code JsonParseException} is thrown, wrapped in a
* {@code JsonProcessingException}.
*
* @param aID An optional ID
* @param aParser A JSON parser to extract parsing location from in case on an error
* @return An ID
* @throws JsonProcessingException If the expected ID cannot be found
*/
private String checkID(final Optional<String> aID, final JsonParser aParser) throws JsonProcessingException {
return aID.orElseThrow(() -> new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_111),
aParser.currentLocation()));
}

/**
* Deserializes an image service's extra formats.
*
Expand Down Expand Up @@ -139,7 +153,6 @@ private void deserializeExtraQualities(final JsonNode aNode, final ImageService
* @param aID A service ID
* @return The GeoJSON service
*/
@SuppressWarnings(JDK.DEPRECATION)
private Service deserializeGeoJsonService(final JsonNode aNode, final String aID) {
final GeoJsonService service = new GeoJsonService(aID);
final JsonNode typeNode = aNode.get(JsonKeys.TYPE);
Expand Down Expand Up @@ -229,17 +242,21 @@ private Service deserializeOtherService(final JsonNode aNode, final String aID)
*
* @param aNode A JSON node
* @param aID A service ID
* @param aParser A parser from which the current location can be extracted
* @return The physical dims service
* @throws JsonParseException If there is trouble parsing the incoming JSON
*/
private Service deserializePhysicalDimsService(final JsonNode aNode, final String aID) {
private Service deserializePhysicalDimsService(final JsonNode aNode, final Optional<String> aID,
final JsonParser aParser) throws JsonParseException {
final JsonNode scale = aNode.get(JsonKeys.PHYSICAL_SCALE);
final JsonNode units = aNode.get(JsonKeys.PHYSICAL_UNITS);

if (scale == null || units == null) {
return new PhysicalDimsService(aID);
throw new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_147), aParser.currentLocation());
}

return new PhysicalDimsService(aID).setDims(scale.asDouble(), units.textValue());
return aID.isPresent() ? new PhysicalDimsService(aID.get(), scale.asDouble(), units.textValue())
: new PhysicalDimsService(scale.asDouble(), units.textValue());
}

/**
Expand All @@ -261,7 +278,7 @@ private Service deserializeServiceNode(final JsonParser aParser, final JsonNode
} else if (aNode.isObject()) {
final List<Service> services = getRelatedServices(aParser, aNode.get(JsonKeys.SERVICE));
final JsonNode profileNode = aNode.get(JsonKeys.PROFILE);
final String id = getServiceID(aNode, aParser);
final Optional<String> id = getServiceID(aNode);

if (profileNode != null) {
final Optional<Service.Profile> optProfile = Service.Profile.fromLabel(profileNode.asText());
Expand All @@ -270,35 +287,35 @@ private Service deserializeServiceNode(final JsonParser aParser, final JsonNode
final Service.Profile serviceProfile = optProfile.get();

if (serviceProfile instanceof final ImageService3.Profile profile) {
final ImageService imageService = new ImageService3(id, profile);
final ImageService imageService = new ImageService3(checkID(id, aParser), profile);
service = deserializeImageService(aNode, imageService).setServices(services);
} else if (serviceProfile instanceof final ImageService2.Profile profile) {
final ImageService imageService = new ImageService2(id, profile);
final ImageService imageService = new ImageService2(checkID(id, aParser), profile);
service = deserializeImageService(aNode, imageService).setServices(services);
} else if (serviceProfile instanceof AuthCookieService.Profile) {
service = deserializeV1AuthCookieService(aParser, aNode, id).setServices(services);
} else if (serviceProfile instanceof AuthTokenService1.Profile) {
service = new AuthTokenService1(id);
service = new AuthTokenService1(checkID(id, aParser));
} else if (serviceProfile instanceof PhysicalDimsService.Profile) {
service = deserializePhysicalDimsService(aNode, id).setServices(services);
service = deserializePhysicalDimsService(aNode, id, aParser).setServices(services);
} else {
service = deserializeOtherService(aNode, id).setServices(services);
service = deserializeOtherService(aNode, checkID(id, aParser)).setServices(services);
}
} else {
service = deserializeOtherService(aNode, id).setServices(services);
service = deserializeOtherService(aNode, checkID(id, aParser)).setServices(services);
}
} else {
final JsonNode contextNode = aNode.get(JsonKeys.CONTEXT);

if (contextNode != null && GeoJsonService.CONTEXT.equals(contextNode.asText())) {
service = deserializeGeoJsonService(aNode, id).setServices(services);
service = deserializeGeoJsonService(aNode, checkID(id, aParser)).setServices(services);
} else {
service = deserializeOtherService(aNode, id).setServices(services);
service = deserializeOtherService(aNode, checkID(id, aParser)).setServices(services);
}
}
} else {
throw new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_016, aNode.getClass().getName()),
aParser.getCurrentLocation());
aParser.currentLocation());
}

return service;
Expand Down Expand Up @@ -367,9 +384,8 @@ private void deserializeTiles(final JsonNode aNode, final ImageService aImageSer
* @return The v1 auth cookie service
* @throws JsonParseException If there is trouble parsing the JSON
*/
@SuppressWarnings(JDK.DEPRECATION)
private Service deserializeV1AuthCookieService(final JsonParser aParser, final JsonNode aNode, final String aID)
throws JsonParseException {
private Service deserializeV1AuthCookieService(final JsonParser aParser, final JsonNode aNode,
final Optional<String> aID) throws JsonParseException {
final String profile = aNode.get(JsonKeys.PROFILE).asText(); // To get here, presence has been confirmed
final JsonNode failureDescription = aNode.get(JsonKeys.FAILURE_DESCRIPTION);
final JsonNode failureHeader = aNode.get(JsonKeys.FAILURE_HEADER);
Expand All @@ -383,24 +399,33 @@ private Service deserializeV1AuthCookieService(final JsonParser aParser, final J
// User mediated cookie services must have a label
if (labelNode == null) {
throw new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_124, profile),
aParser.getCurrentLocation());
aParser.currentLocation());
}

if (AuthCookieService.Profile.LOGIN.toString().equals(profile)) {
wrapper = new UserMediatedServiceWrapper(new LoginCookieService1(aID, labelNode.textValue()));
wrapper =
new UserMediatedServiceWrapper(new LoginCookieService1(
aID.orElseThrow(() -> new JsonParseException(aParser,
LOGGER.getMessage(MessageCodes.JPA_111), aParser.currentLocation())),
labelNode.textValue()));
} else {
wrapper = new UserMediatedServiceWrapper(new ClickthroughCookieService1(aID, labelNode.textValue()));
wrapper =
new UserMediatedServiceWrapper(new ClickthroughCookieService1(
aID.orElseThrow(() -> new JsonParseException(aParser,
LOGGER.getMessage(MessageCodes.JPA_111), aParser.currentLocation())),
labelNode.textValue()));
}

wrapper.setConfirmLabel(aNode.get(JsonKeys.CONFIRM_LABEL)).setHeader(aNode.get(JsonKeys.HEADER));
cookieService = wrapper.setDescription(aNode.get(JsonKeys.DESCRIPTION)).getService();
} else if (AuthCookieService.Profile.EXTERNAL.toString().equals(profile)) {
cookieService = new ExternalCookieService1();
} else if (AuthCookieService.Profile.KIOSK.toString().equals(profile)) {
cookieService = new KioskCookieService1(aID);
cookieService = new KioskCookieService1(aID.orElseThrow(() -> new JsonParseException(aParser,
LOGGER.getMessage(MessageCodes.JPA_111), aParser.currentLocation())));
} else {
throw new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_123, "AbstractCookieService"),
aParser.getCurrentLocation());
aParser.currentLocation());
}

getValue(failureHeader).ifPresent(cookieService::setFailureHeader);
Expand Down Expand Up @@ -439,27 +464,23 @@ private List<Service> getRelatedServices(final JsonParser aParser, final JsonNod
* Gets the ID associated with the service.
*
* @param aNode A JSON node
* @param aParser A JSON parser
* @return The ID associated with the service
* @throws JsonParseException If the service was lacking an ID
*/
@SuppressWarnings(JDK.DEPRECATION)
private String getServiceID(final JsonNode aNode, final JsonParser aParser) throws JsonParseException {
private Optional<String> getServiceID(final JsonNode aNode) {
final JsonNode idNode = aNode.get(JsonKeys.ID);
final JsonNode v2IdNode;

if (idNode != null) {
return idNode.textValue();
return Optional.ofNullable(idNode.textValue());
}

v2IdNode = aNode.get(JsonKeys.V2_ID);

if (v2IdNode != null) {
return v2IdNode.textValue();
return Optional.ofNullable(v2IdNode.textValue());
}

// Assumption: All "other" services are going to have an ID; could return optional here if ever disproven
throw new JsonParseException(aParser, LOGGER.getMessage(MessageCodes.JPA_111), aParser.getCurrentLocation());
return Optional.empty();
}

/**
Expand All @@ -468,7 +489,6 @@ private String getServiceID(final JsonNode aNode, final JsonParser aParser) thro
* @param aNode A JSON node
* @return An optional value
*/
@SuppressWarnings(JDK.DEPRECATION)
private Optional<String> getValue(final JsonNode aNode) {
if (aNode != null) {
return Optional.ofNullable(aNode.asText(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package info.freelibrary.iiif.presentation.v3.services;

import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand Down Expand Up @@ -35,10 +37,12 @@ abstract class AbstractCookieService<T extends AbstractCookieService<T>> extends

/** The service's failure description. */
@JsonProperty(JsonKeys.FAILURE_DESCRIPTION)
@JsonInclude(Include.NON_ABSENT)
private String myFailureDescription;

/** The failure header for the service's failure description. */
@JsonProperty(JsonKeys.FAILURE_HEADER)
@JsonInclude(Include.NON_ABSENT)
private String myFailureHeader;

/**
Expand Down Expand Up @@ -75,17 +79,17 @@ public String getContext() {
*
* @return The failure description for the cookie service
*/
public String getFailureDescription() {
return myFailureDescription;
public Optional<String> getFailureDescription() {
return Optional.ofNullable(myFailureDescription);
}

/**
* Gets the failure description header for the cookie service.
*
* @return The failure description header for the cookie service
*/
public String getFailureHeader() {
return myFailureHeader;
public Optional<String> getFailureHeader() {
return Optional.ofNullable(myFailureHeader);
}

/**
Expand All @@ -95,7 +99,7 @@ public String getFailureHeader() {
*/
@Override
@JsonGetter(JsonKeys.V2_ID)
public String getID() {
public Optional<String> getID() {
return super.getID();
}

Expand All @@ -104,7 +108,7 @@ public String getID() {
*/
@Override
@JsonGetter(JsonKeys.V2_TYPE)
public String getType() {
public Optional<String> getType() {
return super.getType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ protected AbstractService(final String aID, final String aType, final Service.Pr
* @return The service ID
*/
@JsonSetter(JsonKeys.ID)
public String getID() {
return myID;
public Optional<String> getID() {
return Optional.ofNullable(myID);
}

/**
Expand Down Expand Up @@ -115,8 +115,8 @@ public List<Service> getServices() {
* @return The service type
*/
@JsonGetter(JsonKeys.TYPE)
public String getType() {
return myType;
public Optional<String> getType() {
return Optional.ofNullable(myType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public interface AuthCookieService extends Service {
*
* @return This service's failure description
*/
String getFailureDescription();
Optional<String> getFailureDescription();

/**
* Gets the auth cookie service failure header.
*
* @return The failure header
*/
String getFailureHeader();
Optional<String> getFailureHeader();

/**
* Sets the auth cookie service failure description.
Expand Down
Loading

0 comments on commit 64212b4

Please sign in to comment.