Skip to content

Commit

Permalink
Merge pull request #595 from liquibase/DAT-18810
Browse files Browse the repository at this point in the history
DAT-18810: app naming updated, it contains now versions of core/pro liquibase and extension version also.
  • Loading branch information
SvampX authored Jan 31, 2025
2 parents b016cf9 + d76a535 commit 1f69bf0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package liquibase.ext.mongodb.database;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientSettings.Builder;
import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.util.StringUtil;
Expand All @@ -23,12 +22,13 @@ public Connection connect(final String url, final Properties info) {
throw new UnsupportedOperationException("Cannot initiate a SQL Connection for a NoSql DB");
}

public MongoClient connect(final ConnectionString connectionString) throws DatabaseException {
public MongoClient connect(final ConnectionString connectionString, String appName) throws DatabaseException {

final MongoClient client;

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.applicationName("Liquibase")
.applicationName(appName)
.build();

try {
Expand All @@ -40,6 +40,10 @@ public MongoClient connect(final ConnectionString connectionString) throws Datab
return client;
}

public MongoClient connect(final ConnectionString connectionString) throws DatabaseException {
return connect(connectionString, "Liquibase");
}

@Override
public boolean acceptsURL(final String url) {
final String trimmedUrl = StringUtil.trimToEmpty(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@
import liquibase.ext.mongodb.statement.BsonUtils;
import liquibase.logging.Logger;
import liquibase.nosql.database.AbstractNoSqlConnection;
import liquibase.util.LiquibaseUtil;
import liquibase.util.StringUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Driver;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
Expand Down Expand Up @@ -144,7 +150,7 @@ public void open(final String url, final Driver driverObject, final Properties d

this.connectionString = new ConnectionString(resolveRetryWrites(urlWithCredentials));

this.mongoClient = ((MongoClientDriver) driverObject).connect(connectionString);
this.mongoClient = ((MongoClientDriver) driverObject).connect(connectionString, getAppName(driverProperties, false));

final String database = this.connectionString.getDatabase();
if (database == null) {
Expand All @@ -158,6 +164,42 @@ public void open(final String url, final Driver driverObject, final Properties d
}
}

protected String getAppName(Properties driverProperties, boolean isProExt) {
if(driverProperties == null) {
return "Liquibase";
}
if(driverProperties.getProperty("appName") != null) {
return driverProperties.getProperty("appName");
}
String appType = isProExt ? "PRO_" : "OSS_";
String extType = isProExt ? "_ProExt_" : "_OssExt_";
String buildVersion = LiquibaseUtil.getBuildVersion();
return "Liquibase_" + appType + buildVersion + extType + getVersion();
}

//
protected String getVersion() {
String className = this.getClass().getSimpleName() + ".class";
URL url = this.getClass().getResource(className);
String classPath = url == null ? "" : url.toString();

if (!classPath.startsWith("jar")) {
// Class is not from a JAR, but from the file system.
return "LOCAL_BUILD";
}
try (InputStream is = new URL(classPath.substring(0, classPath.indexOf("!")) + "!/META-INF/MANIFEST.MF").openStream()) {
Attributes attributes = new Manifest(is).getMainAttributes();
String version = attributes.getValue("Implementation-Version");
if (version == null) {
version = attributes.getValue("Bundle-Version"); //For OSGi bundles
}
return version;
} catch (IOException e) {
Scope.getCurrentScope().getLog(this.getClass()).warning("Could not extract version within classPath = " + classPath);
return null;
}
}

private String resolveRetryWrites(String url) {
final Logger log = Scope.getCurrentScope().getLog(getClass());
if (MongoConfiguration.RETRY_WRITES.getCurrentConfiguredValue().wasDefaultValueUsed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -81,7 +82,7 @@ void getDatabaseMinorVersion() {
@Test
void getConnectionUserName() {

when(driverMock.connect(any(ConnectionString.class))).thenReturn(clientMock);
when(driverMock.connect(any(ConnectionString.class), anyString())).thenReturn(clientMock);
when(clientMock.getDatabase(any())).thenReturn(databaseMock);

assertThat(connection.getConnectionUserName()).isEmpty();
Expand All @@ -97,7 +98,7 @@ void getConnectionUserName() {
@Test
void isClosed() {

when(driverMock.connect(any(ConnectionString.class))).thenReturn(clientMock);
when(driverMock.connect(any(ConnectionString.class), anyString())).thenReturn(clientMock);
when(clientMock.getDatabase(any())).thenReturn(databaseMock);

assertThat(connection.isClosed()).isTrue();
Expand All @@ -116,7 +117,7 @@ void isClosed() {
@SneakyThrows
@Test
void getCatalog() {
when(driverMock.connect(any(ConnectionString.class))).thenReturn(clientMock);
when(driverMock.connect(any(ConnectionString.class), anyString())).thenReturn(clientMock);
when(clientMock.getDatabase(any())).thenReturn(databaseMock);
when(databaseMock.getName()).thenReturn("test_db");
when(databaseMock.withCodecRegistry(any())).thenReturn(databaseMock);
Expand All @@ -136,7 +137,7 @@ void getDatabaseProductName() {
@SneakyThrows
@Test
void open() {
when(driverMock.connect(any(ConnectionString.class))).thenReturn(clientMock);
when(driverMock.connect(any(ConnectionString.class), anyString())).thenReturn(clientMock);
when(clientMock.getDatabase(any())).thenReturn(databaseMock);
when(databaseMock.withCodecRegistry(any())).thenReturn(databaseMock);

Expand All @@ -147,7 +148,7 @@ void open() {
assertThat(connection.getConnectionUserName()).isEmpty();
assertThat(connection.getURL()).isEqualTo("localhost:27017");

verify(driverMock).connect(any(ConnectionString.class));
verify(driverMock).connect(any(ConnectionString.class), anyString());
verify(clientMock).getDatabase(any());
verify(databaseMock).withCodecRegistry(any());
verifyNoMoreInteractions(driverMock, clientMock, databaseMock);
Expand All @@ -161,7 +162,7 @@ void open() {
assertThat(connection.getConnectionUserName()).isEqualTo("user1");
assertThat(connection.getURL()).isEqualTo("localhost:27017");

verify(driverMock, times(2)).connect(any(ConnectionString.class));
verify(driverMock, times(2)).connect(any(ConnectionString.class), anyString());
verify(clientMock, times(2)).getDatabase(any());
verify(databaseMock, times(2)).withCodecRegistry(any());
verifyNoMoreInteractions(driverMock, clientMock, databaseMock);
Expand All @@ -178,7 +179,7 @@ void open() {
assertThat(connection.getConnectionUserName()).isEqualTo("user2");
assertThat(connection.getURL()).isEqualTo("mongodb1.example.com:27317,mongodb2.example.com:27017");

verify(driverMock, times(3)).connect(any(ConnectionString.class));
verify(driverMock, times(3)).connect(any(ConnectionString.class), anyString());
verify(clientMock, times(3)).getDatabase(any());
verify(databaseMock, times(3)).withCodecRegistry(any());
verifyNoMoreInteractions(driverMock, clientMock, databaseMock);
Expand All @@ -194,7 +195,7 @@ void open() {
assertThat(connection.getConnectionUserName()).isEqualTo("user3");
assertThat(connection.getURL()).isEqualTo("localhost:27017");

verify(driverMock, times(4)).connect(any(ConnectionString.class));
verify(driverMock, times(4)).connect(any(ConnectionString.class), anyString());
verify(clientMock, times(4)).getDatabase(any());
verify(databaseMock, times(4)).withCodecRegistry(any());
verifyNoMoreInteractions(driverMock, clientMock, databaseMock);
Expand Down

0 comments on commit 1f69bf0

Please sign in to comment.