From 686f0ad171ac17f3ed98adc92e04f5932936336f Mon Sep 17 00:00:00 2001 From: Mateus Molina Date: Wed, 13 Dec 2023 12:20:27 +0100 Subject: [PATCH 1/4] Add test case for observable MongoDBAggregator --- .../mongodb/TestMongoDBAggregator.java | 69 ++++++++++++++++++- basyx.components/pom.xml | 8 ++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java index 380a0db0..3f257af9 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java @@ -25,6 +25,10 @@ package org.eclipse.basyx.regression.AASServer.mongodb; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import java.io.IOException; import java.util.List; @@ -43,10 +47,18 @@ import org.eclipse.basyx.components.aas.AASServerComponent; import org.eclipse.basyx.components.aas.configuration.AASServerBackend; import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration; +import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAPIFactory; import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAggregator; +import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAggregatorFactory; +import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAPIFactory; +import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAggregatorFactory; import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration; import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration; import org.eclipse.basyx.components.registry.mongodb.MongoDBRegistryHandler; +import org.eclipse.basyx.submodel.aggregator.api.ISubmodelAggregator; +import org.eclipse.basyx.submodel.aggregator.api.ISubmodelAggregatorFactory; +import org.eclipse.basyx.submodel.aggregator.observing.ISubmodelAggregatorObserverV2; +import org.eclipse.basyx.submodel.aggregator.observing.ObservableSubmodelAggregatorV2; import org.eclipse.basyx.submodel.metamodel.api.ISubmodel; import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; import org.eclipse.basyx.submodel.metamodel.api.identifier.IdentifierType; @@ -66,7 +78,6 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; - /** * Testing various behaviors of MongoDBAASAggregator Using * MongoDBAASAggregator's constructors to set registry and adding AAS and @@ -279,6 +290,28 @@ public void checkNoExceptionIsObservedAfterPassingRegistry() { assertEquals(SM_IDSHORT, submodelObject.get(Referable.IDSHORT)); } + @Test + public void observerIsNotTriggered_WhenGettingAAS() { + MongoClient client = MongoClients.create(mongoDBConfig.getConnectionUrl()); + + MongoDBSubmodelAggregatorFactory aggregatorFactory = new MongoDBSubmodelAggregatorFactory(mongoDBConfig, new MongoDBSubmodelAPIFactory(mongoDBConfig, client), client); + + MockObservableSubmodelAggregatorV2Factory observableAggregatorFactory = new MockObservableSubmodelAggregatorV2Factory(aggregatorFactory, "aas-server"); + + MongoDBAASAggregatorFactory aasAggregatorFactory = new MongoDBAASAggregatorFactory(mongoDBConfig, registry, new MongoDBAASAPIFactory(mongoDBConfig, client), observableAggregatorFactory, client); + + IAASAggregator aggregator = aasAggregatorFactory.create(); + + // When + restartAasServer(); + aggregator.getAAS(registry.lookupAAS(new ModelUrn(AAS_ID)).getIdentifier()); + + // Expects + ISubmodelAggregatorObserverV2 observer = observableAggregatorFactory.getMockObserver(); + verify(observer, never()).submodelCreated(any(), any(), any()); + + } + private void restartAasServer() { component.stopComponent(); component.startComponent(); @@ -301,4 +334,38 @@ public static void tearDownClass() { component.stopComponent(); } + + + private class MockObservableSubmodelAggregatorV2Factory implements ISubmodelAggregatorFactory { + + private final ISubmodelAggregatorFactory smAggregatorFactory; + + private final String aasServerId; + + private ISubmodelAggregatorObserverV2 mockObserver; + + public MockObservableSubmodelAggregatorV2Factory(ISubmodelAggregatorFactory smAggregatorFactory, String aasServerId) { + this.smAggregatorFactory = smAggregatorFactory; + this.aasServerId = aasServerId; + mockObserver = mock(ISubmodelAggregatorObserverV2.class); + } + + @Override + public ISubmodelAggregator create() { + ObservableSubmodelAggregatorV2 smAggregator = new ObservableSubmodelAggregatorV2(smAggregatorFactory.create(), aasServerId); + smAggregator.addObserver(mockObserver); + return smAggregator; + } + + @Override + public ISubmodelAggregator create(IIdentifier aasIdentifier) { + return create(); + } + + public ISubmodelAggregatorObserverV2 getMockObserver() { + return mockObserver; + } + + } + } diff --git a/basyx.components/pom.xml b/basyx.components/pom.xml index 059ab2c7..95dd7036 100644 --- a/basyx.components/pom.xml +++ b/basyx.components/pom.xml @@ -262,8 +262,14 @@ 4.13.2 test + + + org.mockito + mockito-core + 5.5.0 + test + - From 5df89a2f5a0630b617cf8ac19a033f47941857a1 Mon Sep 17 00:00:00 2001 From: Mateus Molina Date: Thu, 14 Dec 2023 08:49:34 +0100 Subject: [PATCH 2/4] Implement TestAASServerWithMongoDBMqttV2 --- .../AASServer/MqttV2AASServerSuite.java | 2 +- .../TestAASServerWithMongoDBMqttV2.java | 115 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/MqttV2AASServerSuite.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/MqttV2AASServerSuite.java index 47dfe9b6..05a68394 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/MqttV2AASServerSuite.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/MqttV2AASServerSuite.java @@ -60,7 +60,7 @@ public abstract class MqttV2AASServerSuite extends AASServerSuite { protected static AASServerComponent component; protected static Server mqttBroker; protected MqttTestListener listener; - private static final String AAS_SERVER_ID = "aas-server"; + protected static final String AAS_SERVER_ID = "aas-server"; @Override protected String getURL() { diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java new file mode 100644 index 00000000..d77ae9ea --- /dev/null +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java @@ -0,0 +1,115 @@ +/******************************************************************************* + * 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.basyx.regression.AASServer; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; +import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId; +import org.eclipse.basyx.components.aas.AASServerComponent; +import org.eclipse.basyx.components.aas.configuration.AASServerBackend; +import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration; +import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAggregator; +import org.eclipse.basyx.components.aas.mqtt.MqttV2AASServerFeature; +import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration; +import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration; +import org.eclipse.basyx.components.configuration.BaSyxMqttConfiguration; +import org.eclipse.basyx.extensions.shared.encoding.Base64URLEncoder; +import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; +import org.eclipse.basyx.submodel.metamodel.map.Submodel; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test MQTT Observer behavior with MongoDB as backend + * + * @author mateusmolina + * + */ +public class TestAASServerWithMongoDBMqttV2 extends MqttV2AASServerSuite { + + private static BaSyxMongoDBConfiguration mongoDBConfig = buildBasyxMongoDBConfiguration("TestAASServerWithMongoDBMqttV2"); + private static IIdentifier mongoDBTestShell = new CustomId("mongoDBTestShellId"); + + @BeforeClass + public static void setUpClass() throws IOException { + BaSyxAASServerConfiguration serverConfig = new BaSyxAASServerConfiguration(); + serverConfig.setAASBackend(AASServerBackend.MONGODB); + + startMqttBroker(); + BaSyxContextConfiguration contextConfig = createBaSyxContextConfiguration(); + BaSyxMqttConfiguration mqttConfig = createMqttConfig(); + + resetMongoDBTestData(); + + component = new AASServerComponent(contextConfig, serverConfig, mongoDBConfig); + component.addAASServerFeature(new MqttV2AASServerFeature(mqttConfig, "MqttAASServerSuiteClientId", AAS_SERVER_ID, new Base64URLEncoder())); + component.startComponent(); + } + + @Test + public void observerIsNotTriggered_WhenGettingAAS() { + addMongoDBTestShellToServer(); + + int expectedMsgCounter = listener.msgCounter; + + restartAASServerComponent(); + manager.retrieveAAS(mongoDBTestShell); + + assertEquals(expectedMsgCounter, listener.msgCounter); + } + + private void restartAASServerComponent() { + component.stopComponent(); + component.startComponent(); + } + + private void addMongoDBTestShellToServer() { + AssetAdministrationShell shell = createShell(mongoDBTestShell.getId(), mongoDBTestShell); + manager.createAAS(shell, getURL()); + + Submodel submodel = createSubmodel(submodelIdentifier.getId(), submodelIdentifier); + manager.createSubmodel(mongoDBTestShell, submodel); + } + + @SuppressWarnings("deprecation") + private static void resetMongoDBTestData() { + new MongoDBAASAggregator(mongoDBConfig).reset(); + } + + private static BaSyxMongoDBConfiguration buildBasyxMongoDBConfiguration(String collectionPrefix) { + BaSyxMongoDBConfiguration mongoDBConfig; + mongoDBConfig = new BaSyxMongoDBConfiguration(); + mongoDBConfig.setAASCollection(collectionPrefix + "_AAS"); + mongoDBConfig.setSubmodelCollection(collectionPrefix + "_SM"); + return mongoDBConfig; + } + + + +} From 0c3645d64c6cd68acfdecc4e90b3cda25745a7b8 Mon Sep 17 00:00:00 2001 From: Mateus Molina Date: Thu, 14 Dec 2023 08:50:18 +0100 Subject: [PATCH 3/4] Remove observer tests from TestMongoDBAggregator --- .../mongodb/TestMongoDBAggregator.java | 67 ------------------- 1 file changed, 67 deletions(-) diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java index 3f257af9..4cef0664 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBAggregator.java @@ -25,10 +25,6 @@ package org.eclipse.basyx.regression.AASServer.mongodb; import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import java.io.IOException; import java.util.List; @@ -47,18 +43,10 @@ import org.eclipse.basyx.components.aas.AASServerComponent; import org.eclipse.basyx.components.aas.configuration.AASServerBackend; import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration; -import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAPIFactory; import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAggregator; -import org.eclipse.basyx.components.aas.mongodb.MongoDBAASAggregatorFactory; -import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAPIFactory; -import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAggregatorFactory; import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration; import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration; import org.eclipse.basyx.components.registry.mongodb.MongoDBRegistryHandler; -import org.eclipse.basyx.submodel.aggregator.api.ISubmodelAggregator; -import org.eclipse.basyx.submodel.aggregator.api.ISubmodelAggregatorFactory; -import org.eclipse.basyx.submodel.aggregator.observing.ISubmodelAggregatorObserverV2; -import org.eclipse.basyx.submodel.aggregator.observing.ObservableSubmodelAggregatorV2; import org.eclipse.basyx.submodel.metamodel.api.ISubmodel; import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; import org.eclipse.basyx.submodel.metamodel.api.identifier.IdentifierType; @@ -290,28 +278,6 @@ public void checkNoExceptionIsObservedAfterPassingRegistry() { assertEquals(SM_IDSHORT, submodelObject.get(Referable.IDSHORT)); } - @Test - public void observerIsNotTriggered_WhenGettingAAS() { - MongoClient client = MongoClients.create(mongoDBConfig.getConnectionUrl()); - - MongoDBSubmodelAggregatorFactory aggregatorFactory = new MongoDBSubmodelAggregatorFactory(mongoDBConfig, new MongoDBSubmodelAPIFactory(mongoDBConfig, client), client); - - MockObservableSubmodelAggregatorV2Factory observableAggregatorFactory = new MockObservableSubmodelAggregatorV2Factory(aggregatorFactory, "aas-server"); - - MongoDBAASAggregatorFactory aasAggregatorFactory = new MongoDBAASAggregatorFactory(mongoDBConfig, registry, new MongoDBAASAPIFactory(mongoDBConfig, client), observableAggregatorFactory, client); - - IAASAggregator aggregator = aasAggregatorFactory.create(); - - // When - restartAasServer(); - aggregator.getAAS(registry.lookupAAS(new ModelUrn(AAS_ID)).getIdentifier()); - - // Expects - ISubmodelAggregatorObserverV2 observer = observableAggregatorFactory.getMockObserver(); - verify(observer, never()).submodelCreated(any(), any(), any()); - - } - private void restartAasServer() { component.stopComponent(); component.startComponent(); @@ -335,37 +301,4 @@ public static void tearDownClass() { component.stopComponent(); } - - private class MockObservableSubmodelAggregatorV2Factory implements ISubmodelAggregatorFactory { - - private final ISubmodelAggregatorFactory smAggregatorFactory; - - private final String aasServerId; - - private ISubmodelAggregatorObserverV2 mockObserver; - - public MockObservableSubmodelAggregatorV2Factory(ISubmodelAggregatorFactory smAggregatorFactory, String aasServerId) { - this.smAggregatorFactory = smAggregatorFactory; - this.aasServerId = aasServerId; - mockObserver = mock(ISubmodelAggregatorObserverV2.class); - } - - @Override - public ISubmodelAggregator create() { - ObservableSubmodelAggregatorV2 smAggregator = new ObservableSubmodelAggregatorV2(smAggregatorFactory.create(), aasServerId); - smAggregator.addObserver(mockObserver); - return smAggregator; - } - - @Override - public ISubmodelAggregator create(IIdentifier aasIdentifier) { - return create(); - } - - public ISubmodelAggregatorObserverV2 getMockObserver() { - return mockObserver; - } - - } - } From ad750e826e38f626cfe038c0cc4548e35a884196 Mon Sep 17 00:00:00 2001 From: Mateus Molina Date: Thu, 14 Dec 2023 08:52:19 +0100 Subject: [PATCH 4/4] Fix spacing --- .../regression/AASServer/TestAASServerWithMongoDBMqttV2.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java index d77ae9ea..f1f5d6c1 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/TestAASServerWithMongoDBMqttV2.java @@ -110,6 +110,4 @@ private static BaSyxMongoDBConfiguration buildBasyxMongoDBConfiguration(String c return mongoDBConfig; } - - }