Skip to content

Commit

Permalink
Adds Testenvironment for FileRepository (#336)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ahoimariew authored Aug 20, 2024
1 parent 33f614c commit d574c3a
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
6 changes: 6 additions & 0 deletions basyx.common/basyx.filerepository-backend-inmemory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 6 additions & 0 deletions basyx.common/basyx.filerepository-backend-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
5 changes: 5 additions & 0 deletions basyx.common/basyx.filerepository-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.core</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d574c3a

Please sign in to comment.