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;
5b26096d   jcartign   Switch to @Spring...
8
  import org.springframework.boot.test.context.SpringBootTest;
a800fde0   jcartign   First version, in...
9
10
  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
  @RunWith(SpringJUnit4ClassRunner.class)
29f04eb8   jcartign   Parameter "classe...
14
  @SpringBootTest
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");
fa02d31b   rvangrev   Commit avant le m...
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() {
fa02d31b   rvangrev   Commit avant le m...
73
          userService.delete("user");
292aec6f   jcartign   First version bef...
74
          assertFalse(userService.delete("user"));
bda41d02   jcartign   All tests method ...
75
76
77
78
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteUserFailsIfNameNull() {
292aec6f   jcartign   First version bef...
79
          userService.delete(null);
bda41d02   jcartign   All tests method ...
80
81
82
      }
  
      public void testFindUserByNameIfUserNotFound() {
292aec6f   jcartign   First version bef...
83
          assertNull(userService.findByName("blabla"));
bda41d02   jcartign   All tests method ...
84
85
86
87
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindUserByNameFailsIfNameNull() {
292aec6f   jcartign   First version bef...
88
          assertNull(userService.findByName(null));
bda41d02   jcartign   All tests method ...
89
90
      }
  
77b66887   jcartign   Forgot some @Test...
91
      @Test
bda41d02   jcartign   All tests method ...
92
      public void testIsUserHasExactRight() {
292aec6f   jcartign   First version bef...
93
94
          Right right = rightService.findByName("parent").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
95
96
      }
  
77b66887   jcartign   Forgot some @Test...
97
      @Test
bda41d02   jcartign   All tests method ...
98
      public void testIsUserHasRightByParents() {
292aec6f   jcartign   First version bef...
99
100
          Right right = rightService.findByName("sibling").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
101
102
      }
  
77b66887   jcartign   Forgot some @Test...
103
      @Test
bda41d02   jcartign   All tests method ...
104
      public void testIsUserHasNotTheExactRight() {
292aec6f   jcartign   First version bef...
105
106
          Right right = rightService.findByName("not-for-me").get(0);
          assertFalse(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
107
108
109
110
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenUsernameNull() {
292aec6f   jcartign   First version bef...
111
112
          Right right = rightService.findByName("parent").get(0);
          userService.isUserHasRight(null, right);
bda41d02   jcartign   All tests method ...
113
114
115
116
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNull() {
292aec6f   jcartign   First version bef...
117
          userService.isUserHasRight("user", null);
bda41d02   jcartign   All tests method ...
118
119
120
121
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNotInDatabase() {
292aec6f   jcartign   First version bef...
122
123
124
          Right right = new Right();
          right.setName("dummy");
          userService.isUserHasRight("user", right);
a800fde0   jcartign   First version, in...
125
126
      }
  }