BAEL-2886 Added Integration test cases

This commit is contained in:
Pavan Shankar Koli 2019-05-14 22:51:49 +05:30
parent 7a4f96ea2e
commit 8ff89c0c05
6 changed files with 96 additions and 12 deletions

View File

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

View File

@ -1,4 +1,4 @@
package springsessionmongodb;
package com.baeldung.springsessionmongodb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

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

View File

@ -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

View File

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

View File

@ -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)