Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes storageAPI not converting to specific element types #354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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