Commit c3fe52adf097ab55eb21bdd7af66b42d8a32babe

Authored by Mohammed-Zakaria Sahmane
2 parents 3de62e47 b6a31a21

Merge branch 'master' of archives.plil.fr:GIS2A4-Java/spring-persistence

Conflicts:
	src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
	src/test/resources/application.properties
.gitignore
1 1 *.iml
2   -*/target/
  2 +target/
3 3 .idea/
4 4 */.idea/
5 5 /target/
6 6 \ No newline at end of file
... ...
1   -<project xmlns="http://maven.apache.org/POM/4.0.0"
2   - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  1 +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2 + xmlns="http://maven.apache.org/POM/4.0.0"
3 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 4 http://maven.apache.org/maven-v4_0_0.xsd">
5 5 <modelVersion>4.0.0</modelVersion>
... ... @@ -11,7 +11,7 @@
11 11 <parent>
12 12 <groupId>org.springframework.boot</groupId>
13 13 <artifactId>spring-boot-starter-parent</artifactId>
14   - <version>1.3.6.RELEASE</version>
  14 + <version>1.4.0.RELEASE</version>
15 15 </parent>
16 16  
17 17 <dependencies>
... ... @@ -22,6 +22,7 @@
22 22 <dependency>
23 23 <groupId>com.h2database</groupId>
24 24 <artifactId>h2</artifactId>
  25 + <scope>runtime</scope>
25 26 </dependency>
26 27 <dependency>
27 28 <groupId>org.springframework.boot</groupId>
... ...
src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java renamed to src/main/java/fr/plil/sio/persistence/JdbcApplication.java
1   -package fr.plil.sio.persistence.jdbc;
  1 +package fr.plil.sio.persistence;
2 2  
3 3 import org.springframework.boot.autoconfigure.SpringBootApplication;
4 4  
... ...
src/main/java/fr/plil/sio/persistence/api/Group.java
... ... @@ -2,9 +2,11 @@ package fr.plil.sio.persistence.api;
2 2  
3 3 import java.util.LinkedList;
4 4 import java.util.List;
5   -import java.util.Set;
6   -import java.util.TreeSet;
7 5  
  6 +/**
  7 + * A group is unique by its name (no two groups with the same name or the same ID can exist in the database).
  8 + * A group contains a list of rights unique by their ID (no two groups with the same ID can exist in the database).
  9 + */
