Add sample for reusable repository extensions.

Closes #690
This commit is contained in:
Christoph Strobl
2024-08-06 15:36:27 +02:00
committed by Mark Paluch
parent a4c5214b39
commit 8d0bde037b
18 changed files with 630380 additions and 8 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.util;
import java.util.List;
import org.springframework.util.StringUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
/**
* @author Christoph Strobl
*/
public class AtlasContainer extends GenericContainer<AtlasContainer> {
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mongodb/mongodb-atlas-local");
private static final String DEFAULT_TAG = "latest";
private static final String MONGODB_DATABASE_NAME_DEFAULT = "test";
public AtlasContainer() {
this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));
}
public AtlasContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}
public AtlasContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
setExposedPorts(List.of(27017));
}
public String getConnectionString() {
return getConnectionString(MONGODB_DATABASE_NAME_DEFAULT);
}
/**
* Gets a connection string url.
*
* @return a connection url pointing to a mongodb instance
*/
public String getConnectionString(String database) {
return String.format("mongodb://%s:%d/%s?directConnection=true", getHost(), getMappedPort(27017), StringUtils.hasText(database) ? database : MONGODB_DATABASE_NAME_DEFAULT);
}
}

View File

@@ -22,14 +22,22 @@ import org.testcontainers.utility.DockerImageName;
* Utility methods to create a {@link MongoDBContainer}.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
public class MongoContainers {
private static final String IMAGE_NAME = "mongo:8.0";
private static final String IMAGE_NAME_PROPERTY = "mongo.default.image.name";
private static final String IMAGE_NAME = "mongo:8.0";
private static final String IMAGE_NAME_PROPERTY = "mongo.default.image.name";
public static MongoDBContainer getDefaultContainer() {
return new MongoDBContainer(DockerImageName.parse(System.getProperty(IMAGE_NAME_PROPERTY, IMAGE_NAME)))
.withReuse(true);
}
private static final String ATLAS_IMAGE_NAME = "mongodb/mongodb-atlas-local:latest";
private static final String ATLAS_IMAGE_NAME_PROPERTY = "mongo.atlas.image.name";
public static MongoDBContainer getDefaultContainer() {
return new MongoDBContainer(DockerImageName.parse(System.getProperty(IMAGE_NAME_PROPERTY, IMAGE_NAME)))
.withReuse(true);
}
public static AtlasContainer getAtlasContainer() {
return new AtlasContainer(System.getProperty(ATLAS_IMAGE_NAME_PROPERTY, ATLAS_IMAGE_NAME)).withReuse(true);
}
}