Skip to content

Commit 2caa6cb

Browse files
committed
Polish "Add smoke tests for Spring Session Redis/Mongo"
See gh-28362
1 parent 9a16c24 commit 2caa6cb

File tree

10 files changed

+47
-38
lines changed

10 files changed

+47
-38
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/resources/application.properties

-2
This file was deleted.

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/resources/application.properties

-1
This file was deleted.
+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id "org.springframework.boot.conventions"
44
}
55

6-
description = "Spring Boot Http Session Mongodb smoke test"
6+
description = "Spring Boot Session Mongodb smoke test"
77

88
dependencies {
99
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222

2323
@SpringBootApplication
2424
@EnableMongoHttpSession
25-
public class SampleHttpSessionMongoApplication {
25+
public class SampleSessionMongoApplication {
2626

2727
public static void main(String[] args) {
28-
SpringApplication.run(SampleHttpSessionMongoApplication.class);
28+
SpringApplication.run(SampleSessionMongoApplication.class);
2929
}
3030

3131
}
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
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,51 +40,54 @@
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
4242

43+
/**
44+
* Tests for {@link SampleSessionMongoApplication}.
45+
*
46+
* @author Angel L. Villalain
47+
*/
4348
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
4449
@Testcontainers(disabledWithoutDocker = true)
45-
public class SampleHttpSessionMongoApplicationTests {
50+
public class SampleSessionMongoApplicationTests {
4651

47-
static final String USERNAME = "user";
48-
static final String PASSWORD = "password";
49-
static final String ROOT = "/";
52+
private static final String USERNAME = "user";
53+
54+
private static final String PASSWORD = "password";
55+
56+
@Autowired
57+
private TestRestTemplate restTemplate;
5058

5159
@Container
5260
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
5361
.withStartupTimeout(Duration.ofMinutes(2));
5462

55-
@Autowired
56-
private TestRestTemplate template;
57-
5863
@DynamicPropertySource
5964
static void applicationProperties(DynamicPropertyRegistry registry) {
60-
registry.add("spring.security.user.name", () -> USERNAME);
61-
registry.add("spring.security.user.password", () -> PASSWORD);
6265
registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl);
6366
}
6467

6568
@Test
6669
@SuppressWarnings("unchecked")
6770
void sessionsEndpointShouldReturnUserSessions() {
6871
createSession();
69-
ResponseEntity<Map<String, Object>> response = this.getSessions();
72+
ResponseEntity<Map<String, Object>> response = getSessions();
7073
assertThat(response).isNotNull();
7174
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
7275
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
7376
assertThat(sessions.size()).isEqualTo(1);
7477
}
7578

7679
private void createSession() {
77-
URI uri = URI.create(ROOT);
80+
URI uri = URI.create("/");
7881
HttpHeaders headers = new HttpHeaders();
7982
headers.setBasicAuth(USERNAME, PASSWORD);
8083
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
81-
this.template.exchange(request, String.class);
84+
this.restTemplate.exchange(request, String.class);
8285
}
8386

8487
@SuppressWarnings("unchecked")
8588
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);
89+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
90+
.withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
8891
}
8992

9093
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id "org.springframework.boot.conventions"
44
}
55

6-
description = "Spring Boot Http Session Mongodb smoke test"
6+
description = "Spring Boot Session Mongodb smoke test"
77

88
dependencies {
99
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222

2323
@SpringBootApplication
2424
@EnableRedisHttpSession
25-
public class SampleHttpSessionRedisApplication {
25+
public class SampleSessionRedisApplication {
2626

2727
public static void main(String[] args) {
28-
SpringApplication.run(SampleHttpSessionRedisApplication.class);
28+
SpringApplication.run(SampleSessionRedisApplication.class);
2929
}
3030

3131
}
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
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,24 +38,27 @@
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040

41+
/**
42+
* Tests for {@link SampleSessionRedisApplication}.
43+
*
44+
* @author Angel L. Villalain
45+
*/
4146
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
4247
@Testcontainers(disabledWithoutDocker = true)
43-
public class SampleHttpSessionRedisApplicationTests {
48+
public class SampleSessionRedisApplicationTests {
49+
50+
private static final String USERNAME = "user";
4451

45-
static final String USERNAME = "user";
46-
static final String PASSWORD = "password";
47-
static final String ROOT = "/";
52+
private static final String PASSWORD = "password";
4853

4954
@Container
5055
static RedisContainer redis = new RedisContainer();
5156

5257
@Autowired
53-
private TestRestTemplate template;
58+
private TestRestTemplate restTemplate;
5459

5560
@DynamicPropertySource
5661
static void applicationProperties(DynamicPropertyRegistry registry) {
57-
registry.add("spring.security.user.name", () -> USERNAME);
58-
registry.add("spring.security.user.password", () -> PASSWORD);
5962
registry.add("spring.redis.host", redis::getHost);
6063
registry.add("spring.redis.port", redis::getFirstMappedPort);
6164
}
@@ -72,17 +75,17 @@ void sessionsEndpointShouldReturnUserSessions() {
7275
}
7376

7477
private void createSession() {
75-
URI uri = URI.create(ROOT);
78+
URI uri = URI.create("/");
7679
HttpHeaders headers = new HttpHeaders();
7780
headers.setBasicAuth(USERNAME, PASSWORD);
7881
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
79-
this.template.exchange(request, String.class);
82+
this.restTemplate.exchange(request, String.class);
8083
}
8184

8285
@SuppressWarnings("unchecked")
8386
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);
87+
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
88+
.withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
8689
}
8790

8891
}

0 commit comments

Comments
 (0)