Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DAT-17886] fix incorrect result for 'connect' command #543

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(String.format("User doesn't have access to database '%s'", urlDatabaseName));
} 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,19 @@ 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())
.withMessageContaining("User doesn't have access to database 'lbcat'");
}
}
Loading