Skip to content

Commit a030afa

Browse files
aravind-r-ranganathanpedja4
authored andcommitted
BAEL-1911 - Fixing author's review comments (eugenp#4782)
* Strange git issue with README.MD, wouldn't revert the file * Fixing review comments * BAEL-1911 Refactored SQL code, fixed formatting
1 parent 3205de6 commit a030afa

7 files changed

Lines changed: 105 additions & 65 deletions

File tree

‎spring-session/spring-session-jdbc/pom.xml‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<name>spring-session-jdbc</name>
1111
<description>Spring Session with JDBC tutorial</description>
1212

13+
<properties>
14+
<h2.version>1.4.197</h2.version>
15+
</properties>
16+
1317
<parent>
1418
<artifactId>parent-boot-2</artifactId>
1519
<groupId>com.baeldung</groupId>
@@ -18,40 +22,25 @@
1822
</parent>
1923

2024
<dependencies>
21-
<dependency>
22-
<groupId>org.springframework.boot</groupId>
23-
<artifactId>spring-boot-starter-data-jpa</artifactId>
24-
</dependency>
25-
<dependency>
26-
<groupId>org.springframework.boot</groupId>
27-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
28-
</dependency>
2925
<dependency>
3026
<groupId>org.springframework.boot</groupId>
3127
<artifactId>spring-boot-starter-web</artifactId>
3228
</dependency>
3329
<dependency>
3430
<groupId>org.springframework.session</groupId>
35-
<artifactId>spring-session-core</artifactId>
31+
<artifactId>spring-session-jdbc</artifactId>
3632
</dependency>
3733
<dependency>
3834
<groupId>com.h2database</groupId>
3935
<artifactId>h2</artifactId>
36+
<version>${h2.version}</version>
4037
<scope>runtime</scope>
4138
</dependency>
4239
<dependency>
4340
<groupId>org.springframework.boot</groupId>
4441
<artifactId>spring-boot-starter-test</artifactId>
4542
<scope>test</scope>
4643
</dependency>
47-
<dependency>
48-
<groupId>org.springframework.session</groupId>
49-
<artifactId>spring-session-jdbc</artifactId>
50-
</dependency>
51-
<!--<dependency>-->
52-
<!--<groupId>org.springframework.boot</groupId>-->
53-
<!--<artifactId>spring-boot-starter-security</artifactId>-->
54-
<!--</dependency>-->
5544
</dependencies>
5645

5746
<build>

‎spring-session/spring-session-jdbc/src/main/java/com/baeldung/springsessionjdbc/SpringSessionJdbcApplication.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@SpringBootApplication
77
public class SpringSessionJdbcApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(SpringSessionJdbcApplication.class, args);
11-
}
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringSessionJdbcApplication.class, args);
11+
}
1212
}

‎spring-session/spring-session-jdbc/src/main/java/com/baeldung/springsessionjdbc/controller/SpringSessionJdbcController.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ public String index(Model model, HttpSession session) {
2424
}
2525

2626
@PostMapping("/saveColor")
27-
public String saveMessage(@RequestParam("color") String color,
28-
HttpServletRequest request) {
27+
public String saveMessage(@RequestParam("color") String color, HttpServletRequest request) {
2928
List<String> favoriteColors = getFavColors(request.getSession());
3029
if (!StringUtils.isEmpty(color)) {
3130
favoriteColors.add(color);
32-
request.getSession().
33-
setAttribute("favoriteColors", favoriteColors);
31+
request
32+
.getSession()
33+
.setAttribute("favoriteColors", favoriteColors);
3434
}
3535
return "redirect:/";
3636
}
3737

3838
private List<String> getFavColors(HttpSession session) {
39-
List<String> favoriteColors = (List<String>) session.
40-
getAttribute("favoriteColors");
39+
List<String> favoriteColors = (List<String>) session.getAttribute("favoriteColors");
4140
if (favoriteColors == null) {
4241
favoriteColors = new ArrayList<>();
4342
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
spring.session.store-type=jdbc
2-
#spring.session.jdbc.initialize-schema=embedded
3-
#spring.session.jdbc.table-name=SPRING_SESSION
4-
#server.servlet.session.timeout=60s
5-
#spring.datasource.url=jdbc:h2:mem:AZ
6-
#spring.security.user.name=admin
7-
#spring.security.user.password=secret
82
spring.h2.console.enabled=true
93
spring.h2.console.path=/h2-console

‎spring-session/spring-session-jdbc/src/main/resources/templates/index.html‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎spring-session/spring-session-jdbc/src/test/java/com/baeldung/springsessionjdbc/SpringSessionJdbcApplicationTests.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.springsessionjdbc;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.FixMethodOrder;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.MethodSorters;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.boot.web.server.LocalServerPort;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
import org.springframework.util.LinkedMultiValueMap;
15+
import org.springframework.util.MultiValueMap;
16+
17+
import java.io.ByteArrayInputStream;
18+
import java.io.IOException;
19+
import java.io.ObjectInput;
20+
import java.io.ObjectInputStream;
21+
import java.sql.*;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@RunWith(SpringRunner.class)
28+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
29+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
30+
public class SpringSessionJdbcIntegrationTest {
31+
32+
@LocalServerPort
33+
private int port;
34+
35+
@Autowired
36+
private TestRestTemplate testRestTemplate;
37+
38+
@Before
39+
public void setup() throws ClassNotFoundException {
40+
Class.forName("org.h2.Driver");
41+
}
42+
43+
@Test
44+
public void givenApiHasStarted_whenH2DbIsQueried_thenSessionTablesAreEmpty() throws SQLException {
45+
Assert.assertEquals(0, getSessionIdsFromDatabase().size());
46+
Assert.assertEquals(0, getSessionAttributeBytesFromDatabase().size());
47+
}
48+
49+
@Test
50+
public void givenGetInvoked_whenH2DbIsQueried_thenOneSessionIsCreated() throws SQLException {
51+
assertThat(this.testRestTemplate.getForObject("http://localhost:" + port + "/", String.class)).isNotEmpty();
52+
Assert.assertEquals(1, getSessionIdsFromDatabase().size());
53+
}
54+
55+
@Test
56+
public void givenPostInvoked_whenH2DbIsQueried_thenSessionAttributeIsRetrieved() throws ClassNotFoundException, SQLException, IOException {
57+
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
58+
map.add("color", "red");
59+
this.testRestTemplate.postForObject("http://localhost:" + port + "/saveColor", map, String.class);
60+
List<byte[]> queryResponse = getSessionAttributeBytesFromDatabase();
61+
Assert.assertEquals(1, queryResponse.size());
62+
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(queryResponse.get(0)));
63+
List<String> obj = (List<String>) in.readObject(); //Deserialize byte[] to object
64+
Assert.assertEquals("red", obj.get(0));
65+
}
66+
67+
private List<String> getSessionIdsFromDatabase() throws SQLException {
68+
List<String> result = new ArrayList<>();
69+
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION");
70+
while (rs.next()) {
71+
result.add(rs.getString("SESSION_ID"));
72+
}
73+
return result;
74+
}
75+
76+
private List<byte[]> getSessionAttributeBytesFromDatabase() throws SQLException {
77+
List<byte[]> result = new ArrayList<>();
78+
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION_ATTRIBUTES");
79+
while (rs.next()) {
80+
result.add(rs.getBytes("ATTRIBUTE_BYTES"));
81+
}
82+
return result;
83+
}
84+
85+
private ResultSet getResultSet(String sql) throws SQLException {
86+
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "");
87+
Statement stat = conn.createStatement();
88+
return stat.executeQuery(sql);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)