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