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 4 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
@@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to 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 CustomDataSpecification extends DataSpecificationContent {

LangStringNameType getName();
sebbader-sap marked this conversation as resolved.
Show resolved Hide resolved

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 CustomDataSpecificationBuilder<T extends CustomDataSpecification,
B extends CustomDataSpecificationBuilder<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
@@ -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 DefaultCustomDataSpecification implements CustomDataSpecification {
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 {
DefaultCustomDataSpecification other = (DefaultCustomDataSpecification) 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 CustomDataSpecificationBuilder<DefaultCustomDataSpecification,
Builder> {

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

@Override
protected DefaultCustomDataSpecification newBuildingInstance() {
return new DefaultCustomDataSpecification();
}
}
}
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,12 +19,26 @@
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.DataTypeIec61360;
import org.eclipse.digitaltwin.aas4j.v3.model.DefaultCustomDataSpecification;
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 All @@ -40,6 +55,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 +107,59 @@ 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(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();

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);
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 @@ -21,17 +21,18 @@
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, JsonProcessingException {
ObjectNode node = DeserializationHelper.getRootObjectNode(parser);
if (node == null) {
return null;
Expand All @@ -41,13 +42,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 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
}
}
};

private DataSpecificationIec61360 createDefaultDataSpecificationIec61360FromNode(JsonParser parser, JsonNode nodeContent) throws IOException {
return DeserializationHelper.createInstanceFromNode(parser, nodeContent, DefaultDataSpecificationIec61360.class);
return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw exception

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.eclipse.digitaltwin.aas4j.v3.model.DataSpecificationIec61360;
import com.google.common.base.CaseFormat;
import org.eclipse.digitaltwin.aas4j.v3.model.DataSpecificationContent;

import java.io.IOException;

Expand All @@ -30,22 +31,29 @@
* of a reference. Uses DataSpecificationManager to resolve java type to
* reference.
*/
public class EmbeddedDataSpecificationSerializer extends JsonSerializer<DataSpecificationIec61360> {
public class EmbeddedDataSpecificationSerializer extends JsonSerializer<DataSpecificationContent> {

@Override
public void serialize(DataSpecificationIec61360 data, JsonGenerator generator, SerializerProvider provider)
public void serialize(DataSpecificationContent data, JsonGenerator generator, SerializerProvider provider)
throws IOException {
if (data == null) {
return;
}
String class_name = "dataSpecification"; // default
try {
class_name = data.getClass().getInterfaces()[0].getSimpleName();
} catch (Exception e) {
// do nothing
}
class_name = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, class_name);
generator.writeStartObject();
generator.writeObjectField("dataSpecificationIec61360", data);
generator.writeObjectField(class_name, data);
generator.writeEndObject();

}

@Override
public void serializeWithType(DataSpecificationIec61360 data, JsonGenerator generator, SerializerProvider provider,
public void serializeWithType(DataSpecificationContent data, JsonGenerator generator, SerializerProvider provider,
TypeSerializer typedSerializer) throws IOException, JsonProcessingException {
serialize(data, generator, provider);
}
Expand Down
Loading