Skip to content

Commit 42d21a8

Browse files
committed
Remove parameterization of session smoke tests
There are dedicated smoke tests for Hazelcast, MongoDB and Redis that run on CI. This commit also polishes some of the other smoke tests related to Spring Session
1 parent 718e727 commit 42d21a8

File tree

11 files changed

+161
-178
lines changed

11 files changed

+161
-178
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-hazelcast/src/test/java/smoketest/session/hazelcast/SampleSessionHazelcastApplicationTests.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.boot.test.context.SpringBootTest;
2828
import org.springframework.boot.test.web.client.TestRestTemplate;
29-
import org.springframework.boot.web.server.LocalServerPort;
3029
import org.springframework.http.HttpHeaders;
3130
import org.springframework.http.HttpMethod;
3231
import org.springframework.http.HttpStatus;
@@ -36,7 +35,7 @@
3635
import static org.assertj.core.api.Assertions.assertThat;
3736

3837
/**
39-
* Tests for {@link SampleSessionHazelcastApplication},
38+
* Tests for {@link SampleSessionHazelcastApplication}.
4039
*
4140
* @author Susmitha Kandula
4241
* @author Madhura Bhave
@@ -47,28 +46,24 @@ class SampleSessionHazelcastApplicationTests {
4746
@Autowired
4847
private TestRestTemplate restTemplate;
4948

50-
@LocalServerPort
51-
private int port;
52-
5349
@Test
5450
@SuppressWarnings("unchecked")
5551
void sessionsEndpointShouldReturnUserSession() {
56-
URI uri = URI.create("http://localhost:" + this.port + "/");
57-
ResponseEntity<String> firstResponse = performRequest(this.restTemplate, uri, null);
52+
URI uri = URI.create("/");
53+
ResponseEntity<String> firstResponse = performRequest(uri, null);
5854
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
59-
performRequest(this.restTemplate, uri, cookie).getBody();
60-
ResponseEntity<Map<String, Object>> entity = (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
61-
.withBasicAuth("user", "password").getForEntity("/actuator/sessions?username=user", Map.class);
55+
performRequest(uri, cookie).getBody();
56+
ResponseEntity<Map<String, Object>> entity = getSessions();
6257
assertThat(entity).isNotNull();
6358
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
6459
List<Map<String, Object>> sessions = (List<Map<String, Object>>) entity.getBody().get("sessions");
6560
assertThat(sessions.size()).isEqualTo(1);
6661
}
6762

68-
private ResponseEntity<String> performRequest(TestRestTemplate restTemplate, URI uri, String cookie) {
63+
private ResponseEntity<String> performRequest(URI uri, String cookie) {
6964
HttpHeaders headers = getHeaders(cookie);
7065
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
71-
return restTemplate.exchange(request, String.class);
66+
return this.restTemplate.exchange(request, String.class);
7267
}
7368

7469
private HttpHeaders getHeaders(String cookie) {
@@ -86,4 +81,12 @@ private String getBasicAuth() {
8681
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
8782
}
8883

84+
@SuppressWarnings("unchecked")
85+
private ResponseEntity<Map<String, Object>> getSessions() {
86+
HttpHeaders headers = getHeaders(null);
87+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET,
88+
URI.create("/actuator/sessions?username=user"));
89+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate.exchange(request, Map.class);
90+
}
91+
8992
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
}
5+
6+
description = "Spring Boot Session JDBC 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-web"))
12+
runtimeOnly(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jdbc"))
13+
runtimeOnly("org.springframework.session:spring-session-jdbc")
14+
runtimeOnly("com.h2database:h2")
15+
16+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
17+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import org.springframework.boot.autoconfigure.SpringBootApplication;
2121

2222
@SpringBootApplication
23-
public class SampleSessionApplication {
23+
public class SampleSessionJdbcApplication {
2424

2525
public static void main(String[] args) {
26-
SpringApplication.run(SampleSessionApplication.class);
26+
SpringApplication.run(SampleSessionJdbcApplication.class);
2727
}
2828

2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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;
18+
19+
import java.net.URI;
20+
import java.util.Base64;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.boot.test.web.client.TestRestTemplate;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.http.HttpMethod;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.RequestEntity;
33+
import org.springframework.http.ResponseEntity;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link SampleSessionJdbcApplication}.
39+
*
40+
* @author Andy Wilkinson
41+
* @author Vedran Pavic
42+
* @author Madhura Bhave
43+
*/
44+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
45+
properties = "server.servlet.session.timeout:2")
46+
class SampleSessionJdbcApplicationTests {
47+
48+
@Autowired
49+
private TestRestTemplate restTemplate;
50+
51+
private static final URI ROOT_URI = URI.create("/");
52+
53+
@Test
54+
void sessionExpiry() throws Exception {
55+
ResponseEntity<String> firstResponse = performRequest(ROOT_URI, null);
56+
String sessionId1 = firstResponse.getBody();
57+
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
58+
String sessionId2 = performRequest(ROOT_URI, cookie).getBody();
59+
assertThat(sessionId1).isEqualTo(sessionId2);
60+
Thread.sleep(2100);
61+
String loginPage = performRequest(ROOT_URI, cookie).getBody();
62+
assertThat(loginPage).containsIgnoringCase("login");
63+
}
64+
65+
@Test
66+
@SuppressWarnings("unchecked")
67+
void sessionsEndpointShouldReturnUserSession() {
68+
performRequest(ROOT_URI, null);
69+
ResponseEntity<Map<String, Object>> response = 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 ResponseEntity<String> performRequest(URI uri, String cookie) {
77+
HttpHeaders headers = getHeaders(cookie);
78+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
79+
return this.restTemplate.exchange(request, String.class);
80+
}
81+
82+
private HttpHeaders getHeaders(String cookie) {
83+
HttpHeaders headers = new HttpHeaders();
84+
if (cookie != null) {
85+
headers.set("Cookie", cookie);
86+
}
87+
else {
88+
headers.set("Authorization", getBasicAuth());
89+
}
90+
return headers;
91+
}
92+
93+
private String getBasicAuth() {
94+
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
95+
}
96+
97+
@SuppressWarnings("unchecked")
98+
private ResponseEntity<Map<String, Object>> getSessions() {
99+
HttpHeaders headers = getHeaders(null);
100+
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET,
101+
URI.create("/actuator/sessions?username=user"));
102+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate.exchange(request, Map.class);
103+
}
104+
105+
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
@Testcontainers(disabledWithoutDocker = true)
5050
public class SampleSessionMongoApplicationTests {
5151

52-
private static final String USERNAME = "user";
53-
54-
private static final String PASSWORD = "password";
55-
5652
@Autowired
5753
private TestRestTemplate restTemplate;
5854

@@ -68,26 +64,29 @@ static void applicationProperties(DynamicPropertyRegistry registry) {
6864
@Test
6965
@SuppressWarnings("unchecked")
7066
void sessionsEndpointShouldReturnUserSessions() {
71-
createSession();
67+
createSession(URI.create("/"));
7268
ResponseEntity<Map<String, Object>> response = getSessions();
7369
assertThat(response).isNotNull();
7470
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
7571
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
7672
assertThat(sessions.size()).isEqualTo(1);
7773
}
7874

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);
75+
private void createSession(URI uri) {
76+
RequestEntity<Object> request = getRequestEntity(uri);
8477
this.restTemplate.exchange(request, String.class);
8578
}
8679

80+
private RequestEntity<Object> getRequestEntity(URI uri) {
81+
HttpHeaders headers = new HttpHeaders();
82+
headers.setBasicAuth("user", "password");
83+
return new RequestEntity<>(headers, HttpMethod.GET, uri);
84+
}
85+
8786
@SuppressWarnings("unchecked")
8887
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);
88+
RequestEntity<Object> request = getRequestEntity(URI.create("/actuator/sessions?username=user"));
89+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate.exchange(request, Map.class);
9190
}
9291

