Commit 2070653e09ba1fa6b85c7afb3df7a620d1c8f75e

Authored by Julien Iguchi-Cartigny
Committed by Julien Iguchi-Cartigny
1 parent 8efec875

Switching to spring-data-jpa

1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion> 4 <modelVersion>4.0.0</modelVersion>
5 5
@@ -27,6 +27,10 @@ @@ -27,6 +27,10 @@
27 <dependencies> 27 <dependencies>
28 <dependency> 28 <dependency>
29 <groupId>org.springframework.boot</groupId> 29 <groupId>org.springframework.boot</groupId>
  30 + <artifactId>spring-boot-starter-data-jpa</artifactId>
  31 + </dependency>
  32 + <dependency>
  33 + <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-data-rest</artifactId> 34 <artifactId>spring-boot-starter-data-rest</artifactId>
31 </dependency> 35 </dependency>
32 <dependency> 36 <dependency>
@@ -34,11 +38,6 @@ @@ -34,11 +38,6 @@
34 <artifactId>spring-boot-starter-web</artifactId> 38 <artifactId>spring-boot-starter-web</artifactId>
35 </dependency> 39 </dependency>
36 <dependency> 40 <dependency>
37 - <groupId>com.h2database</groupId>  
38 - <artifactId>h2</artifactId>  
39 - <scope>runtime</scope>  
40 - </dependency>  
41 - <dependency>  
42 <groupId>org.springframework.boot</groupId> 41 <groupId>org.springframework.boot</groupId>
43 <artifactId>spring-boot-starter-tomcat</artifactId> 42 <artifactId>spring-boot-starter-tomcat</artifactId>
44 <scope>provided</scope> 43 <scope>provided</scope>
@@ -57,6 +56,11 @@ @@ -57,6 +56,11 @@
57 <groupId>javax.servlet</groupId> 56 <groupId>javax.servlet</groupId>
58 <artifactId>jstl</artifactId> 57 <artifactId>jstl</artifactId>
59 </dependency> 58 </dependency>
  59 + <dependency>
  60 + <groupId>com.h2database</groupId>
  61 + <artifactId>h2</artifactId>
  62 + <scope>runtime</scope>
  63 + </dependency>
60 </dependencies> 64 </dependencies>
61 65
62 <build> 66 <build>
src/main/java/fr/plil/sio/web/mvc/Application.java
@@ -6,10 +6,12 @@ import org.springframework.boot.builder.SpringApplicationBuilder; @@ -6,10 +6,12 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
6 import org.springframework.boot.context.web.SpringBootServletInitializer; 6 import org.springframework.boot.context.web.SpringBootServletInitializer;
7 import org.springframework.context.annotation.ComponentScan; 7 import org.springframework.context.annotation.ComponentScan;
8 import org.springframework.context.annotation.Configuration; 8 import org.springframework.context.annotation.Configuration;
  9 +import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9 10
10 @EnableAutoConfiguration 11 @EnableAutoConfiguration
11 @Configuration 12 @Configuration
12 @ComponentScan 13 @ComponentScan
  14 +@EnableJpaRepositories
