Skip to content

Commit

Permalink
[DAT-17886] fix incorrect result for 'connect' command (#543)
Browse files Browse the repository at this point in the history
* trying showCollections

* replaced listCollection by listDatabases

* reverted local changes, added test

* removed misleading error message

* updated test
  • Loading branch information
KushnirykOleh authored Jul 16, 2024
1 parent 04a4ebb commit 50d371d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ public Boolean getSupportsValidator() {
@Override
public void checkDatabaseConnection() throws DatabaseException {
MongoConnection.showErrorMessageIfSomeRequiredDependenciesAreNotPresent(true);
MongoLiquibaseDatabaseUtil.sendPingSignal(getConnection());
MongoLiquibaseDatabaseUtil.checkDatabaseAccessibility((MongoConnection) getConnection());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> iterableMock;

@Mock
protected MongoCursor<String> mongoCursor;

protected MongoLiquibaseDatabase database;

@SneakyThrows
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 50d371d

Please sign in to comment.