|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2024 the Eclipse BaSyx Authors |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + * SPDX-License-Identifier: MIT |
| 24 | + ******************************************************************************/ |
| 25 | + |
| 26 | +package org.eclipse.digitaltwin.basyx.aasenvironment.component; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +import org.eclipse.digitaltwin.basyx.common.mongocore.MongoDBUtilities; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 35 | +import org.springframework.context.ConfigurableApplicationContext; |
| 36 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 37 | + |
| 38 | +import com.mongodb.client.MongoClient; |
| 39 | +import com.mongodb.client.MongoClients; |
| 40 | + |
| 41 | +/** |
| 42 | + * TestMongoDbCollections |
| 43 | + * |
| 44 | + * @author mateusmolina |
| 45 | + * |
| 46 | + */ |
| 47 | +public class TestMongoDbCollections { |
| 48 | + private static ConfigurableApplicationContext appContext; |
| 49 | + |
| 50 | + // MongoDB configuration |
| 51 | + private static final String CONNECTION_URL = "mongodb://mongoAdmin:mongoPassword@localhost:27017/"; |
| 52 | + private static final String DB_NAME = "aas-env"; |
| 53 | + private static final String AAS_REPO_COLLECTION = "aas-repo"; |
| 54 | + private static final String SM_REPO_COLLECTION = "submodel-repo"; |
| 55 | + private static final String CD_REPO_COLLECTION = "cd-repo"; |
| 56 | + |
| 57 | + private static final MongoTemplate mongoTemplate = buildMongoTemplate(CONNECTION_URL, DB_NAME); |
| 58 | + |
| 59 | + @BeforeClass |
| 60 | + public static void startAASEnvironment() throws Exception { |
| 61 | + appContext = new SpringApplicationBuilder(AasEnvironmentComponent.class).profiles("mongodb").run(new String[] {}); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterClass |
| 65 | + public static void deleteDatabase() { |
| 66 | + appContext.close(); |
| 67 | + MongoDBUtilities.clearCollection(mongoTemplate, AAS_REPO_COLLECTION); |
| 68 | + MongoDBUtilities.clearCollection(mongoTemplate, SM_REPO_COLLECTION); |
| 69 | + MongoDBUtilities.clearCollection(mongoTemplate, CD_REPO_COLLECTION); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void aasRepoCollectionIsCorrectlyDefined() { |
| 74 | + assertMongoDBCollectionExists(AAS_REPO_COLLECTION); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void smRepoCollectionIsCorrectlyDefined() { |
| 79 | + assertMongoDBCollectionExists(SM_REPO_COLLECTION); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void cdRepoCollectionIsCorrectlyDefined() { |
| 84 | + assertMongoDBCollectionExists(CD_REPO_COLLECTION); |
| 85 | + } |
| 86 | + |
| 87 | + private void assertMongoDBCollectionExists(String collectionName) { |
| 88 | + assertTrue(mongoTemplate.collectionExists(collectionName)); |
| 89 | + } |
| 90 | + |
| 91 | + private static MongoTemplate buildMongoTemplate(String connectionUrl, String dbName) { |
| 92 | + MongoClient client = MongoClients.create(connectionUrl); |
| 93 | + return new MongoTemplate(client, dbName); |
| 94 | + } |
| 95 | +} |
0 commit comments