From d574c3a12cbbbf4551c6a27dad482b0c6d8d5144 Mon Sep 17 00:00:00 2001
From: ahoimariew <134478949+ahoimariew@users.noreply.github.com>
Date: Tue, 20 Aug 2024 08:28:50 +0200
Subject: [PATCH] Adds Testenvironment for FileRepository (#336)
* adds Testsuite for FileReository and Testfiles for MongoDB and InMemory of FileRepository
* attends review remarks
* Adds Formatter, adds Test for saving existing File tp FileRepositoryTestSuite and fixes save function in MongoDBFileRepository
* fixes fileHandlingException Issue
* attends vertical spacing and refactoring issues
* refactores some variables
* reruns the CI
---
.../mongodb/TestMongoDBAasRepository.java | 3 +
.../backend/InMemoryAasService.java | 7 +-
.../pom.xml | 6 +
.../TestInMemoryFileRepository.java | 41 +++++
.../pom.xml | 6 +
.../filerepository/MongoDBFileRepository.java | 7 +-
.../TestMongoDBFileRepository.java | 62 +++++++
.../basyx.filerepository-backend/pom.xml | 5 +
.../backend/FileRepositoryTestSuite.java | 152 ++++++++++++++++++
.../InMemorySubmodelService.java | 3 +
10 files changed, 290 insertions(+), 2 deletions(-)
create mode 100644 basyx.common/basyx.filerepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestInMemoryFileRepository.java
create mode 100644 basyx.common/basyx.filerepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestMongoDBFileRepository.java
create mode 100644 basyx.common/basyx.filerepository-backend/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/backend/FileRepositoryTestSuite.java
diff --git a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/TestMongoDBAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/TestMongoDBAasRepository.java
index 6424938dc..c854b5652 100644
--- a/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/TestMongoDBAasRepository.java
+++ b/basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/mongodb/TestMongoDBAasRepository.java
@@ -92,6 +92,9 @@ protected AasService getAasServiceWithThumbnail() throws IOException {
FileMetadata defaultThumbnail = new FileMetadata("dummyImgA.jpeg", "", createDummyImageIS_A());
+ if(fileRepository.exists(defaultThumbnail.getFileName()))
+ fileRepository.delete(defaultThumbnail.getFileName());
+
String thumbnailFilePath = fileRepository.save(defaultThumbnail);
Resource defaultResource = new DefaultResource.Builder().path(thumbnailFilePath).contentType("").build();
diff --git a/basyx.aasservice/basyx.aasservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/backend/InMemoryAasService.java b/basyx.aasservice/basyx.aasservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/backend/InMemoryAasService.java
index 56ade886c..892edacd1 100644
--- a/basyx.aasservice/basyx.aasservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/backend/InMemoryAasService.java
+++ b/basyx.aasservice/basyx.aasservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/backend/InMemoryAasService.java
@@ -159,7 +159,12 @@ public File getThumbnail() {
@Override
public void setThumbnail(String fileName, String contentType, InputStream inputStream) {
- String filePath = fileRepository.save(new FileMetadata(fileName, contentType, inputStream));
+ FileMetadata thumbnailMetadata = new FileMetadata(fileName, contentType, inputStream);
+
+ if(fileRepository.exists(thumbnailMetadata.getFileName()))
+ fileRepository.delete(thumbnailMetadata.getFileName());
+
+ String filePath = fileRepository.save(thumbnailMetadata);
setAssetInformation(configureAssetInformationThumbnail(getAssetInformation(), contentType, filePath));
}
diff --git a/basyx.common/basyx.filerepository-backend-inmemory/pom.xml b/basyx.common/basyx.filerepository-backend-inmemory/pom.xml
index 1d133b9b8..c1fd8376b 100644
--- a/basyx.common/basyx.filerepository-backend-inmemory/pom.xml
+++ b/basyx.common/basyx.filerepository-backend-inmemory/pom.xml
@@ -18,6 +18,12 @@
org.eclipse.digitaltwin.basyx
basyx.filerepository-backend
+
+ org.eclipse.digitaltwin.basyx
+ basyx.filerepository-backend
+ tests
+ test
+
commons-io
commons-io
diff --git a/basyx.common/basyx.filerepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestInMemoryFileRepository.java b/basyx.common/basyx.filerepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestInMemoryFileRepository.java
new file mode 100644
index 000000000..b0c495679
--- /dev/null
+++ b/basyx.common/basyx.filerepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestInMemoryFileRepository.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (C) 2024 the Eclipse BaSyx Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * SPDX-License-Identifier: MIT
+ ******************************************************************************/
+package org.eclipse.digitaltwin.basyx.core.filerepository;
+
+import org.eclipse.digitaltwin.basyx.core.filerepository.backend.FileRepositoryTestSuite;
+
+/**
+ * Integration Test for InMemoryFileRepository
+ *
+ * @author witt
+ *
+ */
+public class TestInMemoryFileRepository extends FileRepositoryTestSuite{
+
+ @Override
+ protected FileRepository getFileRepository() {
+ return new InMemoryFileRepository();
+ }
+}
diff --git a/basyx.common/basyx.filerepository-backend-mongodb/pom.xml b/basyx.common/basyx.filerepository-backend-mongodb/pom.xml
index 5bb93fe44..4b2bfd0c6 100644
--- a/basyx.common/basyx.filerepository-backend-mongodb/pom.xml
+++ b/basyx.common/basyx.filerepository-backend-mongodb/pom.xml
@@ -13,6 +13,12 @@
org.eclipse.digitaltwin.basyx
basyx.filerepository-backend
+
+
+ org.eclipse.digitaltwin.basyx
+ basyx.filerepository-backend
+ tests
+ test
org.eclipse.digitaltwin.basyx
diff --git a/basyx.common/basyx.filerepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/core/filerepository/MongoDBFileRepository.java b/basyx.common/basyx.filerepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/core/filerepository/MongoDBFileRepository.java
index efdab453e..d38725bd8 100644
--- a/basyx.common/basyx.filerepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/core/filerepository/MongoDBFileRepository.java
+++ b/basyx.common/basyx.filerepository-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/core/filerepository/MongoDBFileRepository.java
@@ -68,8 +68,13 @@ public static GridFsTemplate buildDefaultGridFsTemplate(MongoTemplate mongoTempl
@Override
public String save(FileMetadata fileMetadata) throws FileHandlingException {
+
+ if (exists(fileMetadata.getFileName()))
+ throw new FileHandlingException();
+
gridFsTemplate.store(fileMetadata.getFileContent(), fileMetadata.getFileName(), fileMetadata.getContentType());
- return fileMetadata.getFileName();
+
+ return fileMetadata.getFileName();
}
@Override
diff --git a/basyx.common/basyx.filerepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestMongoDBFileRepository.java b/basyx.common/basyx.filerepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestMongoDBFileRepository.java
new file mode 100644
index 000000000..6d0059efd
--- /dev/null
+++ b/basyx.common/basyx.filerepository-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/TestMongoDBFileRepository.java
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (C) 2024 the Eclipse BaSyx Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * SPDX-License-Identifier: MIT
+ ******************************************************************************/
+package org.eclipse.digitaltwin.basyx.core.filerepository;
+
+import org.eclipse.digitaltwin.basyx.core.filerepository.backend.FileRepositoryTestSuite;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.gridfs.GridFsTemplate;
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+
+/**
+ * Integration Test for MongoDBFileRepository
+ *
+ * Requires that a mongoDb server is running
+ *
+ * @author witt
+ *
+ */
+public class TestMongoDBFileRepository extends FileRepositoryTestSuite{
+ private static MongoTemplate mongoTemplate = createMongoTemplate();
+ private static GridFsTemplate gridFsTemplate = configureDefaultGridFsTemplate(mongoTemplate);
+
+ @Override
+ protected FileRepository getFileRepository() {
+ return new MongoDBFileRepository(gridFsTemplate);
+ }
+
+ private static MongoTemplate createMongoTemplate() {
+ String connectionURL = "mongodb://mongoAdmin:mongoPassword@localhost:27017/";
+
+ MongoClient client = MongoClients.create(connectionURL);
+
+ return new MongoTemplate(client, "BaSyxTestDb");
+ }
+
+ private static GridFsTemplate configureDefaultGridFsTemplate(MongoTemplate mongoTemplate) {
+ return new GridFsTemplate(mongoTemplate.getMongoDatabaseFactory(), mongoTemplate.getConverter());
+ }
+}
diff --git a/basyx.common/basyx.filerepository-backend/pom.xml b/basyx.common/basyx.filerepository-backend/pom.xml
index db8a3a238..e1cb40fb9 100644
--- a/basyx.common/basyx.filerepository-backend/pom.xml
+++ b/basyx.common/basyx.filerepository-backend/pom.xml
@@ -12,5 +12,10 @@
org.eclipse.digitaltwin.basyx
basyx.core
+
+ commons-io
+ commons-io
+
+
\ No newline at end of file
diff --git a/basyx.common/basyx.filerepository-backend/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/backend/FileRepositoryTestSuite.java b/basyx.common/basyx.filerepository-backend/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/backend/FileRepositoryTestSuite.java
new file mode 100644
index 000000000..1a01b47b3
--- /dev/null
+++ b/basyx.common/basyx.filerepository-backend/src/test/java/org/eclipse/digitaltwin/basyx/core/filerepository/backend/FileRepositoryTestSuite.java
@@ -0,0 +1,152 @@
+/*******************************************************************************
+ * Copyright (C) 2024 the Eclipse BaSyx Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * SPDX-License-Identifier: MIT
+ ******************************************************************************/
+package org.eclipse.digitaltwin.basyx.core.filerepository.backend;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.commons.io.IOUtils;
+import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
+import org.eclipse.digitaltwin.basyx.core.exceptions.FileHandlingException;
+import org.eclipse.digitaltwin.basyx.core.filerepository.FileMetadata;
+import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
+import org.junit.Test;
+
+/**
+ * Testsuite for implementations of the FileRepository interface
+ *
+ * @author witt
+ */
+public abstract class FileRepositoryTestSuite {
+ protected abstract FileRepository getFileRepository();
+
+ @Test
+ public void saveValidFile() {
+ FileRepository fileRepo = getFileRepository();
+
+ FileMetadata fileMetadata = createDummyFile();
+ String filePath = saveTestFile(fileRepo, fileMetadata);
+
+ assertEquals(true, fileRepo.exists(filePath));
+ }
+
+ @Test (expected = FileHandlingException.class)
+ public void saveExistingFile() {
+ FileRepository fileRepo = getFileRepository();
+
+ FileMetadata fileMetadata = createDummyFile();
+
+ String filePath = fileRepo.save(fileMetadata);
+ filePath = fileRepo.save(fileMetadata);
+ }
+
+ @Test
+ public void findExistingFile() throws IOException {
+ FileRepository fileRepo = getFileRepository();
+
+ FileMetadata fileMetadata = createDummyFile();
+ String filePath = saveTestFile(fileRepo, fileMetadata);
+
+ InputStream streamToCompare = new ByteArrayInputStream("this is test data".getBytes());
+
+ InputStream actualStream = fileRepo.find(filePath);
+
+ IOUtils.contentEquals(streamToCompare, actualStream);
+ }
+
+ @Test(expected = FileDoesNotExistException.class)
+ public void findNonExistingFile() {
+ FileRepository fileRepo = getFileRepository();
+
+ InputStream testInputStream = fileRepo.find("does not exist");
+ }
+
+ @Test
+ public void deleteFile() {
+ FileRepository fileRepo = getFileRepository();
+
+ FileMetadata fileMetadata = createDummyFile();
+ String filePath = saveTestFile(fileRepo, fileMetadata);
+
+ fileRepo.delete(filePath);
+
+ assertFalse(fileRepo.exists(filePath));
+ }
+
+ @Test(expected = FileDoesNotExistException.class)
+ public void deleteNonExistingFile() {
+ FileRepository fileRepo = getFileRepository();
+
+ String filePath = "does not exist";
+
+ fileRepo.delete(filePath);
+ }
+
+ @Test
+ public void fileDoesExists() {
+ FileRepository fileRepo = getFileRepository();
+
+ FileMetadata fileMetadata = createDummyFile();
+ String filePath = saveTestFile(fileRepo, fileMetadata);
+
+ boolean fileExists = fileRepo.exists(filePath);
+
+ assertTrue(fileExists);
+ }
+
+ @Test
+ public void fileDoesNotExists() {
+ FileRepository fileRepo = getFileRepository();
+
+ String filePath = "does not exist";
+
+ boolean fileNotExists = fileRepo.exists(filePath);
+
+ assertFalse(fileNotExists);
+ }
+
+ protected FileMetadata createDummyFile() {
+ String name = "test";
+ String contentType = "txt";
+ String contentInput ="this is test data";
+
+ InputStream content = new ByteArrayInputStream(contentInput.getBytes());
+
+ return new FileMetadata(name, contentType, content);
+ }
+
+ protected String saveTestFile(FileRepository fileRepo, FileMetadata fileMetadata) {
+ String filePath = fileMetadata.getFileName();
+
+ if(fileRepo.exists(fileMetadata.getFileName()))
+ fileRepo.delete(filePath);
+
+ return fileRepo.save(fileMetadata);
+ }
+}
diff --git a/basyx.submodelservice/basyx.submodelservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/InMemorySubmodelService.java b/basyx.submodelservice/basyx.submodelservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/InMemorySubmodelService.java
index ce0c38ac8..ac9eaaccd 100644
--- a/basyx.submodelservice/basyx.submodelservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/InMemorySubmodelService.java
+++ b/basyx.submodelservice/basyx.submodelservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/InMemorySubmodelService.java
@@ -271,6 +271,9 @@ public void setFileValue(String idShortPath, String fileName, InputStream inputS
String uniqueFileName = createUniqueFileName(idShortPath, fileName);
FileMetadata fileMetadata = new FileMetadata(uniqueFileName, fileSmElement.getContentType(), inputStream);
+
+ if(fileRepository.exists(fileMetadata.getFileName()))
+ fileRepository.delete(fileMetadata.getFileName());
String filePath = fileRepository.save(fileMetadata);