Commit 2b83e93979463934e59af3b8c3078b4d1694d628

Authored by rvangrev
1 parent bb4bc541

TP terminé

src/main/java/fr/plil/sio/persistence/api/Group.java
@@ -2,26 +2,35 @@ package fr.plil.sio.persistence.api; @@ -2,26 +2,35 @@ 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 javax.persistence.*;
5 6
6 /** 7 /**
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 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 * A group contains a list of rights unique by their ID (no two groups with the same ID can exist in the database).
9 */ 10 */
10 11
  12 +@Entity
  13 +@Table (name = "GROUP_T")
11 public class Group { 14 public class Group {
12 15
  16 + @Id
  17 + @GeneratedValue(strategy = GenerationType.AUTO)
  18 + @Column(name = "GROUP_ID")
13 private Long id; 19 private Long id;
14 20
  21 + @Column(name = "GROUP_NAME")
15 private String name; 22 private String name;
16 23
17 /** 24 /**
18 * Users in the group. 25 * Users in the group.
19 */ 26 */
  27 + @OneToMany(mappedBy ="group", cascade = CascadeType.REMOVE)
20 private List<User> users = new LinkedList<>(); 28 private List<User> users = new LinkedList<>();
21 29
22 /** 30 /**
23 * List of rights. The list CANNOT contains duplicate rights. 31 * List of rights. The list CANNOT contains duplicate rights.
24 */ 32 */
  33 + @OneToMany(mappedBy="parent", cascade = CascadeType.REMOVE)
25 private List<Right> rights = new LinkedList<>(); 34 private List<Right> rights = new LinkedList<>();
26 35
27 public List<Right> getRights() { 36 public List<Right> getRights() {
src/main/java/fr/plil/sio/persistence/api/Right.java
@@ -2,6 +2,7 @@ package fr.plil.sio.persistence.api; @@ -2,6 +2,7 @@ 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 javax.persistence.*;
5 6
6 /** 7 /**
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 is unique by itd ID, i.e. it can exist two rights with the same name in the database.
@@ -9,16 +10,25 @@ import java.util.List; @@ -9,16 +10,25 @@ import java.util.List;
9 * A right can have zero, one or more siblings. 10 * A right can have zero, one or more siblings.
10 */ 11 */
11 12
  13 +@Entity
  14 +@Table (name = "RIGHT_T")
12 public class Right { 15 public class Right {
13 16
  17 + @Id
  18 + @GeneratedValue(strategy = GenerationType.AUTO)
  19 + @Column(name = "RIGHT_ID")
14 private Long id; 20 private Long id;
15 21
  22 + @Column(name = "RIGHT_NAME")
16 private String name; 23 private String name;
17 24
18 /// the parent right 25 /// the parent right
  26 + @ManyToOne
  27 + @JoinColumn(name = "PARENT")
19 private Right parent; 28 private Right parent;
20 29
21 /// the sibling right(s), eventually empty 30 /// the sibling right(s), eventually empty
  31 + @OneToMany(mappedBy="parent", cascade = CascadeType.REMOVE)
22 private List<Right> siblings = new LinkedList<>(); 32 private List<Right> siblings = new LinkedList<>();
23 33
24 public List<Right> getSiblings() { 34 public List<Right> getSiblings() {
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 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.GeneratedValue;
  6 +import javax.persistence.GenerationType;
  7 +import javax.persistence.Id;
  8 +import javax.persistence.JoinColumn;
  9 +import javax.persistence.ManyToOne;
  10 +import javax.persistence.Table;
  11 +
3 /** 12 /**
4 * An user MUST have a group in the database. 13 * 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. 14 * An user is unique by it name, i.e. database cannot contain two user with the same name or the same ID.
6 */ 15 */
7 16
  17 +@Entity
  18 +@Table (name = "USER_T")
8 public class User { 19 public class User {
9 20
  21 + @Id
  22 + @GeneratedValue(strategy = GenerationType.AUTO)
  23 + @Column(name = "USER_ID")
10 private Long id; 24 private Long id;
11 25
  26 + @Column(name = "USER_NAME")
12 private String name; 27 private String name;
13 28
  29 + @ManyToOne(optional = false)
  30 + @JoinColumn(name = "GROUP_ID")
14 private Group group; 31 private Group group;
15 32
16 public Long getId() { 33 public Long getId() {
src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java
@@ -3,6 +3,8 @@ package fr.plil.sio.persistence.jpa; @@ -3,6 +3,8 @@ package fr.plil.sio.persistence.jpa;
3 import fr.plil.sio.persistence.api.Group; 3 import fr.plil.sio.persistence.api.Group;
4 import fr.plil.sio.persistence.api.GroupService; 4 import fr.plil.sio.persistence.api.GroupService;
5 import fr.plil.sio.persistence.api.Right; 5 import fr.plil.sio.persistence.api.Right;
  6 +import java.util.ArrayList;
  7 +import java.util.List;
6 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service; 9 import org.springframework.stereotype.Service;
8 10
@@ -11,7 +13,13 @@ public class GroupServiceJpa implements GroupService { @@ -11,7 +13,13 @@ public class GroupServiceJpa implements GroupService {
11 13
12 @Autowired 14 @Autowired
13 private GroupRepository groupRepository; 15 private GroupRepository groupRepository;
14 - 16 +
  17 + @Autowired
  18 + private RightRepository rightRepository;
  19 +
  20 + @Autowired
  21 + private UserRepository userRepository;
  22 +
15 @Override 23 @Override
16 public Group create(String name) { 24 public Group create(String name) {
17 if (name == null) { 25 if (name == null) {
@@ -23,13 +31,27 @@ public class GroupServiceJpa implements GroupService { @@ -23,13 +31,27 @@ public class GroupServiceJpa implements GroupService {
23 } 31 }
24 group = new Group(); 32 group = new Group();
25 group.setName(name); 33 group.setName(name);
  34 +
26 groupRepository.save(group); 35 groupRepository.save(group);
  36 +
27 return group; 37 return group;
28 } 38 }
29 39
30 @Override 40 @Override
31 public boolean delete(String name) { 41 public boolean delete(String name) {
32 - throw new IllegalStateException("not implemented !"); 42 + if (name == null) {
  43 + throw new IllegalArgumentException("name cannot be null");
  44 + }
  45 +
  46 + Group group = findByName(name);
  47 +
  48 + if(group==null){
  49 + return false;
  50 + }
  51 +
  52 + groupRepository.delete(group.getId());
  53 +
  54 + return true;
33 } 55 }
34 56
35 @Override 57 @Override
@@ -42,11 +64,51 @@ public class GroupServiceJpa implements GroupService { @@ -42,11 +64,51 @@ public class GroupServiceJpa implements GroupService {
42 64
43 @Override 65 @Override
44 public boolean addRight(String groupName, Right right) { 66 public boolean addRight(String groupName, Right right) {
45 - return false; 67 + if(groupName==null){
  68 + throw new IllegalArgumentException("groupname cannot be null");
  69 + }
  70 + if(right == null){
  71 + throw new IllegalArgumentException("right cannot be null");
  72 + }
  73 +
  74 + Group group = groupRepository.findByName(groupName);
  75 +
  76 + if (group == null) {
  77 + throw new IllegalArgumentException("group cannot be null");
  78 + }
  79 +
  80 + List<Right> list = group.getRights();
  81 + if(list.contains(right)){
  82 + return false;
  83 + }
  84 + assert list != null;
  85 + list.add(right);
  86 + group.setRights(list);
  87 +
  88 + return true;
46 } 89 }
47 90
48 @Override 91 @Override
49 public boolean removeRight(String groupName, Right right) { 92 public boolean removeRight(String groupName, Right right) {
50 - return false; 93 + if(groupName==null){
  94 + throw new IllegalArgumentException("Groupname cannot be null");
  95 + }
  96 + if(right==null){
  97 + throw new IllegalArgumentException("right cannot be null");
  98 + }
  99 + if(rightRepository.findByName(right.getName()).size()==0){
  100 + throw new IllegalArgumentException("right not in database");
  101 + }
  102 + Group group = groupRepository.findByName(groupName);
  103 +
  104 + if (group == null) {
  105 + throw new IllegalArgumentException("group cannot be null");
  106 + }
  107 +
  108 + List<Right> list = group.getRights();
  109 + list.remove(right);
  110 + group.setRights(list);
  111 +
  112 + return true;
51 } 113 }
52 } 114 }
src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +package fr.plil.sio.persistence.jpa;
  2 +
  3 +import fr.plil.sio.persistence.api.Right;
  4 +import java.util.List;
  5 +import org.springframework.data.jpa.repository.JpaRepository;
  6 +
  7 +public interface RightRepository extends JpaRepository<Right, Long> {
  8 +
  9 + List<Right> findByName(String name);
  10 +
  11 + Right findById(Long id);
  12 +
  13 +}
src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java
@@ -5,31 +5,78 @@ import fr.plil.sio.persistence.api.RightService; @@ -5,31 +5,78 @@ import fr.plil.sio.persistence.api.RightService;
5 import org.springframework.stereotype.Service; 5 import org.springframework.stereotype.Service;
6 6
7 import java.util.List; 7 import java.util.List;
  8 +import org.springframework.beans.factory.annotation.Autowired;
8 9
9 @Service 10 @Service
10 public class RightServiceJpa implements RightService { 11 public class RightServiceJpa implements RightService {
  12 +
  13 + @Autowired
  14 + private RightRepository rightRepository;
  15 +
11 @Override 16 @Override
12 public Right create(String name) { 17 public Right create(String name) {
13 - return null; 18 + if(name==null){
  19 + throw new IllegalArgumentException("name cannot be null");
  20 + }
  21 + Right right = new Right();
  22 + right.setName(name);
  23 +
  24 + rightRepository.save(right);
  25 +
  26 + return right;
14 } 27 }
15 28
16 @Override 29 @Override
17 public Right create(String name, Right parent) { 30 public Right create(String name, Right parent) {
18 - return null; 31 + if(name==null){
  32 + throw new IllegalArgumentException("name cannot be null");
  33 + }
  34 + if(parent==null){
  35 + throw new IllegalArgumentException("parent cannot be null");
  36 + }
  37 +
  38 + Right right = new Right();
  39 + right.setName(name);
  40 + if(rightRepository.findByName(parent.getName()).size()==0){
  41 + throw new IllegalArgumentException("parent cannot be null");
  42 + }
  43 +
  44 + right.setParent(parent);
  45 +
  46 + rightRepository.save(right);
  47 +
  48 + parent.getSiblings().add(right);
  49 +
  50 + return right;
19 } 51 }
20 52
21 @Override 53 @Override
22 public boolean delete(Right right) { 54 public boolean delete(Right right) {
23 - return false; 55 + if(right==null){
  56 + throw new IllegalArgumentException("right cannot be null");
  57 + }
  58 + if(rightRepository.findByName(right.getName()).size()==0){
  59 + throw new IllegalArgumentException("right not in database");
  60 + }
  61 +
  62 + rightRepository.delete(right.getId());
  63 +
  64 + return true;
24 } 65 }
25 66
26 @Override 67 @Override
27 public List<Right> findByName(String name) { 68 public List<Right> findByName(String name) {
28 - return null; 69 + if(name==null){
  70 + throw new IllegalArgumentException("name cannot be null");
  71 + }
  72 + return rightRepository.findByName(name);
29 } 73 }
30 74
31 @Override 75 @Override
32 public Right findOne(Long id) { 76 public Right findOne(Long id) {
33 - return null; 77 + if(id==null){
  78 + throw new IllegalArgumentException("name cannot be null");
  79 + }
  80 + return rightRepository.findById(id);
34 } 81 }
35 } 82 }
src/main/java/fr/plil/sio/persistence/jpa/UserRepository.java 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +package fr.plil.sio.persistence.jpa;
  2 +
  3 +import fr.plil.sio.persistence.api.User;
  4 +import org.springframework.data.jpa.repository.JpaRepository;
  5 +
  6 +public interface UserRepository extends JpaRepository<User, Long> {
  7 +
  8 + User findByName(String name);
  9 +}
src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java
1 package fr.plil.sio.persistence.jpa; 1 package fr.plil.sio.persistence.jpa;
2 2
3 import fr.plil.sio.persistence.api.*; 3 import fr.plil.sio.persistence.api.*;
  4 +import javax.transaction.Transactional;
4 import org.junit.Before; 5 import org.junit.Before;
5 import org.junit.Test; 6 import org.junit.Test;
6 import org.junit.runner.RunWith; 7 import org.junit.runner.RunWith;
@@ -12,6 +13,7 @@ import static org.junit.Assert.*; @@ -12,6 +13,7 @@ import static org.junit.Assert.*;
12 13
13 @RunWith(SpringJUnit4ClassRunner.class) 14 @RunWith(SpringJUnit4ClassRunner.class)
14 @SpringBootTest 15 @SpringBootTest
  16 +@Transactional
15 public class GroupServiceTest { 17 public class GroupServiceTest {
16 18
17 @Autowired 19 @Autowired
src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java
1 package fr.plil.sio.persistence.jpa; 1 package fr.plil.sio.persistence.jpa;
2 2
3 import fr.plil.sio.persistence.api.*; 3 import fr.plil.sio.persistence.api.*;
  4 +import javax.transaction.Transactional;
4 import org.junit.Before; 5 import org.junit.Before;
5 import org.junit.Test; 6 import org.junit.Test;
6 import org.junit.runner.RunWith; 7 import org.junit.runner.RunWith;
@@ -12,6 +13,7 @@ import static org.junit.Assert.*; @@ -12,6 +13,7 @@ import static org.junit.Assert.*;
12 13
13 @RunWith(SpringJUnit4ClassRunner.class) 14 @RunWith(SpringJUnit4ClassRunner.class)
14 @SpringBootTest 15 @SpringBootTest
  16 +@Transactional
15 public class UserServiceTest { 17 public class UserServiceTest {
16 18
17 @Autowired 19 @Autowired