Skip to content

Commit

Permalink
BAEL-2886 Added Integration test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoli committed May 14, 2019
1 parent 7a4f96e commit 8ff89c0
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
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]));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package springsessionmongodb;
package com.baeldung.springsessionmongodb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
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);
}

}
}
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
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]));
}

}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 8ff89c0

Please sign in to comment.