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
1 *.iml 1 *.iml
2 -*/target/ 2 +target/
3 .idea/ 3 .idea/
4 */.idea/ 4 */.idea/
5 /target/ 5 /target/
6 \ No newline at end of file 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 xsi:schemaLocation="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"> 4 http://maven.apache.org/maven-v4_0_0.xsd">
5 <modelVersion>4.0.0</modelVersion> 5 <modelVersion>4.0.0</modelVersion>
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 <parent> 11 <parent>
12 <groupId>org.springframework.boot</groupId> 12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-parent</artifactId> 13 <artifactId>spring-boot-starter-parent</artifactId>
14 - <version>1.3.6.RELEASE</version> 14 + <version>1.4.0.RELEASE</version>
15 </parent> 15 </parent>
16 16
17 <dependencies> 17 <dependencies>
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 <dependency> 22 <dependency>
23 <groupId>com.h2database</groupId> 23 <groupId>com.h2database</groupId>
24 <artifactId>h2</artifactId> 24 <artifactId>h2</artifactId>
  25 + <scope>runtime</scope>
25 </dependency> 26 </dependency>
26 <dependency> 27 <dependency>
27 <groupId>org.springframework.boot</groupId> 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 import org.springframework.boot.autoconfigure.SpringBootApplication; 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,9 +2,11 @@ package fr.plil.sio.persistence.api;
2 2
3 import java.util.LinkedList; 3 import java.util.LinkedList;
4 import java.util.List; 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 public class Group { 10 public class Group {
9 11
10 private Long id; 12 private Long id;
@@ -14,7 +16,7 @@ public class Group { @@ -14,7 +16,7 @@ public class Group {
14 /** 16 /**
15 * Users in the group. 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 * List of rights. The list CANNOT contains duplicate rights. 22 * List of rights. The list CANNOT contains duplicate rights.
@@ -45,11 +47,11 @@ public class Group { @@ -45,11 +47,11 @@ public class Group {
45 this.name = name; 47 this.name = name;
46 } 48 }
47 49
48 - public Set<User> getUsers() { 50 + public List<User> getUsers() {
49 return users; 51 return users;
50 } 52 }
51 53
52 - public void setUsers(Set<User> users) { 54 + public void setUsers(List<User> users) {
53 this.users = users; 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,7 +4,7 @@ public interface GroupService {
4 4
5 /** 5 /**
6 * Create a group with a specific name in the database. 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 * @param name the name of the group 9 * @param name the name of the group
10 * @return an instance of the group 10 * @return an instance of the group
@@ -23,7 +23,8 @@ public interface GroupService { @@ -23,7 +23,8 @@ public interface GroupService {
23 boolean delete(String name); 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 * @param name the name of the group to search for. 29 * @param name the name of the group to search for.
29 * @return an instance of the group if found, else null. 30 * @return an instance of the group if found, else null.
@@ -32,7 +33,7 @@ public interface GroupService { @@ -32,7 +33,7 @@ public interface GroupService {
32 Group findByName(String name); 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 * @param groupName the name of the group. 38 * @param groupName the name of the group.
38 * @param right the right to add 39 * @param right the right to add
src/main/java/fr/plil/sio/persistence/api/Right.java
1 package fr.plil.sio.persistence.api; 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 public class Right { 11 public class Right {
7 12
8 private Long id; 13 private Long id;
9 14
10 private String name; 15 private String name;
11 16
12 - /// the parent group 17 + /// the parent right
13 private Right parent; 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 return siblings; 24 return siblings;
20 } 25 }
21 26
22 - public void setSiblings(Set<Right> siblings) { 27 + public void setSiblings(List<Right> siblings) {
23 this.siblings = siblings; 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,6 +16,8 @@ public interface RightService {
16 /** 16 /**
17 * Create a sibling right attached to a parent right with a specific name in the database. 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. 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 * @param name the name of the right 22 * @param name the name of the right
21 * @param parent the parent right 23 * @param parent the parent right
@@ -34,6 +36,7 @@ public interface RightService { @@ -34,6 +36,7 @@ public interface RightService {
34 36
35 /** 37 /**
36 * Find a list of rights in the database based on their name. 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 * @param name the name of the rights to search for. 41 * @param name the name of the rights to search for.
39 * @return A list of rights, eventually empty. 42 * @return A list of rights, eventually empty.
@@ -43,6 +46,7 @@ public interface RightService { @@ -43,6 +46,7 @@ public interface RightService {
43 46
44 /** 47 /**
45 * Find a right in the database based on its id. 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 * @param id the name of the right to search for. 51 * @param id the name of the right to search for.
48 * @return an instance of the right if found, else null. 52 * @return an instance of the right if found, else null.
src/main/java/fr/plil/sio/persistence/api/User.java
1 package fr.plil.sio.persistence.api; 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 public class User { 7 public class User {
4 8
5 private Long id; 9 private Long id;
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
@@ -44,7 +44,6 @@ public class RightServiceJdbc implements RightService { @@ -44,7 +44,6 @@ public class RightServiceJdbc implements RightService {
44 rightRepository.save(right); 44 rightRepository.save(right);
45 45
46 return right; 46 return right;
47 -  
48 } 47 }
49 48
50 @Override 49 @Override
src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java
@@ -5,7 +5,7 @@ import org.junit.Before; @@ -5,7 +5,7 @@ import org.junit.Before;
5 import org.junit.Test; 5 import org.junit.Test;
6 import org.junit.runner.RunWith; 6 import org.junit.runner.RunWith;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
8 -import org.springframework.boot.test.SpringApplicationConfiguration; 8 +import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10
11 import static org.junit.Assert.*; 11 import static org.junit.Assert.*;
@@ -13,7 +13,7 @@ import org.slf4j.Logger; @@ -13,7 +13,7 @@ import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 13 import org.slf4j.LoggerFactory;
14 14
15 @RunWith(SpringJUnit4ClassRunner.class) 15 @RunWith(SpringJUnit4ClassRunner.class)
16 -@SpringApplicationConfiguration(classes = JdbcApplication.class) 16 +@SpringBootTest
17 public class GroupServiceTest extends AbstractServiceSupport { 17 public class GroupServiceTest extends AbstractServiceSupport {
18 18
19 private static final Logger logger = LoggerFactory.getLogger(GroupServiceTest.class); 19 private static final Logger logger = LoggerFactory.getLogger(GroupServiceTest.class);
@@ -56,6 +56,7 @@ public class GroupServiceTest extends AbstractServiceSupport { @@ -56,6 +56,7 @@ public class GroupServiceTest extends AbstractServiceSupport {
56 groupService.create("group"); 56 groupService.create("group");
57 } 57 }
58 58
  59 + @Test
59 public void testDeleteGroup() { 60 public void testDeleteGroup() {
60 logger.info("testDeleteGroup"); 61 logger.info("testDeleteGroup");
61 assertTrue(groupService.delete("group")); 62 assertTrue(groupService.delete("group"));
@@ -63,6 +64,7 @@ public class GroupServiceTest extends AbstractServiceSupport { @@ -63,6 +64,7 @@ public class GroupServiceTest extends AbstractServiceSupport {
63 assertFalse(groupService.delete("group")); 64 assertFalse(groupService.delete("group"));
64 } 65 }
65 66
  67 + @Test
66 public void testDeleteNotExistingGroup() { 68 public void testDeleteNotExistingGroup() {
67 logger.info("testDeleteNotExistingGroup"); 69 logger.info("testDeleteNotExistingGroup");
68 assertFalse(groupService.delete("not-a-group")); 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,13 +5,13 @@ import fr.plil.sio.persistence.api.RightService;
5 import org.junit.Test; 5 import org.junit.Test;
6 import org.junit.runner.RunWith; 6 import org.junit.runner.RunWith;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
8 -import org.springframework.boot.test.SpringApplicationConfiguration; 8 +import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10
11 import static org.junit.Assert.*; 11 import static org.junit.Assert.*;
12 12
13 @RunWith(SpringJUnit4ClassRunner.class) 13 @RunWith(SpringJUnit4ClassRunner.class)
14 -@SpringApplicationConfiguration(classes = JdbcApplication.class) 14 +@SpringBootTest
15 public class RightServiceTest extends AbstractServiceSupport { 15 public class RightServiceTest extends AbstractServiceSupport {
16 16
17 @Autowired 17 @Autowired
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
@@ -5,13 +5,13 @@ import org.junit.Before; @@ -5,13 +5,13 @@ import org.junit.Before;
5 import org.junit.Test; 5 import org.junit.Test;
6 import org.junit.runner.RunWith; 6 import org.junit.runner.RunWith;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
8 -import org.springframework.boot.test.SpringApplicationConfiguration; 8 +import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 10
11 import static org.junit.Assert.*; 11 import static org.junit.Assert.*;
12 12
13 @RunWith(SpringJUnit4ClassRunner.class) 13 @RunWith(SpringJUnit4ClassRunner.class)
14 -@SpringApplicationConfiguration(classes = JdbcApplication.class) 14 +@SpringBootTest
15 public class UserServiceTest extends AbstractServiceSupport { 15 public class UserServiceTest extends AbstractServiceSupport {
16 16
17 @Autowired 17 @Autowired
@@ -63,10 +63,12 @@ public class UserServiceTest extends AbstractServiceSupport { @@ -63,10 +63,12 @@ public class UserServiceTest extends AbstractServiceSupport {
63 userService.create("user", "group"); 63 userService.create("user", "group");
64 } 64 }
65 65
  66 + @Test
66 public void testDeleteUser() { 67 public void testDeleteUser() {
67 assertTrue(userService.delete("user")); 68 assertTrue(userService.delete("user"));
68 } 69 }
69 70
  71 + @Test
70 public void testDeleteUserIfNotFound() { 72 public void testDeleteUserIfNotFound() {
71 assertFalse(userService.delete("user")); 73 assertFalse(userService.delete("user"));
72 } 74 }
@@ -85,16 +87,19 @@ public class UserServiceTest extends AbstractServiceSupport { @@ -85,16 +87,19 @@ public class UserServiceTest extends AbstractServiceSupport {
85 assertNull(userService.findByName(null)); 87 assertNull(userService.findByName(null));
86 } 88 }
87 89
  90 + @Test
88 public void testIsUserHasExactRight() { 91 public void testIsUserHasExactRight() {
89 Right right = rightService.findByName("parent").get(0); 92 Right right = rightService.findByName("parent").get(0);
90 assertTrue(userService.isUserHasRight("user", right)); 93 assertTrue(userService.isUserHasRight("user", right));
91 } 94 }
92 95
  96 + @Test
93 public void testIsUserHasRightByParents() { 97 public void testIsUserHasRightByParents() {
94 Right right = rightService.findByName("sibling").get(0); 98 Right right = rightService.findByName("sibling").get(0);
95 assertTrue(userService.isUserHasRight("user", right)); 99 assertTrue(userService.isUserHasRight("user", right));
96 } 100 }
97 101
  102 + @Test
98 public void testIsUserHasNotTheExactRight() { 103 public void testIsUserHasNotTheExactRight() {
99 Right right = rightService.findByName("not-for-me").get(0); 104 Right right = rightService.findByName("not-for-me").get(0);
100 assertFalse(userService.isUserHasRight("user", right)); 105 assertFalse(userService.isUserHasRight("user", right));