Skip to content

Commit 718e727

Browse files
committed
Merge pull request #28362 from avillalain
* pr/28362: Polish "Add smoke tests for Spring Session Redis/Mongo" Add smoke tests for Spring Session Redis/Mongo Closes gh-28362
2 parents 384a07c + 2caa6cb commit 718e727

File tree

8 files changed

+289
-0
lines changed

8 files changed

+289
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
}
5+
6+
description = "Spring Boot Session Mongodb smoke test"
7+
8+
dependencies {
9+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
10+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security"))
11+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-mongodb"))
12+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
13+
implementation("org.springframework.session:spring-session-data-mongodb")
14+
testImplementation("org.testcontainers:mongodb")
15+
testImplementation("org.testcontainers:testcontainers")
16+
testImplementation("org.testcontainers:junit-jupiter")
17+
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
18+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2021 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.session.mongodb;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
22+
23+
@SpringBootApplication
24+
@EnableMongoHttpSession
25+
public class SampleSessionMongoApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SampleSessionMongoApplication.class);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
management.endpoints.web.exposure.include=*
2+
spring.security.user.name=user
3+
spring.security.user.password=password
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2012-2021 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.session.mongodb;
18+
19+
import java.net.URI;
20+
import java.time.Duration;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.testcontainers.containers.MongoDBContainer;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.boot.test.web.client.TestRestTemplate;
32+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
33+
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpMethod;
35+
import org.springframework.http.HttpStatus;
36+
import org.springframework.http.RequestEntity;
37+
import org.springframework.http.ResponseEntity;
38+
import org.springframework.test.context.DynamicPropertyRegistry;
39+
import org.springframework.test.context.DynamicPropertySource;
40+
41+
import static org.assertj.core.api.Assertions.assertThat;
42+
43+
/**
44+
* Tests for {@link SampleSessionMongoApplication}.
45+
*
46+
* @author Angel L. Villalain
47+
*/
48+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
49+
@Testcontainers(disabledWithoutDocker = true)
50+
public class SampleSessionMongoApplicationTests {
51+
52+
private static final String USERNAME = "user";
53+
54+
private static final String PASSWORD = "password";
55+
56+
@Autowired
57+
private TestRestTemplate restTemplate;
58+
59+
@Container
60+
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
61+
.withStartupTimeout(Duration.ofMinutes(2));
62+
63+
@DynamicPropertySource
64+
static void applicationProperties(DynamicPropertyRegistry registry) {
65+
registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl);
66+
}
67+
68+
@Test
69+
@SuppressWarnings("unchecked")
70+
void sessionsEndpointShouldReturnUserSessions() {
71+
createSession();
72+
ResponseEntity<Map<String, Object>> response = getSessions();
73+
assertThat(response).isNotNull();
74+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
75+
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
76+
assertThat(sessions.size()).isEqualTo(1);
77+
}
78+
79+
private void createSession() {
80+
URI uri = URI.create("/");
81+
HttpHeaders headers = new HttpHeaders();
82+
headers.setBasicAuth(USERNAME, PASSWORD);
83+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
84+
this.restTemplate.exchange(request, String.class);
85+
}
86+
87+
@SuppressWarnings("unchecked")
88+
private ResponseEntity<Map<String, Object>> getSessions() {
89+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
90+
.withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
91+
}
92+
93+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
}
5+
6+
description = "Spring Boot Session Mongodb smoke test"
7+
8+
dependencies {
9+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
10+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security"))
11+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis"))
12+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
13+
implementation("org.springframework.session:spring-session-data-redis")
14+
testImplementation("org.testcontainers:testcontainers")
15+
testImplementation("org.testcontainers:junit-jupiter")
16+
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
17+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2021 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.session.redis;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
22+
23+
@SpringBootApplication
24+
@EnableRedisHttpSession
25+
public class SampleSessionRedisApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SampleSessionRedisApplication.class);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
management.endpoints.web.exposure.include=*
2+
spring.security.user.name=user
3+
spring.security.user.password=password
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012-2021 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.session.redis;
18+
19+
import java.net.URI;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.testcontainers.junit.jupiter.Container;
25+
import org.testcontainers.junit.jupiter.Testcontainers;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.web.client.TestRestTemplate;
30+
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
31+
import org.springframework.http.HttpHeaders;
32+
import org.springframework.http.HttpMethod;
33+
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.RequestEntity;
35+
import org.springframework.http.ResponseEntity;
36+
import org.springframework.test.context.DynamicPropertyRegistry;
37+
import org.springframework.test.context.DynamicPropertySource;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Tests for {@link SampleSessionRedisApplication}.
43+
*
44+
* @author Angel L. Villalain
45+
*/
46+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
47+
@Testcontainers(disabledWithoutDocker = true)
48+
public class SampleSessionRedisApplicationTests {
49+
50+
private static final String USERNAME = "user";
51+
52+
private static final String PASSWORD = "password";
53+
54+
@Container
55+
static RedisContainer redis = new RedisContainer();
56+
57+
@Autowired
58+
private TestRestTemplate restTemplate;
59+
60+
@DynamicPropertySource
61+
static void applicationProperties(DynamicPropertyRegistry registry) {
62+
registry.add("spring.redis.host", redis::getHost);
63+
registry.add("spring.redis.port", redis::getFirstMappedPort);
64+
}
65+
66+
@Test
67+
@SuppressWarnings("unchecked")
68+
void sessionsEndpointShouldReturnUserSessions() {
69+
createSession();
70+
ResponseEntity<Map<String, Object>> response = this.getSessions();
71+
assertThat(response).isNotNull();
72+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
73+
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
74+
assertThat(sessions.size()).isEqualTo(1);
75+
}
76+
77+
private void createSession() {
78+
URI uri = URI.create("/");
79+
HttpHeaders headers = new HttpHeaders();
80+
headers.setBasicAuth(USERNAME, PASSWORD);
81+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
82+
this.restTemplate.exchange(request, String.class);
83+
}
84+
85+
@SuppressWarnings("unchecked")
86+
private ResponseEntity<Map<String, Object>> getSessions() {
87+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
88+
.withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)