From 5ffa72d3e570b45385ccea2e7bcf1de6ce6359f8 Mon Sep 17 00:00:00 2001 From: Mateus Molina Date: Mon, 29 Jul 2024 13:02:59 +0200 Subject: [PATCH] Test if Environment Repositories are being saved in different MongoDB collections (#337) * Restrict http test configs to profile 'httptests' * Add 'httptests' profile to affected tests * Add 'authorization' profile for auth related tests not using 'httptests' config file * Add aasenv test for checking if collections are being correctly created * Add close statement to appContext * Manually triggering the CI * Add basyx.mongodbcore to test aasenv.component pom --- .../basyx.aasenvironment.component/pom.xml | 5 + .../component/TestMongoDbCollections.java | 95 +++++++++++++++++++ .../resources/application-mongodb.properties | 13 +++ 3 files changed, 113 insertions(+) create mode 100644 basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java create mode 100644 basyx.aasenvironment/basyx.aasenvironment.component/src/test/resources/application-mongodb.properties diff --git a/basyx.aasenvironment/basyx.aasenvironment.component/pom.xml b/basyx.aasenvironment/basyx.aasenvironment.component/pom.xml index 93c7663e5..2c8abcb64 100644 --- a/basyx.aasenvironment/basyx.aasenvironment.component/pom.xml +++ b/basyx.aasenvironment/basyx.aasenvironment.component/pom.xml @@ -49,6 +49,11 @@ tests test + + org.eclipse.digitaltwin.basyx + basyx.mongodbcore + test + org.eclipse.digitaltwin.basyx basyx.conceptdescriptionrepository.component 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 new file mode 100644 index 000000000..173f7fbd9 --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/component/TestMongoDbCollections.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (C) 2024 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.digitaltwin.basyx.aasenvironment.component; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.digitaltwin.basyx.common.mongocore.MongoDBUtilities; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +/** + * TestMongoDbCollections + * + * @author mateusmolina + * + */ +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); + + @BeforeClass + public static void startAASEnvironment() throws Exception { + appContext = new SpringApplicationBuilder(AasEnvironmentComponent.class).profiles("mongodb").run(new String[] {}); + } + + @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); + } + + @Test + public void aasRepoCollectionIsCorrectlyDefined() { + assertMongoDBCollectionExists(AAS_REPO_COLLECTION); + } + + @Test + public void smRepoCollectionIsCorrectlyDefined() { + assertMongoDBCollectionExists(SM_REPO_COLLECTION); + } + + @Test + public void cdRepoCollectionIsCorrectlyDefined() { + assertMongoDBCollectionExists(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); + } +} diff --git a/basyx.aasenvironment/basyx.aasenvironment.component/src/test/resources/application-mongodb.properties b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/resources/application-mongodb.properties new file mode 100644 index 000000000..18cb5c871 --- /dev/null +++ b/basyx.aasenvironment/basyx.aasenvironment.component/src/test/resources/application-mongodb.properties @@ -0,0 +1,13 @@ +basyx.backend = MongoDB + +spring.data.mongodb.host=127.0.0.1 +spring.data.mongodb.port=27017 +spring.data.mongodb.database=aas-env +spring.data.mongodb.authentication-database=admin +spring.data.mongodb.username=mongoAdmin +spring.data.mongodb.password=mongoPassword + +# default values +basyx.aasrepository.mongodb.collectionName=aas-repo +basyx.submodelrepository.mongodb.collectionName=submodel-repo +basyx.cdrepository.mongodb.collectionName=cd-repo \ No newline at end of file