UserServiceTest.java
3.76 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package fr.plil.sio.persistence.jpa;
import fr.plil.sio.persistence.api.*;
import javax.transaction.Transactional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Transactional
public class UserServiceTest {
@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");
}
@Test
public void testCreateAndFindByName() {
User user = userService.findByName("user");
assertNotNull(user);
assertEquals("user", user.getName());
Group group = user.getGroup();
assertNotNull(group);
assertEquals("group", group.getName());
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFailsWhenNameNull() {
userService.create(null, "group");
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFailsWhenGroupNameNull() {
userService.create("user", null);
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFailsWhenGroupDoesNotExist() {
userService.create("user", "notGroup");
}
@Test(expected = IllegalStateException.class)
public void testCreateFailsWhenSameNameUserAlreadyPresent() {
userService.create("user", "group");
}
@Test
public void testDeleteUser() {
assertTrue(userService.delete("user"));
}
@Test
public void testDeleteUserIfNotFound() {
userService.delete("user");
assertFalse(userService.delete("user"));
}
@Test(expected = IllegalArgumentException.class)
public void testDeleteUserFailsIfNameNull() {
userService.delete(null);
}
public void testFindUserByNameIfUserNotFound() {
assertNull(userService.findByName("blabla"));
}
@Test(expected = IllegalArgumentException.class)
public void testFindUserByNameFailsIfNameNull() {
assertNull(userService.findByName(null));
}
@Test
public void testIsUserHasExactRight() {
Right right = rightService.findByName("parent").get(0);
assertTrue(userService.isUserHasRight("user", right));
}
@Test
public void testIsUserHasRightByParents() {
Right right = rightService.findByName("sibling").get(0);
assertTrue(userService.isUserHasRight("user", right));
}
@Test
public void testIsUserHasNotTheExactRight() {
Right right = rightService.findByName("not-for-me").get(0);
assertFalse(userService.isUserHasRight("user", right));
}
@Test(expected = IllegalArgumentException.class)
public void testIsUserHasRightFailsWhenUsernameNull() {
Right right = rightService.findByName("parent").get(0);
userService.isUserHasRight(null, right);
}
@Test(expected = IllegalArgumentException.class)
public void testIsUserHasRightFailsWhenRightNull() {
userService.isUserHasRight("user", null);
}
@Test(expected = IllegalArgumentException.class)
public void testIsUserHasRightFailsWhenRightNotInDatabase() {
Right right = new Right();
right.setName("dummy");
userService.isUserHasRight("user", right);
}
}