Commit 2070653e09ba1fa6b85c7afb3df7a620d1c8f75e
Committed by
Julien Iguchi-Cartigny
1 parent
8efec875
Switching to spring-data-jpa
Showing
15 changed files
with
101 additions
and
137 deletions
Show diff stats
pom.xml
1 | 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 | 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
4 | 4 | <modelVersion>4.0.0</modelVersion> |
5 | 5 | |
... | ... | @@ -27,6 +27,10 @@ |
27 | 27 | <dependencies> |
28 | 28 | <dependency> |
29 | 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 | 34 | <artifactId>spring-boot-starter-data-rest</artifactId> |
31 | 35 | </dependency> |
32 | 36 | <dependency> |
... | ... | @@ -34,11 +38,6 @@ |
34 | 38 | <artifactId>spring-boot-starter-web</artifactId> |
35 | 39 | </dependency> |
36 | 40 | <dependency> |
37 | - <groupId>com.h2database</groupId> | |
38 | - <artifactId>h2</artifactId> | |
39 | - <scope>runtime</scope> | |
40 | - </dependency> | |
41 | - <dependency> | |
42 | 41 | <groupId>org.springframework.boot</groupId> |
43 | 42 | <artifactId>spring-boot-starter-tomcat</artifactId> |
44 | 43 | <scope>provided</scope> |
... | ... | @@ -57,6 +56,11 @@ |
57 | 56 | <groupId>javax.servlet</groupId> |
58 | 57 | <artifactId>jstl</artifactId> |
59 | 58 | </dependency> |
59 | + <dependency> | |
60 | + <groupId>com.h2database</groupId> | |
61 | + <artifactId>h2</artifactId> | |
62 | + <scope>runtime</scope> | |
63 | + </dependency> | |
60 | 64 | </dependencies> |
61 | 65 | |
62 | 66 | <build> | ... | ... |
src/main/java/fr/plil/sio/web/mvc/Application.java
... | ... | @@ -6,10 +6,12 @@ import org.springframework.boot.builder.SpringApplicationBuilder; |
6 | 6 | import org.springframework.boot.context.web.SpringBootServletInitializer; |
7 | 7 | import org.springframework.context.annotation.ComponentScan; |
8 | 8 | import org.springframework.context.annotation.Configuration; |
9 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
9 | 10 | |
10 | 11 | @EnableAutoConfiguration |
11 | 12 | @Configuration |
12 | 13 | @ComponentScan |
14 | +@EnableJpaRepositories | |
13 | 15 | public class Application extends SpringBootServletInitializer { |
14 | 16 | |
15 | 17 | public static void main(String[] args) { | ... | ... |
src/main/java/fr/plil/sio/web/mvc/CheckUserInterceptor.java
1 | 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 | 3 | import org.slf4j.Logger; |
8 | 4 | import org.slf4j.LoggerFactory; |
9 | -import org.springframework.beans.factory.annotation.Autowired; | |
10 | 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 | 12 | public class CheckUserInterceptor extends HandlerInterceptorAdapter { |
13 | 13 | |
14 | 14 | private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class); |
15 | 15 | |
16 | - @Autowired | |
16 | + @Resource | |
17 | 17 | private UserSession userSession; |
18 | 18 | |
19 | 19 | @Override | ... | ... |
src/main/java/fr/plil/sio/web/mvc/LoginController.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import javax.annotation.Resource; | |
4 | 3 | import org.springframework.stereotype.Controller; |
5 | 4 | import org.springframework.validation.BindingResult; |
6 | 5 | import org.springframework.web.bind.annotation.RequestMapping; |
7 | 6 | import org.springframework.web.bind.annotation.RequestMethod; |
8 | 7 | import org.springframework.web.servlet.ModelAndView; |
9 | 8 | |
9 | +import javax.annotation.Resource; | |
10 | + | |
10 | 11 | @Controller |
11 | 12 | @RequestMapping(value = "/login") |
12 | 13 | public class LoginController { |
... | ... | @@ -25,7 +26,7 @@ public class LoginController { |
25 | 26 | @RequestMapping(method = RequestMethod.POST) |
26 | 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 | 31 | if (userFromDao == null) { |
31 | 32 | result.rejectValue("username","login.form.invalid"); | ... | ... |
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import javax.annotation.Resource; | |
4 | 3 | import org.springframework.stereotype.Controller; |
5 | 4 | import org.springframework.validation.BindingResult; |
6 | 5 | import org.springframework.web.bind.annotation.RequestMapping; |
7 | 6 | import org.springframework.web.bind.annotation.RequestMethod; |
8 | 7 | import org.springframework.web.servlet.ModelAndView; |
9 | 8 | |
9 | +import javax.annotation.Resource; | |
10 | + | |
10 | 11 | @Controller |
11 | 12 | public class NewUserController { |
12 | 13 | |
... | ... | @@ -33,7 +34,7 @@ public class NewUserController { |
33 | 34 | |
34 | 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 | 39 | if (present) { |
39 | 40 | result.rejectValue("username", "new.user.form.present"); | ... | ... |
src/main/java/fr/plil/sio/web/mvc/User.java
1 | 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 | 15 | private String username; |
16 | + | |
17 | + @Column | |
6 | 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 | 28 | public String getUsername() { |
9 | 29 | return username; |
10 | 30 | } | ... | ... |
src/main/java/fr/plil/sio/web/mvc/UserRepository.java
1 | 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 | -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 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import java.util.Set; | |
4 | -import javax.annotation.Resource; | |
5 | 3 | import org.springframework.stereotype.Controller; |
6 | 4 | import org.springframework.web.bind.annotation.ModelAttribute; |
7 | 5 | import org.springframework.web.bind.annotation.RequestMapping; |
8 | 6 | import org.springframework.web.bind.annotation.RequestMethod; |
9 | 7 | |
8 | +import javax.annotation.Resource; | |
9 | +import java.util.List; | |
10 | + | |
10 | 11 | @Controller |
11 | 12 | public class ViewUsersController { |
12 | 13 | |
... | ... | @@ -17,8 +18,8 @@ public class ViewUsersController { |
17 | 18 | private UserSession userSession; |
18 | 19 | |
19 | 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 | 25 | @ModelAttribute("userSession") | ... | ... |
src/main/resources/application.properties
src/test/java/fr/plil/sio/web/mvc/LoginControllerTest.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import static org.junit.Assert.*; | |
4 | 3 | import org.junit.Before; |
5 | 4 | import org.junit.Test; |
6 | 5 | import org.springframework.validation.BeanPropertyBindingResult; |
7 | 6 | import org.springframework.validation.BindingResult; |
8 | 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 | 13 | public class LoginControllerTest { |
11 | 14 | |
12 | 15 | private LoginController loginController; |
... | ... | @@ -20,7 +23,8 @@ public class LoginControllerTest { |
20 | 23 | loginController = new LoginController(); |
21 | 24 | user = new User(); |
22 | 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 | 28 | loginController.setUserRepository(userRepository); |
25 | 29 | userSession = new UserSession(); |
26 | 30 | loginController.setUserSession(userSession); | ... | ... |
src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import static org.junit.Assert.*; | |
4 | 3 | import org.junit.Before; |
5 | 4 | import org.junit.Test; |
6 | 5 | import org.springframework.validation.BeanPropertyBindingResult; |
7 | 6 | import org.springframework.validation.BindingResult; |
8 | 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 | 13 | public class NewUserControllerTest { |
11 | 14 | |
12 | 15 | private NewUserController newUserController; |
... | ... | @@ -21,7 +24,11 @@ public class NewUserControllerTest { |
21 | 24 | newUserController = new NewUserController(); |
22 | 25 | user = new User(); |
23 | 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 | 32 | newUserController.setUserRepository(userRepository); |
26 | 33 | userSession = new UserSession(); |
27 | 34 | userSession.setUsername("admin"); | ... | ... |
src/test/java/fr/plil/sio/web/mvc/UserRepositoryTest.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import static org.junit.Assert.*; | |
4 | -import org.junit.Before; | |
5 | 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 | 19 | public class UserRepositoryTest { |
8 | 20 | |
21 | + @Resource | |
9 | 22 | private UserRepository userRepository; |
10 | 23 | |
11 | - @Before | |
12 | - public void createInstances() { | |
13 | - userRepository = new UserRepositoryImpl(); | |
14 | - } | |
15 | - | |
16 | 24 | @Test |
17 | 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 | 31 | \ No newline at end of file | ... | ... |
src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java
1 | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | |
3 | -import static org.junit.Assert.*; | |
4 | 3 | import org.junit.Before; |
5 | 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 | 13 | public class ViewUsersControllerTest { |
8 | 14 | |
9 | 15 | private ViewUsersController viewUsersController; |
... | ... | @@ -13,7 +19,10 @@ public class ViewUsersControllerTest { |
13 | 19 | @Before |
14 | 20 | public void createInstances() { |
15 | 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 | 26 | viewUsersController.setUserRepository(userRepository); |
18 | 27 | userSession = new UserSession(); |
19 | 28 | userSession.setUsername("admin"); | ... | ... |