From 239592e5f525ea9a15d6e66467306fb3850f481a Mon Sep 17 00:00:00 2001 From: Anthony Bosch <55814237+boscha1@users.noreply.github.com> Date: Sun, 2 Jul 2023 04:50:21 -0400 Subject: [PATCH] fix: Issue 2368/fix broken tests (#2511) * fixing typo * added embedded mongo db to fix MongoBankTest * cleaning up tests * added embedded mongo db to fix MongoBankTest * cleaning up tests --- hexagonal/pom.xml | 6 ++ .../hexagonal/banking/MongoBankTest.java | 90 ++++++++++++------- strategy/README.md | 2 +- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/hexagonal/pom.xml b/hexagonal/pom.xml index 0b570558e..a3d94abc2 100644 --- a/hexagonal/pom.xml +++ b/hexagonal/pom.xml @@ -43,6 +43,12 @@ com.google.inject guice + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + 3.0.0 + test + org.mongodb bson diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/banking/MongoBankTest.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/banking/MongoBankTest.java index 9ed9c2e02..30b10ed1d 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/banking/MongoBankTest.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/banking/MongoBankTest.java @@ -1,56 +1,65 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * 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. - */ package com.iluwatar.hexagonal.banking; import static org.junit.jupiter.api.Assertions.assertEquals; -import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader; -import com.mongodb.MongoClient; +import com.mongodb.MongoClientSettings; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoDatabase; +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodProcess; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.MongodConfig; +import de.flapdoodle.embed.mongo.distribution.Version; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import java.util.List; + /** * Tests for Mongo banking adapter */ -@Disabled class MongoBankTest { private static final String TEST_DB = "lotteryDBTest"; private static final String TEST_ACCOUNTS_COLLECTION = "testAccounts"; + private static final String TEST_HOST = "localhost"; + private static final int TEST_PORT = 27017; + + private static MongodExecutable mongodExe; + private static MongodProcess mongodProcess; + private static MongoClient mongoClient; + private static MongoDatabase mongoDatabase; private MongoBank mongoBank; + @BeforeAll + static void setUp() throws Exception { + MongodStarter starter = MongodStarter.getDefaultInstance(); + MongodConfig mongodConfig = buildMongoConfig(); + + mongoClient = buildMongoClient(); + mongodExe = starter.prepare(mongodConfig); + mongodProcess = mongodExe.start(); + mongoDatabase = mongoClient.getDatabase(TEST_DB); + } + @BeforeEach void init() { - MongoConnectionPropertiesLoader.load(); - var mongoClient = new MongoClient(System.getProperty("mongo-host"), - Integer.parseInt(System.getProperty("mongo-port"))); - mongoClient.dropDatabase(TEST_DB); + System.setProperty("mongo-host", TEST_HOST); + System.setProperty("mongo-port", String.valueOf(TEST_PORT)); + mongoDatabase.drop(); + mongoBank = new MongoBank(mongoDatabase.getName(), TEST_ACCOUNTS_COLLECTION); + } + + @AfterAll + static void tearDown() { mongoClient.close(); - mongoBank = new MongoBank(TEST_DB, TEST_ACCOUNTS_COLLECTION); + mongodProcess.stop(); + mongodExe.stop(); } @Test @@ -68,4 +77,19 @@ class MongoBankTest { assertEquals(1, mongoBank.getFunds("000-000")); assertEquals(9, mongoBank.getFunds("111-111")); } + + private static MongodConfig buildMongoConfig() { + return MongodConfig.builder() + .version(Version.Main.PRODUCTION) + .net(new de.flapdoodle.embed.mongo.config.Net(TEST_HOST, TEST_PORT, true)) + .build(); + } + + private static MongoClient buildMongoClient() { + return MongoClients.create( + MongoClientSettings.builder() + .applyToClusterSettings(builder -> builder.hosts(List.of(new ServerAddress(TEST_HOST, TEST_PORT)))) + .build() + ); + } } diff --git a/strategy/README.md b/strategy/README.md index a163b6622..b70ed94f2 100644 --- a/strategy/README.md +++ b/strategy/README.md @@ -13,7 +13,7 @@ Policy ## Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets -the algorithm vary independently from the clients that use it. +the algorithm vary independently of the clients that use it. ## Explanation