Blame view

src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java 2.51 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  package fr.plil.sio.web.mvc;
  
  import static org.junit.Assert.*;
  import org.junit.Before;
  import org.junit.Test;
  import org.springframework.validation.BeanPropertyBindingResult;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.servlet.ModelAndView;
  
  public class NewUserControllerTest {
  
      private NewUserController newUserController;
      private BindingResult results;
      private User user;
      private UserDao userDao;
      private UserSession userSession;
      private UserValidator userValidator;
  
      @Before
      public void createInstances() {
          newUserController = new NewUserController();
          user = new User();
          results = new BeanPropertyBindingResult(user, "user");
          userDao = new UserDaoImpl();
          newUserController.setUserDao(userDao);
          userSession = new UserSession();
          userSession.setUsername("admin");
          newUserController.setUserSession(userSession);
          userValidator = new UserValidator();
          newUserController.setUserValidator(userValidator);
      }
  
      @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");
          user.setPassword("abc");
          String view = newUserController.postNewUser(user, results);
          assertFalse(results.hasErrors());
          assertEquals("redirect:/",view);
      }
  
      @Test
      public void testPostNewUserFailedNotAdmin() {
          user.setUsername("abc");
          user.setPassword("abc");
          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);
      }
  }