forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2886 Added Integration test cases
- Loading branch information
Showing
6 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
.../src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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])); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...godb/src/main/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 8 additions & 7 deletions
15
...ain/java/com/baeldung/springsessionmongodb/controller/SpringSessionMongoDBController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
package springsessionmongodb.controller; | ||
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; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
@RestController | ||
public class SpringSessionMongoDBController { | ||
|
||
@GetMapping("/") | ||
public String count(HttpSession session) { | ||
public ResponseEntity<Integer> count(HttpSession session) { | ||
|
||
Integer counter = (Integer) session.getAttribute("count"); | ||
|
||
if (counter == null) { | ||
counter = 0; | ||
counter = 1; | ||
} else { | ||
counter += 1; | ||
} | ||
|
||
session.setAttribute("count", counter); | ||
|
||
return "<h1>Count is "+counter+"</h1>"; | ||
return ResponseEntity.ok(counter); | ||
} | ||
|
||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
spring-session/spring-session-mongodb/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
spring.session.store-type=mongodb | ||
spring.session.store-type=mongodb | ||
server.port=8080 | ||
|
||
spring.data.mongodb.host=localhost | ||
spring.data.mongodb.port=27017 | ||
spring.data.mongodb.database=springboot-mongo |
38 changes: 38 additions & 0 deletions
38
.../src/test/java/com/baeldung/springsessionmongodb/SpringSessionMongoDBIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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])); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
...ssion/spring-session-mongodb/src/test/java/org/baeldung/SpringContextIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters