Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public SubmodelElement getSubmodelElementFromIdShortPath(String idShortPath) thr
*/
public String getIdShortPathOfParentElement(String idShortPath) {

boolean isLastElementABracket = idShortPath.endsWith("]");
if (isLastElementABracket){
int lastElementBracketIndex = idShortPath.lastIndexOf("[");
return idShortPath.substring(0, lastElementBracketIndex);
}

int lastElementIdShortIndex = idShortPath.lastIndexOf(".");

if (lastElementIdShortIndex == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Stack;

import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.submodelservice.pathparsing.HierarchicalSubmodelElementParser;
import org.eclipse.digitaltwin.basyx.submodelservice.pathparsing.PathToken;
import org.eclipse.digitaltwin.basyx.submodelservice.pathparsing.SubmodelElementIdShortPathParser;
import org.junit.Test;
Expand Down Expand Up @@ -78,4 +79,27 @@ public void idShortWithSpecialCharactersDoesNotThrowError() {
Stack<PathToken> tokenStack = pathParser.parsePathTokens(ID_SHORT_WITH_SPECIAL_CHARACTERS);
assertEquals(ID_SHORT_WITH_SPECIAL_CHARACTERS, tokenStack.pop().getToken());
}

@Test
public void parentPathIsExtractedCorrectly() {
String[][] testCases = {
// input expected output
{ "a", "a" },
{ "a.b", "a" },
{ "a.b[0]", "a.b" },
{ "a.b[0].c", "a.b[0]" },
{ "a.b[0].c.d", "a.b[0].c" },
{ "sensor[3].value[1]", "sensor[3].value" },
{ "root.list[12][3]", "root.list[12]" },
{ "deep.nest[4].elem[2].data", "deep.nest[4].elem[2]" },
};

for (String[] testCase : testCases) {
String input = testCase[0];
String expected = testCase[1];
HierarchicalSubmodelElementParser hierarchicalSubmodelElementParser = new HierarchicalSubmodelElementParser(null);
String actual = hierarchicalSubmodelElementParser.getIdShortPathOfParentElement(input);
assertEquals("Failed for input: " + input, expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,23 @@ public void updateFileSMEWithFileSME() throws FileNotFoundException, IOException
assertStoredFileContentEquals(submodelService, idShortPathPropertyInSmeCol, DUMMY_JSON_2);
}

@Test
public void updateSMEInSubmodelElementList(){
Submodel operationDataSubmodel = DummySubmodelFactory.createOperationalDataSubmodelWithHierarchicalSubmodelElements();
SubmodelService submodelService = getSubmodelService(operationDataSubmodel);

DefaultProperty submodelElement = (DefaultProperty) submodelService.getSubmodelElement(generateIdShortPath());

String expectedValue = "1308";
submodelElement.setValue(expectedValue);

submodelService.updateSubmodelElement(generateIdShortPath(), submodelElement);

DefaultProperty actualElement = (DefaultProperty) submodelService.getSubmodelElement(generateIdShortPath());

assertEquals(expectedValue, actualElement.getValue());
}

@Test
public void deleteNestedSubmodelElementInSubmodelElementCollection() {
Submodel operationDataSubmodel = DummySubmodelFactory.createOperationalDataSubmodelWithHierarchicalSubmodelElements();
Expand Down