Browse Source

Polish "Add smoke tests for Spring Session Redis/Mongo"

See gh-28362
pull/28398/head
Madhura Bhave 4 years ago
parent
commit
2caa6cb227
  1. 2
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/resources/application.properties
  2. 1
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/resources/application.properties
  3. 2
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/build.gradle
  4. 6
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/java/smoketest/session/mongodb/SampleSessionMongoApplication.java
  5. 3
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/resources/application.properties
  6. 33
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java
  7. 2
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/build.gradle
  8. 6
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/java/smoketest/session/redis/SampleSessionRedisApplication.java
  9. 3
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/resources/application.properties
  10. 27
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/test/java/smoketest/session/redis/SampleSessionRedisApplicationTests.java

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

@ -1,2 +0,0 @@
management.endpoints.web.exposure.include=*
spring.mongodb.embedded.version=3.6.5

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

@ -1 +0,0 @@
management.endpoints.web.exposure.include=*

2
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/build.gradle → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/build.gradle

@ -3,7 +3,7 @@ plugins {
id "org.springframework.boot.conventions" id "org.springframework.boot.conventions"
} }
description = "Spring Boot Http Session Mongodb smoke test" description = "Spring Boot Session Mongodb smoke test"
dependencies { dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))

6
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/java/smoketest/session/mongodb/SampleHttpSessionMongoApplication.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/java/smoketest/session/mongodb/SampleSessionMongoApplication.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,10 +22,10 @@ import org.springframework.session.data.mongo.config.annotation.web.http.EnableM
@SpringBootApplication @SpringBootApplication
@EnableMongoHttpSession @EnableMongoHttpSession
public class SampleHttpSessionMongoApplication { public class SampleSessionMongoApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleHttpSessionMongoApplication.class); SpringApplication.run(SampleSessionMongoApplication.class);
} }
} }

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

@ -0,0 +1,3 @@
management.endpoints.web.exposure.include=*
spring.security.user.name=user
spring.security.user.password=password

33
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/test/java/smoketest/session/mongodb/SampleHttpSessionMongoApplicationTests.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,25 +40,28 @@ import org.springframework.test.context.DynamicPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SampleSessionMongoApplication}.
*
* @author Angel L. Villalain
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers(disabledWithoutDocker = true) @Testcontainers(disabledWithoutDocker = true)
public class SampleHttpSessionMongoApplicationTests { public class SampleSessionMongoApplicationTests {
static final String USERNAME = "user"; private static final String USERNAME = "user";
static final String PASSWORD = "password";
static final String ROOT = "/"; private static final String PASSWORD = "password";
@Autowired
private TestRestTemplate restTemplate;
@Container @Container
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3) static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
.withStartupTimeout(Duration.ofMinutes(2)); .withStartupTimeout(Duration.ofMinutes(2));
@Autowired
private TestRestTemplate template;
@DynamicPropertySource @DynamicPropertySource
static void applicationProperties(DynamicPropertyRegistry registry) { static void applicationProperties(DynamicPropertyRegistry registry) {
registry.add("spring.security.user.name", () -> USERNAME);
registry.add("spring.security.user.password", () -> PASSWORD);
registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl); registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl);
} }
@ -66,7 +69,7 @@ public class SampleHttpSessionMongoApplicationTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void sessionsEndpointShouldReturnUserSessions() { void sessionsEndpointShouldReturnUserSessions() {
createSession(); createSession();
ResponseEntity<Map<String, Object>> response = this.getSessions(); ResponseEntity<Map<String, Object>> response = getSessions();
assertThat(response).isNotNull(); assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions"); List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
@ -74,17 +77,17 @@ public class SampleHttpSessionMongoApplicationTests {
} }
private void createSession() { private void createSession() {
URI uri = URI.create(ROOT); URI uri = URI.create("/");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(USERNAME, PASSWORD); headers.setBasicAuth(USERNAME, PASSWORD);
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri); RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
this.template.exchange(request, String.class); this.restTemplate.exchange(request, String.class);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private ResponseEntity<Map<String, Object>> getSessions() { private ResponseEntity<Map<String, Object>> getSessions() {
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD) return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
.getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); .withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
} }
} }

2
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/build.gradle → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/build.gradle

@ -3,7 +3,7 @@ plugins {
id "org.springframework.boot.conventions" id "org.springframework.boot.conventions"
} }
description = "Spring Boot Http Session Mongodb smoke test" description = "Spring Boot Session Mongodb smoke test"
dependencies { dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))

6
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/java/smoketest/session/redis/SampleHttpSessionRedisApplication.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/main/java/smoketest/session/redis/SampleSessionRedisApplication.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,10 +22,10 @@ import org.springframework.session.data.redis.config.annotation.web.http.EnableR
@SpringBootApplication @SpringBootApplication
@EnableRedisHttpSession @EnableRedisHttpSession
public class SampleHttpSessionRedisApplication { public class SampleSessionRedisApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleHttpSessionRedisApplication.class); SpringApplication.run(SampleSessionRedisApplication.class);
} }
} }

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

@ -0,0 +1,3 @@
management.endpoints.web.exposure.include=*
spring.security.user.name=user
spring.security.user.password=password

27
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/test/java/smoketest/session/redis/SampleHttpSessionRedisApplicationTests.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-redis/src/test/java/smoketest/session/redis/SampleSessionRedisApplicationTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,24 +38,27 @@ import org.springframework.test.context.DynamicPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SampleSessionRedisApplication}.
*
* @author Angel L. Villalain
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers(disabledWithoutDocker = true) @Testcontainers(disabledWithoutDocker = true)
public class SampleHttpSessionRedisApplicationTests { public class SampleSessionRedisApplicationTests {
private static final String USERNAME = "user";
static final String USERNAME = "user"; private static final String PASSWORD = "password";
static final String PASSWORD = "password";
static final String ROOT = "/";
@Container @Container
static RedisContainer redis = new RedisContainer(); static RedisContainer redis = new RedisContainer();
@Autowired @Autowired
private TestRestTemplate template; private TestRestTemplate restTemplate;
@DynamicPropertySource @DynamicPropertySource
static void applicationProperties(DynamicPropertyRegistry registry) { static void applicationProperties(DynamicPropertyRegistry registry) {
registry.add("spring.security.user.name", () -> USERNAME);
registry.add("spring.security.user.password", () -> PASSWORD);
registry.add("spring.redis.host", redis::getHost); registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", redis::getFirstMappedPort); registry.add("spring.redis.port", redis::getFirstMappedPort);
} }
@ -72,17 +75,17 @@ public class SampleHttpSessionRedisApplicationTests {
} }
private void createSession() { private void createSession() {
URI uri = URI.create(ROOT); URI uri = URI.create("/");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(USERNAME, PASSWORD); headers.setBasicAuth(USERNAME, PASSWORD);
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri); RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
this.template.exchange(request, String.class); this.restTemplate.exchange(request, String.class);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private ResponseEntity<Map<String, Object>> getSessions() { private ResponseEntity<Map<String, Object>> getSessions() {
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD) return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
.getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); .withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class);
} }
} }
Loading…
Cancel
Save