JAVA-32063: Migrate spring-session-mongodb to spring boot 3 (#16184)
This commit is contained in:
parent
ec6524f294
commit
1f5e5f0752
@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../../../parent-boot-2</relativePath>
|
<relativePath>../../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -36,8 +36,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>de.flapdoodle.embed</groupId>
|
<groupId>de.flapdoodle.embed</groupId>
|
||||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
|
||||||
<scope>test</scope>
|
<version>${embedded-mongodb.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -50,4 +50,8 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<embedded-mongodb.version>4.12.2</embedded-mongodb.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.springsessionmongodb;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
|
||||||
|
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
||||||
|
import org.springframework.session.web.http.DefaultCookieSerializer;
|
||||||
|
|
||||||
|
@EnableMongoHttpSession
|
||||||
|
public class HttpSessionConfig {
|
||||||
|
@Bean
|
||||||
|
public JdkMongoSessionConverter jdkMongoSessionConverter() {
|
||||||
|
return new JdkMongoSessionConverter(Duration.ofMinutes(30));
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public DefaultCookieSerializer customCookieSerializer() {
|
||||||
|
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
|
||||||
|
cookieSerializer.setUseHttpOnlyCookie(false);
|
||||||
|
return cookieSerializer;
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class SpringSessionMongoDBController {
|
public class SpringSessionMongoDBController {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
spring.session.store-type=mongodb
|
#spring.session.store-type=mongodb
|
||||||
server.port=8080
|
server.port=8080
|
||||||
|
|
||||||
spring.data.mongodb.host=localhost
|
spring.data.mongodb.host=localhost
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
||||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
||||||
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
// Ensuring the context is spring boot application is started.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,21 @@
|
|||||||
package com.baeldung.springsessionmongodb;
|
package com.baeldung.springsessionmongodb;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
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.HttpEntity;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
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.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class SpringSessionMongoDBIntegrationTest {
|
class SpringSessionMongoDBIntegrationTest {
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int port;
|
private int port;
|
||||||
@ -26,17 +23,18 @@ public class SpringSessionMongoDBIntegrationTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private MongoIndexedSessionRepository repository;
|
private MongoIndexedSessionRepository repository;
|
||||||
|
|
||||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
private final TestRestTemplate restTemplate = new TestRestTemplate();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||||
HttpEntity<String> response = restTemplate
|
HttpEntity<String> response = restTemplate
|
||||||
.exchange("http://localhost:" + port, HttpMethod.GET, null, String.class);
|
.exchange("http://localhost:" + port, HttpMethod.GET, null, String.class);
|
||||||
HttpHeaders headers = response.getHeaders();
|
HttpHeaders headers = response.getHeaders();
|
||||||
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
|
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
|
||||||
|
|
||||||
Assert.assertEquals(response.getBody(),
|
Session sessionById = repository.findById(getSessionId(set_cookie));
|
||||||
repository.findById(getSessionId(set_cookie)).getAttribute("count").toString());
|
|
||||||
|
Assertions.assertEquals(response.getBody(), sessionById.getAttribute("count").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getSessionId(String cookie) {
|
private String getSessionId(String cookie) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#To use a randomly allocated free port during tests to avoid port conflict across tests
|
#To use a randomly allocated free port during tests to avoid port conflict across tests
|
||||||
spring.data.mongodb.port=0
|
spring.data.mongodb.port=0
|
||||||
|
de.flapdoodle.mongodb.embedded.version=5.0.5
|
||||||
|
|
||||||
spring.mongodb.embedded.version=4.4.9
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user