From 28353bb1c731bb99cd6a6e43cc5e76130233d14e Mon Sep 17 00:00:00 2001 From: Mateus Molina <113896966+mateusmolina-iese@users.noreply.github.com> Date: Thu, 14 Dec 2023 09:15:41 +0100 Subject: [PATCH] Remove observer notifying when creating submodel with the API (#378) * Remove notifying when createSubmodel with the submodelAPI * Address review remarks * Add msgCounter to MqttTestListener --- .../ObservableSubmodelAggregatorV2.java | 1 - .../shared/mqtt/MqttTestListener.java | 2 + .../TestMqttV2SubmodelAggregatorObserver.java | 38 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/eclipse/basyx/submodel/aggregator/observing/ObservableSubmodelAggregatorV2.java b/src/main/java/org/eclipse/basyx/submodel/aggregator/observing/ObservableSubmodelAggregatorV2.java index da6183c8..fae31cf5 100644 --- a/src/main/java/org/eclipse/basyx/submodel/aggregator/observing/ObservableSubmodelAggregatorV2.java +++ b/src/main/java/org/eclipse/basyx/submodel/aggregator/observing/ObservableSubmodelAggregatorV2.java @@ -94,7 +94,6 @@ public void createSubmodel(Submodel submodel) { @Override public void createSubmodel(ISubmodelAPI submodelAPI) { submodelAggregator.createSubmodel(submodelAPI); - observers.stream().forEach(observer -> observer.submodelCreated(getParentAASId(submodelAPI.getSubmodel()), submodelAPI.getSubmodel(), this.aasServerId)); } @Override diff --git a/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/shared/mqtt/MqttTestListener.java b/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/shared/mqtt/MqttTestListener.java index c67cc108..b862116f 100644 --- a/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/shared/mqtt/MqttTestListener.java +++ b/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/shared/mqtt/MqttTestListener.java @@ -48,6 +48,7 @@ public class MqttTestListener implements InterceptHandler { public String lastTopic; public String lastPayload; private ArrayList topics = new ArrayList<>(); + public int msgCounter; @Override public String getID() { @@ -80,6 +81,7 @@ public synchronized void onPublish(InterceptPublishMessage msg) { topics.add(msg.getTopicName()); lastTopic = msg.getTopicName(); lastPayload = msg.getPayload().toString(StandardCharsets.UTF_8); + msgCounter++; } @Override diff --git a/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/submodel/aggregator/mqtt/TestMqttV2SubmodelAggregatorObserver.java b/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/submodel/aggregator/mqtt/TestMqttV2SubmodelAggregatorObserver.java index 92e35045..02ca675e 100644 --- a/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/submodel/aggregator/mqtt/TestMqttV2SubmodelAggregatorObserver.java +++ b/src/test/java/org/eclipse/basyx/testsuite/regression/extensions/submodel/aggregator/mqtt/TestMqttV2SubmodelAggregatorObserver.java @@ -42,9 +42,13 @@ import org.eclipse.basyx.submodel.metamodel.map.identifier.Identifier; import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElementCollection; import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property; +import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPI; +import org.eclipse.basyx.submodel.restapi.vab.VABSubmodelAPI; import org.eclipse.basyx.testsuite.regression.extensions.shared.mqtt.MqttTestListener; import org.eclipse.basyx.vab.coder.json.serialization.DefaultTypeFactory; import org.eclipse.basyx.vab.coder.json.serialization.GSONTools; +import org.eclipse.basyx.vab.modelprovider.generic.VABModelProvider; +import org.eclipse.basyx.vab.modelprovider.map.VABMapHandler; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; import org.junit.After; @@ -62,7 +66,7 @@ /** * Tests events emitting with the MqttSubmodelAggregatorObserver * - * @author fischer, jungjan, siebert + * @author fischer, jungjan, siebert, mateusmolina * */ public class TestMqttV2SubmodelAggregatorObserver { @@ -120,18 +124,24 @@ public void tearDown() { @Test public void testCreateSubmodel() { - String newSubmodelIdShort = "newSubmodelIdShort"; - String newSubmodelId = "newSubmodelId"; - Identifier newSubmodelIdentifier = new Identifier(IdentifierType.IRDI, newSubmodelId); - Submodel newSubmodel = new Submodel(newSubmodelIdShort, newSubmodelIdentifier); - newSubmodel.addSubmodelElement(new SubmodelElementCollection("smeColl")); - newSubmodel.addSubmodelElement(new Property("prop", false)); + Submodel newSubmodel = createTestSubmodel(); observedSubmodelAggregator.createSubmodel(newSubmodel); assertEquals(removeSubmodelElements(newSubmodel), deserializePayload(listener.lastPayload)); assertEquals(payloadFactory.createCreateSubmodelTopic(null, observedSubmodelAggregator.getAasServerId()), listener.lastTopic); } + @Test + public void noEventExpectedWhenSubmodelIsCreatedViaAPI() { + ISubmodelAPI smApi = new VABSubmodelAPI(new VABModelProvider(createTestSubmodel(), new VABMapHandler())); + + String lastPayload = listener.lastPayload; + + observedSubmodelAggregator.createSubmodel(smApi); + + assertEquals(lastPayload, listener.lastPayload); + } + @Test public void testUpdateSubmodel() { submodel.setCategory("newCategory"); @@ -174,4 +184,18 @@ private Submodel removeSubmodelElements(Submodel submodel) { return copy; } + + private static Submodel createTestSubmodel() { + String newSubmodelIdShort = "newSubmodelIdShort"; + String newSubmodelId = "newSubmodelId"; + Identifier newSubmodelIdentifier = new Identifier(IdentifierType.IRDI, newSubmodelId); + + Submodel newSubmodel = new Submodel(newSubmodelIdShort, newSubmodelIdentifier); + + newSubmodel.addSubmodelElement(new SubmodelElementCollection("smeColl")); + newSubmodel.addSubmodelElement(new Property("prop", false)); + + return newSubmodel; + } + }