Skip to content

Commit

Permalink
Merge pull request #202 from eclipse-basyx/fix-xml-qualifier
Browse files Browse the repository at this point in the history
Fixes qualifier XML parsing bug
  • Loading branch information
FrankSchnicke committed Jan 5, 2023
2 parents e1dc35f + 16220c0 commit a0ad687
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ public static List<Map<String, Object>> getList(Object xmlObject) {
/**
* If the content of a XML-Element is requested, the parser returns an Object or
* null, if it doesn't exist.<br>
* This function casts the Object into a String and replaces null with an empty
* String.
* This function returns a String representation if possible. Null and Maps are
* replaced with an empty String.
*
* @param object
* a Object that is either a String or null
* @return the given String or an empty String
* @return the trimmed String object or an empty String
*/
public static String getString(Object object) {
return object instanceof String ? ((String) object).trim() : "";
// On XML level, it is not differentiated between an empty tag that should
// contain text and an empty tag that should contain further text. Thus, Maps
// are replaced with ""
if (object == null || (object instanceof Map<?, ?>))
return "";

return object.toString().trim();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ public void setValue(Object obj) {
@Override
public Object getValue() {
Object value = get(Qualifier.VALUE);
if (value instanceof String) {
return ValueTypeHelper.getJavaObject(value, getValueType());
} else {
return value;
}
return ValueTypeHelper.getJavaObject(value, getValueType());
}

public void setValueId(IReference obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -59,13 +61,16 @@
import org.eclipse.basyx.aas.metamodel.map.parts.Asset;
import org.eclipse.basyx.submodel.metamodel.api.ISubmodel;
import org.eclipse.basyx.submodel.metamodel.api.parts.IConceptDescription;
import org.eclipse.basyx.submodel.metamodel.api.qualifier.qualifiable.IConstraint;
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.qualifiable.Qualifier;
import org.eclipse.basyx.submodel.metamodel.map.reference.Reference;
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.property.Property;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.valuetype.ValueType;
import org.eclipse.basyx.submodel.metamodel.map.submodelelement.operation.Operation;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -93,6 +98,8 @@ public class TestAASXToMetamodelConverterFromBaSyx {
private static final String INTEGER_PROPERTY_IDSHORT = "int";
private static final String DOUBLE_PROPERTY_IDSHORT = "decimal";
private static final String STRING_PROPERTY_IDSHORT = "string";
private static final String OPERATION_WITH_QUALIFIER_IDSHORT = "operationWithQualifier";
private static final Integer QUALIFIER_VALUE = 100;
private static final Boolean EXPECTED_BOOLEAN_VALUE = true;
private static final int EXPECTED_INTEGER_VALUE = 42;
private static final double EXPECTED_DOUBLE_VALUE = 3.14159265359;
Expand Down Expand Up @@ -197,6 +204,23 @@ public void submodelElementPropertyValues() throws InvalidFormatException, IOExc
assertEquals(EXPECTED_STRING_VALUE, stringProperty.getValue());
}

/**
* Test Qualifiers are parsed properly
*/
@Test
public void testQualifierValue() throws Exception {
Set<AASBundle> bundles = packageManager.retrieveAASBundles();
AASBundle bundle = bundles.stream().findFirst().get();

ISubmodel parsedSubmodel = bundle.getSubmodels().stream().findFirst().get();
Map<String, ISubmodelElement> submodelElements = parsedSubmodel.getSubmodelElements();

ISubmodelElement operationWithQualifer = submodelElements.get(OPERATION_WITH_QUALIFIER_IDSHORT);
IConstraint[] constraints = operationWithQualifer.getQualifiers().toArray(new IConstraint[] {});
Qualifier qualifier = (Qualifier)constraints[0];
assertEquals(QUALIFIER_VALUE, qualifier.getValue());
}

/**
* Tests the connected files of the parsed AASX file.
*
Expand Down Expand Up @@ -429,6 +453,7 @@ private void createAASXFile(String filePath) throws IOException, TransformerExce
sm.addSubmodelElement(createIntegerProperty());
sm.addSubmodelElement(createDoubleProperty());
sm.addSubmodelElement(createStringProperty());
sm.addSubmodelElement(createOperationWithQualifier());
sm.addSubmodelElement(createBaSyxFile(PDF_PATH, PDF_MIMETYPE, PDF_IDSHORT));
aas.addSubmodel(sm);

Expand All @@ -448,7 +473,7 @@ private void createAASXFile(String filePath) throws IOException, TransformerExce
// size for the test comparison
fileList.add(createInMemoryFile(IMAGE_PATH));
fileList.add(createInMemoryFile(PDF_PATH));
submodelElementsSize = 7;
submodelElementsSize = 8;

Thumbnail thumbnail = new Thumbnail(ThumbnailExtension.PNG, new ByteArrayInputStream(THUMBNAIL));

Expand Down Expand Up @@ -501,6 +526,16 @@ private ISubmodelElement createStringProperty() {
return stringProperty;
}

private ISubmodelElement createOperationWithQualifier() {
Operation operation = new Operation(OPERATION_WITH_QUALIFIER_IDSHORT);
Qualifier qualifier = new Qualifier();
qualifier.setValue(QUALIFIER_VALUE);
qualifier.setValueType(ValueType.Int32);
qualifier.setType("newType");
operation.setQualifiers(Collections.singletonList(qualifier));
return operation;
}

/**
* Delete created files
*/
Expand Down

0 comments on commit a0ad687

Please sign in to comment.