Commit 0bd6dc75f3b002e28aa4db1980b5aa4f01c9ad06
Merge migration from spring-jdbc
- add javadocs in Group, Right and User - fixing few mistakes in Group and Right service tests - update application properties to increase JDBC connection tomcat pool
Showing
6 changed files
with
60 additions
and
16 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/api/Group.java
1 | 1 | package fr.plil.sio.persistence.api; |
2 | 2 | |
3 | +import javax.persistence.*; | |
3 | 4 | import java.util.LinkedList; |
4 | 5 | import java.util.List; |
5 | -import java.util.Set; | |
6 | -import java.util.TreeSet; | |
7 | 6 | |
7 | +/** | |
8 | + * A group is unique by its name (no two groups with the same name or the same ID can exist in the database). | |
9 | + * A group contains a list of rights unique by their ID (no two groups with the same ID can exist in the database). | |
10 | + */ | |
11 | + | |
12 | +@Entity | |
13 | +@Table(name = "GROUP_T") | |
8 | 14 | public class Group { |
9 | 15 | |
16 | + @Id | |
17 | + @GeneratedValue(strategy = GenerationType.AUTO) | |
18 | + @Column(name = "GROUP_ID") | |
10 | 19 | private Long id; |
11 | 20 | |
21 | + @Column(name = "NAME_C") | |
12 | 22 | private String name; |
13 | 23 | |
14 | 24 | /** |
15 | 25 | * Users in the group. |
16 | 26 | */ |
17 | - private Set<User> users = new TreeSet<>(); | |
27 | + @OneToMany(mappedBy = "group") | |
28 | + private List<User> users = new LinkedList<>(); | |
18 | 29 | |
19 | 30 | /** |
20 | 31 | * List of rights. The list CANNOT contains duplicate rights. |
21 | 32 | */ |
33 | + @OneToMany | |
22 | 34 | private List<Right> rights = new LinkedList<>(); |
23 | 35 | |
24 | 36 | public List<Right> getRights() { |
... | ... | @@ -45,11 +57,11 @@ public class Group { |
45 | 57 | this.name = name; |
46 | 58 | } |
47 | 59 | |
48 | - public Set<User> getUsers() { | |
60 | + public List<User> getUsers() { | |
49 | 61 | return users; |
50 | 62 | } |
51 | 63 | |
52 | - public void setUsers(Set<User> users) { | |
64 | + public void setUsers(List<User> users) { | |
53 | 65 | this.users = users; |
54 | 66 | } |
55 | 67 | } | ... | ... |
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; | |
3 | +import javax.persistence.*; | |
4 | +import java.util.LinkedList; | |
5 | +import java.util.List; | |
5 | 6 | |
7 | +/** | |
8 | + * A right is unique by itd ID, i.e. it can exist two rights with the same name in the database. | |
9 | + * A right may have a parent, null else. | |
10 | + * A right can have zero, one or more siblings. | |
11 | + */ | |
12 | + | |
13 | +@Entity | |
14 | +@Table(name = "RIGHT_T") | |
6 | 15 | public class Right { |
7 | 16 | |
17 | + @Id | |
18 | + @GeneratedValue(strategy = GenerationType.AUTO) | |
19 | + @Column(name = "RIGHT_ID") | |
8 | 20 | private Long id; |
9 | 21 | |
22 | + @Column(name = "NAME_C") | |
10 | 23 | private String name; |
11 | 24 | |
12 | - /// the parent group | |
25 | + /// the parent right | |
26 | + @ManyToOne | |
27 | + @JoinColumn(name = "PARENT_C") | |
13 | 28 | private Right parent; |
14 | 29 | |
15 | - /// the sibling group(s), eventually empty | |
16 | - private Set<Right> siblings = new HashSet<>(); | |
30 | + /// the sibling right(s), eventually empty | |
31 | + @OneToMany(mappedBy = "parent") | |
32 | + private List<Right> siblings = new LinkedList<>(); | |
17 | 33 | |
18 | - public Set<Right> getSiblings() { | |
34 | + public List<Right> getSiblings() { | |
19 | 35 | return siblings; |
20 | 36 | } |
21 | 37 | |
22 | - public void setSiblings(Set<Right> siblings) { | |
38 | + public void setSiblings(List<Right> siblings) { | |
23 | 39 | this.siblings = siblings; |
24 | 40 | } |
25 | 41 | ... | ... |
src/main/java/fr/plil/sio/persistence/api/User.java
1 | 1 | package fr.plil.sio.persistence.api; |
2 | 2 | |
3 | +import javax.persistence.*; | |
4 | + | |
5 | +/** | |
6 | + * An user MUST have a group in the database. | |
7 | + * An user is unique by it name, i.e. database cannot contain two user with the same name or the same ID. | |
8 | + */ | |
9 | + | |
10 | +@Entity | |
11 | +@Table(name = "USER_T") | |
3 | 12 | public class User { |
4 | 13 | |
14 | + @Id | |
15 | + @GeneratedValue(strategy = GenerationType.AUTO) | |
16 | + @Column(name = "USER_ID") | |
5 | 17 | private Long id; |
6 | 18 | |
19 | + @Column(name = "NAME_C") | |
7 | 20 | private String name; |
8 | 21 | |
22 | + @ManyToOne | |
23 | + @JoinColumn(name = "GROUP_C") | |
9 | 24 | private Group group; |
10 | 25 | |
11 | 26 | public Long getId() { | ... | ... |
src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java
... | ... | @@ -69,7 +69,7 @@ public class GroupServiceTest { |
69 | 69 | @Test |
70 | 70 | public void deleteGroupDoesDeleteUsers() { |
71 | 71 | userService.create("user1", "group"); |
72 | - userService.create("user1", "group"); | |
72 | + userService.create("user2", "group"); | |
73 | 73 | assertNotNull(userService.findByName("user1")); |
74 | 74 | assertNotNull(userService.findByName("user2")); |
75 | 75 | groupService.delete("group"); | ... | ... |
src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java
... | ... | @@ -86,8 +86,8 @@ public class RightServiceTest { |
86 | 86 | assertEquals(0, rightService.findByName("parent").size()); |
87 | 87 | } |
88 | 88 | |
89 | - @Test | |
90 | - public void testDeleteRightIfNotFound() { | |
89 | + @Test(expected = IllegalArgumentException.class) | |
90 | + public void testDeleteRightIfNotInDatabase() { | |
91 | 91 | Right right = new Right(); |
92 | 92 | right.setName("not-a-right"); |
93 | 93 | assertFalse(rightService.delete(right)); | ... | ... |
src/test/resources/application.properties
1 | 1 | logging.level.org.springframework=INFO |
2 | 2 | logging.level.org.hibernate.SQL=DEBUG |
3 | 3 | logging.level.org.hibernate=INFO |
4 | -spring.datasource.url=jdbc:h2:mem:persistence;TRACE_LEVEL_FILE=4 | |
5 | 4 | \ No newline at end of file |
5 | +spring.datasource.url=jdbc:h2:mem:persistence;TRACE_LEVEL_FILE=4; | |
6 | +spring.datasource.tomcat.max-active=300 | |
6 | 7 | \ No newline at end of file | ... | ... |