diff --git a/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java b/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java index cc38fe32f..77b7a99d3 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java +++ b/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java @@ -26,6 +26,7 @@ package org.eclipse.digitaltwin.basyx.submodelrepository.core; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -33,11 +34,13 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd; import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable; import org.eclipse.digitaltwin.aas4j.v3.model.Property; import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; +import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel; import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException; @@ -53,8 +56,12 @@ import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceHelper; import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceSuite; import org.eclipse.digitaltwin.basyx.submodelservice.value.PropertyValue; +import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelValueOnly; import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * Testsuite for implementations of the SubmodelRepository interface * @@ -65,6 +72,7 @@ public abstract class SubmodelRepositorySuite extends SubmodelServiceSuite { private static final PaginationInfo NO_LIMIT_PAGINATION_INFO = new PaginationInfo(null, null); private static final String EMPTY_ID = " "; private static final String NULL_ID = null; + private static final String ID = "testId"; protected abstract SubmodelRepository getSubmodelRepository(); @@ -259,6 +267,57 @@ public void getPaginatedSubmodel() { assertEquals(1, cursorResult.getResult().size()); } + @Test + public void getSubmodelByIdMetadata() throws JsonProcessingException { + SubmodelRepository repo = getSubmodelRepository(); + Submodel expectedSubmodel = buildDummySubmodelWithNoSmElement(ID); + expectedSubmodel.setSubmodelElements(null); + repo.createSubmodel(expectedSubmodel); + + Submodel retrievedSubmodelMetadata = repo.getSubmodelByIdMetadata(ID); + retrievedSubmodelMetadata.setSubmodelElements(null); + + assertEquals(expectedSubmodel, retrievedSubmodelMetadata); + } + + @Test + public void getSubmodelByIdValueOnly() throws JsonProcessingException { + SubmodelRepository repo = getSubmodelRepository(); + Submodel submodel = buildDummySubmodelWithNoSmElement(ID); + + List submodelElements = buildDummySubmodelElements(); + submodel.setSubmodelElements(submodelElements); + repo.createSubmodel(submodel); + + SubmodelValueOnly expectedSmValueOnly = new SubmodelValueOnly(submodelElements); + SubmodelValueOnly retrievedSmValueOnly = repo.getSubmodelByIdValueOnly(ID); + + ObjectMapper mapper = new ObjectMapper(); + String expectedSmValueOnlyJSONContent = mapper.writeValueAsString(expectedSmValueOnly); + String retrievedSmValueOnlyJSONContent = mapper.writeValueAsString(retrievedSmValueOnly); + + assertEquals(expectedSmValueOnlyJSONContent, retrievedSmValueOnlyJSONContent); + } + + @Override + @Test + public void patchSubmodelElements() { + SubmodelRepository repo = getSubmodelRepository(); + Submodel submodel = buildDummySubmodelWithNoSmElement(ID); + + List submodelElements = buildDummySubmodelElements(); + submodel.setSubmodelElements(submodelElements); + repo.createSubmodel(submodel); + + List submodelElementsPatch = buildDummySubmodelElementsToPatch(); + repo.patchSubmodelElements(ID, submodelElementsPatch); + + Submodel patchedSubmodel = repo.getSubmodel(ID); + + assertEquals(submodel.getSubmodelElements().size(), patchedSubmodel.getSubmodelElements().size()); + assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements()); + } + // Has to be overwritten if backend does not support operations @Test public void invokeOperation() { diff --git a/basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java b/basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java index 495533893..9e73e7d40 100644 --- a/basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java +++ b/basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java @@ -52,8 +52,10 @@ import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEntity; +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringTextType; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty; +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList; import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException; @@ -89,6 +91,7 @@ public abstract class SubmodelServiceSuite { private static final String DUMMY_JSON_1 = "{\"name\":\"SampleJsonFile\",\"description\":\"A JSON file for verification\",\"version\":1}"; private static final String DUMMY_JSON_2 = "{\"name\":\"SampleJsonFile\",\"description\":\"A JSON file for verification\",\"version\":2}"; + private static final String ID = "testId"; @Test public void getSubmodel() { @@ -363,22 +366,22 @@ public void updateNonFileSME() { } @Test - public void updateNonNestedSME() { - Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel(); - SubmodelService submodelService = getSubmodelService(technicalSubmodel); - + public void updateNonNestedSME() { + Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel(); + SubmodelService submodelService = getSubmodelService(technicalSubmodel); + String idShortPath = "dummyProperty"; - Property property = createDummyProperty(idShortPath); - submodelService.createSubmodelElement(property); - - Property expectedUpdatedProperty = SubmodelServiceHelper.createDummyProperty(idShortPath, "arbitraryValue", DataTypeDefXsd.STRING); - submodelService.updateSubmodelElement(idShortPath, expectedUpdatedProperty); + Property property = createDummyProperty(idShortPath); + submodelService.createSubmodelElement(property); + + Property expectedUpdatedProperty = SubmodelServiceHelper.createDummyProperty(idShortPath, "arbitraryValue", DataTypeDefXsd.STRING); + submodelService.updateSubmodelElement(idShortPath, expectedUpdatedProperty); + + Property actualUpdatedProperty = (Property) submodelService.getSubmodelElement(idShortPath); + assertEquals(expectedUpdatedProperty, actualUpdatedProperty); + } - Property actualUpdatedProperty = (Property) submodelService.getSubmodelElement(idShortPath); - assertEquals(expectedUpdatedProperty, actualUpdatedProperty); - } - @Test public void updateNonFileSMEWithFileSME() { Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel(); @@ -606,6 +609,44 @@ public void deleteNonExistingFile() throws IOException { submodelService.deleteFileValue(SubmodelServiceHelper.SUBMODEL_TECHNICAL_DATA_FILE_ID_SHORT); } + @Test + public void patchSubmodelElements() { + + List submodelElements = buildDummySubmodelElements(); + Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements); + SubmodelService submodelService = getSubmodelService(submodel); + + List submodelElementsPatch = buildDummySubmodelElementsToPatch(); + submodelService.patchSubmodelElements(submodelElementsPatch); + + Submodel patchedSubmodel = submodelService.getSubmodel(); + + assertEquals(submodel.getSubmodelElements().size(), patchedSubmodel.getSubmodelElements().size()); + assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements()); + } + + protected Submodel buildDummySubmodelWithSmElement(String id, List submodelElements) { + return new DefaultSubmodel.Builder().id(id).submodelElements(submodelElements).build(); + } + + protected Submodel buildDummySubmodelWithNoSmElement(String id) { + return new DefaultSubmodel.Builder().id(id).build(); + } + + protected List buildDummySubmodelElements() { + Property prop = new DefaultProperty.Builder().idShort("propId").value("propValue").build(); + File file = new DefaultFile.Builder().idShort("fileId").contentType("contentTypeValue").value("fileValue").build(); + + return Arrays.asList(prop, file); + } + + protected List buildDummySubmodelElementsToPatch() { + Property prop = new DefaultProperty.Builder().idShort("propId").value("propNewValue").build(); + File file = new DefaultFile.Builder().idShort("fileId").contentType("contentTypeNewValue").value("fileNewValue").build(); + + return Arrays.asList(prop, file); + } + private void assertStoredFileContentEquals(SubmodelService submodelService, String fileIdShort, String content) throws IOException { java.io.File retrievedValue = submodelService.getFileByPath(fileIdShort); @@ -671,7 +712,6 @@ private String getExtension(String filename) { return FilenameUtils.getExtension(filename); } - private void deleteFileIfExisted(SubmodelService service, String idShort) { try { service.getFileByPath(idShort);