8 10 public class Group {
9 11  
10 12 private Long id;
... ... @@ -14,7 +16,7 @@ public class Group {
14 16 /**
15 17 * Users in the group.
16 18 */
17   - private Set<User> users = new TreeSet<>();
  19 + private List<User> users = new LinkedList<>();
18 20  
19 21 /**
20 22 * List of rights. The list CANNOT contains duplicate rights.
... ... @@ -45,11 +47,11 @@ public class Group {
45 47 this.name = name;
46 48 }
47 49  
48   - public Set<User> getUsers() {
  50 + public List<User> getUsers() {
49 51 return users;
50 52 }
51 53  
52   - public void setUsers(Set<User> users) {
  54 + public void setUsers(List<User> users) {
53 55 this.users = users;
54 56 }
55 57 }
... ...
src/main/java/fr/plil/sio/persistence/api/GroupService.java
... ... @@ -4,7 +4,7 @@ public interface GroupService {
4 4  
5 5 /**
6 6 * Create a group with a specific name in the database.
7   - * There is no two groups with the same name in the database.
  7 + * There is no two groups with the same name or the same ID in the database.
8 8 *
9 9 * @param name the name of the group
10 10 * @return an instance of the group
... ... @@ -23,7 +23,8 @@ public interface GroupService {
23 23 boolean delete(String name);
24 24  
25 25 /**
26   - * Find a group in the database based on its name.
  26 + * Find a group in the database based on its name. Only references at one level are loaded (i.e. the users
  27 + * who belong to the group).
27 28 *
28 29 * @param name the name of the group to search for.
29 30 * @return an instance of the group if found, else null.
... ... @@ -32,7 +33,7 @@ public interface GroupService {
32 33 Group findByName(String name);
33 34  
34 35 /**
35   - * Add a right in the group. Right is inserted at the end of rights list of the group.
  36 + * Add a right in the group.
36 37 *
37 38 * @param groupName the name of the group.
38 39 * @param right the right to add
... ...
src/main/java/fr/plil/sio/persistence/api/Right.java
1 1 package fr.plil.sio.persistence.api;
2 2  
3   -import java.util.HashSet;
4   -import java.util.Set;
5   -
  3 +import java.util.LinkedList;
  4 +import java.util.List;
  5 +
  6 +/**
  7 + * A right is unique by itd ID, i.e. it can exist two rights with the same name in the database.
  8 + * A right may have a parent, null else.
  9 + * A right can have zero, one or more siblings.
  10 + */
6 11 public class Right {
7 12  
8 13 private Long id;
9 14  
10 15 private String name;
11 16  
12   - /// the parent group
  17 + /// the parent right
13 18 private Right parent;
14 19  
15   - /// the sibling group(s), eventually empty
16   - private Set<Right> siblings = new HashSet<>();
  20 + /// the sibling right(s), eventually empty
  21 + private List<Right> siblings = new LinkedList<>();
17 22  
18   - public Set<Right> getSiblings() {
  23 + public List<Right> getSiblings() {
19 24 return siblings;
20 25 }
21 26  
22   - public void setSiblings(Set<Right> siblings) {
  27 + public void setSiblings(List<Right> siblings) {
23 28 this.siblings = siblings;
24 29 }
25 30  
... ...
src/main/java/fr/plil/sio/persistence/api/RightService.java
... ... @@ -16,6 +16,8 @@ public interface RightService {
16 16 /**
17 17 * Create a sibling right attached to a parent right with a specific name in the database.
18 18 * It is possible that two rights has the same name.
  19 + * Return only the right with the parent in the field parent.
  20 + *
19 21 *
20 22 * @param name the name of the right
21 23 * @param parent the parent right
... ... @@ -34,6 +36,7 @@ public interface RightService {
34 36  
35 37 /**
36 38 * Find a list of rights in the database based on their name.
  39 + * All dependencies at one-level are loaded, i.e for each right returned the parent and sibling are present.
37 40 *
38 41 * @param name the name of the rights to search for.
39 42 * @return A list of rights, eventually empty.
... ... @@ -43,6 +46,7 @@ public interface RightService {
43 46  
44 47 /**
45 48 * Find a right in the database based on its id.
  49 + * All dependencies at one-level are loaded, i.e the parent and sibling are present.
46 50 *
47 51 * @param id the name of the right to search for.
48 52 * @return an instance of the right if found, else null.
... ...
src/main/java/fr/plil/sio/persistence/api/User.java
1 1 package fr.plil.sio.persistence.api;
2 2  
  3 +/**
  4 + * An user MUST have a group in the database.
  5 + * An user is unique by it name, i.e. database cannot contain two user with the same name or the same ID.
  6 + */
3 7 public class User {
4 8  
5 9 private Long id;
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... ... @@ -44,7 +44,6 @@ public class RightServiceJdbc implements RightService {
44 44 rightRepository.save(right);
45 45  
46 46 return right;
47   -
48 47 }
49 48  
50 49 @Override
... ...
src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java
... ... @@ -5,7 +5,7 @@ import org.junit.Before;
5 5 import org.junit.Test;
6 6 import org.junit.runner.RunWith;
7 7 import org.springframework.beans.factory.annotation.Autowired;
8   -import org.springframework.boot.test.SpringApplicationConfiguration;
  8 +import org.springframework.boot.test.context.SpringBootTest;
9 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10  
11 11 import static org.junit.Assert.*;
... ... @@ -13,7 +13,7 @@ import org.slf4j.Logger;
13 13 import org.slf4j.LoggerFactory;
14 14  
15 15 @RunWith(SpringJUnit4ClassRunner.class)
16   -@SpringApplicationConfiguration(classes = JdbcApplication.class)
  16 +@SpringBootTest
17 17 public class GroupServiceTest extends AbstractServiceSupport {
18 18  
19 19 private static final Logger logger = LoggerFactory.getLogger(GroupServiceTest.class);
... ... @@ -56,6 +56,7 @@ public class GroupServiceTest extends AbstractServiceSupport {
56 56 groupService.create("group");
57 57 }
58 58  
  59 + @Test
59 60 public void testDeleteGroup() {
60 61 logger.info("testDeleteGroup");
61 62 assertTrue(groupService.delete("group"));
... ... @@ -63,6 +64,7 @@ public class GroupServiceTest extends AbstractServiceSupport {
63 64 assertFalse(groupService.delete("group"));
64 65 }
65 66  
  67 + @Test
66 68 public void testDeleteNotExistingGroup() {
67 69 logger.info("testDeleteNotExistingGroup");
68 70 assertFalse(groupService.delete("not-a-group"));
... ...
src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java
... ... @@ -5,13 +5,13 @@ import fr.plil.sio.persistence.api.RightService;
5 5 import org.junit.Test;
6 6 import org.junit.runner.RunWith;
7 7 import org.springframework.beans.factory.annotation.Autowired;
8   -import org.springframework.boot.test.SpringApplicationConfiguration;
  8 +import org.springframework.boot.test.context.SpringBootTest;
9 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10  
11 11 import static org.junit.Assert.*;
12 12  
13 13 @RunWith(SpringJUnit4ClassRunner.class)
14   -@SpringApplicationConfiguration(classes = JdbcApplication.class)
  14 +@SpringBootTest
15 15 public class RightServiceTest extends AbstractServiceSupport {
16 16  
17 17 @Autowired
... ...
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
... ... @@ -5,13 +5,13 @@ import org.junit.Before;
5 5 import org.junit.Test;
6 6 import org.junit.runner.RunWith;
7 7 import org.springframework.beans.factory.annotation.Autowired;
8   -import org.springframework.boot.test.SpringApplicationConfiguration;
  8 +import org.springframework.boot.test.context.SpringBootTest;
9 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10  
11 11 import static org.junit.Assert.*;
12 12  
13 13 @RunWith(SpringJUnit4ClassRunner.class)
14   -@SpringApplicationConfiguration(classes = JdbcApplication.class)
  14 +@SpringBootTest
15 15 public class UserServiceTest extends AbstractServiceSupport {
16 16  
17 17 @Autowired
... ... @@ -63,10 +63,12 @@ public class UserServiceTest extends AbstractServiceSupport {
63 63 userService.create("user", "group");
64 64 }
65 65  
  66 + @Test
66 67 public void testDeleteUser() {
67 68 assertTrue(userService.delete("user"));
68 69 }
69 70  
  71 + @Test
70 72 public void testDeleteUserIfNotFound() {
71 73 assertFalse(userService.delete("user"));
72 74 }
... ... @@ -85,16 +87,19 @@ public class UserServiceTest extends AbstractServiceSupport {
85 87 assertNull(userService.findByName(null));
86 88 }
87 89  
  90 + @Test
88 91 public void testIsUserHasExactRight() {
89 92 Right right = rightService.findByName("parent").get(0);
90 93 assertTrue(userService.isUserHasRight("user", right));
91 94 }
92 95  
  96 + @Test
93 97 public void testIsUserHasRightByParents() {
94 98 Right right = rightService.findByName("sibling").get(0);
95 99 assertTrue(userService.isUserHasRight("user", right));
96 100 }
97 101  
  102 + @Test
98 103 public void testIsUserHasNotTheExactRight() {
99 104 Right right = rightService.findByName("not-for-me").get(0);
100 105 assertFalse(userService.isUserHasRight("user", right));
... ...