Blame view

src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java 3.74 KB
a800fde0   jcartign   First version, in...
1
2
  package fr.plil.sio.persistence.jdbc;
  
292aec6f   jcartign   First version bef...
3
4
  import fr.plil.sio.persistence.api.*;
  import org.junit.Before;
a800fde0   jcartign   First version, in...
5
6
  import org.junit.Test;
  import org.junit.runner.RunWith;
292aec6f   jcartign   First version bef...
7
  import org.springframework.beans.factory.annotation.Autowired;
a800fde0   jcartign   First version, in...
8
9
10
  import org.springframework.boot.test.SpringApplicationConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
292aec6f   jcartign   First version bef...
11
12
  import static org.junit.Assert.*;
  
a800fde0   jcartign   First version, in...
13
14
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = JdbcApplication.class)
292aec6f   jcartign   First version bef...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  public class UserServiceTest extends AbstractServiceSupport {
  
      @Autowired
      private GroupService groupService;
  
      @Autowired
      private UserService userService;
  
      @Autowired
      private RightService rightService;
  
      @Before
      public void before() {
          groupService.create("group");
          userService.create("user", "group");
e1ed8749   Rémi Vangrevelynghe   save before merge
30
31
32
33
  //        Right right = rightService.create("parent");
  //        rightService.create("sibling", right);
  //        groupService.addRight("group", right);
  //        rightService.create("not-for-me");
292aec6f   jcartign   First version bef...
34
      }
a800fde0   jcartign   First version, in...
35
  
a800fde0   jcartign   First version, in...
36
37
      @Test
      public void testCreateAndFindByName() {
292aec6f   jcartign   First version bef...
38
39
40
41
42
43
          User user = userService.findByName("user");
          assertNotNull(user);
          assertEquals("user", user.getName());
          Group group = user.getGroup();
          assertNotNull(group);
          assertEquals("group", group.getName());
bda41d02   jcartign   All tests method ...
44
45
46
47
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateFailsWhenNameNull() {
292aec6f   jcartign   First version bef...
48
          userService.create(null, "group");
bda41d02   jcartign   All tests method ...
49
50
51
52
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateFailsWhenGroupNameNull() {
292aec6f   jcartign   First version bef...
53
          userService.create("user", null);
bda41d02   jcartign   All tests method ...
54
55
56
57
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateFailsWhenGroupDoesNotExist() {
292aec6f   jcartign   First version bef...
58
          userService.create("user", "notGroup");
bda41d02   jcartign   All tests method ...
59
60
61
62
      }
  
      @Test(expected = IllegalStateException.class)
      public void testCreateFailsWhenSameNameUserAlreadyPresent() {
292aec6f   jcartign   First version bef...
63
          userService.create("user", "group");
bda41d02   jcartign   All tests method ...
64
65
      }
  
e1ed8749   Rémi Vangrevelynghe   save before merge
66
      @Test
bda41d02   jcartign   All tests method ...
67
      public void testDeleteUser() {
292aec6f   jcartign   First version bef...
68
          assertTrue(userService.delete("user"));
bda41d02   jcartign   All tests method ...
69
70
      }
  
e1ed8749   Rémi Vangrevelynghe   save before merge
71
      @Test
bda41d02   jcartign   All tests method ...
72
      public void testDeleteUserIfNotFound() {
292aec6f   jcartign   First version bef...
73
          assertFalse(userService.delete("user"));
bda41d02   jcartign   All tests method ...
74
75
76
77
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteUserFailsIfNameNull() {
292aec6f   jcartign   First version bef...
78
          userService.delete(null);
bda41d02   jcartign   All tests method ...
79
80
81
      }
  
      public void testFindUserByNameIfUserNotFound() {
292aec6f   jcartign   First version bef...
82
          assertNull(userService.findByName("blabla"));
bda41d02   jcartign   All tests method ...
83
84
85
86
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindUserByNameFailsIfNameNull() {
292aec6f   jcartign   First version bef...
87
          assertNull(userService.findByName(null));
bda41d02   jcartign   All tests method ...
88
89
90
      }
  
      public void testIsUserHasExactRight() {
292aec6f   jcartign   First version bef...
91
92
          Right right = rightService.findByName("parent").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
93
94
95
      }
  
      public void testIsUserHasRightByParents() {
292aec6f   jcartign   First version bef...
96
97
          Right right = rightService.findByName("sibling").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
98
99
100
      }
  
      public void testIsUserHasNotTheExactRight() {
292aec6f   jcartign   First version bef...
101
102
          Right right = rightService.findByName("not-for-me").get(0);
          assertFalse(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
103
104
105
106
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenUsernameNull() {
292aec6f   jcartign   First version bef...
107
108
          Right right = rightService.findByName("parent").get(0);
          userService.isUserHasRight(null, right);
bda41d02   jcartign   All tests method ...
109
110
111
112
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNull() {
292aec6f   jcartign   First version bef...
113
          userService.isUserHasRight("user", null);
bda41d02   jcartign   All tests method ...
114
115
116
117
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNotInDatabase() {
292aec6f   jcartign   First version bef...
118
119
120
          Right right = new Right();
          right.setName("dummy");
          userService.isUserHasRight("user", right);
a800fde0   jcartign   First version, in...
121
122
      }
  }