From 05ba8a7ac34b7bb539f0d45512a08bcbfddf8e26 Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Fri, 10 May 2019 22:38:50 +0530 Subject: [PATCH 1/5] BAEL-2886 Mongodb spring session code --- .../spring-session-mongodb/README.md | 4 ++ spring-session/spring-session-mongodb/pom.xml | 51 +++++++++++++++++++ .../SpringSessionMongoDBApplication.java | 12 +++++ .../SpringSessionMongoDBController.java | 27 ++++++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/logback.xml | 13 +++++ .../SpringContextIntegrationTest.java | 16 ++++++ 7 files changed, 124 insertions(+) create mode 100644 spring-session/spring-session-mongodb/README.md create mode 100644 spring-session/spring-session-mongodb/pom.xml create mode 100644 spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java create mode 100644 spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java create mode 100644 spring-session/spring-session-mongodb/src/main/resources/application.properties create mode 100644 spring-session/spring-session-mongodb/src/main/resources/logback.xml create mode 100644 spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java diff --git a/spring-session/spring-session-mongodb/README.md b/spring-session/spring-session-mongodb/README.md new file mode 100644 index 0000000000..94fd1cd3e7 --- /dev/null +++ b/spring-session/spring-session-mongodb/README.md @@ -0,0 +1,4 @@ +This module is for Spring Session with JDBC tutorial. +Jira BAEL-1911 + +### Relevant Articles: diff --git a/spring-session/spring-session-mongodb/pom.xml b/spring-session/spring-session-mongodb/pom.xml new file mode 100644 index 0000000000..432715bc0e --- /dev/null +++ b/spring-session/spring-session-mongodb/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + com.baeldung + spring-session-jdbc + 0.0.1-SNAPSHOT + spring-session-jdbc + jar + Spring Session with JDBC tutorial + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.session + spring-session-jdbc + + + com.h2database + h2 + ${h2.version} + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java b/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java new file mode 100644 index 0000000000..c574501472 --- /dev/null +++ b/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java @@ -0,0 +1,12 @@ +package springsessionmongodb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringSessionMongoDBApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringSessionMongoDBApplication.class, args); + } +} diff --git a/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java b/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java new file mode 100644 index 0000000000..50fc432d56 --- /dev/null +++ b/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java @@ -0,0 +1,27 @@ +package springsessionmongodb.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpSession; + +@RestController +public class SpringSessionMongoDBController { + + @GetMapping("/") + public String count(HttpSession session) { + + Integer counter = (Integer) session.getAttribute("count"); + + if (counter == null) { + counter = 0; + } else { + counter += 1; + } + + session.setAttribute("count", counter); + + return "

Count is "+counter+"

