diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/build.gradle new file mode 100644 index 000000000000..2e14a54ddcdc --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/build.gradle @@ -0,0 +1,19 @@ +plugins { + id "java" + id "org.springframework.boot.conventions" +} + +description = "Spring Boot Http Session Mongodb smoke test" + +dependencies { + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-mongodb")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web")) + implementation("org.springframework.session:spring-session-data-mongodb") + testImplementation("org.testcontainers:mongodb") + testImplementation("org.testcontainers:testcontainers") + testImplementation("org.testcontainers:junit-jupiter") + testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) + testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/java/smoketest/session/mongodb/SampleHttpSessionMongoApplication.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/java/smoketest/session/mongodb/SampleHttpSessionMongoApplication.java new file mode 100644 index 000000000000..2d98a3a579a9 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/java/smoketest/session/mongodb/SampleHttpSessionMongoApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.session.mongodb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession; + +@SpringBootApplication +@EnableMongoHttpSession +public class SampleHttpSessionMongoApplication { + + public static void main(String[] args) { + SpringApplication.run(SampleHttpSessionMongoApplication.class); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/resources/application.properties new file mode 100644 index 000000000000..ca5b9bafe245 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/main/resources/application.properties @@ -0,0 +1,2 @@ +management.endpoints.web.exposure.include=* +spring.mongodb.embedded.version=3.6.5 \ No newline at end of file diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/test/java/smoketest/session/mongodb/SampleHttpSessionMongoApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/test/java/smoketest/session/mongodb/SampleHttpSessionMongoApplicationTests.java new file mode 100644 index 000000000000..aab4385d6f68 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-mongo/src/test/java/smoketest/session/mongodb/SampleHttpSessionMongoApplicationTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.session.mongodb; + +import java.net.URI; +import java.time.Duration; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Testcontainers(disabledWithoutDocker = true) +public class SampleHttpSessionMongoApplicationTests { + + static final String USERNAME = "user"; + static final String PASSWORD = "password"; + static final String ROOT = "/"; + + @Container + static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3) + .withStartupTimeout(Duration.ofMinutes(2)); + + @Autowired + private TestRestTemplate template; + + @DynamicPropertySource + 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); + } + + @Test + @SuppressWarnings("unchecked") + void sessionsEndpointShouldReturnUserSessions() { + createSession(); + ResponseEntity> response = this.getSessions(); + assertThat(response).isNotNull(); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + List> sessions = (List>) response.getBody().get("sessions"); + assertThat(sessions.size()).isEqualTo(1); + } + + private void createSession() { + URI uri = URI.create(ROOT); + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth(USERNAME, PASSWORD); + RequestEntity request = new RequestEntity<>(headers, HttpMethod.GET, uri); + this.template.exchange(request, String.class); + } + + @SuppressWarnings("unchecked") + private ResponseEntity> getSessions() { + return (ResponseEntity>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD) + .getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/build.gradle new file mode 100644 index 000000000000..715c15c489d1 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/build.gradle @@ -0,0 +1,18 @@ +plugins { + id "java" + id "org.springframework.boot.conventions" +} + +description = "Spring Boot Http Session Mongodb smoke test" + +dependencies { + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web")) + implementation("org.springframework.session:spring-session-data-redis") + testImplementation("org.testcontainers:testcontainers") + testImplementation("org.testcontainers:junit-jupiter") + testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) + testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/java/smoketest/session/redis/SampleHttpSessionRedisApplication.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/java/smoketest/session/redis/SampleHttpSessionRedisApplication.java new file mode 100644 index 000000000000..a3f4aa080cf0 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/java/smoketest/session/redis/SampleHttpSessionRedisApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.session.redis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; + +@SpringBootApplication +@EnableRedisHttpSession +public class SampleHttpSessionRedisApplication { + + public static void main(String[] args) { + SpringApplication.run(SampleHttpSessionRedisApplication.class); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/resources/application.properties new file mode 100644 index 000000000000..705d36ce69d1 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/main/resources/application.properties @@ -0,0 +1 @@ +management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/test/java/smoketest/session/redis/SampleHttpSessionRedisApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/test/java/smoketest/session/redis/SampleHttpSessionRedisApplicationTests.java new file mode 100644 index 000000000000..dcc36c7e1df7 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-http-redis/src/test/java/smoketest/session/redis/SampleHttpSessionRedisApplicationTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.session.redis; + +import java.net.URI; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.testsupport.testcontainers.RedisContainer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Testcontainers(disabledWithoutDocker = true) +public class SampleHttpSessionRedisApplicationTests { + + static final String USERNAME = "user"; + static final String PASSWORD = "password"; + static final String ROOT = "/"; + + @Container + static RedisContainer redis = new RedisContainer(); + + @Autowired + private TestRestTemplate template; + + @DynamicPropertySource + 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.port", redis::getFirstMappedPort); + } + + @Test + @SuppressWarnings("unchecked") + void sessionsEndpointShouldReturnUserSessions() { + createSession(); + ResponseEntity> response = this.getSessions(); + assertThat(response).isNotNull(); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + List> sessions = (List>) response.getBody().get("sessions"); + assertThat(sessions.size()).isEqualTo(1); + } + + private void createSession() { + URI uri = URI.create(ROOT); + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth(USERNAME, PASSWORD); + RequestEntity request = new RequestEntity<>(headers, HttpMethod.GET, uri); + this.template.exchange(request, String.class); + } + + @SuppressWarnings("unchecked") + private ResponseEntity> getSessions() { + return (ResponseEntity>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD) + .getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); + } + +}