Add utility methods in ExtractorAsserts to check whether a collection is empty and to test image collections

Two new methods have been added in ExtractorAsserts to check if a collection is
empty:

- assertNotEmpty(String, Collection<?>), checking:
  - the non nullity of the collection;
  - its non emptiness (if that's not case, an exception will be thrown using
    the provided message).

- assertNotEmpty(Collection<?>), calling assertNotEmpty(String, Collection<?>)
  with null as the value of the string argument.

A new one has been added to this assertion class to check the contrary:
assertEmpty(Collection<?>), checking emptiness of the collection only if it is
not null.

Three new methods have been added in ExtractorAsserts as utility test methods
for image collections:

- assertContainsImageUrlInImageCollection(String, Collection<Image>), checking
that:
  - the provided URL and image collection are not null;
  - the image collection contains at least one image which has the provided
    string value as its URL (which is a string) property.

- assertContainsOnlyEquivalentImages(Collection<Image>, Collection<Image>),
  checking that:
  - both collections are not null;
  - they have the same size;
  - each image of the first collection has its equivalent in the second one.
    This means that the properties of an image in the first collection must be
    equal in an image of the second one.

- assertNotOnlyContainsEquivalentImages(Collection<Image>, Collection<Image>),
  checking that:
  - both collections are not null;
  - one of the following conditions is met:
    - they have different sizes;
    - an image of the first collection has not its equivalent in the second one.
      This means that the properties of an image in the first collection must
      be not equal in an image of the second one.

These methods will be used by services extractors tests (and default ones) to
test image collections.
This commit is contained in:
AudricV 2022-08-03 13:03:50 +02:00
parent 5158472852
commit 434e885708
No known key found for this signature in database
GPG Key ID: DA92EC7905614198
1 changed files with 63 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@ -60,6 +61,16 @@ public class ExtractorAsserts {
assertFalse(stringToCheck.isEmpty(), message);
}
public static void assertNotEmpty(@Nullable final Collection<?> collectionToCheck) {
assertNotEmpty(null, collectionToCheck);
}
public static void assertNotEmpty(@Nullable final String message,
@Nullable final Collection<?> collectionToCheck) {
assertNotNull(collectionToCheck);
assertFalse(collectionToCheck.isEmpty(), message);
}
public static void assertEmpty(String stringToCheck) {
assertEmpty(null, stringToCheck);
}
@ -70,6 +81,12 @@ public class ExtractorAsserts {
}
}
public static void assertEmpty(@Nullable final Collection<?> collectionToCheck) {
if (collectionToCheck != null) {
assertTrue(collectionToCheck.isEmpty());
}
}
public static void assertNotBlank(String stringToCheck) {
assertNotBlank(stringToCheck, null);
}
@ -160,4 +177,50 @@ public class ExtractorAsserts {
.forEach(expectedTab -> assertTrue(tabSet.contains(expectedTab),
"Missing " + expectedTab + " tab (got " + tabSet + ")"));
}
public static void assertContainsImageUrlInImageCollection(
@Nullable final String exceptedImageUrlContained,
@Nullable final Collection<Image> imageCollection) {
assertNotNull(exceptedImageUrlContained, "exceptedImageUrlContained is null");
assertNotNull(imageCollection, "imageCollection is null");
assertTrue(imageCollection.stream().anyMatch(image ->
image.getUrl().equals(exceptedImageUrlContained)));
}
public static void assertContainsOnlyEquivalentImages(
@Nullable final Collection<Image> firstImageCollection,
@Nullable final Collection<Image> secondImageCollection) {
assertNotNull(firstImageCollection);
assertNotNull(secondImageCollection);
assertEquals(firstImageCollection.size(), secondImageCollection.size());
firstImageCollection.forEach(exceptedImage ->
assertTrue(secondImageCollection.stream().anyMatch(image ->
exceptedImage.getUrl().equals(image.getUrl())
&& exceptedImage.getHeight() == image.getHeight()
&& exceptedImage.getWidth() == image.getWidth())));
}
public static void assertNotOnlyContainsEquivalentImages(
@Nullable final Collection<Image> firstImageCollection,
@Nullable final Collection<Image> secondImageCollection) {
assertNotNull(firstImageCollection);
assertNotNull(secondImageCollection);
if (secondImageCollection.size() != firstImageCollection.size()) {
return;
}
for (final Image unexpectedImage : firstImageCollection) {
for (final Image image : secondImageCollection) {
if (!image.getUrl().equals(unexpectedImage.getUrl())
|| image.getHeight() != unexpectedImage.getHeight()
|| image.getWidth() != unexpectedImage.getWidth()) {
return;
}
}
}
throw new AssertionError("All excepted images have an equivalent in the image list");
}
}