|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.testcontainers.service.connection.mongo; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import com.mongodb.ConnectionString; |
| 22 | +import org.testcontainers.containers.GenericContainer; |
| 23 | + |
| 24 | +import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails; |
| 25 | +import org.springframework.boot.ssl.SslBundle; |
| 26 | +import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory; |
| 27 | +import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource; |
| 28 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 29 | + |
| 30 | +/** |
| 31 | + * Abstract {@link ContainerConnectionDetailsFactory} to create {@link MongoConnectionDetails} from |
| 32 | + * a {@link ServiceConnection @ServiceConnection}-annotated {@link T}. |
| 33 | + * |
| 34 | + * @author Moritz Halbritter |
| 35 | + * @author Andy Wilkinson |
| 36 | + * @author Phillip Webb |
| 37 | + * @author Wouter Blancquaert |
| 38 | + */ |
| 39 | +abstract class AbstractMongoContainerConnectionDetailsFactory<T extends GenericContainer<T>> |
| 40 | + extends ContainerConnectionDetailsFactory<T, MongoConnectionDetails> { |
| 41 | + |
| 42 | + private final Function<T, String> connectionStringFunction; |
| 43 | + |
| 44 | + AbstractMongoContainerConnectionDetailsFactory(Function<T, String> connectionStringFunction) { |
| 45 | + super(ANY_CONNECTION_NAME, "com.mongodb.ConnectionString"); |
| 46 | + this.connectionStringFunction = connectionStringFunction; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected MongoConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<T> source) { |
| 51 | + return new MongoContainerConnectionDetails<>(source, this.connectionStringFunction); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@link MongoConnectionDetails} backed by a {@link ContainerConnectionSource}. |
| 56 | + */ |
| 57 | + private static final class MongoContainerConnectionDetails<T extends GenericContainer<T>> extends ContainerConnectionDetails<T> |
| 58 | + implements MongoConnectionDetails { |
| 59 | + |
| 60 | + private final Function<T, String> connectionStringFunction; |
| 61 | + |
| 62 | + private MongoContainerConnectionDetails(ContainerConnectionSource<T> source, Function<T, String> connectionStringFunction) { |
| 63 | + super(source); |
| 64 | + this.connectionStringFunction = connectionStringFunction; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ConnectionString getConnectionString() { |
| 69 | + return new ConnectionString(this.connectionStringFunction.apply(getContainer())); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public SslBundle getSslBundle() { |
| 74 | + return super.getSslBundle(); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments