Skip to content

Commit

Permalink
Add moar tests (#194)
Browse files Browse the repository at this point in the history
* Update freelib-utils version
* Fix tests and add more
  • Loading branch information
ksclarke authored Aug 6, 2024
1 parent 59483e8 commit a6218d5
Show file tree
Hide file tree
Showing 72 changed files with 3,912 additions and 754 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/auto-approve.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- v2
- v3
- develop

jobs:
build:
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/pr-auto-approve.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
name: PR Auto-Approver

# Auto-approve on projects that have a "secret" variable saying this is okay. This lets us set up a restricted PR
# process on projects with just a single active developer. It prevents the developer from pushing directly to the
# main branch (so enforces a particular workflow that our other GitHub Actions expect).
on:
pull_request:
branches:
- main
- v2
- v3
- develop

jobs:
approve-pr:
Expand Down
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.3</freelib.utils.version>
<freelib.utils.version>5.0.4</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
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.databind.type.TypeFactory;

import info.freelibrary.util.Constants;
Expand All @@ -44,7 +39,6 @@
import info.freelibrary.iiif.presentation.v3.utils.JSON;
import info.freelibrary.iiif.presentation.v3.utils.JsonKeys;
import info.freelibrary.iiif.presentation.v3.utils.MessageCodes;
import info.freelibrary.iiif.presentation.v3.utils.json.JsonParsingException;

/**
* A virtual container that represents a page or view and has content resources associated with it or with parts of it.
Expand All @@ -68,9 +62,6 @@ abstract class AbstractCanvas<T extends AbstractCanvas<T>> extends NavigableReso
/** A temporal constant. */
private static final String TEMPORAL = "temporal";

/** A Jackson serialization filter name for width and height. */
private static final String WIDTH_HEIGHT_FILTER = "JPv3WidthHeightFilter";

/** A zero (non-existent) duration. */
private static final float ZERO_DURATION = 0.0f;

Expand Down Expand Up @@ -142,6 +133,28 @@ protected AbstractCanvas(final String aID, final Label aLabel) {
super(ResourceTypes.CANVAS, aID, aLabel, CanvasBehavior.class);
}

@Override
@SuppressWarnings(JDK.UNCHECKED)
public boolean equals(final Object aObject) {
final AbstractCanvas<T> other;

if (this == aObject) {
return true;
}

if (aObject == null || getClass() != aObject.getClass()) {
return false;
}

other = (AbstractCanvas<T>) aObject;

return Objects.equals(myDuration, other.myDuration) && Objects.equals(myHeight, other.myHeight) &&
Objects.equals(myWidth, other.myWidth) &&
Objects.equals(myOtherAnnotations, other.myOtherAnnotations) &&
Objects.equals(myPaintingPageList, other.myPaintingPageList) &&
Objects.equals(mySupplementingPageList, other.mySupplementingPageList) && super.equals(other);
}

/**
* Gets the duration of the canvas.
*
Expand Down Expand Up @@ -228,6 +241,12 @@ public int getWidth() {
return myWidth;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), myDuration, myHeight, myWidth, myOtherAnnotations, myPaintingPageList,
mySupplementingPageList);
}

/**
* Sets the canvas' behaviors.
*
Expand All @@ -252,11 +271,16 @@ public T setBehaviors(final Behavior... aBehaviorArray) {
@JsonSetter(JsonKeys.BEHAVIOR)
@SuppressWarnings({ JDK.UNCHECKED })
public T setBehaviors(final List<Behavior> aBehaviorList) {
final T canvas;

if (aBehaviorList instanceof final BehaviorList behaviorList) {
behaviorList.checkType(CanvasBehavior.class, getClass());
canvas = super.setBehaviors(behaviorList);
} else {
canvas = super.setBehaviors(new BehaviorList(CanvasBehavior.class, aBehaviorList));
}

return super.setBehaviors(aBehaviorList);
return canvas;
}

/**
Expand Down Expand Up @@ -292,8 +316,8 @@ public T setMinter(final Minter aMinter) {
* @return The canvas
*/
@JsonIgnore
@SuppressWarnings({ JDK.UNCHECKED })
public T setOtherAnnotations(final AnnotationPage<WebAnnotation>... aAnnotationArray) {
@SafeVarargs
public final T setOtherAnnotations(final AnnotationPage<WebAnnotation>... aAnnotationArray) {
return setOtherAnnotations(Arrays.asList(aAnnotationArray));
}

Expand Down Expand Up @@ -406,37 +430,6 @@ public T setWidthHeight(final int aWidth, final int aHeight) {
return (T) this;
}

/**
* Converts the canvas to its string/JSON representation.
*
* @return A string representation of the canvas
*/
@Override
public String toString() {
final SimpleFilterProvider filterProvider = new SimpleFilterProvider();
final Set<String> filtered = new HashSet<>();

// Don't write duration if it's zero
if (myDuration == ZERO_DURATION) {
filtered.add(JsonKeys.DURATION);
}

// Don't write width and height if they're both zero
if (myHeight == 0 && myWidth == 0) {
filtered.add(JsonKeys.HEIGHT);
filtered.add(JsonKeys.WIDTH);
}

// These are the things we filter when we serialize to JSON
filterProvider.addFilter(WIDTH_HEIGHT_FILTER, SimpleBeanPropertyFilter.serializeAllExcept(filtered));

try {
return JSON.getWriter(filterProvider).writeValueAsString(this);
} catch (final JsonProcessingException details) {
throw new JsonParsingException(details);
}
}

/**
* Gets the manifest context. The manifest can either have a single context or an array of contexts (Cf.
* https://iiif.io/api/presentation/3.0/#46-linked-data-context-and-extensions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSetter;

import info.freelibrary.util.ListUtils;
import info.freelibrary.util.Logger;
import info.freelibrary.util.LoggerFactory;
import info.freelibrary.util.warnings.JDK;
Expand Down Expand Up @@ -129,6 +130,26 @@ public boolean bodyHasChoice() {
return myBodyHasChoice;
}

@Override
public boolean equals(final Object aObject) {
final AbstractCanvasAnnotation<?> other;

if (this == aObject) {
return true;
}

if (aObject == null || getClass() != aObject.getClass()) {
return false;
}

other = (AbstractCanvasAnnotation<?>) aObject;

return Objects.equals(myBodyHasChoice, other.myBodyHasChoice) &&
Objects.equals(myMotivation, other.myMotivation) && ListUtils.equals(myResources, other.myResources) &&
Objects.equals(myTarget, other.myTarget) && Objects.equals(myTimeMode, other.myTimeMode) &&
super.equals(other);
}

/**
* Gets the content resources associated with this annotation.
*
Expand Down Expand Up @@ -171,6 +192,11 @@ public Optional<TimeMode> getTimeMode() {
return Optional.ofNullable(myTimeMode);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), myBodyHasChoice, myMotivation, myResources, myTarget, myTimeMode);
}

/**
* Sets the annotation resource's behaviors. The supplied behaviors are checked for compatibility with the resource.
*
Expand All @@ -193,11 +219,16 @@ public A setBehaviors(final Behavior... aBehaviorArray) {
@Override
@JsonSetter(JsonKeys.BEHAVIOR)
public A setBehaviors(final List<Behavior> aBehaviorList) {
final A canvasAnno;

if (aBehaviorList instanceof final BehaviorList behaviorList) {
behaviorList.checkType(ResourceBehavior.class, getClass());
canvasAnno = super.setBehaviors(behaviorList);
} else {
canvasAnno = super.setBehaviors(new BehaviorList(ResourceBehavior.class, aBehaviorList));
}

return super.setBehaviors(aBehaviorList);
return canvasAnno;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import info.freelibrary.util.ListUtils;
import info.freelibrary.util.warnings.JDK;

import info.freelibrary.iiif.presentation.v3.annotations.WebAnnotation;
Expand Down Expand Up @@ -73,6 +74,24 @@ protected AbstractContentResource(final String aType, final String aID,
: MediaType.parse(aID).orElse(null);
}

@Override
public boolean equals(final Object aObject) {
if (this == aObject) {
return true;
}

if (aObject == null || getClass() != aObject.getClass()) {
return false;
}

if (aObject instanceof final AbstractContentResource<?> other) {
return Objects.equals(myFormat, other.myFormat) && ListUtils.equals(myAnnotations, other.myAnnotations) &&
ListUtils.equals(myLanguages, other.myLanguages) && super.equals(other);
}

return false;
}

/**
* Gets the content resource's annotations.
*
Expand Down Expand Up @@ -113,6 +132,11 @@ public List<String> getLanguages() {
return myLanguages;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), myFormat, myAnnotations, myLanguages);
}

/**
* Sets the content resource's annotation pages from an array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import info.freelibrary.util.I18nRuntimeException;
import info.freelibrary.util.ListUtils;
import info.freelibrary.util.Logger;
import info.freelibrary.util.LoggerFactory;
import info.freelibrary.util.warnings.JDK;
Expand Down Expand Up @@ -154,6 +155,32 @@ protected AbstractResource(final String aType, final String aID, final Label aLa
myLabel = Objects.requireNonNull(aLabel);
}

@Override
public boolean equals(final Object aObject) {
if (this == aObject) {
return true;
}

if (aObject == null || getClass() != aObject.getClass()) {
return false;
}

if (aObject instanceof final AbstractResource<?> other) {
return Objects.equals(myType, other.myType) && Objects.equals(myBehaviorClass, other.myBehaviorClass) &&
ListUtils.equals(myBehaviors, other.myBehaviors) &&
ListUtils.equals(myHomepages, other.myHomepages) && Objects.equals(myID, other.myID) &&
Objects.equals(myLabel, other.myLabel) && ListUtils.equals(myMetadata, other.myMetadata) &&
ListUtils.equals(myPartOfs, other.myPartOfs) && ListUtils.equals(myProviders, other.myProviders) &&
ListUtils.equals(myRenderings, other.myRenderings) &&
Objects.equals(myRequiredStatement, other.myRequiredStatement) &&
Objects.equals(myRights, other.myRights) && Objects.equals(mySeeAlsoRefs, other.mySeeAlsoRefs) &&
ListUtils.equals(myServices, other.myServices) && Objects.equals(mySummary, other.mySummary) &&
ListUtils.equals(myThumbnails, other.myThumbnails);
}

return false;
}

/**
* Gets the resource's behaviors.
*
Expand Down Expand Up @@ -347,6 +374,13 @@ public String getType() {
return myType;
}

@Override
public int hashCode() {
return Objects.hash(myType, myBehaviorClass, myBehaviors, myHomepages, myID, myLabel, myMetadata, myPartOfs,
myProviders, myRenderings, myRequiredStatement, myRights, mySeeAlsoRefs, myServices, mySummary,
myThumbnails);
}

@Override
@JsonIgnore
public T setBehaviors(final Behavior... aBehaviorArray) {
Expand Down
Loading

0 comments on commit a6218d5

Please sign in to comment.