Skip to content

Commit

Permalink
Add test case for observable MongoDBAggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmolina-iese committed Dec 13, 2023
1 parent b1bd2b6 commit 686f0ad
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}

}

}
8 changes: 7 additions & 1 deletion basyx.components/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,14 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Mockito for mocking in JUnit tests -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- Specifies dependency settings for all submodules using these dependencies -->
<dependencyManagement>
<dependencies>
Expand Down

0 comments on commit 686f0ad

Please sign in to comment.