From 434e88570866a36ab5e767f32ba74e9b43aa36b7 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Wed, 3 Aug 2022 13:03:50 +0200 Subject: [PATCH] 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), 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, Collection), 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, Collection), 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. --- .../newpipe/extractor/ExtractorAsserts.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java b/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java index b47f6d5ee..d66e433ec 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java @@ -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 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 firstImageCollection, + @Nullable final Collection 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 firstImageCollection, + @Nullable final Collection 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"); + } }