From 8e0c4ac20d55ec3796020d24cb5410b9127140dc Mon Sep 17 00:00:00 2001 From: Mohammad Ghazanfar Ali Danish Date: Tue, 21 Nov 2023 14:03:07 +0100 Subject: [PATCH] Fixes wrong ObjectMapper usage in AAS Service eventing Signed-off-by: Mohammad Ghazanfar Ali Danish --- .../basyx.aasservice-feature-mqtt/pom.xml | 5 +++++ .../feature/mqtt/MqttAasService.java | 5 +++-- .../feature/mqtt/MqttAasServiceFactory.java | 8 ++++++-- .../feature/mqtt/MqttAasServiceFeature.java | 9 ++++++--- .../feature/mqtt/TestMqttAasService.java | 18 +++++++++++++++--- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/pom.xml b/basyx.aasservice/basyx.aasservice-feature-mqtt/pom.xml index 2897c1937..3af669970 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/pom.xml +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/pom.xml @@ -53,5 +53,10 @@ org.eclipse.digitaltwin.basyx basyx.mqttcore + + org.eclipse.digitaltwin.basyx + basyx.http + test + \ No newline at end of file diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasService.java b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasService.java index 2614e2100..448bb1c10 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasService.java +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasService.java @@ -60,16 +60,17 @@ public class MqttAasService implements AasService { private IMqttClient mqttClient; private String repoId; + private ObjectMapper objectMapper; - public MqttAasService(AasService decorated, IMqttClient mqttClient, MqttAasServiceTopicFactory topicFactory, String repoId) { + public MqttAasService(AasService decorated, IMqttClient mqttClient, MqttAasServiceTopicFactory topicFactory, String repoId, ObjectMapper objectMapper) { this.topicFactory = topicFactory; this.decorated = decorated; this.mqttClient = mqttClient; this.repoId = repoId; + this.objectMapper = objectMapper; } public String serialize(Object obj) { - ObjectMapper objectMapper = new ObjectMapper(); try { return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFactory.java b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFactory.java index 904fa58e2..95677a393 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFactory.java +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFactory.java @@ -29,23 +29,27 @@ import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory; import org.eclipse.paho.client.mqttv3.IMqttClient; +import com.fasterxml.jackson.databind.ObjectMapper; + public class MqttAasServiceFactory implements AasServiceFactory { private AasServiceFactory decorated; private IMqttClient client; private MqttAasServiceTopicFactory topicFactory; private String repoId; + private ObjectMapper objectMapper; - public MqttAasServiceFactory(AasServiceFactory decorated, IMqttClient client, MqttAasServiceTopicFactory topicFactory, String repoId) { + public MqttAasServiceFactory(AasServiceFactory decorated, IMqttClient client, MqttAasServiceTopicFactory topicFactory, String repoId, ObjectMapper objectMapper) { this.decorated = decorated; this.client = client; this.topicFactory = topicFactory; this.repoId = repoId; + this.objectMapper = objectMapper; } @Override public AasService create(AssetAdministrationShell aas) { - return new MqttAasService(decorated.create(aas), client, topicFactory, repoId); + return new MqttAasService(decorated.create(aas), client, topicFactory, repoId, objectMapper); } } diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFeature.java b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFeature.java index 24e35648e..244d2254c 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFeature.java +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/MqttAasServiceFeature.java @@ -34,6 +34,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; + @ConditionalOnExpression("#{${" + MqttAasServiceFeature.FEATURENAME + ".enabled:false} or ${basyx.feature.mqtt.enabled:false}}") @Component public class MqttAasServiceFeature implements AasServiceFeature { @@ -43,18 +45,19 @@ public class MqttAasServiceFeature implements AasServiceFeature { private boolean enabled; private IMqttClient mqttClient; - private String repoId; + private ObjectMapper objectMapper; @Autowired - public MqttAasServiceFeature(IMqttClient mqttClient, AasRepository repo) { + public MqttAasServiceFeature(IMqttClient mqttClient, AasRepository repo, ObjectMapper objectMapper) { this.mqttClient = mqttClient; this.repoId = repo.getName(); + this.objectMapper = objectMapper; } @Override public AasServiceFactory decorate(AasServiceFactory aasServiceFactory) { - return new MqttAasServiceFactory(aasServiceFactory, mqttClient, new MqttAasServiceTopicFactory(new URLEncoder()), repoId); + return new MqttAasServiceFactory(aasServiceFactory, mqttClient, new MqttAasServiceTopicFactory(new URLEncoder()), repoId, objectMapper); } @Override diff --git a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java index bdbe59fea..21a085424 100644 --- a/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java +++ b/basyx.aasservice/basyx.aasservice-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasservice/feature/mqtt/TestMqttAasService.java @@ -28,6 +28,8 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; +import java.util.Arrays; +import java.util.List; import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; @@ -45,6 +47,9 @@ import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory; import org.eclipse.digitaltwin.basyx.common.mqttcore.encoding.URLEncoder; import org.eclipse.digitaltwin.basyx.common.mqttcore.listener.MqttTestListener; +import org.eclipse.digitaltwin.basyx.http.Aas4JHTTPSerializationExtension; +import org.eclipse.digitaltwin.basyx.http.BaSyxHTTPConfiguration; +import org.eclipse.digitaltwin.basyx.http.SerializationExtension; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttSecurityException; @@ -74,16 +79,17 @@ public class TestMqttAasService extends AasServiceSuite { private MqttAasService mqttAasService; private AssetAdministrationShell shell; + private static ObjectMapper objectMapper; @BeforeClass public static void setUpClass() throws MqttException, IOException { + objectMapper = configureObjectMapper(); mqttBroker = startBroker(); listener = configureInterceptListener(mqttBroker); mqttClient = createAndConnectClient(); aasRepository = createMqttAasRepository(); mqttAasServiceFactory = createMqttAasServiceFactory(mqttClient); - } @Before @@ -105,7 +111,8 @@ protected AasServiceFactory getAASServiceFactory() { private static AasServiceFactory createMqttAasServiceFactory(MqttClient client) { AasServiceFactory serviceFactory = new InMemoryAasServiceFactory(); - MqttAasServiceFeature mqttFeature = new MqttAasServiceFeature(client, aasRepository); + MqttAasServiceFeature mqttFeature = new MqttAasServiceFeature(client, aasRepository, objectMapper); + return mqttFeature.decorate(serviceFactory); } @@ -137,7 +144,6 @@ public void addSubmodelReferenceEvent() throws DeserializationException, JsonPro } private String serialize(Object obj) { - ObjectMapper objectMapper = new ObjectMapper(); try { return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException ignore) { @@ -156,6 +162,12 @@ public void removeSubmodelReferenceEvent() throws DeserializationException, Json assertEquals(topicFactory.createRemoveSubmodelReferenceTopic(repoId, shell.getId()), listener.lastTopic); assertEquals(serialize(DummyAssetAdministrationShell.submodelReference), listener.lastPayload); } + + private static ObjectMapper configureObjectMapper() { + List extensions = Arrays.asList(new Aas4JHTTPSerializationExtension()); + + return new BaSyxHTTPConfiguration().jackson2ObjectMapperBuilder(extensions).build(); + } private static AasRepository createMqttAasRepository() { AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), mqttAasServiceFactory);