Blame view

src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java 2.94 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
5
6
7
8
  import org.junit.Before;
  import org.junit.Test;
  import org.springframework.validation.BeanPropertyBindingResult;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.servlet.ModelAndView;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
9
  import static org.junit.Assert.*;
2afad654   Julien Iguchi-Cartigny   deal with userForm
10
  import static org.mockito.Mockito.*;
2070653e   Julien Iguchi-Cartigny   Switching to spri...
11
  
aa193b2b   Julien Iguchi-Cartigny   First version
12
13
14
15
  public class NewUserControllerTest {
  
      private NewUserController newUserController;
      private BindingResult results;
2afad654   Julien Iguchi-Cartigny   deal with userForm
16
      private UserForm user;
8efec875   Julien Iguchi-Cartigny   switch to spring ...
17
      private UserRepository userRepository;
aa193b2b   Julien Iguchi-Cartigny   First version
18
      private UserSession userSession;
0399d385   Julien Iguchi-Cartigny   Adding user form
19
      private UserFormValidator userFormValidator;
2afad654   Julien Iguchi-Cartigny   deal with userForm
20
      private UserService userService;
aa193b2b   Julien Iguchi-Cartigny   First version
21
22
23
24
  
      @Before
      public void createInstances() {
          newUserController = new NewUserController();
2afad654   Julien Iguchi-Cartigny   deal with userForm
25
          user = new UserForm();
aa193b2b   Julien Iguchi-Cartigny   First version
26
          results = new BeanPropertyBindingResult(user, "user");
2070653e   Julien Iguchi-Cartigny   Switching to spri...
27
          userRepository = mock(UserRepository.class);
2070653e   Julien Iguchi-Cartigny   Switching to spri...
28
29
          User user = new User("admin", "password");
          when(userRepository.findByUsername("admin")).thenReturn(user);
8efec875   Julien Iguchi-Cartigny   switch to spring ...
30
          newUserController.setUserRepository(userRepository);
2afad654   Julien Iguchi-Cartigny   deal with userForm
31
32
          userService = mock(UserService.class);
          newUserController.setUserService(userService);
aa193b2b   Julien Iguchi-Cartigny   First version
33
34
35
          userSession = new UserSession();
          userSession.setUsername("admin");
          newUserController.setUserSession(userSession);
0399d385   Julien Iguchi-Cartigny   Adding user form
36
37
          userFormValidator = new UserFormValidator();
          newUserController.setUserFormValidator(userFormValidator);
aa193b2b   Julien Iguchi-Cartigny   First version
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      }
  
      @Test
      public void testGetNewUserForm() {
          ModelAndView mav = newUserController.getNewUserForm();
          assertEquals("newUser", mav.getViewName());
          assertEquals(1, mav.getModelMap().size());
          assertTrue(mav.getModel().containsKey("user"));
          assertTrue(mav.getModel().get("user") instanceof User);
      }
  
      @Test
      public void testPostNewUserSucceed() {
          user.setUsername("abc");
2afad654   Julien Iguchi-Cartigny   deal with userForm
52
          user.setPassword("abcD#");
aa193b2b   Julien Iguchi-Cartigny   First version
53
54
          String view = newUserController.postNewUser(user, results);
          assertFalse(results.hasErrors());
2afad654   Julien Iguchi-Cartigny   deal with userForm
55
56
          assertEquals("redirect:/", view);
          verify(userService).createUser("abc", "abcD#");
aa193b2b   Julien Iguchi-Cartigny   First version
57
58
59
60
61
      }
  
      @Test
      public void testPostNewUserFailedNotAdmin() {
          user.setUsername("abc");
2afad654   Julien Iguchi-Cartigny   deal with userForm
62
          user.setPassword("abcD#");
aa193b2b   Julien Iguchi-Cartigny   First version
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
          userSession.setUsername("blabla");
          String view = newUserController.postNewUser(user, results);
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  
      @Test
      public void testPostNewUserFailedValidate() {
          user.setUsername("a");
          user.setPassword("abc");
          String view = newUserController.postNewUser(user, results);
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  
      @Test
      public void testPostNewUserFailedAlreadyPresent() {
          user.setUsername("admin");
          user.setPassword("blabla");
          String view = newUserController.postNewUser(user, results);
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  }