BAEL-2886- post incremented counter variable

This commit is contained in:
pkoli 2019-06-01 13:50:48 +05:30
parent d143f54a51
commit dc73b7abff
5 changed files with 10 additions and 52 deletions

View File

@ -1,43 +0,0 @@
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

@ -32,6 +32,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>${spring-boot-starter-data-mongodb.version}</version>
</dependency>
<dependency>
@ -43,6 +44,7 @@
<properties>
<spring-session-data-mongodb.version>2.1.3.RELEASE</spring-session-data-mongodb.version>
<spring-boot-starter-data-mongodb.version>2.1.5.RELEASE</spring-boot-starter-data-mongodb.version>
</properties>
<build>

View File

@ -1,11 +1,11 @@
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 {
@ -17,7 +17,7 @@ public class SpringSessionMongoDBController {
if (counter == null) {
counter = 1;
} else {
counter += 1;
counter++;
}
session.setAttribute("count", counter);

View File

@ -1,7 +1,5 @@
package com.baeldung.springsessionmongodb;
import java.util.Base64;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -14,6 +12,8 @@ import org.springframework.http.HttpMethod;
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Base64;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ -26,8 +26,8 @@ public class SpringSessionMongoDBIntegrationTest {
@Test
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
HttpEntity<String> response = restTemplate.
exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class);
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);

View File

@ -1,12 +1,11 @@
package org.baeldung;
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
public class SpringContextIntegrationTest {