Skip to content

Commit 9a16c24

Browse files
Angel L. Villalain Garciambhave
Angel L. Villalain Garcia
authored andcommitted
Add smoke tests for Spring Session Redis/Mongo
Add smoke tests that verify the correct behavior of the sessions endpoint when using Spring Session with MongoDB and Redis. See gh-28362
1 parent 9ff17ed commit 9a16c24

File tree

8 files changed

+280
-0
lines changed

8 files changed

+280
-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 Http 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-2019 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 SampleHttpSessionMongoApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SampleHttpSessionMongoApplication.class);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
management.endpoints.web.exposure.include=*
2+
spring.mongodb.embedded.version=3.6.5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2019 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+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
44+
@Testcontainers(disabledWithoutDocker = true)
45+
public class SampleHttpSessionMongoApplicationTests {
46+
47+
static final String USERNAME = "user";
48+
static final String PASSWORD = "password";
49+
static final String ROOT = "/";
50+
51+
@Container
52+
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
53+
.withStartupTimeout(Duration.ofMinutes(2));
54+
55+
@Autowired
56+
private TestRestTemplate template;
57+
58+
@DynamicPropertySource
59+
static void applicationProperties(DynamicPropertyRegistry registry) {
60+
registry.add("spring.security.user.name", () -> USERNAME);
61+
registry.add("spring.security.user.password", () -> PASSWORD);
62+
registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl);
63+
}
64+
65+
@Test
66+
@SuppressWarnings("unchecked")
67+
void sessionsEndpointShouldReturnUserSessions() {
68+
createSession();
69+
ResponseEntity<Map<String, Object>> response = this.getSessions();
70+
assertThat(response).isNotNull();
71+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
72+
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
73+
assertThat(sessions.size()).isEqualTo(1);
74+
}
75+
76+
private void createSession() {
77+
URI uri = URI.create(ROOT);
78+
HttpHeaders headers = new HttpHeaders();
79+
headers.setBasicAuth(USERNAME, PASSWORD);
80+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
81+
this.template.exchange(request, String.class);
82+
}
83+
84+
@SuppressWarnings("unchecked")
85+
private ResponseEntity<Map<String, Object>> getSessions() {
86+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD)
87+
.getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
88+
}
89+
90+
}
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 Http 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-2019 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 SampleHttpSessionRedisApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SampleHttpSessionRedisApplication.class);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
management.endpoints.web.exposure.include=*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2012-2019 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+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
42+
@Testcontainers(disabledWithoutDocker = true)
43+
public class SampleHttpSessionRedisApplicationTests {
44+
45+
static final String USERNAME = "user";
46+
static final String PASSWORD = "password";
47+
static final String ROOT = "/";
48+
49+
@Container
50+
static RedisContainer redis = new RedisContainer();
51+
52+
@Autowired
53+
private TestRestTemplate template;
54+
55+
@DynamicPropertySource
56+
static void applicationProperties(DynamicPropertyRegistry registry) {
57+
registry.add("spring.security.user.name", () -> USERNAME);
58+
registry.add("spring.security.user.password", () -> PASSWORD);
59+
registry.add("spring.redis.host", redis::getHost);
60+
registry.add("spring.redis.port", redis::getFirstMappedPort);
61+
}
62+
63+
@Test
64+
@SuppressWarnings("unchecked")
65+
void sessionsEndpointShouldReturnUserSessions() {
66+
createSession();
67+
ResponseEntity<Map<String, Object>> response = this.getSessions();
68+
assertThat(response).isNotNull();
69+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
70+
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
71+
assertThat(sessions.size()).isEqualTo(1);
72+
}
73+
74+
private void createSession() {
75+
URI uri = URI.create(ROOT);
76+
HttpHeaders headers = new HttpHeaders();
77+
headers.setBasicAuth(USERNAME, PASSWORD);
78+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
79+
this.template.exchange(request, String.class);
80+
}
81+
82+
@SuppressWarnings("unchecked")
83+
private ResponseEntity<Map<String, Object>> getSessions() {
84+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD)
85+
.getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)