Skip to content

Commit

Permalink
move CustomDataSpecification into model
Browse files Browse the repository at this point in the history
* reuse same environment for JSON and XML tests
* rename DataSpecification to DummyDataSpecfication
  • Loading branch information
sebbader-sap committed Nov 24, 2023
1 parent 7f16113 commit c33a3aa
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@

import org.eclipse.digitaltwin.aas4j.v3.model.AssetKind;
import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd;
import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.DefaultDummyDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultDataSpecificationIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEmbeddedDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEnvironment;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultExtension;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringDefinitionTypeIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringNameType;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultReference;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;

public class Examples {

Expand Down Expand Up @@ -77,4 +85,57 @@ public class Examples {
.build())
.build())
.build();


public static final Environment ENVIRONMENT_WITH_DUMMYDATASPEC = new DefaultEnvironment.Builder()
.submodels(
new DefaultSubmodel.Builder()
.id("urn:test")
.submodelElements(new DefaultFile.Builder()
.idShort("myIdShort").value("FileValue")
.build())
.embeddedDataSpecifications(
new DefaultEmbeddedDataSpecification.Builder()
.dataSpecificationContent(
new DefaultDummyDataSpecification.Builder()
.name(new DefaultLangStringNameType.Builder()
.language("en").text("myName").build())
.text("myText")
.pages(42)
.build())
.dataSpecification(
new DefaultReference.Builder()
.type(ReferenceTypes.EXTERNAL_REFERENCE)
.keys(
new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value("https://admin-shell.io/aas/3/0/CustomDataSpecification")
.build()
)
.build()
)
.build())
.embeddedDataSpecifications(
new DefaultEmbeddedDataSpecification.Builder().dataSpecificationContent(
new DefaultDataSpecificationIec61360.Builder()
.dataType(DataTypeIec61360.BLOB)
.definition(new DefaultLangStringDefinitionTypeIec61360.Builder()
.language("en").text("myDefinition")
.build())
.build()
)
.dataSpecification(
new DefaultReference.Builder()
.type(ReferenceTypes.EXTERNAL_REFERENCE)
.keys(
new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value("https://admin-shell.io/aas/3/0/RC02/DataSpecificationIec61360")
.build()
)
.build()
)
.build())
.build()
).build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* This interface is needed to test the serialization/deserialization of a custom data specification content.
* See: https://github.com/eclipse-aas4j/aas4j/issues/196
*/
public class DefaultCustomDataSpecification implements CustomDataSpecification {
public class DefaultDummyDataSpecification implements DummyDataSpecification {
private LangStringNameType name;
private String text;
private int pages;
Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean equals(Object obj) {
} else if (this.getClass() != obj.getClass()) {
return false;
} else {
DefaultCustomDataSpecification other = (DefaultCustomDataSpecification) obj;
DefaultDummyDataSpecification other = (DefaultDummyDataSpecification) obj;
return Objects.equals(this.name, other.name) &&
Objects.equals(this.text, other.text) &&
Objects.equals(this.pages, other.pages);
Expand All @@ -73,17 +73,17 @@ public boolean equals(Object obj) {
/**
* This builder class can be used to construct a DefaultCustomDataSpecificationContent.
*/
public static class Builder extends CustomDataSpecificationBuilder<DefaultCustomDataSpecification,
Builder> {
public static class Builder extends DummyDataSpecificationBuilder<DefaultDummyDataSpecification,
Builder> {

@Override
protected DefaultCustomDataSpecification.Builder getSelf() {
protected DefaultDummyDataSpecification.Builder getSelf() {
return this;
}

@Override
protected DefaultCustomDataSpecification newBuildingInstance() {
return new DefaultCustomDataSpecification();
protected DefaultDummyDataSpecification newBuildingInstance() {
return new DefaultDummyDataSpecification();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2023 SAP SE or an SAP affiliate company.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.digitaltwin.aas4j.v3.model;

/**
* This interface is needed to test the serialization/deserialization of a custom data specification content.
* See: https://github.com/eclipse-aas4j/aas4j/issues/196
*/
public interface DummyDataSpecification extends CustomDataSpecification {

LangStringNameType getName();

void setName(LangStringNameType name);


String getText();

void setText(String text);


int getPages();

void setPages(int pages);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
* See: https://github.com/eclipse-aas4j/aas4j/issues/196
*/
public abstract class CustomDataSpecificationBuilder<T extends CustomDataSpecification,
B extends CustomDataSpecificationBuilder<T, B>> extends ExtendableBuilder<T, B> {
public abstract class DummyDataSpecificationBuilder<T extends DummyDataSpecification,
B extends DummyDataSpecificationBuilder<T, B>> extends ExtendableBuilder<T, B> {

public B name(LangStringNameType name) {
getBuildingInstance().setName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.util.ExampleData;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.util.Examples;
import org.eclipse.digitaltwin.aas4j.v3.model.DataSpecificationContent;
import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.DefaultCustomDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.DefaultDummyDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Referable;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultDataSpecificationIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEmbeddedDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringDefinitionTypeIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringNameType;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultReference;
import org.json.JSONException;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -120,42 +110,16 @@ public void testSerializeCustomDataSpecification() throws SerializationException

// This is the only way to make the serialization to work.
Set<Class<?>> subtypes = ReflectionHelper.SUBTYPES.get(DataSpecificationContent.class);
subtypes.add(DefaultCustomDataSpecification.class);

org.eclipse.digitaltwin.aas4j.v3.model.File origin = new DefaultFile.Builder()
.idShort("myIdShort").value("FileValue")
.embeddedDataSpecifications(
new DefaultEmbeddedDataSpecification.Builder()
.dataSpecificationContent(
new DefaultCustomDataSpecification.Builder()
.name(new DefaultLangStringNameType.Builder()
.language("en").text("myName").build())
.text("myText")
.pages(42)
.build())
.dataSpecification(
new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE)
.keys(new DefaultKey.Builder().type(KeyTypes.REFERENCE_ELEMENT)
.build())
.build()
)
.build())
.embeddedDataSpecifications(
new DefaultEmbeddedDataSpecification.Builder().dataSpecificationContent(
new DefaultDataSpecificationIec61360.Builder()
.dataType(DataTypeIec61360.BLOB)
.definition(new DefaultLangStringDefinitionTypeIec61360.Builder()
.language("en").text("myDefinition")
.build())
.build()
).build())
.build();
subtypes.add(DefaultDummyDataSpecification.class);

Environment origin = org.eclipse.digitaltwin.aas4j.v3.dataformat.core.Examples.ENVIRONMENT_WITH_DUMMYDATASPEC ;

String jsonString = serializer.write(origin);
assertNotNull(jsonString);
org.eclipse.digitaltwin.aas4j.v3.model.File copy = deserializer.readReferable(
jsonString, org.eclipse.digitaltwin.aas4j.v3.model.File.class);

Environment copy = deserializer.read(jsonString);
assertNotNull(copy);

assertTrue(origin.equals(copy));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.deserialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -32,7 +31,7 @@ public class EmbeddedDataSpecificationsDeserializer extends JsonDeserializer<Dat


@Override
public DataSpecificationContent deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
public DataSpecificationContent deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException {
ObjectNode node = DeserializationHelper.getRootObjectNode(parser);
if (node == null) {
return null;
Expand All @@ -53,12 +52,12 @@ private DataSpecificationContent createEmbeddedDataSpecificationsFromContent(Jso
JsonNode nodeContent = node.get(class_name);
return (DataSpecificationContent) DeserializationHelper.createInstanceFromNode(parser, nodeContent, clazz);
} catch (Exception e) {
// do nothing
// do nothing and try next in list
}
}
};

return null;
throw new IOException("Was expecting a known subclass of DataSpecificationContent but found " + class_name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public void serialize(DataSpecificationContent data, JsonGenerator generator, Se
}
String class_name = "dataSpecification"; // default
try {
// known limitation: Only one interface must be implemented by the class, and the name of this interface must match exactly to the name of the DataSpecification
class_name = data.getClass().getInterfaces()[0].getSimpleName();
} catch (Exception e) {
// do nothing
// do nothing and continue with the default
}
class_name = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, class_name);
generator.writeStartObject();
Expand Down
Loading

0 comments on commit c33a3aa

Please sign in to comment.