diff --git a/samples/httpsession-redis-json/src/integration-test/groovy/samples/HttpRedisJsonTest.groovy b/samples/httpsession-redis-json/src/integration-test/groovy/samples/HttpRedisJsonTest.groovy index 02f3c6ff..a557c490 100644 --- a/samples/httpsession-redis-json/src/integration-test/groovy/samples/HttpRedisJsonTest.groovy +++ b/samples/httpsession-redis-json/src/integration-test/groovy/samples/HttpRedisJsonTest.groovy @@ -1,15 +1,14 @@ package samples -import geb.spock.GebSpec +import geb.spock.* +import sample.Application +import samples.pages.* +import spock.lang.Stepwise + import org.springframework.boot.test.IntegrationTest import org.springframework.boot.test.SpringApplicationContextLoader import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.web.WebAppConfiguration -import sample.Application -import samples.pages.HomePage -import samples.pages.LoginPage -import samples.pages.SetAttributePage -import spock.lang.Stepwise /** * @author jitendra on 15/3/16. @@ -20,36 +19,36 @@ import spock.lang.Stepwise @IntegrationTest class HttpRedisJsonTest extends GebSpec { - def 'login page test'() { - when: - to LoginPage - then: - at LoginPage - } + def'login page test'() { + when: + to LoginPage + then: + at LoginPage + } - def "Unauthenticated user sent to login page"() { - when: - via HomePage - then: - at LoginPage - } + def"Unauthenticated user sent to login page"() { + when: + via HomePage + then: + at LoginPage + } - def "Successful Login test"() { - when: - login() - then: - at HomePage - driver.manage().cookies.find {it.name == "SESSION"} - !driver.manage().cookies.find {it.name == "JSESSIONID"} - } + def"Successful Login test"() { + when: + login() + then: + at HomePage + driver.manage().cookies.find {it.name == "SESSION"} + !driver.manage().cookies.find {it.name == "JSESSIONID"} + } - def "Set and get attributes in session"() { - when: - setAttribute("Demo Key", "Demo Value") + def"Set and get attributes in session"() { + when: + setAttribute("Demo Key", "Demo Value") - then: - at SetAttributePage - tdKey()*.text().contains("Demo Key") - tdKey()*.text().contains("Demo Value") - } + then: + at SetAttributePage + tdKey()*.text().contains("Demo Key") + tdKey()*.text().contains("Demo Value") + } } diff --git a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/HomePage.groovy b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/HomePage.groovy index 82273464..214a20c6 100644 --- a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/HomePage.groovy +++ b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/HomePage.groovy @@ -6,13 +6,15 @@ import geb.Page * @author jitendra on 15/3/16. */ class HomePage extends Page { - static url = "/" + static url="/" - static at = { + static at= + { driver.title == "Spring Session Sample - Home" } - static content = { + static content= + { form { $('form') } submit { $('button[type=submit]') } setAttribute(required: false) { key = 'project', value = 'SessionRedisJson' -> diff --git a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/LoginPage.groovy b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/LoginPage.groovy index 7ba93020..4402fe9b 100644 --- a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/LoginPage.groovy +++ b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/LoginPage.groovy @@ -6,14 +6,16 @@ import geb.Page * @author jitendra on 15/3/16. */ class LoginPage extends Page { - static url = "/login" + static url="/login" - static at = { + static at= + { assert title == "Spring Session Sample - Login" return true } - static content = { + static content= + { form { $('form') } submit { $('button[type=submit]') } login(required: false) { user = 'user', pass = 'password' -> diff --git a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/SetAttributePage.groovy b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/SetAttributePage.groovy index f0aaa649..83a61556 100644 --- a/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/SetAttributePage.groovy +++ b/samples/httpsession-redis-json/src/integration-test/groovy/samples/pages/SetAttributePage.groovy @@ -6,13 +6,15 @@ import geb.Page * @author jitendra on 15/3/16. */ class SetAttributePage extends Page { - static url = "/setValue" + static url="/setValue" - static at = { + static at= + { title == "Spring Session Sample - Home" } - static content = { + static content= + { tdKey { $('td') } } } diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java b/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java index 04e20e43..0dc68f22 100644 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java +++ b/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java @@ -1,16 +1,31 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package samples; import org.junit.Test; import org.junit.runner.RunWith; +import sample.Application; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import sample.Application; -import static org.springframework.util.Assert.notNull; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author jitendra on 8/3/16. @@ -19,13 +34,14 @@ import static org.junit.Assert.*; @SpringApplicationConfiguration(Application.class) public class RedisSerializerTest { - @Autowired - RedisTemplate sessionRedisTemplate; + @Autowired + RedisTemplate sessionRedisTemplate; - @Test - public void testRedisTemplate() { - notNull(sessionRedisTemplate); - notNull(sessionRedisTemplate.getDefaultSerializer()); - assertTrue(sessionRedisTemplate.getDefaultSerializer() instanceof GenericJackson2JsonRedisSerializer); - } + @Test + public void testRedisTemplate() { + assertThat(this.sessionRedisTemplate).isNotNull(); + assertThat(this.sessionRedisTemplate.getDefaultSerializer()).isNotNull(); + assertThat(this.sessionRedisTemplate.getDefaultSerializer()) + .isInstanceOf(GenericJackson2JsonRedisSerializer.class); + } } diff --git a/samples/httpsession-redis-json/src/main/java/sample/Application.java b/samples/httpsession-redis-json/src/main/java/sample/Application.java index 0fe70d72..5c988d69 100644 --- a/samples/httpsession-redis-json/src/main/java/sample/Application.java +++ b/samples/httpsession-redis-json/src/main/java/sample/Application.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package sample; import org.springframework.boot.SpringApplication; @@ -9,7 +24,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } } diff --git a/samples/httpsession-redis-json/src/main/java/sample/config/SecurityConfig.java b/samples/httpsession-redis-json/src/main/java/sample/config/SecurityConfig.java index 43b46ccc..ff865af2 100644 --- a/samples/httpsession-redis-json/src/main/java/sample/config/SecurityConfig.java +++ b/samples/httpsession-redis-json/src/main/java/sample/config/SecurityConfig.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package sample.config; import org.springframework.beans.factory.annotation.Autowired; @@ -12,25 +27,28 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { - @Override - protected void configure(HttpSecurity http) throws Exception { - http - .formLogin() - .loginPage("/login") - .permitAll() - .and() - .authorizeRequests() - .antMatchers("/resources/**").permitAll() - .anyRequest().authenticated() - .and() - .logout() - .permitAll(); - } + // @formatter:off + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/resources/**").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout().permitAll(); + } + // @formatter:on - @Autowired - public void configureGlobal(AuthenticationManagerBuilder builder) throws Exception { - builder - .inMemoryAuthentication() - .withUser("user").password("password").roles("USER"); - } + // @formatter:off + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } + // @formatter:on } diff --git a/samples/httpsession-redis-json/src/main/java/sample/config/SessionConfig.java b/samples/httpsession-redis-json/src/main/java/sample/config/SessionConfig.java index 2b4ce003..681f885a 100644 --- a/samples/httpsession-redis-json/src/main/java/sample/config/SessionConfig.java +++ b/samples/httpsession-redis-json/src/main/java/sample/config/SessionConfig.java @@ -1,6 +1,23 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package sample.config; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; @@ -12,26 +29,40 @@ import org.springframework.session.data.redis.config.annotation.web.http.EnableR * @author jitendra on 3/3/16. */ @EnableRedisHttpSession -public class SessionConfig { +public class SessionConfig implements BeanClassLoaderAware { - @Bean - public RedisSerializer springSessionDefaultRedisSerializer() { - return new GenericJackson2JsonRedisSerializer(objectMapper()); - } + private ClassLoader loader; - @Bean - public JedisConnectionFactory connectionFactory() { - return new JedisConnectionFactory(); - } + @Bean + public RedisSerializer springSessionDefaultRedisSerializer() { + return new GenericJackson2JsonRedisSerializer(objectMapper()); + } - /** - * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default constructors - * - * @return - */ - ObjectMapper objectMapper() { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModules(SecurityJacksonModules.getModules()); - return mapper; - } + @Bean + public JedisConnectionFactory connectionFactory() { + return new JedisConnectionFactory(); + } + + /** + * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default + * constructors + * + * @return the {@link ObjectMapper} to use + */ + ObjectMapper objectMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModules(SecurityJacksonModules.getModules(this.loader)); + return mapper; + } + + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang + * .ClassLoader) + */ + public void setBeanClassLoader(ClassLoader classLoader) { + this.loader = classLoader; + } } diff --git a/samples/httpsession-redis-json/src/main/java/sample/web/HomeController.java b/samples/httpsession-redis-json/src/main/java/sample/web/HomeController.java index f509dd70..6d0d5ee2 100644 --- a/samples/httpsession-redis-json/src/main/java/sample/web/HomeController.java +++ b/samples/httpsession-redis-json/src/main/java/sample/web/HomeController.java @@ -1,12 +1,26 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package sample.web; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; - import javax.servlet.http.HttpServletRequest; -import static org.springframework.util.ObjectUtils.isEmpty; +import org.springframework.stereotype.Controller; +import org.springframework.util.ObjectUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; /** * @author jitendra on 5/3/16. @@ -14,18 +28,18 @@ import static org.springframework.util.ObjectUtils.isEmpty; @Controller public class HomeController { - @RequestMapping("/") - public String home() { - return "home"; - } + @RequestMapping("/") + public String home() { + return "home"; + } - @RequestMapping("/setValue") - public String setValue(@RequestParam(name = "key", required = false) String key, - @RequestParam(name = "value", required = false) String value, - HttpServletRequest request) { - if (!isEmpty(key) && !isEmpty(value)) { - request.getSession().setAttribute(key, value); - } - return "home"; - } + @RequestMapping("/setValue") + public String setValue(@RequestParam(name = "key", required = false) String key, + @RequestParam(name = "value", required = false) String value, + HttpServletRequest request) { + if (!ObjectUtils.isEmpty(key) && !ObjectUtils.isEmpty(value)) { + request.getSession().setAttribute(key, value); + } + return "home"; + } } diff --git a/samples/httpsession-redis-json/src/main/java/sample/web/LoginController.java b/samples/httpsession-redis-json/src/main/java/sample/web/LoginController.java index 1a7c0d7c..9d99b9e0 100644 --- a/samples/httpsession-redis-json/src/main/java/sample/web/LoginController.java +++ b/samples/httpsession-redis-json/src/main/java/sample/web/LoginController.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package sample.web; import org.springframework.stereotype.Controller; @@ -9,8 +24,8 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginController { - @RequestMapping("/login") - public String login() { - return "login"; - } + @RequestMapping("/login") + public String login() { + return "login"; + } }