Skip to content

Commit

Permalink
Refactor AasRepository to use AASService for Thumbnail operations (#247)
Browse files Browse the repository at this point in the history
* Move FileRepository interface/implementations to Common

* Add file handling methods to SubmodelService API

* Move file related tests from RepositorySuite to ServiceSuite

* Refactor SubmodelRepo to use file handling logic from Service

* Fix name typo in MongoDBFileRepository

* Implement /attachment endpoints to the SubmodelServiceHttpController

* Modify injection method of FileRepository in SubmodelServiceFactory

* Implement methods for handling thumbnails in the AasService

* Move thumbnail rel. tests from AasRepository to AasServiceSuite

* Refactor CrudAasRepository to use AasService for thumbnail handling

* Override failing tests in AasServiceClient (TBD in another ticket)

* Remove FileRepository config beans from AasRepository

* Fix inconsistent FileRepository in
MongoDBSubmodelRepositoryConfiguration

* Address review remarks
  • Loading branch information
mateusmolina-iese committed Apr 10, 2024
1 parent 90f3477 commit 3ec6d72
Show file tree
Hide file tree
Showing 27 changed files with 394 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class AasEnvironmentLoaderTest {
@Before
public void setUp() {
submodelRepository = Mockito.spy(new CrudSubmodelRepository(new SubmodelInMemoryBackendProvider(), new InMemorySubmodelServiceFactory(new InMemoryFileRepository())));
aasRepository = Mockito.spy(new CrudAasRepository(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory()));
aasRepository = Mockito.spy(new CrudAasRepository(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())));
conceptDescriptionRepository = Mockito.spy(new CrudConceptDescriptionRepository(new ConceptDescriptionInMemoryBackendProvider()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*******************************************************************************
* 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.aasenvironment;

import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -64,7 +89,7 @@ public class TestAASEnvironmentSerialization {
@Before
public void setup() {
submodelRepository = new SimpleSubmodelRepositoryFactory(new SubmodelInMemoryBackendProvider(), new InMemorySubmodelServiceFactory(new InMemoryFileRepository())).create();
aasRepository = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory()).create();
aasRepository = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository())).create();
conceptDescriptionRepository = new SimpleConceptDescriptionRepositoryFactory(new ConceptDescriptionInMemoryBackendProvider(), createDummyConceptDescriptions(), "cdRepo").create();

for (Submodel submodel : createDummySubmodels()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend-inmemory</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.digitaltwin.basyx.aasrepository.backend.CrudAasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.backend.SimpleAasRepositoryFactory;
import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory;
import org.eclipse.digitaltwin.basyx.core.filerepository.InMemoryFileRepository;
import org.junit.Test;

/**
Expand All @@ -49,12 +50,12 @@ public class TestInMemoryAasRepository extends AasRepositorySuite {

@Override
protected AasRepository getAasRepository() {
return new SimpleAasRepositoryFactory(backendProvider, new InMemoryAasServiceFactory()).create();
return new SimpleAasRepositoryFactory(backendProvider, new InMemoryAasServiceFactory(new InMemoryFileRepository())).create();
}

@Test
public void getConfiguredInMemoryAasRepositoryName() {
AasRepository repo = new CrudAasRepository(backendProvider, new InMemoryAasServiceFactory(), CONFIGURED_AAS_REPO_NAME);
AasRepository repo = new CrudAasRepository(backendProvider, new InMemoryAasServiceFactory(new InMemoryFileRepository()), CONFIGURED_AAS_REPO_NAME);

assertEquals(CONFIGURED_AAS_REPO_NAME, repo.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.filerepository-backend-mongodb</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory;
import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory;
import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -46,7 +47,7 @@
@ConditionalOnExpression("'${basyx.backend}'.equals('MongoDB')")
public class AasMongoDBRepositoryConfiguration {
@Bean
public AasServiceFactory getAasServiceFactory() {
return new InMemoryAasServiceFactory();
public AasServiceFactory getAasServiceFactory(FileRepository fileRepository) {
return new InMemoryAasServiceFactory(fileRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory;
import org.eclipse.digitaltwin.basyx.common.mongocore.BasyxMongoMappingContext;
import org.eclipse.digitaltwin.basyx.common.mongocore.MongoDBUtilities;
import org.eclipse.digitaltwin.basyx.core.filerepository.MongoDBFileRepository;
import org.junit.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
Expand All @@ -59,13 +61,15 @@ public class TestMongoDBAasRepository extends AasRepositorySuite {
private static final String COLLECTION = "testAasCollection";
private static final String CONFIGURED_AAS_REPO_NAME = "configured-aas-repo-name";

private MongoTemplate mongoTemplate = createMongoTemplate();
private static MongoTemplate mongoTemplate = createMongoTemplate();

private static GridFsTemplate gridFsTemplate = configureDefaultGridFsTemplate(mongoTemplate);

@Override
protected AasRepository getAasRepository() {
MongoDBUtilities.clearCollection(mongoTemplate, COLLECTION);
AasBackendProvider aasBackendProvider = new AasMongoDBBackendProvider(new BasyxMongoMappingContext(), COLLECTION, mongoTemplate);
AasRepositoryFactory aasRepositoryFactory = new SimpleAasRepositoryFactory(aasBackendProvider, new InMemoryAasServiceFactory());
AasRepositoryFactory aasRepositoryFactory = new SimpleAasRepositoryFactory(aasBackendProvider, new InMemoryAasServiceFactory(new MongoDBFileRepository(gridFsTemplate)));

return aasRepositoryFactory.create();
}
Expand Down Expand Up @@ -96,7 +100,7 @@ public void updatedAasIsPersisted() {

@Test
public void getConfiguredMongoDBAasRepositoryName() {
AasRepository repo = new CrudAasRepository(new AasMongoDBBackendProvider(new BasyxMongoMappingContext(), COLLECTION, mongoTemplate), new InMemoryAasServiceFactory(), CONFIGURED_AAS_REPO_NAME);
AasRepository repo = new CrudAasRepository(new AasMongoDBBackendProvider(new BasyxMongoMappingContext(), COLLECTION, mongoTemplate), new InMemoryAasServiceFactory(new MongoDBFileRepository(gridFsTemplate)), CONFIGURED_AAS_REPO_NAME);

assertEquals(CONFIGURED_AAS_REPO_NAME, repo.getName());
}
Expand All @@ -112,12 +116,15 @@ private AssetAdministrationShell createDummyShellOnRepo(AasRepository aasReposit
return expectedShell;
}

private MongoTemplate createMongoTemplate() {
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());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.Resource;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory;
Expand Down Expand Up @@ -162,34 +161,25 @@ public String getName() {

@Override
public File getThumbnail(String aasId) {
Resource resource = getAssetInformation(aasId).getDefaultThumbnail();

AASThumbnailHandler.throwIfFileDoesNotExist(aasId, resource);
String filePath = resource.getPath();
return new File(filePath);
return getAasServiceOrThrow(aasId).getThumbnail();
}

@Override
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) {
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail();
AasService aasService = getAasServiceOrThrow(aasId);

if (thumbnail != null) {
updateThumbnailFile(aasId, fileName, contentType, inputStream, thumbnail);
return;
}
aasService.setThumbnail(fileName, contentType, inputStream);

String filePath = createFile(aasId, fileName, inputStream);
AASThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath);
updateAas(aasId, aasService.getAAS());
}

@Override
public void deleteThumbnail(String aasId) {
Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail();
AASThumbnailHandler.throwIfFileDoesNotExist(aasId, thumbnail);
AasService aasService = getAasServiceOrThrow(aasId);

deleteThumbnailFile(thumbnail);
aasService.deleteThumbnail();

updateThumbnailInAssetInformation(aasId);
updateAas(aasId, aasService.getAAS());
}

private AasService getAasServiceOrThrow(String aasId) {
Expand Down Expand Up @@ -220,31 +210,4 @@ private void throwIfAasDoesNotExist(String aasId) {
if (!aasBackend.existsById(aasId))
throw new ElementDoesNotExistException(aasId);
}

private void updateThumbnailInAssetInformation(String aasId) {
AssetInformation assetInfor = getAssetInformation(aasId);
assetInfor.getDefaultThumbnail().setContentType("");
assetInfor.getDefaultThumbnail().setPath("");
setAssetInformation(aasId, assetInfor);
}

private void deleteThumbnailFile(Resource thumbnail) {
String filePath = thumbnail.getPath();
java.io.File tmpFile = new java.io.File(filePath);
tmpFile.delete();
}

private void updateThumbnailFile(String aasId, String fileName, String contentType, InputStream inputStream, Resource thumbnail) {
String path = thumbnail.getPath();
AASThumbnailHandler.deleteExistingFile(path);
String filePath = createFile(aasId, fileName, inputStream);
AASThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath);
}

private String createFile(String aasId, String fileName, InputStream inputStream) {
String filePath = AASThumbnailHandler.createFilePath(AASThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName);
AASThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath);
return filePath;
}

}
2 changes: 1 addition & 1 deletion basyx.aasrepository/basyx.aasrepository-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package org.eclipse.digitaltwin.basyx.aasrepository;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
Expand Down Expand Up @@ -81,4 +83,20 @@ public AssetInformation getAssetInformation() {
return repoApi.getAssetInformation(aasId);
}

@Override
public File getThumbnail() {
return repoApi.getThumbnail(aasId);
}

@Override
public void setThumbnail(String fileName, String contentType, InputStream inputStream) {
repoApi.setThumbnail(aasId, fileName, contentType, inputStream);

}

@Override
public void deleteThumbnail() {
repoApi.deleteThumbnail(aasId);
}

}
Loading

0 comments on commit 3ec6d72

Please sign in to comment.