Skip to content

Commit 220f35a

Browse files
committed
feat: smservice: add general CrudSubmodelService and adapt -backend-mongodb to use it
1 parent 933bb0b commit 220f35a

File tree

8 files changed

+63
-136
lines changed

8 files changed

+63
-136
lines changed

basyx.submodelservice/basyx.submodelservice-backend-mongodb/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@
4848
<groupId>org.springframework.boot</groupId>
4949
<artifactId>spring-boot-starter</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
<scope>test</scope>
55+
</dependency>
5156
</dependencies>
5257
</project>

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/MongoDBSubmodelServiceFactory.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/SingleSubmodelMongoDBBackendProvider.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.eclipse.digitaltwin.basyx.submodelservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication(scanBasePackages = "org.eclipse.digitaltwin.basyx")
7+
public class DummyMongoDbSubmodelServiceComponent {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DummyMongoDbSubmodelServiceComponent.class, args);
11+
}
12+
}

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/TestMongoDBSubmodelService.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,46 @@
2727
package org.eclipse.digitaltwin.basyx.submodelservice;
2828

2929
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
30-
import org.eclipse.digitaltwin.basyx.common.mongocore.BasyxMongoMappingContext;
3130
import org.eclipse.digitaltwin.basyx.common.mongocore.MongoDBUtilities;
3231
import org.eclipse.digitaltwin.basyx.core.exceptions.FeatureNotSupportedException;
3332
import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
34-
import org.eclipse.digitaltwin.basyx.core.filerepository.MongoDBFileRepository;
35-
import org.junit.After;
33+
import org.junit.Before;
3634
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.springframework.beans.factory.annotation.Autowired;
37+
import org.springframework.boot.test.context.SpringBootTest;
3738
import org.springframework.data.mongodb.core.MongoTemplate;
38-
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
39-
40-
import com.mongodb.client.MongoClient;
41-
import com.mongodb.client.MongoClients;
39+
import org.springframework.test.context.junit4.SpringRunner;
4240

4341
/**
4442
* Test for mongoDb submodel service backend
4543
*
4644
* @author zhangzai, mateusmolina
4745
*
4846
*/
47+
@SpringBootTest
48+
@RunWith(SpringRunner.class)
4949
public class TestMongoDBSubmodelService extends SubmodelServiceSuite {
50-
private final String COLLECTION = "submodelTestCollection";
51-
private final String CONNECTION_URL = "mongodb://mongoAdmin:mongoPassword@localhost:27017";
52-
private final MongoClient CLIENT = MongoClients.create(CONNECTION_URL);
53-
private final MongoTemplate TEMPLATE = new MongoTemplate(CLIENT, "BaSyxTestDb");
54-
private final GridFsTemplate GRIDFS_TEMPLATE = new GridFsTemplate(TEMPLATE.getMongoDatabaseFactory(), TEMPLATE.getConverter());
55-
private FileRepository fileRepository = new MongoDBFileRepository(GRIDFS_TEMPLATE);
5650

57-
@After
51+
static final String TEST_COLLECTION = "submodelServiceTestCollection";
52+
53+
@Autowired
54+
private FileRepository fileRepository;
55+
56+
@Autowired
57+
private SubmodelServiceFactory submodelServiceFactory;
58+
59+
@Autowired
60+
private MongoTemplate mongoTemplate;
61+
62+
@Before
5863
public void clear() {
59-
MongoDBUtilities.clearCollection(TEMPLATE, COLLECTION);
64+
MongoDBUtilities.clearCollection(mongoTemplate, TEST_COLLECTION);
6065
}
6166

6267
@Override
6368
protected SubmodelService getSubmodelService(Submodel submodel) {
64-
return null; // todo temp
69+
return submodelServiceFactory.create(submodel);
6570
}
6671

6772
@Override
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
basyx.backend = MongoDB
2+
3+
basyx.submodelservice.mongodb.collectionName= submodelServiceTestCollection
4+
5+
spring.data.mongodb.host=127.0.0.1
6+
spring.data.mongodb.port=27017
7+
spring.data.mongodb.database=aas-env
8+
spring.data.mongodb.authentication-database=admin
9+
spring.data.mongodb.username=mongoAdmin
10+
spring.data.mongodb.password=mongoPassword

basyx.submodelservice/basyx.submodelservice-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/CrudSubmodelService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
1010
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
1111
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
12+
import org.eclipse.digitaltwin.basyx.core.exceptions.FeatureNotSupportedException;
1213
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
1314
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
1415
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
@@ -64,11 +65,15 @@ public void createSubmodelElement(String idShortPath, SubmodelElement submodelEl
6465

6566
@Override
6667
public void updateSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
68+
deleteAssociatedFileIfAny(idShortPath);
69+
6770
backend.updateSubmodelElement(submodelId, idShortPath, submodelElement);
6871
}
6972

7073
@Override
7174
public void deleteSubmodelElement(String idShortPath) throws ElementDoesNotExistException {
75+
deleteAssociatedFileIfAny(idShortPath);
76+
7277
backend.deleteSubmodelElement(submodelId, idShortPath);
7378
}
7479

@@ -79,7 +84,7 @@ public void patchSubmodelElements(List<SubmodelElement> submodelElementList) {
7984

8085
@Override
8186
public OperationVariable[] invokeOperation(String idShortPath, OperationVariable[] input) throws ElementDoesNotExistException {
82-
throw new UnsupportedOperationException("Unimplemented method 'invokeOperation'");
87+
throw new FeatureNotSupportedException("Unimplemented method 'invokeOperation'");
8388
}
8489

8590
@Override
@@ -106,4 +111,11 @@ public InputStream getFileByFilePath(String filePath) {
106111
private void hostSubmodel(Submodel submodel) {
107112
this.backend.save(submodel);
108113
}
114+
115+
private void deleteAssociatedFileIfAny(String idShortPath) {
116+
try {
117+
backend.deleteFileValue(submodelId, idShortPath);
118+
} catch (Exception e) {
119+
}
120+
}
109121
}

basyx.submodelservice/basyx.submodelservice-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/CrudSubmodelServiceFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
44
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService;
55
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceFactory;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
78
import org.springframework.stereotype.Component;
89

910
@Component
1011
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${basyx.backend:}')")
12+
@ConditionalOnBean(SingleSubmodelBackend.class)
1113
public class CrudSubmodelServiceFactory implements SubmodelServiceFactory{
1214

1315
private final SingleSubmodelBackend backend;

0 commit comments

Comments
 (0)