Skip to content

Commit

Permalink
Value-Only serialization of BasicEvent (eclipse-basyx#262)
Browse files Browse the repository at this point in the history
* Add serialization of basic event

Signed-off-by: Zai Zhang <[email protected]>

* Add unit test for value only basic event serialization

Signed-off-by: Zai Zhang <[email protected]>

---------

Signed-off-by: Zai Zhang <[email protected]>
  • Loading branch information
zhangzai123 authored Apr 19, 2024
1 parent afaef43 commit 241b96e
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class TestAasEnvironmentHTTP {
private static final String JSON_ENV_PATH = "testEnvironment.json";
private static final String XML_ENV_PATH = "testEnvironment.xml";
private static final String WRONGEXT_ENV_PATH = "testEnvironment.txt";
private static final String JSON_OPERATIONALDATA_ENV_PATH = "operationalDataEnvironment.json";
private static final String BASIC_EVENT_VALUE_ONLY = "BasicEventValueOnly.json";

private static ConfigurableApplicationContext appContext;
private static SubmodelRepository submodelRepo;
Expand Down Expand Up @@ -104,6 +106,17 @@ public void testAASEnvironmentSertializationWithJSON() throws IOException, Parse
TestAASEnvironmentSerialization.validateJSON(actual, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription);
}

@Test
public void testAASEnvironmentSerialization_ValueOnly() throws FileNotFoundException, IOException, ParseException {
CloseableHttpResponse response = BaSyxHttpTestUtils.executePostRequest(HttpClients.createDefault(), createPostRequestWithFile(JSON_OPERATIONALDATA_ENV_PATH, JSON_MIMETYPE));
assertEquals(HttpStatus.OK.value(), response.getCode());

response = executeGetOnURL(getOperationalDataValueOnlyURL(), JSON_MIMETYPE);
assertEquals(HttpStatus.OK.value(), response.getCode());

BaSyxHttpTestUtils.assertSameJSONContent(BaSyxHttpTestUtils.readJSONStringFromClasspath(BASIC_EVENT_VALUE_ONLY), BaSyxHttpTestUtils.getResponseAsString(response));
}

@Test
public void testAASEnvironmentSertializationWithXML() throws IOException, ParseException, DeserializationException {
boolean includeConceptDescription = true;
Expand Down Expand Up @@ -265,6 +278,10 @@ private static String getAASXUploadURL() {
return getURL() + "/upload";
}

public static String getOperationalDataValueOnlyURL() {
return getURL() + "/submodels/d3d3LmV4YW1wbGUuY29tL2lkcy9zbS8yMjIyXzgwNDFfMTA0Ml84MDU3/$value";
}

public static String getSerializationURL(Collection<String> aasIds, Collection<String> submodelIds, boolean includeConceptDescription) {
String aasIdsArrayString = createIdsArrayString(aasIds);
String submodelIdsArrayString = createIdsArrayString(submodelIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"AnnotationBottom": "",
"ObserveValueUpdates": {
"observed": {
"keys": [{
"type": "Submodel",
"value": "www.example.com/ids/sm/2222_8041_1042_8057"
}
],
"type": "ExternalReference"
}
},
"ViaHTTP": [{
"L1": "291.4213289500366"
}, {
"L2": "296.869547091201"
}, {
"L3": "299.65015492058336"
}, {
"Active_Power_All": "109572.7967447997"
}
],
"ViaModbus": [{
"L1": "160.26637"
}, {
"L2": "160.44434"
}, {
"L3": "163.39528"
}, {
"Active_Power_All": "59738.68"
}
],
"ViaMqtt": [{
"L1": "290.6217782649107"
}, {
"L2": "282.4599521889145"
}, {
"L3": "272.2067133660983"
}, {
"Active_Power_All": "104308.59396737856"
}
],
"ViaOpcUa": [{
"L1": "299.53634421194454"
}, {
"L2": "296.55226321871635"
}, {
"L3": "290.9149535087691"
}, {
"Active_Power_All": "109456.23941992565"
}
]
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import java.util.stream.Stream;

import org.eclipse.digitaltwin.basyx.submodelservice.value.AnnotatedRelationshipElementValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.BasicEventValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.EntityValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.FileBlobValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.RangeValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.ReferenceValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.RelationshipElementValue;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

Expand All @@ -55,6 +57,10 @@ public static boolean isTypeOfPropertyValue(JsonNode node) {
return node.isValueNode() && node.isTextual();
}

public static boolean isTypeOfBasicEventValue(JsonNode node) {
return isTypeOf(BasicEventValue.class, node);
}

public static boolean isTypeOfFileBlobValue(JsonNode node) {
return isTypeOf(FileBlobValue.class, node);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.submodelservice.value;

public class BasicEventValue implements SubmodelElementValue {

private ReferenceValue observed;

@SuppressWarnings("unused")
private BasicEventValue() {
super();
}

public BasicEventValue(ReferenceValue observed) {
super();
this.observed = observed;
}

public ReferenceValue getObserved() {
return observed;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.eclipse.digitaltwin.basyx.submodelservice.value.factory;

import org.eclipse.digitaltwin.aas4j.v3.model.AnnotatedRelationshipElement;
import org.eclipse.digitaltwin.aas4j.v3.model.BasicEventElement;
import org.eclipse.digitaltwin.aas4j.v3.model.Blob;
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
import org.eclipse.digitaltwin.aas4j.v3.model.File;
Expand All @@ -38,6 +39,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.basyx.submodelservice.value.exception.ValueMapperNotFoundException;
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.AnnotatedRelationshipElementValueMapper;
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.BasicEventValueMapper;
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.BlobValueMapper;
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.EntityValueMapper;
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.FileValueMapper;
Expand Down Expand Up @@ -68,6 +70,8 @@ public ValueMapper create(SubmodelElement submodelElement) {
return new MultiLanguagePropertyValueMapper((MultiLanguageProperty) submodelElement);
} else if (submodelElement instanceof File) {
return new FileValueMapper((File) submodelElement);
} else if (submodelElement instanceof BasicEventElement) {
return new BasicEventValueMapper((BasicEventElement) submodelElement);
} else if (submodelElement instanceof Blob) {
return new BlobValueMapper((Blob) submodelElement);
} else if (submodelElement instanceof Entity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.submodelservice.value.mapper;

import org.eclipse.digitaltwin.aas4j.v3.model.BasicEventElement;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.basyx.submodelservice.value.BasicEventValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.ReferenceValue;

public class BasicEventValueMapper implements ValueMapper<BasicEventValue> {
private BasicEventElement basicEvent;


public BasicEventValueMapper(BasicEventElement basicEvent) {
this.basicEvent = basicEvent;
}

@Override
public void setValue(BasicEventValue basicEventValue) {

}

@Override
public BasicEventValue getValue() {
Reference referenceElement = basicEvent.getObserved();
return new BasicEventValue(new ReferenceValue(referenceElement.getType(), referenceElement.getKeys()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.stream.Collectors;

import org.eclipse.digitaltwin.aas4j.v3.model.AnnotatedRelationshipElement;
import org.eclipse.digitaltwin.aas4j.v3.model.BasicEventElement;
import org.eclipse.digitaltwin.aas4j.v3.model.Blob;
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
import org.eclipse.digitaltwin.aas4j.v3.model.File;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.basyx.submodelservice.value.AnnotatedRelationshipElementValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.BasicEventValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.EntityValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.FileBlobValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.MultiLanguagePropertyValue;
Expand Down Expand Up @@ -72,6 +74,7 @@ public class ValueMapperUtil {
Map.entry(Range.class, RangeValue.class),
Map.entry(MultiLanguageProperty.class, MultiLanguagePropertyValue.class),
Map.entry(File.class, FileBlobValue.class),
Map.entry(BasicEventElement.class, BasicEventValue.class),
Map.entry(Blob.class, FileBlobValue.class),
Map.entry(Entity.class, EntityValue.class),
Map.entry(ReferenceElement.class, ReferenceElementValue.class),
Expand Down

0 comments on commit 241b96e

Please sign in to comment.