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

Adding CustomDataspecification Feature and UnitTests #207

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 @@ -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
@@ -0,0 +1,89 @@
/*
* 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;

import java.util.Objects;

/**
* 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 DefaultDummyDataSpecification implements DummyDataSpecification {
private LangStringNameType name;
private String text;
private int pages;

public LangStringNameType getName() {
return name;
}

public void setName(LangStringNameType name) {
this.name = name;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public int getPages() {
return pages;
};
public void setPages(int pages) {
this.pages = pages;
}

@Override
public int hashCode() {
return Objects.hash(this.name, this.text, this.pages);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null) {
return false;
} else if (this.getClass() != obj.getClass()) {
return false;
} else {
DefaultDummyDataSpecification other = (DefaultDummyDataSpecification) obj;
return Objects.equals(this.name, other.name) &&
Objects.equals(this.text, other.text) &&
Objects.equals(this.pages, other.pages);
}
}

/**
* This builder class can be used to construct a DefaultCustomDataSpecificationContent.
*/
public static class Builder extends DummyDataSpecificationBuilder<DefaultDummyDataSpecification,
Builder> {

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

@Override
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
@@ -0,0 +1,42 @@
/*
* 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;

import org.eclipse.digitaltwin.aas4j.v3.model.builder.ExtendableBuilder;

/**
* 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 abstract class DummyDataSpecificationBuilder<T extends DummyDataSpecification,
B extends DummyDataSpecificationBuilder<T, B>> extends ExtendableBuilder<T, B> {

public B name(LangStringNameType name) {
getBuildingInstance().setName(name);
return getSelf();
}

public B text(String text) {
getBuildingInstance().setText(text);
return getSelf();
}

public B pages(int pages) {
getBuildingInstance().setPages(pages);
return getSelf();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
* 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.
Expand All @@ -18,10 +19,14 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.AASSimple;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.util.ReflectionHelper;
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.DefaultDummyDataSpecification;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.Referable;
import org.json.JSONException;
Expand All @@ -40,6 +45,7 @@
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class JsonSerializerTest {
Expand Down Expand Up @@ -91,6 +97,33 @@ public void testSerializeEmptyReferableList() throws SerializationException {
assertEquals("[]", serialized);
}

/**
* This test ensures that future DataSpecificationContents can be added without adjustments in the code.
*
* @throws SerializationException
* @throws DeserializationException
*/
@Test
public void testSerializeCustomDataSpecification() throws SerializationException, DeserializationException {
JsonSerializer serializer = new JsonSerializer();
JsonDeserializer deserializer = new JsonDeserializer();

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

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

String jsonString = serializer.write(origin);
assertNotNull(jsonString);

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

assertTrue(origin.equals(copy));
}


private void validateAndCompare(ExampleData<Environment> exampleData) throws IOException, SerializationException, JSONException {
String expected = exampleData.fileContent();
String actual = new JsonSerializer().write(exampleData.getModel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
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;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.digitaltwin.aas4j.v3.model.DataSpecificationIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultDataSpecificationIec61360;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.util.ReflectionHelper;
import org.eclipse.digitaltwin.aas4j.v3.model.DataSpecificationContent;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

public class EmbeddedDataSpecificationsDeserializer extends JsonDeserializer<DataSpecificationIec61360> {
public class EmbeddedDataSpecificationsDeserializer extends JsonDeserializer<DataSpecificationContent> {

private static final String PROP_DATA_SPECIFICATION_CONTENT = "dataSpecificationIec61360";

@Override
public DataSpecificationIec61360 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 @@ -41,13 +41,23 @@ public DataSpecificationIec61360 deserialize(JsonParser parser, DeserializationC
}


private DataSpecificationIec61360 createEmbeddedDataSpecificationsFromContent(JsonParser parser, JsonNode node) throws IOException {
JsonNode nodeContent = node.get(PROP_DATA_SPECIFICATION_CONTENT);
return createDefaultDataSpecificationIec61360FromNode(parser, nodeContent);
}

private DataSpecificationIec61360 createDefaultDataSpecificationIec61360FromNode(JsonParser parser, JsonNode nodeContent) throws IOException {
return DeserializationHelper.createInstanceFromNode(parser, nodeContent, DefaultDataSpecificationIec61360.class);
private DataSpecificationContent createEmbeddedDataSpecificationsFromContent(JsonParser parser, JsonNode node) throws IOException {
String class_name = node.fieldNames().next();
Set<Class<?>> subtypes = ReflectionHelper.SUBTYPES.get(DataSpecificationContent.class);
Iterator<Class<?>> iter = subtypes.iterator();
while (iter.hasNext()) {
Class clazz = iter.next();
if (clazz.getSimpleName().toLowerCase().contains(class_name.toLowerCase())) {
try {
JsonNode nodeContent = node.get(class_name);
return (DataSpecificationContent) DeserializationHelper.createInstanceFromNode(parser, nodeContent, clazz);
} catch (Exception e) {
// do nothing and try next in list
}
}
};

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

}
Loading