Skip to content

Commit

Permalink
Fixes storageAPI not converting to specific element types
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Espen <Daniel.Espen@iese.fraunhofer.de>
  • Loading branch information
Daespen committed Jun 20, 2023
1 parent b437ff7 commit 44a8d93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement;
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElementCollection;
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.operation.IOperation;
import org.eclipse.basyx.submodel.metamodel.facade.SubmodelElementMapCollectionConverter;
import org.eclipse.basyx.submodel.metamodel.facade.submodelelement.SubmodelElementFacadeFactory;
import org.eclipse.basyx.submodel.metamodel.map.Submodel;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElement;
Expand Down Expand Up @@ -173,35 +174,20 @@ private void throwPathCouldNotBeResolvedException(String idShort) {

@Override
public ISubmodelElement getSubmodelElement(String idShortPath) {
if (idShortPath.contains("/")) {
List<String> idShorts = idShortsPathAsList(idShortPath);
return getNestedSubmodelElement(idShorts);
} else {
return getTopLevelSubmodelElement(idShortPath);
}
List<String> idShorts = idShortsPathAsList(idShortPath);
ISubmodelElement submodelElement = getNestedSubmodelElement(idShorts);
return convertSubmodelElement(submodelElement);
}

private ISubmodelElement getNestedSubmodelElement(List<String> idShorts) {
Submodel submodel = (Submodel) getSubmodel();
return convertSubmodelElement(getNestedSubmodelElement(submodel, idShorts));
}

private ISubmodelElement getTopLevelSubmodelElement(String idShort) {
Submodel submodel = (Submodel) getSubmodel();
Map<String, ISubmodelElement> submodelElements = submodel.getSubmodelElements();
ISubmodelElement element = submodelElements.get(idShort);
if (element == null) {
throw new ResourceNotFoundException("The element \"" + idShort + "\" could not be found");
}
return convertSubmodelElement(element);
return getNestedSubmodelElement(submodel, idShorts);
}

@SuppressWarnings("unchecked")
private ISubmodelElement convertSubmodelElement(ISubmodelElement element) {
Map<String, Object> elementMap = (Map<String, Object>) element;
IModelProvider elementProvider = new SubmodelElementProvider(new VABMapProvider(elementMap));
Object elementVABObj = elementProvider.getValue("");
return SubmodelElement.createAsFacade((Map<String, Object>) elementVABObj);
Map<String, Object> convertedCollectionMap = SubmodelElementMapCollectionConverter.smElementToMap((Map<String, Object>) element);
return SubmodelElement.createAsFacade(convertedCollectionMap);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;

import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId;
import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAPI;
import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration;
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement;
import org.eclipse.basyx.submodel.metamodel.map.Submodel;
import org.eclipse.basyx.submodel.metamodel.map.qualifier.LangStrings;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElementCollection;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.File;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.MultiLanguageProperty;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property;
import org.eclipse.basyx.vab.exception.provider.ResourceNotFoundException;
import org.junit.Test;

Expand Down Expand Up @@ -106,6 +110,7 @@ public void fileSubmodelElementFileIsAutomaticallyDeleted() throws FileNotFoundE
bucket.downloadToStream("fileSmeIdShort.xml", os);
}

@SuppressWarnings("unchecked")
@Test
public void submodelElementInCollection() {
MongoDBSubmodelAPI submodelAPI = createAPIWithPreconfiguredSubmodel();
Expand All @@ -119,6 +124,12 @@ public void submodelElementInCollection() {

Object value = submodelAPI.getSubmodelElementValue(collection.getIdShort() + "/" + mlprop.getIdShort());

Collection<ISubmodelElement> updatedSubmodelElements = submodelAPI.getSubmodelElements();
Map<String, Object> updatedCollectionMap = (Map<String, Object>) submodelAPI.getSubmodelElement("collection");
Collection<?> collectionValue = (Collection<?>) updatedCollectionMap.get(Property.VALUE);

assertEquals(1, updatedSubmodelElements.size());
assertEquals(1, collectionValue.size());
assertEquals(expected, value);
}

Expand Down

0 comments on commit 44a8d93

Please sign in to comment.