9392
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/test/java/smoketest/session/redis/SampleSessionRedisApplicationTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
@Testcontainers(disabledWithoutDocker = true)
4848
public class SampleSessionRedisApplicationTests {
4949

50-
private static final String USERNAME = "user";
51-
52-
private static final String PASSWORD = "password";
53-
5450
@Container
5551
static RedisContainer redis = new RedisContainer();
5652

@@ -66,26 +62,29 @@ static void applicationProperties(DynamicPropertyRegistry registry) {
6662
@Test
6763
@SuppressWarnings("unchecked")
6864
void sessionsEndpointShouldReturnUserSessions() {
69-
createSession();
65+
createSession(URI.create("/"));
7066
ResponseEntity<Map<String, Object>> response = this.getSessions();
7167
assertThat(response).isNotNull();
7268
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
7369
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
7470
assertThat(sessions.size()).isEqualTo(1);
7571
}
7672

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);
73+
private void createSession(URI uri) {
74+
RequestEntity<Object> request = getRequestEntity(uri);
8275
this.restTemplate.exchange(request, String.class);
8376
}
8477

78+
private RequestEntity<Object> getRequestEntity(URI uri) {
79+
HttpHeaders headers = new HttpHeaders();
80+
headers.setBasicAuth("user", "password");
81+
return new RequestEntity<>(headers, HttpMethod.GET, uri);
82+
}
83+
8584
@SuppressWarnings("unchecked")
8685
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);
86+
RequestEntity<Object> request = getRequestEntity(URI.create("/actuator/sessions?username=user"));
87+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate.exchange(request, Map.class);
8988
}
9089

9190
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/build.gradle

Lines changed: 0 additions & 36 deletions
This file was deleted.

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/src/main/resources/hazelcast.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)