From a289a2cda2019a441d6d86f2da3203d2af957adc Mon Sep 17 00:00:00 2001 From: Jannik Fried Date: Wed, 11 Sep 2024 14:23:15 +0200 Subject: [PATCH] Fixes test (#441) --- .../MongoDbCollectionsTestConfig.java | 22 ++++++++++++++++++ .../component/TestMongoDbCollections.java | 23 ++++++------------- 2 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/MongoDbCollectionsTestConfig.java diff --git a/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/MongoDbCollectionsTestConfig.java b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/MongoDbCollectionsTestConfig.java new file mode 100644 index 000000000..41ef118df --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/MongoDbCollectionsTestConfig.java @@ -0,0 +1,22 @@ +package org.eclipse.digitaltwin.basyx.aasenvironment.component; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; + +@Configuration +public class MongoDbCollectionsTestConfig { + protected static final String CONNECTION_URL = "mongodb://mongoAdmin:mongoPassword@localhost:27017/"; + protected static final String DB_NAME = "aas-env"; + protected static final String AAS_REPO_COLLECTION = "aas-repo"; + protected static final String SM_REPO_COLLECTION = "submodel-repo"; + protected static final String CD_REPO_COLLECTION = "cd-repo"; + + @Bean + public static MongoTemplate buildMongoTemplate() { + MongoClient client = MongoClients.create(CONNECTION_URL); + return new MongoTemplate(client, DB_NAME); + } +} diff --git a/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java index 173f7fbd9..8bb416517 100644 --- a/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java +++ b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java @@ -48,13 +48,8 @@ public class TestMongoDbCollections { private static ConfigurableApplicationContext appContext; // MongoDB configuration - private static final String CONNECTION_URL = "mongodb://mongoAdmin:mongoPassword@localhost:27017/"; - private static final String DB_NAME = "aas-env"; - private static final String AAS_REPO_COLLECTION = "aas-repo"; - private static final String SM_REPO_COLLECTION = "submodel-repo"; - private static final String CD_REPO_COLLECTION = "cd-repo"; - private static final MongoTemplate mongoTemplate = buildMongoTemplate(CONNECTION_URL, DB_NAME); + private static final MongoTemplate mongoTemplate = MongoDbCollectionsTestConfig.buildMongoTemplate(); @BeforeClass public static void startAASEnvironment() throws Exception { @@ -64,32 +59,28 @@ public static void startAASEnvironment() throws Exception { @AfterClass public static void deleteDatabase() { appContext.close(); - MongoDBUtilities.clearCollection(mongoTemplate, AAS_REPO_COLLECTION); - MongoDBUtilities.clearCollection(mongoTemplate, SM_REPO_COLLECTION); - MongoDBUtilities.clearCollection(mongoTemplate, CD_REPO_COLLECTION); + MongoDBUtilities.clearCollection(mongoTemplate, MongoDbCollectionsTestConfig.AAS_REPO_COLLECTION); + MongoDBUtilities.clearCollection(mongoTemplate, MongoDbCollectionsTestConfig.SM_REPO_COLLECTION); + MongoDBUtilities.clearCollection(mongoTemplate, MongoDbCollectionsTestConfig.CD_REPO_COLLECTION); } @Test public void aasRepoCollectionIsCorrectlyDefined() { - assertMongoDBCollectionExists(AAS_REPO_COLLECTION); + assertMongoDBCollectionExists(MongoDbCollectionsTestConfig.AAS_REPO_COLLECTION); } @Test public void smRepoCollectionIsCorrectlyDefined() { - assertMongoDBCollectionExists(SM_REPO_COLLECTION); + assertMongoDBCollectionExists(MongoDbCollectionsTestConfig.SM_REPO_COLLECTION); } @Test public void cdRepoCollectionIsCorrectlyDefined() { - assertMongoDBCollectionExists(CD_REPO_COLLECTION); + assertMongoDBCollectionExists(MongoDbCollectionsTestConfig.CD_REPO_COLLECTION); } private void assertMongoDBCollectionExists(String collectionName) { assertTrue(mongoTemplate.collectionExists(collectionName)); } - private static MongoTemplate buildMongoTemplate(String connectionUrl, String dbName) { - MongoClient client = MongoClients.create(connectionUrl); - return new MongoTemplate(client, dbName); - } }