Skip to content

Commit

Permalink
Fixes test (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedJannik authored Sep 11, 2024
1 parent 3a4b9cc commit a289a2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

0 comments on commit a289a2c

Please sign in to comment.