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

SQL-2271: Fix appending of clientInfo to appName if it exists #279

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 18 additions & 23 deletions src/main/java/com/mongodb/jdbc/MongoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class MongoConnection implements Connection {
private String logDirPath;
private boolean extJsonMode;
private UuidRepresentation uuidRepresentation;
private String appName;

public MongoConnection(
MongoClient mongoClient, MongoConnectionProperties connectionProperties) {
Expand All @@ -104,7 +105,13 @@ public MongoConnection(
this.mongoClientSettings = createMongoClientSettings(connectionProperties);

if (mongoClient == null) {
this.mongoClient = createMongoClient(connectionProperties);
this.mongoClient =
MongoClients.create(
this.mongoClientSettings,
MongoDriverInformation.builder()
.driverName(MongoDriver.NAME)
.driverVersion(MongoDriver.getVersion())
.build());
} else {
this.mongoClient = mongoClient;
}
Expand All @@ -121,44 +128,32 @@ private void initializeConnection(MongoConnectionProperties connectionProperties
this.extJsonMode = connectionProperties.getExtJsonMode();
this.uuidRepresentation =
connectionProperties.getConnectionString().getUuidRepresentation();
this.appName = buildAppName(connectionProperties);

this.isClosed = false;
}

private MongoClient createMongoClient(MongoConnectionProperties connectionProperties) {
StringBuilder appName =
private String buildAppName(MongoConnectionProperties connectionProperties) {
StringBuilder appNameBuilder =
new StringBuilder(MongoDriver.NAME).append("+").append(MongoDriver.getVersion());

MongoDriverInformation.Builder mdiBuilder;
String clientInfo = connectionProperties.getClientInfo();
String[] clientInfoSplit = (clientInfo == null) ? null : clientInfo.split("\\+");
if (clientInfoSplit != null && clientInfoSplit.length == 2) {
appName.append('|').append(clientInfo);
MongoDriverInformation driverInfoWithClientInfo =
MongoDriverInformation.builder()
.driverName(clientInfoSplit[0])
.driverVersion(clientInfoSplit[1])
.build();
mdiBuilder = MongoDriverInformation.builder(driverInfoWithClientInfo);
} else {
mdiBuilder = MongoDriverInformation.builder();
if (clientInfo != null) {
String[] clientInfoSplit = clientInfo.split("\\+");
if (clientInfoSplit.length == 2) {
appNameBuilder.append('|').append(clientInfo);
}
}
MongoDriverInformation mongoDriverInformation =
mdiBuilder
.driverName(MongoDriver.NAME)
.driverVersion(MongoDriver.getVersion())
.build();

return MongoClients.create(this.mongoClientSettings, mongoDriverInformation);
return appNameBuilder.toString();
}

private MongoClientSettings createMongoClientSettings(
MongoConnectionProperties connectionProperties) {
String appName = MongoDriver.NAME + "+" + MongoDriver.getVersion();

MongoClientSettings.Builder settingsBuilder =
MongoClientSettings.builder()
.applicationName(appName)
.applicationName(this.appName)
.applyConnectionString(connectionProperties.getConnectionString());

MongoCredential credential = connectionProperties.getConnectionString().getCredential();
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/mongodb/jdbc/MongoConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.internal.MongoClientImpl;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
Expand All @@ -28,6 +31,7 @@
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.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
Expand All @@ -37,6 +41,9 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@MockitoSettings(strictness = Strictness.WARN)
class MongoConnectionTest extends MongoMock {
static final String localhost = "mongodb://localhost";
@Mock private MongoConnectionProperties mockConnectionProperties;

@BeforeAll
protected void initMocks() {
MockitoAnnotations.initMocks(this);
Expand All @@ -49,6 +56,52 @@ void setupTest() throws NoSuchFieldException {
resetMockObjs();
}

@BeforeEach
void setUp() {
when(mockConnectionProperties.getConnectionString())
.thenReturn(new ConnectionString(localhost));
when(mockConnectionProperties.getDatabase()).thenReturn("test");
}

private String getApplicationName(MongoConnection connection) {
MongoClientImpl mongoClientImpl = (MongoClientImpl) connection.getMongoClient();
MongoClientSettings mcs = mongoClientImpl.getSettings();
return mcs.getApplicationName();
}

@Test
void testBuildAppNameWithoutClientInfo() {
when(mockConnectionProperties.getClientInfo()).thenReturn(null);

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName = MongoDriver.NAME + "+" + MongoDriver.getVersion();
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

@Test
void testAppNameWithValidClientInfo() {
String clientInfo = "test-client+1.0.0";
when(mockConnectionProperties.getClientInfo()).thenReturn(clientInfo);

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName =
MongoDriver.NAME + "+" + MongoDriver.getVersion() + "|" + clientInfo;
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

@Test
void testAppNameWithInvalidClientInfo() {
// Client information has to be in the format 'name+version'
when(mockConnectionProperties.getClientInfo()).thenReturn("invalid-client-info");

mongoConnection = new MongoConnection(null, mockConnectionProperties);

String expectedAppName = MongoDriver.NAME + "+" + MongoDriver.getVersion();
assertEquals(expectedAppName, getApplicationName(mongoConnection));
}

// to replace lambda as input in the testExceptionAfterConnectionClosed
interface TestInterface {
void test() throws SQLException;
Expand Down