Blame view

src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java 3.7 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
30
31
32
33
34
  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");
          Right right = rightService.create("parent");
          rightService.create("sibling", right);
          groupService.addRight("group", right);
          rightService.create("not-for-me");
      }
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
      }
  
77b66887   jcartign   Forgot some @Test...
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
      }
  
77b66887   jcartign   Forgot some @Test...
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
      }
  
77b66887   jcartign   Forgot some @Test...
90
      @Test
bda41d02   jcartign   All tests method ...
91
      public void testIsUserHasExactRight() {
292aec6f   jcartign   First version bef...
92
93
          Right right = rightService.findByName("parent").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
94
95
      }
  
77b66887   jcartign   Forgot some @Test...
96
      @Test
bda41d02   jcartign   All tests method ...
97
      public void testIsUserHasRightByParents() {
292aec6f   jcartign   First version bef...
98
99
          Right right = rightService.findByName("sibling").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
100
101
      }
  
77b66887   jcartign   Forgot some @Test...
102
      @Test
bda41d02   jcartign   All tests method ...
103
      public void testIsUserHasNotTheExactRight() {
292aec6f   jcartign   First version bef...
104
105
          Right right = rightService.findByName("not-for-me").get(0);
          assertFalse(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
106
107
108
109
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenUsernameNull() {
292aec6f   jcartign   First version bef...
110
111
          Right right = rightService.findByName("parent").get(0);
          userService.isUserHasRight(null, right);
bda41d02   jcartign   All tests method ...
112
113
114
115
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNull() {
292aec6f   jcartign   First version bef...
116
          userService.isUserHasRight("user", null);
bda41d02   jcartign   All tests method ...
117
118
119
120
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNotInDatabase() {
292aec6f   jcartign   First version bef...
121
122
123
          Right right = new Right();
          right.setName("dummy");
          userService.isUserHasRight("user", right);
a800fde0   jcartign   First version, in...
124
125
      }
  }