Skip to content

Add compatibility for MongoConnectionDetails.getSslBundle method #2768

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

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,16 @@

package org.springframework.ai.testcontainers.service.connection.mongo;

import java.lang.reflect.Method;

import com.mongodb.ConnectionString;
import org.testcontainers.mongodb.MongoDBAtlasLocalContainer;

import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.util.ReflectionUtils;

/**
* A {@link ContainerConnectionDetailsFactory} implementation that provides
Expand All @@ -36,6 +40,7 @@
* testcontainers in Spring Boot applications.
*
* @author Eddú Meléndez
* @author Soby Chacko
* @since 1.0.0
* @see ContainerConnectionDetailsFactory
* @see MongoConnectionDetails
Expand All @@ -44,6 +49,12 @@
class MongoDbAtlasLocalContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<MongoDBAtlasLocalContainer, MongoConnectionDetails> {

private static final Method GET_SSL_BUNDLE_METHOD;

static {
GET_SSL_BUNDLE_METHOD = ReflectionUtils.findMethod(MongoConnectionDetails.class, "getSslBundle");
}

@Override
protected MongoConnectionDetails getContainerConnectionDetails(
ContainerConnectionSource<MongoDBAtlasLocalContainer> source) {
Expand All @@ -66,6 +77,14 @@ public ConnectionString getConnectionString() {
return new ConnectionString(getContainer().getConnectionString());
}

// Conditional implementation based on whether the method exists
public SslBundle getSslBundle() {
if (GET_SSL_BUNDLE_METHOD != null) { // Boot 3.5.x+
return (SslBundle) ReflectionUtils.invokeMethod(GET_SSL_BUNDLE_METHOD, this);
}
return null; // Boot 3.4.x (No-Op)
}

}

}