Blame view

src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java 3.71 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
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
66
      }
  
      public void testDeleteUser() {
292aec6f   jcartign   First version bef...
67
          assertTrue(userService.delete("user"));
bda41d02   jcartign   All tests method ...
68
69
70
      }
  
      public void testDeleteUserIfNotFound() {
292aec6f   jcartign   First version bef...
71
          assertFalse(userService.delete("user"));
bda41d02   jcartign   All tests method ...
72
73
74
75
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteUserFailsIfNameNull() {
292aec6f   jcartign   First version bef...
76
          userService.delete(null);
bda41d02   jcartign   All tests method ...
77
78
79
      }
  
      public void testFindUserByNameIfUserNotFound() {
292aec6f   jcartign   First version bef...
80
          assertNull(userService.findByName("blabla"));
bda41d02   jcartign   All tests method ...
81
82
83
84
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindUserByNameFailsIfNameNull() {
292aec6f   jcartign   First version bef...
85
          assertNull(userService.findByName(null));
bda41d02   jcartign   All tests method ...
86
87
88
      }
  
      public void testIsUserHasExactRight() {
292aec6f   jcartign   First version bef...
89
90
          Right right = rightService.findByName("parent").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
91
92
93
      }
  
      public void testIsUserHasRightByParents() {
292aec6f   jcartign   First version bef...
94
95
          Right right = rightService.findByName("sibling").get(0);
          assertTrue(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
96
97
98
      }
  
      public void testIsUserHasNotTheExactRight() {
292aec6f   jcartign   First version bef...
99
100
          Right right = rightService.findByName("not-for-me").get(0);
          assertFalse(userService.isUserHasRight("user", right));
bda41d02   jcartign   All tests method ...
101
102
103
104
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenUsernameNull() {
292aec6f   jcartign   First version bef...
105
106
          Right right = rightService.findByName("parent").get(0);
          userService.isUserHasRight(null, right);
bda41d02   jcartign   All tests method ...
107
108
109
110
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNull() {
292aec6f   jcartign   First version bef...
111
          userService.isUserHasRight("user", null);
bda41d02   jcartign   All tests method ...
112
113
114
115
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testIsUserHasRightFailsWhenRightNotInDatabase() {
292aec6f   jcartign   First version bef...
116
117
118
          Right right = new Right();
          right.setName("dummy");
          userService.isUserHasRight("user", right);
a800fde0   jcartign   First version, in...
119
120
      }
  }