diff --git a/spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java b/spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java index 60f0dc97ca..c73335b49b 100644 --- a/spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java +++ b/spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java @@ -1,4 +1,43 @@ 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.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.session.data.mongo.MongoOperationsSessionRepository; +import org.springframework.test.context.junit4.SpringRunner; +import springsessionmongodb.SpringSessionMongoDBApplication; + +import java.util.Base64; + + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class SpringSessionMongoDBIntegrationTest { + + @Autowired + private MongoOperationsSessionRepository repository; + + private TestRestTemplate restTemplate = new TestRestTemplate(); + + @Test + public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() { + HttpEntity response = restTemplate. + exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class); + HttpHeaders headers = response.getHeaders(); + String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE); + + Assert.assertEquals(response.getBody(), + repository.findById(getSessionId(set_cookie)).getAttribute("count").toString()); + } + + private String getSessionId(String set_cookie) { + return new String(Base64.getDecoder().decode(set_cookie.split(";")[0].split("=")[1])); + } + } diff --git a/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java index c574501472..2994efc719 100644 --- a/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java +++ b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java @@ -1,4 +1,4 @@ -package springsessionmongodb; +package com.baeldung.springsessionmongodb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java index 50fc432d56..1c38f419c3 100644 --- a/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java +++ b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java @@ -1,27 +1,28 @@ -package springsessionmongodb.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; +package com.baeldung.springsessionmongodb.controller; import javax.servlet.http.HttpSession; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + @RestController public class SpringSessionMongoDBController { @GetMapping("/") - public String count(HttpSession session) { + public ResponseEntity count(HttpSession session) { Integer counter = (Integer) session.getAttribute("count"); if (counter == null) { - counter = 0; + counter = 1; } else { counter += 1; } session.setAttribute("count", counter); - return "

Count is "+counter+"

"; + return ResponseEntity.ok(counter); } -} +} \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/src/main/resources/application.properties b/spring-session/spring-session-mongodb/src/main/resources/application.properties index aeb7eb91ad..1b1a6cfbcb 100644 --- a/spring-session/spring-session-mongodb/src/main/resources/application.properties +++ b/spring-session/spring-session-mongodb/src/main/resources/application.properties @@ -1 +1,6 @@ -spring.session.store-type=mongodb \ No newline at end of file +spring.session.store-type=mongodb +server.port=8080 + +spring.data.mongodb.host=localhost +spring.data.mongodb.port=27017 +spring.data.mongodb.database=springboot-mongo \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java b/spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java index 60f0dc97ca..24cf50f18d 100644 --- a/spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java +++ b/spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java @@ -1,4 +1,42 @@ package com.baeldung.springsessionmongodb; +import java.util.Base64; + +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.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.session.data.mongo.MongoOperationsSessionRepository; +import org.springframework.test.context.junit4.SpringRunner; + + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class SpringSessionMongoDBIntegrationTest { + + @Autowired + private MongoOperationsSessionRepository repository; + + private TestRestTemplate restTemplate = new TestRestTemplate(); + + @Test + public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() { + HttpEntity response = restTemplate. + exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class); + HttpHeaders headers = response.getHeaders(); + String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE); + + Assert.assertEquals(response.getBody(), + repository.findById(getSessionId(set_cookie)).getAttribute("count").toString()); + } + + private String getSessionId(String set_cookie) { + return new String(Base64.getDecoder().decode(set_cookie.split(";")[0].split("=")[1])); + } + } diff --git a/spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java index b58fd46dae..3c58b2673f 100644 --- a/spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ b/spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -1,10 +1,11 @@ -package baeldung; +package org.baeldung; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import springsessionmongodb.SpringSessionMongoDBApplication; + +import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringSessionMongoDBApplication.class)