"; + } + +} diff --git a/spring-session/spring-session-mongodb/src/main/resources/application.properties b/spring-session/spring-session-mongodb/src/main/resources/application.properties new file mode 100644 index 0000000000..aeb7eb91ad --- /dev/null +++ b/spring-session/spring-session-mongodb/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.session.store-type=mongodb \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/src/main/resources/logback.xml b/spring-session/spring-session-mongodb/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/spring-session/spring-session-mongodb/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java b/spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java new file mode 100644 index 0000000000..b58fd46dae --- /dev/null +++ b/spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java @@ -0,0 +1,16 @@ +package 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; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringSessionMongoDBApplication.class) +public class SpringContextIntegrationTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} From 35693612df65a1dba1b46db8459967e08d3e1e0d Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Fri, 10 May 2019 22:39:38 +0530 Subject: [PATCH 2/5] BAEL-2886 Mongodb spring session code --- spring-session/pom.xml | 1 + .../spring-session-mongodb/README.md | 4 ++-- spring-session/spring-session-mongodb/pom.xml | 22 ++++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/spring-session/pom.xml b/spring-session/pom.xml index bcdbaf2406..639686c8ee 100644 --- a/spring-session/pom.xml +++ b/spring-session/pom.xml @@ -17,6 +17,7 @@ spring-session-jdbc spring-session-redis + spring-session-mongodb \ No newline at end of file diff --git a/spring-session/spring-session-mongodb/README.md b/spring-session/spring-session-mongodb/README.md index 94fd1cd3e7..ab42e8d120 100644 --- a/spring-session/spring-session-mongodb/README.md +++ b/spring-session/spring-session-mongodb/README.md @@ -1,4 +1,4 @@ -This module is for Spring Session with JDBC tutorial. -Jira BAEL-1911 +This module is for Spring Session with MONGO DB tutorial. +Jira BAEL-2886 ### Relevant Articles: diff --git a/spring-session/spring-session-mongodb/pom.xml b/spring-session/spring-session-mongodb/pom.xml index 432715bc0e..714833cf99 100644 --- a/spring-session/spring-session-mongodb/pom.xml +++ b/spring-session/spring-session-mongodb/pom.xml @@ -4,11 +4,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung - spring-session-jdbc + spring-session-mongodb 0.0.1-SNAPSHOT - spring-session-jdbc + spring-session-mongodb jar - Spring Session with JDBC tutorial + Spring Session with MongoDB tutorial parent-boot-2 @@ -22,16 +22,18 @@ org.springframework.boot spring-boot-starter-web + org.springframework.session - spring-session-jdbc + spring-session-data-mongodb + ${spring-session-data-mongodb.version} + - com.h2database - h2 - ${h2.version} - runtime + org.springframework.boot + spring-boot-starter-data-mongodb + org.springframework.boot spring-boot-starter-test @@ -39,6 +41,10 @@ + + 2.1.3.RELEASE + + From 7a4f96ea2ee3ae9c8a702bd656ebd0e8c8784d0b Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Tue, 14 May 2019 22:50:09 +0530 Subject: [PATCH 3/5] BAEL-2886 Added Integration test cases --- .../SpringSessionMongoDBIntegrationTest.java | 4 ++++ .../springsessionmongodb/SpringSessionMongoDBApplication.java | 0 .../controller/SpringSessionMongoDBController.java | 0 .../SpringSessionMongoDBIntegrationTest.java | 4 ++++ .../java/{ => org}/baeldung/SpringContextIntegrationTest.java | 0 5 files changed, 8 insertions(+) create mode 100644 spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java rename spring-session/spring-session-mongodb/src/main/java/{ => com/baeldung}/springsessionmongodb/SpringSessionMongoDBApplication.java (100%) rename spring-session/spring-session-mongodb/src/main/java/{ => com/baeldung}/springsessionmongodb/controller/SpringSessionMongoDBController.java (100%) create mode 100644 spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java rename spring-session/spring-session-mongodb/src/test/java/{ => org}/baeldung/SpringContextIntegrationTest.java (100%) 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 new file mode 100644 index 0000000000..60f0dc97ca --- /dev/null +++ b/spring-session/mongodb-session/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java @@ -0,0 +1,4 @@ +package com.baeldung.springsessionmongodb; + +public class SpringSessionMongoDBIntegrationTest { +} diff --git a/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java similarity index 100% rename from spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/SpringSessionMongoDBApplication.java rename to spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java diff --git a/spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java b/spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java similarity index 100% rename from spring-session/spring-session-mongodb/src/main/java/springsessionmongodb/controller/SpringSessionMongoDBController.java rename to spring-session/spring-session-mongodb/src/main/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java 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 new file mode 100644 index 0000000000..60f0dc97ca --- /dev/null +++ b/spring-session/spring-session-mongodb/src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java @@ -0,0 +1,4 @@ +package com.baeldung.springsessionmongodb; + +public class SpringSessionMongoDBIntegrationTest { +} diff --git a/spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java b/spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java similarity index 100% rename from spring-session/spring-session-mongodb/src/test/java/baeldung/SpringContextIntegrationTest.java rename to spring-session/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java From 8ff89c0c05ed87dd3b852d5e438345ad638494ca Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Tue, 14 May 2019 22:51:49 +0530 Subject: [PATCH 4/5] BAEL-2886 Added Integration test cases --- .../SpringSessionMongoDBIntegrationTest.java | 39 +++++++++++++++++++ .../SpringSessionMongoDBApplication.java | 2 +- .../SpringSessionMongoDBController.java | 17 ++++---- .../src/main/resources/application.properties | 7 +++- .../SpringSessionMongoDBIntegrationTest.java | 38 ++++++++++++++++++ .../SpringContextIntegrationTest.java | 5 ++- 6 files changed, 96 insertions(+), 12 deletions(-) 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) From d143f54a51dcefe1bbb7f3f1b7821a0356faad2a Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Sat, 18 May 2019 08:29:00 +0530 Subject: [PATCH 5/5] BAEL-2886 Updated Integration test cases --- .../SpringSessionMongoDBIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 24cf50f18d..eb9f4164a6 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 @@ -35,8 +35,8 @@ public class SpringSessionMongoDBIntegrationTest { 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])); + private String getSessionId(String cookie) { + return new String(Base64.getDecoder().decode(cookie.split(";")[0].split("=")[1])); } }