13 public class Application extends SpringBootServletInitializer { 15 public class Application extends SpringBootServletInitializer {
14 16
15 public static void main(String[] args) { 17 public static void main(String[] args) {
src/main/java/fr/plil/sio/web/mvc/CheckUserInterceptor.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import java.io.IOException;  
4 -import javax.annotation.Resource;  
5 -import javax.servlet.http.HttpServletRequest;  
6 -import javax.servlet.http.HttpServletResponse;  
7 import org.slf4j.Logger; 3 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 4 import org.slf4j.LoggerFactory;
9 -import org.springframework.beans.factory.annotation.Autowired;  
10 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 5 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
11 6
  7 +import javax.annotation.Resource;
  8 +import javax.servlet.http.HttpServletRequest;
  9 +import javax.servlet.http.HttpServletResponse;
  10 +import java.io.IOException;
  11 +
12 public class CheckUserInterceptor extends HandlerInterceptorAdapter { 12 public class CheckUserInterceptor extends HandlerInterceptorAdapter {
13 13
14 private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class); 14 private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class);
15 15
16 - @Autowired 16 + @Resource
17 private UserSession userSession; 17 private UserSession userSession;
18 18
19 @Override 19 @Override
src/main/java/fr/plil/sio/web/mvc/LoginController.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import javax.annotation.Resource;  
4 import org.springframework.stereotype.Controller; 3 import org.springframework.stereotype.Controller;
5 import org.springframework.validation.BindingResult; 4 import org.springframework.validation.BindingResult;
6 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RequestMethod;
8 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.ModelAndView;
9 8
  9 +import javax.annotation.Resource;
  10 +
10 @Controller 11 @Controller
11 @RequestMapping(value = "/login") 12 @RequestMapping(value = "/login")
12 public class LoginController { 13 public class LoginController {
@@ -25,7 +26,7 @@ public class LoginController { @@ -25,7 +26,7 @@ public class LoginController {
25 @RequestMapping(method = RequestMethod.POST) 26 @RequestMapping(method = RequestMethod.POST)
26 public String postLoginCheck(User user, BindingResult result) { 27 public String postLoginCheck(User user, BindingResult result) {
27 28
28 - User userFromDao = userRepository.getFromUsername(user.getUsername()); 29 + User userFromDao = userRepository.findByUsername(user.getUsername());
29 30
30 if (userFromDao == null) { 31 if (userFromDao == null) {
31 result.rejectValue("username","login.form.invalid"); 32 result.rejectValue("username","login.form.invalid");
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import javax.annotation.Resource;  
4 import org.springframework.stereotype.Controller; 3 import org.springframework.stereotype.Controller;
5 import org.springframework.validation.BindingResult; 4 import org.springframework.validation.BindingResult;
6 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RequestMethod;
8 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.ModelAndView;
9 8
  9 +import javax.annotation.Resource;
  10 +
10 @Controller 11 @Controller
11 public class NewUserController { 12 public class NewUserController {
12 13
@@ -33,7 +34,7 @@ public class NewUserController { @@ -33,7 +34,7 @@ public class NewUserController {
33 34
34 userValidator.validate(user, result); 35 userValidator.validate(user, result);
35 36
36 - boolean present = (userRepository.getFromUsername(user.getUsername()) != null); 37 + boolean present = (userRepository.findByUsername(user.getUsername()) != null);
37 38
38 if (present) { 39 if (present) {
39 result.rejectValue("username", "new.user.form.present"); 40 result.rejectValue("username", "new.user.form.present");
src/main/java/fr/plil/sio/web/mvc/User.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -public class User {  
4 3
  4 +import javax.persistence.*;
  5 +import java.io.Serializable;
  6 +
  7 +@Entity
  8 +public class User implements Serializable {
  9 +
  10 + @Id
  11 + @GeneratedValue(strategy = GenerationType.AUTO)
  12 + private Long id;
  13 +
  14 + @Column
5 private String username; 15 private String username;
  16 +
  17 + @Column
6 private String password; 18 private String password;
7 19
  20 + public User() {
  21 + }
  22 +
  23 + public User(String username, String password) {
  24 + this.username = username;
  25 + this.password = password;
  26 + }
  27 +
8 public String getUsername() { 28 public String getUsername() {
9 return username; 29 return username;
10 } 30 }
src/main/java/fr/plil/sio/web/mvc/UserRepository.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import java.util.Set; 3 +import org.springframework.data.jpa.repository.JpaRepository;
4 4
5 -public interface UserRepository { 5 +public interface UserRepository extends JpaRepository<User, Long> {
6 6
7 - boolean save(User user);  
8 -  
9 - User getFromUsername(String username);  
10 -  
11 - Set<User> getAllUsers();  
12 -  
13 - boolean update(User user);  
14 -  
15 - boolean delete(User user); 7 + User findByUsername(String username);
16 } 8 }
src/main/java/fr/plil/sio/web/mvc/UserRepositoryImpl.java deleted
@@ -1,48 +0,0 @@ @@ -1,48 +0,0 @@
1 -package fr.plil.sio.web.mvc;  
2 -  
3 -import java.util.HashSet;  
4 -import java.util.Set;  
5 -import org.springframework.stereotype.Repository;  
6 -  
7 -@Repository(value = "userDao")  
8 -public class UserRepositoryImpl implements UserRepository {  
9 -  
10 - private Set<User> users = new HashSet<User>();  
11 -  
12 - public UserRepositoryImpl() {  
13 - User user = new User();  
14 - user.setUsername("admin");  
15 - user.setPassword("admin");  
16 - users.add(user);  
17 - }  
18 -  
19 - @Override  
20 - public boolean save(User user) {  
21 - return users.add(user);  
22 - }  
23 -  
24 - @Override  
25 - public User getFromUsername(String username) {  
26 - for (User user : users) {  
27 - if (user.getUsername().equals(username)) {  
28 - return user;  
29 - }  
30 - }  
31 - return null;  
32 - }  
33 -  
34 - @Override  
35 - public Set<User> getAllUsers() {  
36 - return users;  
37 - }  
38 -  
39 - @Override  
40 - public boolean update(User user) {  
41 - throw new UnsupportedOperationException("Not supported.");  
42 - }  
43 -  
44 - @Override  
45 - public boolean delete(User user) {  
46 - throw new UnsupportedOperationException("Not supported.");  
47 - }  
48 -}  
src/main/java/fr/plil/sio/web/mvc/ViewUsersController.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import java.util.Set;  
4 -import javax.annotation.Resource;  
5 import org.springframework.stereotype.Controller; 3 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.ModelAttribute; 4 import org.springframework.web.bind.annotation.ModelAttribute;
7 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RequestMethod;
9 7
  8 +import javax.annotation.Resource;
  9 +import java.util.List;
  10 +
10 @Controller 11 @Controller
11 public class ViewUsersController { 12 public class ViewUsersController {
12 13
@@ -17,8 +18,8 @@ public class ViewUsersController { @@ -17,8 +18,8 @@ public class ViewUsersController {
17 private UserSession userSession; 18 private UserSession userSession;
18 19
19 @ModelAttribute("users") 20 @ModelAttribute("users")
20 - public Set<User> populateUsers() {  
21 - return userRepository.getAllUsers(); 21 + public List<User> populateUsers() {
  22 + return userRepository.findAll();
22 } 23 }
23 24
24 @ModelAttribute("userSession") 25 @ModelAttribute("userSession")
src/main/resources/application.properties
1 logging.level.fr.plil.sio.web.mvc=DEBUG 1 logging.level.fr.plil.sio.web.mvc=DEBUG
  2 +logging.level.org.hibernate=DEBUG
  3 +
  4 +spring.jpa.hibernate.ddl-auto=create-drop
2 5
3 spring.view.prefix=/WEB-INF/views/ 6 spring.view.prefix=/WEB-INF/views/
4 spring.view.suffix=.jsp 7 spring.view.suffix=.jsp
src/main/resources/data.sql 0 โ†’ 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +INSERT INTO USER (username,password) VALUES ('admin','admin')
0 \ No newline at end of file 2 \ No newline at end of file
src/test/java/fr/plil/sio/web/mvc/LoginControllerTest.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import static org.junit.Assert.*;  
4 import org.junit.Before; 3 import org.junit.Before;
5 import org.junit.Test; 4 import org.junit.Test;
6 import org.springframework.validation.BeanPropertyBindingResult; 5 import org.springframework.validation.BeanPropertyBindingResult;
7 import org.springframework.validation.BindingResult; 6 import org.springframework.validation.BindingResult;
8 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.ModelAndView;
9 8
  9 +import static org.junit.Assert.*;
  10 +import static org.mockito.Mockito.mock;
  11 +import static org.mockito.Mockito.when;
  12 +
10 public class LoginControllerTest { 13 public class LoginControllerTest {
11 14
12 private LoginController loginController; 15 private LoginController loginController;
@@ -20,7 +23,8 @@ public class LoginControllerTest { @@ -20,7 +23,8 @@ public class LoginControllerTest {
20 loginController = new LoginController(); 23 loginController = new LoginController();
21 user = new User(); 24 user = new User();
22 results = new BeanPropertyBindingResult(user, "user"); 25 results = new BeanPropertyBindingResult(user, "user");
23 - userRepository = new UserRepositoryImpl(); 26 + userRepository = mock(UserRepository.class);
  27 + when(userRepository.findByUsername("admin")).thenReturn(new User("admin", "admin"));
24 loginController.setUserRepository(userRepository); 28 loginController.setUserRepository(userRepository);
25 userSession = new UserSession(); 29 userSession = new UserSession();
26 loginController.setUserSession(userSession); 30 loginController.setUserSession(userSession);
src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import static org.junit.Assert.*;  
4 import org.junit.Before; 3 import org.junit.Before;
5 import org.junit.Test; 4 import org.junit.Test;
6 import org.springframework.validation.BeanPropertyBindingResult; 5 import org.springframework.validation.BeanPropertyBindingResult;
7 import org.springframework.validation.BindingResult; 6 import org.springframework.validation.BindingResult;
8 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.ModelAndView;
9 8
  9 +import static org.junit.Assert.*;
  10 +import static org.mockito.Mockito.mock;
  11 +import static org.mockito.Mockito.when;
  12 +
10 public class NewUserControllerTest { 13 public class NewUserControllerTest {
11 14
12 private NewUserController newUserController; 15 private NewUserController newUserController;
@@ -21,7 +24,11 @@ public class NewUserControllerTest { @@ -21,7 +24,11 @@ public class NewUserControllerTest {
21 newUserController = new NewUserController(); 24 newUserController = new NewUserController();
22 user = new User(); 25 user = new User();
23 results = new BeanPropertyBindingResult(user, "user"); 26 results = new BeanPropertyBindingResult(user, "user");
24 - userRepository = new UserRepositoryImpl(); 27 + userRepository = mock(UserRepository.class);
  28 +
  29 + User user = new User("admin", "password");
  30 + when(userRepository.findByUsername("admin")).thenReturn(user);
  31 +
25 newUserController.setUserRepository(userRepository); 32 newUserController.setUserRepository(userRepository);
26 userSession = new UserSession(); 33 userSession = new UserSession();
27 userSession.setUsername("admin"); 34 userSession.setUsername("admin");
src/test/java/fr/plil/sio/web/mvc/UserRepositoryTest.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import static org.junit.Assert.*;  
4 -import org.junit.Before;  
5 import org.junit.Test; 3 import org.junit.Test;
  4 +import org.junit.runner.RunWith;
  5 +import org.springframework.boot.test.SpringApplicationConfiguration;
  6 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7 +import org.springframework.test.context.transaction.TransactionConfiguration;
  8 +import org.springframework.transaction.annotation.Transactional;
6 9
  10 +import javax.annotation.Resource;
  11 +
  12 +import static org.junit.Assert.assertEquals;
  13 +import static org.junit.Assert.assertNotNull;
  14 +
  15 +@RunWith(SpringJUnit4ClassRunner.class)
  16 +@SpringApplicationConfiguration(classes = Application.class)
  17 +@Transactional
  18 +@TransactionConfiguration
7 public class UserRepositoryTest { 19 public class UserRepositoryTest {
8 20
  21 + @Resource
9 private UserRepository userRepository; 22 private UserRepository userRepository;
10 23
11 - @Before  
12 - public void createInstances() {  
13 - userRepository = new UserRepositoryImpl();  
14 - }  
15 -  
16 @Test 24 @Test
17 public void testAdminPresent() { 25 public void testAdminPresent() {
18 - assertEquals(1, userRepository.getAllUsers().size());  
19 - assertNotNull(userRepository.getFromUsername("admin"));  
20 - assertEquals("admin", userRepository.getFromUsername("admin").getUsername());  
21 - }  
22 -  
23 - @Test  
24 - public void testSaveSucceed() {  
25 - User user = new User();  
26 - user.setUsername("abc");  
27 - user.setPassword("abc");  
28 - assertTrue(userRepository.save(user));  
29 - }  
30 -  
31 - @Test  
32 - public void testSaveFailed() {  
33 - User user = new User();  
34 - user.setUsername("admin");  
35 - user.setPassword("abc");  
36 - assertFalse(userRepository.save(user));  
37 - }  
38 -  
39 - @Test  
40 - public void testGetFromUsernameFound() {  
41 - User user = new User();  
42 - user.setUsername("abc");  
43 - user.setPassword("abc");  
44 - userRepository.save(user);  
45 - assertNotNull(userRepository.getFromUsername("admin"));  
46 - assertEquals("admin", userRepository.getFromUsername("admin").getUsername());  
47 - }  
48 -  
49 - @Test  
50 - public void testGetFromUsernameNotFound() {  
51 - assertNull(userRepository.getFromUsername("abc"));  
52 - }  
53 -  
54 - @Test  
55 - public void testGetAllUsers() {  
56 - assertEquals(1, userRepository.getAllUsers().size());  
57 - User user = new User();  
58 - user.setUsername("abc");  
59 - user.setPassword("abc");  
60 - assertTrue(userRepository.save(user));  
61 - assertEquals(2, userRepository.getAllUsers().size()); 26 + assertEquals(1, userRepository.findAll().size());
  27 + assertNotNull(userRepository.findByUsername("admin"));
  28 + assertEquals("admin", userRepository.findByUsername("admin").getUsername());
62 } 29 }
63 } 30 }
64 \ No newline at end of file 31 \ No newline at end of file
src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 -import static org.junit.Assert.*;  
4 import org.junit.Before; 3 import org.junit.Before;
5 import org.junit.Test; 4 import org.junit.Test;
6 5
  6 +import java.util.LinkedList;
  7 +import java.util.List;
  8 +
  9 +import static org.junit.Assert.assertEquals;
  10 +import static org.mockito.Mockito.mock;
  11 +import static org.mockito.Mockito.when;
  12 +
7 public class ViewUsersControllerTest { 13 public class ViewUsersControllerTest {
8 14
9 private ViewUsersController viewUsersController; 15 private ViewUsersController viewUsersController;
@@ -13,7 +19,10 @@ public class ViewUsersControllerTest { @@ -13,7 +19,10 @@ public class ViewUsersControllerTest {
13 @Before 19 @Before
14 public void createInstances() { 20 public void createInstances() {
15 viewUsersController = new ViewUsersController(); 21 viewUsersController = new ViewUsersController();
16 - userRepository = new UserRepositoryImpl(); 22 + userRepository = mock(UserRepository.class);
  23 + List<User> users = new LinkedList<>();
  24 + users.add(new User("admin", "password"));
  25 + when(userRepository.findAll()).thenReturn(users);
17 viewUsersController.setUserRepository(userRepository); 26 viewUsersController.setUserRepository(userRepository);
18 userSession = new UserSession(); 27 userSession = new UserSession();
19 userSession.setUsername("admin"); 28 userSession.setUsername("admin");