Commit fd5e74ca92a7e060d47eefe56a26eb3299480bb2
0 parents
First student version
- no persistence annotations in domain objects - not fully tested
Showing
17 changed files
with
868 additions
and
0 deletions
Show diff stats
1 | +++ a/pom.xml | |
... | ... | @@ -0,0 +1,38 @@ |
1 | +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
2 | + xmlns="http://maven.apache.org/POM/4.0.0" | |
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | |
4 | + http://maven.apache.org/maven-v4_0_0.xsd"> | |
5 | + <modelVersion>4.0.0</modelVersion> | |
6 | + | |
7 | + <groupId>fr.plil.sio.persistence</groupId> | |
8 | + <artifactId>spring-jpa</artifactId> | |
9 | + <version>0.1</version> | |
10 | + | |
11 | + <parent> | |
12 | + <groupId>org.springframework.boot</groupId> | |
13 | + <artifactId>spring-boot-starter-parent</artifactId> | |
14 | + <version>1.4.0.RELEASE</version> | |
15 | + </parent> | |
16 | + | |
17 | + <dependencies> | |
18 | + <dependency> | |
19 | + <groupId>org.springframework.boot</groupId> | |
20 | + <artifactId>spring-boot-starter-data-jpa</artifactId> | |
21 | + </dependency> | |
22 | + <dependency> | |
23 | + <groupId>com.h2database</groupId> | |
24 | + <artifactId>h2</artifactId> | |
25 | + </dependency> | |
26 | + <dependency> | |
27 | + <groupId>org.springframework.boot</groupId> | |
28 | + <artifactId>spring-boot-starter-test</artifactId> | |
29 | + <scope>test</scope> | |
30 | + </dependency> | |
31 | + </dependencies> | |
32 | + | |
33 | + <properties> | |
34 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
35 | + <java.version>1.8</java.version> | |
36 | + </properties> | |
37 | + | |
38 | +</project> | ... | ... |
src/main/java/fr/plil/sio/persistence/JpaApplication.java
0 → 100644
src/main/java/fr/plil/sio/persistence/api/Group.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/Group.java | |
... | ... | @@ -0,0 +1,55 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +import java.util.LinkedList; | |
4 | +import java.util.List; | |
5 | +import java.util.Set; | |
6 | +import java.util.TreeSet; | |
7 | + | |
8 | +public class Group { | |
9 | + | |
10 | + private Long id; | |
11 | + | |
12 | + private String name; | |
13 | + | |
14 | + /** | |
15 | + * Users in the group. | |
16 | + */ | |
17 | + private Set<User> users = new TreeSet<>(); | |
18 | + | |
19 | + /** | |
20 | + * List of rights. The list CANNOT contains duplicate rights. | |
21 | + */ | |
22 | + private List<Right> rights = new LinkedList<>(); | |
23 | + | |
24 | + public List<Right> getRights() { | |
25 | + return rights; | |
26 | + } | |
27 | + | |
28 | + public void setRights(List<Right> rights) { | |
29 | + this.rights = rights; | |
30 | + } | |
31 | + | |
32 | + public Long getId() { | |
33 | + return id; | |
34 | + } | |
35 | + | |
36 | + public void setId(Long id) { | |
37 | + this.id = id; | |
38 | + } | |
39 | + | |
40 | + public String getName() { | |
41 | + return name; | |
42 | + } | |
43 | + | |
44 | + public void setName(String name) { | |
45 | + this.name = name; | |
46 | + } | |
47 | + | |
48 | + public Set<User> getUsers() { | |
49 | + return users; | |
50 | + } | |
51 | + | |
52 | + public void setUsers(Set<User> users) { | |
53 | + this.users = users; | |
54 | + } | |
55 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/api/GroupService.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/GroupService.java | |
... | ... | @@ -0,0 +1,53 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +public interface GroupService { | |
4 | + | |
5 | + /** | |
6 | + * Create a group with a specific name in the database. | |
7 | + * There is no two groups with the same name in the database. | |
8 | + * | |
9 | + * @param name the name of the group | |
10 | + * @return an instance of the group | |
11 | + * @throws IllegalArgumentException if name is null | |
12 | + * @throws IllegalStateException if a group with the same name is already present | |
13 | + */ | |
14 | + Group create(String name); | |
15 | + | |
16 | + /** | |
17 | + * Delete a group in the database. Remove all users in the group. | |
18 | + * | |
19 | + * @param name the name of the group to remove | |
20 | + * @return true if group has been deleted, false if group is not found in the database. | |
21 | + * @throws IllegalArgumentException if name is null | |
22 | + */ | |
23 | + boolean delete(String name); | |
24 | + | |
25 | + /** | |
26 | + * Find a group in the database based on its name. | |
27 | + * | |
28 | + * @param name the name of the group to search for. | |
29 | + * @return an instance of the group if found, else null. | |
30 | + * @throws IllegalArgumentException if name is null | |
31 | + */ | |
32 | + Group findByName(String name); | |
33 | + | |
34 | + /** | |
35 | + * Add a right in the group. Right is inserted at the end of rights list of the group. | |
36 | + * | |
37 | + * @param groupName the name of the group. | |
38 | + * @param right the right to add | |
39 | + * @return true if right is added in the group, false if right was already present. | |
40 | + * @throws IllegalArgumentException if groupName or right is null, or if group or right cannot be found. | |
41 | + */ | |
42 | + boolean addRight(String groupName, Right right); | |
43 | + | |
44 | + /** | |
45 | + * Remove a right associated with a group. | |
46 | + * | |
47 | + * @param groupName the name of the group. | |
48 | + * @param right the right to remove | |
49 | + * @return true if right is removed from the group, false if teh right was not present in the group. | |
50 | + * @throws IllegalArgumentException if groupName or right is null, or if group or right cannot be found. | |
51 | + */ | |
52 | + boolean removeRight(String groupName, Right right); | |
53 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/api/Right.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/Right.java | |
... | ... | @@ -0,0 +1,49 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +import java.util.HashSet; | |
4 | +import java.util.Set; | |
5 | + | |
6 | +public class Right { | |
7 | + | |
8 | + private Long id; | |
9 | + | |
10 | + private String name; | |
11 | + | |
12 | + /// the parent group | |
13 | + private Right parent; | |
14 | + | |
15 | + /// the sibling group(s), eventually empty | |
16 | + private Set<Right> siblings = new HashSet<>(); | |
17 | + | |
18 | + public Set<Right> getSiblings() { | |
19 | + return siblings; | |
20 | + } | |
21 | + | |
22 | + public void setSiblings(Set<Right> siblings) { | |
23 | + this.siblings = siblings; | |
24 | + } | |
25 | + | |
26 | + public Long getId() { | |
27 | + return id; | |
28 | + } | |
29 | + | |
30 | + public void setId(Long id) { | |
31 | + this.id = id; | |
32 | + } | |
33 | + | |
34 | + public String getName() { | |
35 | + return name; | |
36 | + } | |
37 | + | |
38 | + public void setName(String name) { | |
39 | + this.name = name; | |
40 | + } | |
41 | + | |
42 | + public Right getParent() { | |
43 | + return parent; | |
44 | + } | |
45 | + | |
46 | + public void setParent(Right parent) { | |
47 | + this.parent = parent; | |
48 | + } | |
49 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/api/RightService.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/RightService.java | |
... | ... | @@ -0,0 +1,52 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +import java.util.List; | |
4 | + | |
5 | +public interface RightService { | |
6 | + | |
7 | + /** | |
8 | + * Create a parent right with a specific name in the database. | |
9 | + * It is possible that two rights has the same name. | |
10 | + * | |
11 | + * @param name the name of the right | |
12 | + * @throws IllegalArgumentException if name is null | |
13 | + */ | |
14 | + Right create(String name); | |
15 | + | |
16 | + /** | |
17 | + * Create a sibling right attached to a parent right with a specific name in the database. | |
18 | + * It is possible that two rights has the same name. | |
19 | + * | |
20 | + * @param name the name of the right | |
21 | + * @param parent the parent right | |
22 | + * @throws IllegalArgumentException if name or parent is null, or if parent does not exist in the database. | |
23 | + */ | |
24 | + Right create(String name, Right parent); | |
25 | + | |
26 | + /** | |
27 | + * Delete a right in the database. Delete sibling rights if present. | |
28 | + * | |
29 | + * @param right the right to delete | |
30 | + * @return true if right has been deleted, false else. | |
31 | + * @throws IllegalArgumentException if right is null. | |
32 | + */ | |
33 | + boolean delete(Right right); | |
34 | + | |
35 | + /** | |
36 | + * Find a list of rights in the database based on their name. | |
37 | + * | |
38 | + * @param name the name of the rights to search for. | |
39 | + * @return A list of rights, eventually empty. | |
40 | + * @throws IllegalArgumentException if name is null | |
41 | + */ | |
42 | + List<Right> findByName(String name); | |
43 | + | |
44 | + /** | |
45 | + * Find a right in the database based on its id. | |
46 | + * | |
47 | + * @param id the name of the right to search for. | |
48 | + * @return an instance of the right if found, else null. | |
49 | + * @throws IllegalArgumentException if id is null | |
50 | + */ | |
51 | + Right findOne(Long id); | |
52 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/api/User.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/User.java | |
... | ... | @@ -0,0 +1,34 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +public class User { | |
4 | + | |
5 | + private Long id; | |
6 | + | |
7 | + private String name; | |
8 | + | |
9 | + private Group group; | |
10 | + | |
11 | + public Long getId() { | |
12 | + return id; | |
13 | + } | |
14 | + | |
15 | + public void setId(Long id) { | |
16 | + this.id = id; | |
17 | + } | |
18 | + | |
19 | + public String getName() { | |
20 | + return name; | |
21 | + } | |
22 | + | |
23 | + public void setName(String name) { | |
24 | + this.name = name; | |
25 | + } | |
26 | + | |
27 | + public Group getGroup() { | |
28 | + return group; | |
29 | + } | |
30 | + | |
31 | + public void setGroup(Group group) { | |
32 | + this.group = group; | |
33 | + } | |
34 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/api/UserService.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/api/UserService.java | |
... | ... | @@ -0,0 +1,45 @@ |
1 | +package fr.plil.sio.persistence.api; | |
2 | + | |
3 | +public interface UserService { | |
4 | + | |
5 | + /** | |
6 | + * Create an user with a specific name in the database and affected to an existing group. | |
7 | + * There is no two users with the same name in the database. | |
8 | + * | |
9 | + * @param name the name of the user | |
10 | + * @param groupName the name of the group | |
11 | + * @return an instance of the user | |
12 | + * @throws IllegalArgumentException if name or groupName is null, or if group does not exist. | |
13 | + * @throws IllegalStateException if an user with the same name is already present | |
14 | + */ | |
15 | + User create(String name, String groupName); | |
16 | + | |
17 | + /** | |
18 | + * Delete an user in the database. | |
19 | + * | |
20 | + * @param name the name of the user to remove | |
21 | + * @return true if user has been deleted, false if user is not found in the database. | |
22 | + * @throws IllegalArgumentException if name is null | |
23 | + */ | |
24 | + boolean delete(String name); | |
25 | + | |
26 | + /** | |
27 | + * Find an user in the database based on its name. | |
28 | + * | |
29 | + * @param name the name of the user to search for. | |
30 | + * @return an instance of the user if found, else null. | |
31 | + * @throws IllegalArgumentException if name is null | |
32 | + */ | |
33 | + User findByName(String name); | |
34 | + | |
35 | + /** | |
36 | + * Check method to control if an user has a specific right. The following rule is used: an user has a specific | |
37 | + * right if the group where the user is contains the specific right or one of its parents. | |
38 | + * | |
39 | + * @param userName the name of the user | |
40 | + * @param right the specific right | |
41 | + * @return true if the group where the user is contains the specific right or one of its parents, false else. | |
42 | + * @throws IllegalArgumentException if userName or right is null, or if the user or right does not exist in the db. | |
43 | + */ | |
44 | + boolean isUserHasRight(String userName, Right right); | |
45 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java | |
... | ... | @@ -0,0 +1,9 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.Group; | |
4 | +import org.springframework.data.jpa.repository.JpaRepository; | |
5 | + | |
6 | +public interface GroupRepository extends JpaRepository<Group, Long> { | |
7 | + | |
8 | + Group findByName(String name); | |
9 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java | |
... | ... | @@ -0,0 +1,52 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.Group; | |
4 | +import fr.plil.sio.persistence.api.GroupService; | |
5 | +import fr.plil.sio.persistence.api.Right; | |
6 | +import org.springframework.beans.factory.annotation.Autowired; | |
7 | +import org.springframework.stereotype.Service; | |
8 | + | |
9 | +@Service | |
10 | +public class GroupServiceJpa implements GroupService { | |
11 | + | |
12 | + @Autowired | |
13 | + private GroupRepository groupRepository; | |
14 | + | |
15 | + @Override | |
16 | + public Group create(String name) { | |
17 | + if (name == null) { | |
18 | + throw new IllegalArgumentException("name cannot be null"); | |
19 | + } | |
20 | + Group group = groupRepository.findByName(name); | |
21 | + if (group != null) { | |
22 | + throw new IllegalStateException("a group with the same name already exists"); | |
23 | + } | |
24 | + group = new Group(); | |
25 | + group.setName(name); | |
26 | + groupRepository.save(group); | |
27 | + return group; | |
28 | + } | |
29 | + | |
30 | + @Override | |
31 | + public boolean delete(String name) { | |
32 | + throw new IllegalStateException("not implemented !"); | |
33 | + } | |
34 | + | |
35 | + @Override | |
36 | + public Group findByName(String name) { | |
37 | + if (name == null) { | |
38 | + throw new IllegalArgumentException("name cannot be null"); | |
39 | + } | |
40 | + return groupRepository.findByName(name); | |
41 | + } | |
42 | + | |
43 | + @Override | |
44 | + public boolean addRight(String groupName, Right right) { | |
45 | + return false; | |
46 | + } | |
47 | + | |
48 | + @Override | |
49 | + public boolean removeRight(String groupName, Right right) { | |
50 | + return false; | |
51 | + } | |
52 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java | |
... | ... | @@ -0,0 +1,35 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.Right; | |
4 | +import fr.plil.sio.persistence.api.RightService; | |
5 | +import org.springframework.stereotype.Service; | |
6 | + | |
7 | +import java.util.List; | |
8 | + | |
9 | +@Service | |
10 | +public class RightServiceJpa implements RightService { | |
11 | + @Override | |
12 | + public Right create(String name) { | |
13 | + return null; | |
14 | + } | |
15 | + | |
16 | + @Override | |
17 | + public Right create(String name, Right parent) { | |
18 | + return null; | |
19 | + } | |
20 | + | |
21 | + @Override | |
22 | + public boolean delete(Right right) { | |
23 | + return false; | |
24 | + } | |
25 | + | |
26 | + @Override | |
27 | + public List<Right> findByName(String name) { | |
28 | + return null; | |
29 | + } | |
30 | + | |
31 | + @Override | |
32 | + public Right findOne(Long id) { | |
33 | + return null; | |
34 | + } | |
35 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java
0 → 100644
1 | +++ a/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java | |
... | ... | @@ -0,0 +1,29 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.Right; | |
4 | +import fr.plil.sio.persistence.api.User; | |
5 | +import fr.plil.sio.persistence.api.UserService; | |
6 | +import org.springframework.stereotype.Service; | |
7 | + | |
8 | +@Service | |
9 | +public class UserServiceJpa implements UserService { | |
10 | + @Override | |
11 | + public User create(String name, String groupName) { | |
12 | + return null; | |
13 | + } | |
14 | + | |
15 | + @Override | |
16 | + public boolean delete(String name) { | |
17 | + return false; | |
18 | + } | |
19 | + | |
20 | + @Override | |
21 | + public User findByName(String name) { | |
22 | + return null; | |
23 | + } | |
24 | + | |
25 | + @Override | |
26 | + public boolean isUserHasRight(String userName, Right right) { | |
27 | + return false; | |
28 | + } | |
29 | +} | ... | ... |
src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java
0 → 100644
1 | +++ a/src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java | |
... | ... | @@ -0,0 +1,151 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.*; | |
4 | +import org.junit.Before; | |
5 | +import org.junit.Test; | |
6 | +import org.junit.runner.RunWith; | |
7 | +import org.springframework.beans.factory.annotation.Autowired; | |
8 | +import org.springframework.boot.test.context.SpringBootTest; | |
9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
10 | + | |
11 | +import static org.junit.Assert.*; | |
12 | + | |
13 | +@RunWith(SpringJUnit4ClassRunner.class) | |
14 | +@SpringBootTest | |
15 | +public class GroupServiceTest { | |
16 | + | |
17 | + @Autowired | |
18 | + private GroupService groupService; | |
19 | + | |
20 | + @Autowired | |
21 | + private UserService userService; | |
22 | + | |
23 | + @Autowired | |
24 | + private RightService rightService; | |
25 | + | |
26 | + private Right parent; | |
27 | + | |
28 | + @Before | |
29 | + public void before() { | |
30 | + parent = rightService.create("parent"); | |
31 | + rightService.create("sibling", parent); | |
32 | + groupService.create("group"); | |
33 | + } | |
34 | + | |
35 | + @Test | |
36 | + public void testCreateGroupAndFindByName() { | |
37 | + Group group = groupService.findByName("group"); | |
38 | + assertEquals("group", group.getName()); | |
39 | + } | |
40 | + | |
41 | + @Test(expected = IllegalArgumentException.class) | |
42 | + public void testCreateGroupFailsWhenNameNull() { | |
43 | + groupService.create(null); | |
44 | + } | |
45 | + | |
46 | + @Test(expected = IllegalStateException.class) | |
47 | + public void testCreateFailsWhenSameGroupUserAlreadyPresent() { | |
48 | + groupService.create("group"); | |
49 | + } | |
50 | + | |
51 | + @Test | |
52 | + public void testDeleteGroup() { | |
53 | + assertTrue(groupService.delete("group")); | |
54 | + assertNull(groupService.findByName("group")); | |
55 | + assertFalse(groupService.delete("group")); | |
56 | + } | |
57 | + | |
58 | + @Test | |
59 | + public void testDeleteNotExistingGroup() { | |
60 | + assertFalse(groupService.delete("not-a-group")); | |
61 | + } | |
62 | + | |
63 | + | |
64 | + @Test(expected = IllegalArgumentException.class) | |
65 | + public void testDeleteGroupFailsIfNameNull() { | |
66 | + groupService.delete(null); | |
67 | + } | |
68 | + | |
69 | + @Test | |
70 | + public void deleteGroupDoesDeleteUsers() { | |
71 | + userService.create("user1", "group"); | |
72 | + userService.create("user1", "group"); | |
73 | + assertNotNull(userService.findByName("user1")); | |
74 | + assertNotNull(userService.findByName("user2")); | |
75 | + groupService.delete("group"); | |
76 | + assertNull(userService.findByName("user1")); | |
77 | + assertNull(userService.findByName("user2")); | |
78 | + } | |
79 | + | |
80 | + public void testFindByNameIfGroupNotFound() { | |
81 | + assertNull(groupService.findByName("unknown")); | |
82 | + } | |
83 | + | |
84 | + @Test(expected = IllegalArgumentException.class) | |
85 | + public void testFindByNameFailsIfNameNull() { | |
86 | + groupService.findByName(null); | |
87 | + } | |
88 | + | |
89 | + @Test | |
90 | + public void testAddRight() { | |
91 | + assertTrue(groupService.addRight("group", parent)); | |
92 | + Group group = groupService.findByName("group"); | |
93 | + assertEquals(1, group.getRights().size()); | |
94 | + assertEquals("parent", group.getRights().get(0).getName()); | |
95 | + assertEquals(1, group.getRights().get(0).getSiblings().size()); | |
96 | + assertEquals("sibling", group.getRights().get(0).getSiblings().iterator().next().getName()); | |
97 | + } | |
98 | + | |
99 | + @Test | |
100 | + public void testAddRightIfAlreadyPresent() { | |
101 | + assertTrue(groupService.addRight("group", parent)); | |
102 | + assertFalse(groupService.addRight("group", parent)); | |
103 | + } | |
104 | + | |
105 | + @Test(expected = IllegalArgumentException.class) | |
106 | + public void testAddRightFailsIfGroupNameNull() { | |
107 | + groupService.addRight(null, parent); | |
108 | + } | |
109 | + | |
110 | + @Test(expected = IllegalArgumentException.class) | |
111 | + public void testAddRightFailsIfRightNull() { | |
112 | + groupService.addRight("group", null); | |
113 | + } | |
114 | + | |
115 | + @Test(expected = IllegalArgumentException.class) | |
116 | + public void testAddRightFailsIfGroupNotInDatabase() { | |
117 | + groupService.addRight("not-a-group", null); | |
118 | + } | |
119 | + | |
120 | + @Test | |
121 | + public void testRemoveRight() { | |
122 | + assertTrue(groupService.addRight("group", parent)); | |
123 | + Group group = groupService.findByName("group"); | |
124 | + assertEquals(1, group.getRights().size()); | |
125 | + assertTrue(groupService.removeRight("group", parent)); | |
126 | + group = groupService.findByName("group"); | |
127 | + assertEquals(0, group.getRights().size()); | |
128 | + } | |
129 | + | |
130 | + @Test | |
131 | + public void testRemoveRightIfNotPresent() { | |
132 | + Right right = new Right(); | |
133 | + right.setName("not-a-right"); | |
134 | + assertFalse(groupService.removeRight("group", right)); | |
135 | + } | |
136 | + | |
137 | + @Test(expected = IllegalArgumentException.class) | |
138 | + public void testRemoveRightFailsIfGroupNameNull() { | |
139 | + groupService.removeRight(null, parent); | |
140 | + } | |
141 | + | |
142 | + @Test(expected = IllegalArgumentException.class) | |
143 | + public void testRemoveRightFailsIfRightNull() { | |
144 | + groupService.removeRight("group", null); | |
145 | + } | |
146 | + | |
147 | + @Test(expected = IllegalArgumentException.class) | |
148 | + public void testRemoveRightFailsIfGroupNotInDatabase() { | |
149 | + groupService.removeRight("not-a-group", null); | |
150 | + } | |
151 | +} | ... | ... |
src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java
0 → 100644
1 | +++ a/src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java | |
... | ... | @@ -0,0 +1,126 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.Right; | |
4 | +import fr.plil.sio.persistence.api.RightService; | |
5 | +import org.junit.Test; | |
6 | +import org.junit.runner.RunWith; | |
7 | +import org.springframework.beans.factory.annotation.Autowired; | |
8 | +import org.springframework.boot.test.context.SpringBootTest; | |
9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
10 | + | |
11 | +import static org.junit.Assert.*; | |
12 | + | |
13 | +@RunWith(SpringJUnit4ClassRunner.class) | |
14 | +@SpringBootTest | |
15 | +public class RightServiceTest { | |
16 | + | |
17 | + @Autowired | |
18 | + private RightService rightService; | |
19 | + | |
20 | + @Test | |
21 | + public void testCreateParentRightAndFindOne() { | |
22 | + rightService.create("right"); | |
23 | + assertEquals(1, rightService.findByName("right").size()); | |
24 | + } | |
25 | + | |
26 | + @Test | |
27 | + public void testCreateTwoParentRightsWithSameNameAndFindByName() { | |
28 | + rightService.create("right"); | |
29 | + rightService.create("right"); | |
30 | + assertEquals(2, rightService.findByName("right").size()); | |
31 | + } | |
32 | + | |
33 | + @Test(expected = IllegalArgumentException.class) | |
34 | + public void testCreateFailsIfNameNull() { | |
35 | + rightService.create(null); | |
36 | + } | |
37 | + | |
38 | + @Test | |
39 | + public void testCreateSiblingRightAndFindOne() { | |
40 | + Right parent = rightService.create("parent"); | |
41 | + rightService.create("sibling", parent); | |
42 | + assertEquals(1, rightService.findByName("sibling").size()); | |
43 | + } | |
44 | + | |
45 | + @Test(expected = IllegalArgumentException.class) | |
46 | + public void testCreateSiblingFailsIfNameNull() { | |
47 | + Right parent = rightService.create("parent"); | |
48 | + rightService.create(null, parent); | |
49 | + } | |
50 | + | |
51 | + @Test(expected = IllegalArgumentException.class) | |
52 | + public void testCreateSiblingFailsIfParentNull() { | |
53 | + rightService.create("parent"); | |
54 | + rightService.create("sibling", null); | |
55 | + } | |
56 | + | |
57 | + @Test(expected = IllegalArgumentException.class) | |
58 | + public void testCreateSiblingFailsIfParentNotInDatabase() { | |
59 | + Right right = new Right(); | |
60 | + right.setName("not-a-right"); | |
61 | + rightService.create("sibling", right); | |
62 | + } | |
63 | + | |
64 | + @Test | |
65 | + public void testDeleteParentRight() { | |
66 | + Right right = rightService.create("right"); | |
67 | + assertEquals(1, rightService.findByName("right").size()); | |
68 | + rightService.delete(right); | |
69 | + assertEquals(0, rightService.findByName("right").size()); | |
70 | + } | |
71 | + | |
72 | + @Test | |
73 | + public void testDeleteSiblingRight() { | |
74 | + Right parent = rightService.create("parent"); | |
75 | + Right sibling = rightService.create("sibling", parent); | |
76 | + rightService.delete(sibling); | |
77 | + assertEquals(0, rightService.findByName("sibling").size()); | |
78 | + } | |
79 | + | |
80 | + @Test | |
81 | + public void testDeleteParentAndSiblingRights() { | |
82 | + Right parent = rightService.create("parent"); | |
83 | + rightService.create("sibling", parent); | |
84 | + rightService.delete(parent); | |
85 | + assertEquals(0, rightService.findByName("sibling").size()); | |
86 | + assertEquals(0, rightService.findByName("parent").size()); | |
87 | + } | |
88 | + | |
89 | + @Test | |
90 | + public void testDeleteRightIfNotFound() { | |
91 | + Right right = new Right(); | |
92 | + right.setName("not-a-right"); | |
93 | + assertFalse(rightService.delete(right)); | |
94 | + } | |
95 | + | |
96 | + @Test(expected = IllegalArgumentException.class) | |
97 | + public void testDeleteRightFailsIfRightNull() { | |
98 | + rightService.delete(null); | |
99 | + } | |
100 | + | |
101 | + @Test | |
102 | + public void testFindByNameIfNameNotFound() { | |
103 | + assertEquals(0, rightService.findByName("no").size()); | |
104 | + } | |
105 | + | |
106 | + @Test(expected = IllegalArgumentException.class) | |
107 | + public void testFindByNameFailsIfNameNull() { | |
108 | + rightService.findByName(null); | |
109 | + } | |
110 | + | |
111 | + @Test | |
112 | + public void testFindOne() { | |
113 | + Right right = rightService.create("right"); | |
114 | + assertNotNull(rightService.findOne(right.getId())); | |
115 | + } | |
116 | + | |
117 | + @Test | |
118 | + public void testFindOneIfIdNotFound() { | |
119 | + assertNull(rightService.findOne(153463167809232L)); | |
120 | + } | |
121 | + | |
122 | + @Test(expected = IllegalArgumentException.class) | |
123 | + public void testFindOneFailsIfIdNull() { | |
124 | + rightService.findOne(null); | |
125 | + } | |
126 | +} | ... | ... |
src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java
0 → 100644
1 | +++ a/src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java | |
... | ... | @@ -0,0 +1,125 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.*; | |
4 | +import org.junit.Before; | |
5 | +import org.junit.Test; | |
6 | +import org.junit.runner.RunWith; | |
7 | +import org.springframework.beans.factory.annotation.Autowired; | |
8 | +import org.springframework.boot.test.context.SpringBootTest; | |
9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
10 | + | |
11 | +import static org.junit.Assert.*; | |
12 | + | |
13 | +@RunWith(SpringJUnit4ClassRunner.class) | |
14 | +@SpringBootTest | |
15 | +public class UserServiceTest { | |
16 | + | |
17 | + @Autowired | |
18 | + private GroupService groupService; | |
19 | + | |
20 | + @Autowired | |
21 | + private UserService userService; | |
22 | + | |
23 | + @Autowired | |
24 | + private RightService rightService; | |
25 | + | |
26 | + @Before | |
27 | + public void before() { | |
28 | + groupService.create("group"); | |
29 | + userService.create("user", "group"); | |
30 | + Right right = rightService.create("parent"); | |
31 | + rightService.create("sibling", right); | |
32 | + groupService.addRight("group", right); | |
33 | + rightService.create("not-for-me"); | |
34 | + } | |
35 | + | |
36 | + @Test | |
37 | + public void testCreateAndFindByName() { | |
38 | + User user = userService.findByName("user"); | |
39 | + assertNotNull(user); | |
40 | + assertEquals("user", user.getName()); | |
41 | + Group group = user.getGroup(); | |
42 | + assertNotNull(group); | |
43 | + assertEquals("group", group.getName()); | |
44 | + } | |
45 | + | |
46 | + @Test(expected = IllegalArgumentException.class) | |
47 | + public void testCreateFailsWhenNameNull() { | |
48 | + userService.create(null, "group"); | |
49 | + } | |
50 | + | |
51 | + @Test(expected = IllegalArgumentException.class) | |
52 | + public void testCreateFailsWhenGroupNameNull() { | |
53 | + userService.create("user", null); | |
54 | + } | |
55 | + | |
56 | + @Test(expected = IllegalArgumentException.class) | |
57 | + public void testCreateFailsWhenGroupDoesNotExist() { | |
58 | + userService.create("user", "notGroup"); | |
59 | + } | |
60 | + | |
61 | + @Test(expected = IllegalStateException.class) | |
62 | + public void testCreateFailsWhenSameNameUserAlreadyPresent() { | |
63 | + userService.create("user", "group"); | |
64 | + } | |
65 | + | |
66 | + @Test | |
67 | + public void testDeleteUser() { | |
68 | + assertTrue(userService.delete("user")); | |
69 | + } | |
70 | + | |
71 | + @Test | |
72 | + public void testDeleteUserIfNotFound() { | |
73 | + assertFalse(userService.delete("user")); | |
74 | + } | |
75 | + | |
76 | + @Test(expected = IllegalArgumentException.class) | |
77 | + public void testDeleteUserFailsIfNameNull() { | |
78 | + userService.delete(null); | |
79 | + } | |
80 | + | |
81 | + public void testFindUserByNameIfUserNotFound() { | |
82 | + assertNull(userService.findByName("blabla")); | |
83 | + } | |
84 | + | |
85 | + @Test(expected = IllegalArgumentException.class) | |
86 | + public void testFindUserByNameFailsIfNameNull() { | |
87 | + assertNull(userService.findByName(null)); | |
88 | + } | |
89 | + | |
90 | + @Test | |
91 | + public void testIsUserHasExactRight() { | |
92 | + Right right = rightService.findByName("parent").get(0); | |
93 | + assertTrue(userService.isUserHasRight("user", right)); | |
94 | + } | |
95 | + | |
96 | + @Test | |
97 | + public void testIsUserHasRightByParents() { | |
98 | + Right right = rightService.findByName("sibling").get(0); | |
99 | + assertTrue(userService.isUserHasRight("user", right)); | |
100 | + } | |
101 | + | |
102 | + @Test | |
103 | + public void testIsUserHasNotTheExactRight() { | |
104 | + Right right = rightService.findByName("not-for-me").get(0); | |
105 | + assertFalse(userService.isUserHasRight("user", right)); | |
106 | + } | |
107 | + | |
108 | + @Test(expected = IllegalArgumentException.class) | |
109 | + public void testIsUserHasRightFailsWhenUsernameNull() { | |
110 | + Right right = rightService.findByName("parent").get(0); | |
111 | + userService.isUserHasRight(null, right); | |
112 | + } | |
113 | + | |
114 | + @Test(expected = IllegalArgumentException.class) | |
115 | + public void testIsUserHasRightFailsWhenRightNull() { | |
116 | + userService.isUserHasRight("user", null); | |
117 | + } | |
118 | + | |
119 | + @Test(expected = IllegalArgumentException.class) | |
120 | + public void testIsUserHasRightFailsWhenRightNotInDatabase() { | |
121 | + Right right = new Right(); | |
122 | + right.setName("dummy"); | |
123 | + userService.isUserHasRight("user", right); | |
124 | + } | |
125 | +} | ... | ... |
1 | +++ a/src/test/resources/application.properties | |
... | ... | @@ -0,0 +1,4 @@ |
1 | +logging.level.org.springframework=INFO | |
2 | +logging.level.org.hibernate.SQL=DEBUG | |
3 | +logging.level.org.hibernate=INFO | |
4 | +spring.datasource.url=jdbc:h2:mem:persistence;TRACE_LEVEL_FILE=4 | |
0 | 5 | \ No newline at end of file | ... | ... |