From 50d371da71c652ff36d1699184150e3b3e474e32 Mon Sep 17 00:00:00 2001 From: KushnirykOleh Date: Tue, 16 Jul 2024 16:56:09 +0300 Subject: [PATCH] [DAT-17886] fix incorrect result for 'connect' command (#543) * trying showCollections * replaced listCollection by listDatabases * reverted local changes, added test * removed misleading error message * updated test --- .../database/MongoLiquibaseDatabase.java | 2 +- .../database/MongoLiquibaseDatabaseUtil.java | 21 ++++------ .../database/MongoLiquibaseDatabaseTest.java | 40 +++++++++++++++++-- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java index d1577670..495dcc10 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java @@ -122,6 +122,6 @@ public Boolean getSupportsValidator() { @Override public void checkDatabaseConnection() throws DatabaseException { MongoConnection.showErrorMessageIfSomeRequiredDependenciesAreNotPresent(true); - MongoLiquibaseDatabaseUtil.sendPingSignal(getConnection()); + MongoLiquibaseDatabaseUtil.checkDatabaseAccessibility((MongoConnection) getConnection()); } } diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java index 76784957..70bb22c6 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseUtil.java @@ -1,28 +1,21 @@ package liquibase.ext.mongodb.database; import com.mongodb.MongoException; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoDatabase; -import liquibase.database.DatabaseConnection; import liquibase.exception.DatabaseException; -import org.bson.BsonDocument; -import org.bson.BsonInt64; -import org.bson.conversions.Bson; - -import static liquibase.ext.mongodb.database.MongoLiquibaseDatabase.ADMIN_DATABASE_NAME; public class MongoLiquibaseDatabaseUtil { private MongoLiquibaseDatabaseUtil() { } - public static void sendPingSignal(DatabaseConnection connection) throws DatabaseException { + public static void checkDatabaseAccessibility(MongoConnection connection) throws DatabaseException { try { - if (connection instanceof MongoConnection) { - MongoClient mongoClient = ((MongoConnection) connection).getMongoClient(); - Bson command = new BsonDocument("ping", new BsonInt64(1)); - MongoDatabase adminDb = mongoClient.getDatabase(ADMIN_DATABASE_NAME); - adminDb.runCommand(command); + String urlDatabaseName = connection.getConnectionString().getDatabase(); + for (String dbName : connection.getMongoClient().listDatabaseNames()) { + if (dbName.equals(urlDatabaseName)) { + return; + } } + throw new DatabaseException(); } catch (MongoException e) { throw new DatabaseException(e); } diff --git a/src/test/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseTest.java b/src/test/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseTest.java index 9f88e91b..5079db89 100644 --- a/src/test/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseTest.java +++ b/src/test/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabaseTest.java @@ -1,23 +1,43 @@ package liquibase.ext.mongodb.database; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoIterable; import liquibase.CatalogAndSchema; -import liquibase.configuration.LiquibaseConfiguration; import liquibase.database.DatabaseFactory; import liquibase.database.ObjectQuotingStrategy; -import liquibase.ext.mongodb.configuration.MongoConfiguration; +import liquibase.exception.DatabaseException; import lombok.SneakyThrows; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; -import static java.lang.Boolean.FALSE; import static liquibase.servicelocator.PrioritizedService.PRIORITY_DATABASE; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) @TestInstance(TestInstance.Lifecycle.PER_METHOD) class MongoLiquibaseDatabaseTest { + @Mock + protected MongoConnection connectionMock; + + @Mock + protected MongoClient clientMock; + + @Mock + protected MongoIterable iterableMock; + + @Mock + protected MongoCursor mongoCursor; + protected MongoLiquibaseDatabase database; @SneakyThrows @@ -121,4 +141,18 @@ void getDefaultSchema() { .containsExactly("catalog1", "catalog1"); } + @SneakyThrows + @Test + void checkDatabaseConnection() { + database.setConnection(connectionMock); + ConnectionString connectionString = new ConnectionString("mongodb://lbuser:LiquibasePass1@localhost:27017/lbcat"); + when(connectionMock.getConnectionString()).thenReturn(connectionString); + when(connectionMock.getMongoClient()).thenReturn(clientMock); + when(clientMock.listDatabaseNames()).thenReturn(iterableMock); + when(iterableMock.iterator()).thenReturn(mongoCursor); + when(mongoCursor.hasNext()).thenReturn(false); + + assertThatExceptionOfType(DatabaseException.class) + .isThrownBy(() -> database.checkDatabaseConnection()); + } }