Skip to content

Commit f3b79ff

Browse files
committed
BAEL-89 Removing spring from integration test and converting to unit test.
1 parent 35020fc commit f3b79ff

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,64 @@
11
package com.baeldung.spring.session;
22

3-
import org.apache.tomcat.util.codec.binary.Base64;
43
import org.junit.Before;
54
import org.junit.Test;
6-
import org.junit.runner.RunWith;
7-
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.boot.test.context.SpringBootTest;
95
import org.springframework.boot.test.web.client.TestRestTemplate;
10-
import org.springframework.data.redis.connection.RedisConnection;
11-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
126
import org.springframework.http.*;
13-
import org.springframework.test.context.junit4.SpringRunner;
7+
import redis.clients.jedis.Jedis;
148

159
import java.util.Set;
1610

1711
import static org.junit.Assert.assertEquals;
1812
import static org.junit.Assert.assertTrue;
1913

20-
@RunWith(SpringRunner.class)
21-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2214
public class SessionControllerTest {
2315

24-
@Autowired
25-
private TestRestTemplate restTemplate;
26-
@Autowired
27-
private JedisConnectionFactory jedisConnectionFactory;
28-
29-
private RedisConnection connection;
16+
private Jedis jedis;
17+
private TestRestTemplate testRestTemplate;
18+
private TestRestTemplate testRestTemplateWithAuth;
19+
private String testUrl = "http://localhost:8080/";
3020

3121
@Before
3222
public void clearRedisData() {
33-
connection = jedisConnectionFactory.getConnection();
34-
connection.flushAll();
23+
testRestTemplate = new TestRestTemplate();
24+
testRestTemplateWithAuth = new TestRestTemplate("admin", "password", null);
25+
26+
jedis = new Jedis("localhost", 6379);
27+
jedis.flushAll();
3528
}
3629

3730
@Test
3831
public void testRedisIsEmpty() {
39-
Set<byte[]> result = connection.keys("*".getBytes());
32+
Set<String> result = jedis.keys("*");
4033
assertEquals(0, result.size());
4134
}
4235

4336
@Test
4437
public void testUnauthenticatedCantAccess() {
45-
ResponseEntity<String> result = restTemplate.getForEntity("/", String.class);
38+
ResponseEntity<String> result = testRestTemplate.getForEntity(testUrl, String.class);
4639
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
4740
}
4841

4942
@Test
5043
public void testRedisControlsSession() {
51-
ResponseEntity<String> result = restTemplate.exchange("/", HttpMethod.GET, makeAuthRequest(), String.class);
44+
ResponseEntity<String> result = testRestTemplateWithAuth.getForEntity(testUrl, String.class);
5245
assertEquals("hello admin", result.getBody()); //login worked
5346

54-
Set<byte[]> redisResult = connection.keys("*".getBytes());
47+
Set<String> redisResult = jedis.keys("*");
5548
assertTrue(redisResult.size() > 0); //redis is populated with session data
5649

5750
String sessionCookie = result.getHeaders().get("Set-Cookie").get(0).split(";")[0];
58-
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class);
59-
assertEquals("hello admin", result.getBody()); //access with session works worked
60-
61-
connection.flushAll(); //clear all keys in redis
62-
63-
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class);
64-
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());//access denied after sessions are removed in redis
65-
66-
}
67-
68-
private HttpEntity<String> makeRequestWithCookie(String sessionCookie) {
6951
HttpHeaders headers = new HttpHeaders();
7052
headers.add("Cookie", sessionCookie);
53+
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
7154

72-
return new HttpEntity<>(headers);
73-
}
55+
result = testRestTemplate.exchange(testUrl, HttpMethod.GET, httpEntity, String.class);
56+
assertEquals("hello admin", result.getBody()); //access with session works worked
7457

75-
private HttpEntity<String> makeAuthRequest() {
76-
String plainCreds = "admin:password";
77-
byte[] plainCredsBytes = plainCreds.getBytes();
78-
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
79-
String base64Creds = new String(base64CredsBytes);
58+
jedis.flushAll(); //clear all keys in redis
8059

81-
HttpHeaders headers = new HttpHeaders();
82-
headers.add("Authorization", "Basic " + base64Creds);
60+
result = testRestTemplate.exchange(testUrl, HttpMethod.GET, httpEntity, String.class);
61+
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());//access denied after sessions are removed in redis
8362

84-
return new HttpEntity<>(headers);
8563
}
8664
}

0 commit comments

Comments
 (0)