diff --git a/backend/src/main/java/pdl/backend/ImageDao.java b/backend/src/main/java/pdl/backend/ImageDao.java index e274c52..9b0c660 100644 --- a/backend/src/main/java/pdl/backend/ImageDao.java +++ b/backend/src/main/java/pdl/backend/ImageDao.java @@ -16,20 +16,23 @@ import org.springframework.stereotype.Repository; @Repository public class ImageDao implements Dao { + private final static String[] ACCEPTED_EXTENSION = { "jpg", "png", "jpeg" }; + private final Map images = new HashMap<>(); public ImageDao() throws IOException { - this(new ClassPathResource("images")); + this("images"); } - public ImageDao(ClassPathResource folderPath) throws IOException { + public ImageDao(final String folderPathStr) throws IOException { + final var folderPath = new ClassPathResource(folderPathStr); if (!folderPath.exists()) throw new ImageResourceMissingException("folder images doesn't not exist"); final var folder = folderPath.getFile(); getAllImages(folder); } - public void getAllImages(final File folder) { + private void getAllImages(final File folder) { for (final File file : folder.listFiles()) { if (file.isDirectory()) { getAllImages(file); @@ -40,7 +43,14 @@ public class ImageDao implements Dao { if (i > 0) { extension = file.getName().substring(i + 1); } - if (!extension.equals("jpg") && !extension.equals("png")) { + boolean accepted = false; + for (final String ext : ACCEPTED_EXTENSION) { + if (ext.equals(extension)) { + accepted = true; + break; + } + } + if (!accepted) { continue; } byte[] fileContent; diff --git a/backend/src/main/resources/images/test.jpg b/backend/src/main/resources/images/test.jpeg similarity index 100% rename from backend/src/main/resources/images/test.jpg rename to backend/src/main/resources/images/test.jpeg diff --git a/backend/src/main/resources/imagesTest/aube.jpg b/backend/src/main/resources/imagesTest/aube.jpg new file mode 100644 index 0000000..dd38f75 Binary files /dev/null and b/backend/src/main/resources/imagesTest/aube.jpg differ diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/birds.jpg b/backend/src/main/resources/imagesTest/imagesAnimals/birds.jpg new file mode 100644 index 0000000..38bae8d Binary files /dev/null and b/backend/src/main/resources/imagesTest/imagesAnimals/birds.jpg differ diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/lion.txt b/backend/src/main/resources/imagesTest/imagesAnimals/lion.txt new file mode 100644 index 0000000..8b42025 --- /dev/null +++ b/backend/src/main/resources/imagesTest/imagesAnimals/lion.txt @@ -0,0 +1 @@ +LION \ No newline at end of file diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/GUITARE.txt b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/GUITARE.txt new file mode 100644 index 0000000..7f27c64 --- /dev/null +++ b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/GUITARE.txt @@ -0,0 +1 @@ +GUITARE \ No newline at end of file diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/DONOTREADME.md b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/DONOTREADME.md new file mode 100644 index 0000000..834b4fc --- /dev/null +++ b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/DONOTREADME.md @@ -0,0 +1 @@ +# BOO \ No newline at end of file diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/poppy.jpg b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/poppy.jpg new file mode 100644 index 0000000..6ad663a Binary files /dev/null and b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/Piano/poppy.jpg differ diff --git a/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/mountains.jpg b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/mountains.jpg new file mode 100644 index 0000000..afa645d Binary files /dev/null and b/backend/src/main/resources/imagesTest/imagesAnimals/oueoueoue/mountains.jpg differ diff --git a/backend/src/main/resources/imagesTest/test.jpeg b/backend/src/main/resources/imagesTest/test.jpeg new file mode 100644 index 0000000..bb7b4da Binary files /dev/null and b/backend/src/main/resources/imagesTest/test.jpeg differ diff --git a/backend/src/test/java/pdl/backend/ImageDaoTests.java b/backend/src/test/java/pdl/backend/ImageDaoTests.java new file mode 100644 index 0000000..7b654de --- /dev/null +++ b/backend/src/test/java/pdl/backend/ImageDaoTests.java @@ -0,0 +1,28 @@ +package pdl.backend; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class ImageDaoTests { + + @Test + @DisplayName("ImageDao should throw an exception when the image folder is missing") + public void exceptionImageFolderNotFound() throws Exception { + assertThrows(IOException.class, () -> { + new ImageDao("testing"); + }); + } + + @Test + @DisplayName("ImageDao should have all images of the folder / subfolders and should ignore non (JPG/PNG) files") + public void initImageDao() throws Exception { + ImageDao imageDao = new ImageDao("imagesTest"); + var listImages = imageDao.retrieveAll(); + assert (listImages.size() == 5); + } +} \ No newline at end of file