Merge pull request #28173 from kandulsh
* pr/28173: Polish "Add smoke test with Spring Session and Hazelcast" Add smoke test with Spring Session and Hazelcast Closes gh-28173pull/28237/head
commit
1ff900cce8
@ -0,0 +1,16 @@
|
||||
plugins {
|
||||
id "java"
|
||||
id "org.springframework.boot.conventions"
|
||||
}
|
||||
|
||||
description = "Spring Boot Session 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-web"))
|
||||
implementation("com.hazelcast:hazelcast")
|
||||
implementation("org.springframework.session:spring-session-hazelcast")
|
||||
|
||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.hazelcast;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleSessionHazelcastApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleSessionHazelcastApplication.class);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
spring.security.user.name=user
|
||||
spring.security.user.password=password
|
||||
|
||||
management.endpoints.web.exposure.include=*
|
@ -0,0 +1,18 @@
|
||||
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.hazelcast.com/schema/config
|
||||
http://www.hazelcast.com/schema/config/hazelcast-config-4.0.xsd">
|
||||
|
||||
<map name="spring:session:sessions">
|
||||
<attributes>
|
||||
<attribute extractor-class-name="org.springframework.session.hazelcast.Hazelcast4PrincipalNameExtractor">principalName</attribute>
|
||||
</attributes>
|
||||
</map>
|
||||
|
||||
<network>
|
||||
<join>
|
||||
<multicast enabled="false"/>
|
||||
</join>
|
||||
</network>
|
||||
|
||||
</hazelcast>
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.hazelcast;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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.web.server.LocalServerPort;
|
||||
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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SampleSessionHazelcastApplication},
|
||||
*
|
||||
* @author Susmitha Kandula
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class SampleSessionHazelcastApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void sessionsEndpointShouldReturnUserSession() {
|
||||
URI uri = URI.create("http://localhost:" + this.port + "/");
|
||||
ResponseEntity<String> firstResponse = performRequest(this.restTemplate, uri, null);
|
||||
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
|
||||
performRequest(this.restTemplate, uri, cookie).getBody();
|
||||
ResponseEntity<Map<String, Object>> entity = (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate
|
||||
.withBasicAuth("user", "password").getForEntity("/actuator/sessions?username=user", Map.class);
|
||||
assertThat(entity).isNotNull();
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
List<Map<String, Object>> sessions = (List<Map<String, Object>>) entity.getBody().get("sessions");
|
||||
assertThat(sessions.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
private ResponseEntity<String> performRequest(TestRestTemplate restTemplate, URI uri, String cookie) {
|
||||
HttpHeaders headers = getHeaders(cookie);
|
||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
|
||||
return restTemplate.exchange(request, String.class);
|
||||
}
|
||||
|
||||
private HttpHeaders getHeaders(String cookie) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
if (cookie != null) {
|
||||
headers.set("Cookie", cookie);
|
||||
}
|
||||
else {
|
||||
headers.set("Authorization", getBasicAuth());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
private String getBasicAuth() {
|
||||
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue