Skip to content

Commit 5ac6a3d

Browse files
Add smoke test to verify MongoDB SSL connections
See gh-35042
1 parent 3bb271e commit 5ac6a3d

File tree

15 files changed

+539
-1
lines changed

15 files changed

+539
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/testcontainers/DockerImageNames.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class DockerImageNames {
3737

3838
private static final String KAFKA_VERSION = "5.4.3";
3939

40-
private static final String MONGO_VERSION = "4.0.23";
40+
private static final String MONGO_VERSION = "5.0.17";
4141

4242
private static final String NEO4J_VERSION = "4.4.11";
4343

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
}
5+
6+
description = "Spring Boot Data MongoDB smoke test"
7+
8+
dependencies {
9+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-mongodb"))
10+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-mongodb-reactive"))
11+
implementation("io.projectreactor:reactor-core")
12+
13+
testImplementation(project(":spring-boot-project:spring-boot-test"))
14+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
15+
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
16+
testImplementation(project(":spring-boot-project:spring-boot-testcontainers"))
17+
testImplementation("io.projectreactor:reactor-test")
18+
testImplementation("org.junit.jupiter:junit-jupiter")
19+
testImplementation("org.junit.platform:junit-platform-engine")
20+
testImplementation("org.junit.platform:junit-platform-launcher")
21+
testImplementation("org.testcontainers:junit-jupiter")
22+
testImplementation("org.testcontainers:testcontainers")
23+
testImplementation("org.testcontainers:mongodb")
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.springframework.data.mongodb.core.mapping.Document;
20+
21+
@Document(collection = "exampleDocuments")
22+
public class ExampleDocument {
23+
24+
private String id;
25+
26+
private String text;
27+
28+
public String getId() {
29+
return this.id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getText() {
37+
return this.text;
38+
}
39+
40+
public void setText(String text) {
41+
this.text = text;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
@SpringBootApplication
22+
public class ExampleMongoApplication {
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
20+
21+
interface ExampleReactiveRepository extends ReactiveMongoRepository<ExampleDocument, String> {
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.springframework.data.mongodb.repository.MongoRepository;
20+
21+
interface ExampleRepository extends MongoRepository<ExampleDocument, String> {
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.springframework.data.mongodb.core.MongoTemplate;
20+
import org.springframework.stereotype.Service;
21+
22+
@Service
23+
public class ExampleService {
24+
25+
private final MongoTemplate mongoTemplate;
26+
27+
public ExampleService(MongoTemplate mongoTemplate) {
28+
this.mongoTemplate = mongoTemplate;
29+
}
30+
31+
public boolean hasCollection(String collectionName) {
32+
return this.mongoTemplate.collectionExists(collectionName);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import java.time.Duration;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Smoke tests for MongoDB using reactive repositories with SSL.
35+
*
36+
* @author Scott Frederick
37+
*/
38+
@Testcontainers(disabledWithoutDocker = true)
39+
@SpringBootTest(properties = { "spring.data.mongodb.ssl.bundle=client",
40+
"spring.ssl.bundle.pem.client.keystore.certificate=classpath:ssl/test-client.crt",
41+
"spring.ssl.bundle.pem.client.keystore.private-key=classpath:ssl/test-client.key",
42+
"spring.ssl.bundle.pem.client.truststore.certificate=classpath:ssl/test-ca.crt" })
43+
class DataMongoTestReactiveSslIntegrationTests {
44+
45+
@Container
46+
@ServiceConnection
47+
static final MongoDBContainer mongoDB = new SecureMongoContainer();
48+
49+
@Autowired
50+
private ReactiveMongoTemplate mongoTemplate;
51+
52+
@Autowired
53+
private ExampleReactiveRepository exampleRepository;
54+
55+
@Test
56+
void testRepository() {
57+
ExampleDocument exampleDocument = new ExampleDocument();
58+
exampleDocument.setText("Look, new @DataMongoTest!");
59+
exampleDocument = this.exampleRepository.save(exampleDocument).block(Duration.ofSeconds(30));
60+
assertThat(exampleDocument.getId()).isNotNull();
61+
assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue();
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.MongoDBContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.data.mongodb.core.MongoTemplate;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Smoke tests for MongoDB with SSL.
35+
*
36+
* @author Scott Frederick
37+
*/
38+
@Testcontainers(disabledWithoutDocker = true)
39+
@SpringBootTest(properties = { "spring.data.mongodb.ssl.bundle=client",
40+
"spring.ssl.bundle.pem.client.keystore.certificate=classpath:ssl/test-client.crt",
41+
"spring.ssl.bundle.pem.client.keystore.private-key=classpath:ssl/test-client.key",
42+
"spring.ssl.bundle.pem.client.truststore.certificate=classpath:ssl/test-ca.crt" })
43+
@ImportAutoConfiguration(SslAutoConfiguration.class)
44+
class DataMongoTestSslIntegrationTests {
45+
46+
@Container
47+
@ServiceConnection
48+
static final MongoDBContainer mongoDB = new SecureMongoContainer();
49+
50+
@Autowired
51+
private MongoTemplate mongoTemplate;
52+
53+
@Autowired
54+
private ExampleRepository exampleRepository;
55+
56+
@Test
57+
void testRepository() {
58+
ExampleDocument exampleDocument = new ExampleDocument();
59+
exampleDocument.setText("Look, new @DataMongoTest!");
60+
exampleDocument = this.exampleRepository.save(exampleDocument);
61+
assertThat(exampleDocument.getId()).isNotNull();
62+
assertThat(this.mongoTemplate.collectionExists("exampleDocuments")).isTrue();
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2023 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 smoketest.data.mongo;
18+
19+
import java.time.Duration;
20+
21+
import com.github.dockerjava.api.command.InspectContainerResponse;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.utility.MountableFile;
24+
25+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
26+
27+
/**
28+
* A {@link MongoDBContainer} for MongoDB with SSL configuration.
29+
*
30+
* @author Scott Frederick
31+
*/
32+
class SecureMongoContainer extends MongoDBContainer {
33+
34+
SecureMongoContainer() {
35+
super(DockerImageNames.mongo());
36+
withStartupAttempts(5);
37+
withStartupTimeout(Duration.ofMinutes(5));
38+
}
39+
40+
@Override
41+
public void configure() {
42+
withCopyFileToContainer(MountableFile.forClasspathResource("/ssl/test-server.pem"), "/ssl/server.pem");
43+
withCopyFileToContainer(MountableFile.forClasspathResource("/ssl/test-ca.crt"), "/ssl/ca.crt");
44+
withCommand("mongod --tlsMode requireTLS --tlsCertificateKeyFile /ssl/server.pem --tlsCAFile /ssl/ca.crt");
45+
}
46+
47+
@Override
48+
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
49+
}
50+
51+
}

0 commit comments

Comments
 (0)