Commit 9ae0ef878b5a6c358ece867cb01f0546b8a0daec

Authored by msahmane
1 parent 4c446810

Amélioration des tests de spring jpa

src/main/java/fr/plil/sio/persistence/api/Right.java
... ... @@ -31,12 +31,10 @@ public class Right implements Serializable{
31 31 private String name;
32 32  
33 33 /// the parent right
34   - @ManyToOne(optional = false)
35   - @JoinColumn(name = "RIGHT_ID")
36   - @Column(name = "PARENT_C")
  34 + @ManyToOne(optional = true)
37 35 private Right parent;
38 36  
39   - @ManyToOne(optional = false)
  37 + @ManyToOne(optional = true)
40 38 @JoinColumn(name = "GROUP_ID")
41 39 private Group group;
42 40  
... ...
src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java
... ... @@ -29,7 +29,15 @@ public class GroupServiceJpa implements GroupService {
29 29  
30 30 @Override
31 31 public boolean delete(String name) {
32   - throw new IllegalStateException("not implemented !");
  32 + if(name == null)
  33 + throw new IllegalArgumentException("Name cannot be null");
  34 +
  35 + Group group = groupRepository.findByName(name);
  36 + if(group == null)
  37 + return false;
  38 +
  39 + groupRepository.delete(group);
  40 + return true;
33 41 }
34 42  
35 43 @Override
... ... @@ -42,6 +50,7 @@ public class GroupServiceJpa implements GroupService {
42 50  
43 51 @Override
44 52 public boolean addRight(String groupName, Right right) {
  53 +
45 54 return false;
46 55 }
47 56  
... ...
src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +package fr.plil.sio.persistence.jpa;
  2 +
  3 +import fr.plil.sio.persistence.api.Right;
  4 +import org.springframework.data.jpa.repository.JpaRepository;
  5 +
  6 +public interface RightRepository extends JpaRepository<Right, Long> {
  7 +
  8 + Right findByName(String name);
  9 +
  10 +}
... ...
src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java
1 1 package fr.plil.sio.persistence.jpa;
2 2  
  3 +import fr.plil.sio.persistence.api.Group;
3 4 import fr.plil.sio.persistence.api.Right;
4 5 import fr.plil.sio.persistence.api.RightService;
  6 +import java.util.ArrayList;
5 7 import org.springframework.stereotype.Service;
6 8  
7 9 import java.util.List;
  10 +import org.springframework.beans.factory.annotation.Autowired;
8 11  
9 12 @Service
10 13 public class RightServiceJpa implements RightService {
  14 +
  15 + @Autowired
  16 + private RightRepository rightRepository;
  17 +
11 18 @Override
12 19 public Right create(String name) {
13   - return null;
  20 + if (name == null) {
  21 + throw new IllegalArgumentException("name cannot be null");
  22 + }
  23 +
  24 + List<Right> rights = new ArrayList<>();
  25 +
  26 + Right right = new Right();
  27 + right.setName(name);
  28 +
  29 + rights.add(right);
  30 + rightRepository.save(rights);
  31 +
  32 + return right;
14 33 }
15 34  
16 35 @Override
17 36 public Right create(String name, Right parent) {
18   - return null;
  37 + if (name == null) {
  38 + throw new IllegalArgumentException("name cannot be null");
  39 + }
  40 +
  41 + List<Right> rights = new ArrayList<>();
  42 +
  43 + Right right = new Right();
  44 + right.setName(name);
  45 + right.setParent(parent);
  46 + rights.add(right);
  47 + rightRepository.save(rights);
  48 +
  49 + return right;
19 50 }
20 51  
21 52 @Override
22 53 public boolean delete(Right right) {
23   - return false;
  54 + if(right == null)
  55 + throw new IllegalArgumentException("Right cannot be null");
  56 +
  57 + rightRepository.delete(right);
  58 + return true;
24 59 }
25 60  
26 61 @Override
27   - public List<Right> findByName(String name) {
28   - return null;
  62 + public List<Right> findByName(String name) {
  63 + if(name == null)
  64 + throw new IllegalArgumentException("Name cannot be null");
  65 +
  66 + Right right = rightRepository.findByName(name);
  67 + List<Right> rights = new ArrayList<>();
  68 + rights.add(right);
  69 +
  70 + return rights;
29 71 }
30 72  
31 73 @Override
32 74 public Right findOne(Long id) {
33   - return null;
  75 +
  76 + if(id == null)
  77 + throw new IllegalArgumentException("Id cannot be null");
  78 +
  79 + return rightRepository.findOne(id);
34 80 }
35 81 }
... ...
src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java
... ... @@ -7,23 +7,57 @@ import org.springframework.stereotype.Service;
7 7  
8 8 @Service
9 9 public class UserServiceJpa implements UserService {
  10 +
  11 + /*@Autowired
  12 + UserRepository userRepository;
  13 + GroupRepository groupRepository; */
  14 +
10 15 @Override
11 16 public User create(String name, String groupName) {
  17 + /* if (name == null) {
  18 + throw new IllegalArgumentException("name cannot be null");
  19 + }
  20 +
  21 + User user = userRepository.findByName(name);
  22 + Group group = groupRepository.findByName(name);
  23 + if (user != null) {
  24 + throw new IllegalStateException("a user with the same name already exists");
  25 + }
  26 + user = new User();
  27 + user.setName(name);
  28 + user.setGroup(group);
  29 + userRepository.save(user);*/
12 30 return null;
13 31 }
14 32  
15 33 @Override
16 34 public boolean delete(String name) {
  35 + /* if(name == null){
  36 + return false;
  37 + }
  38 + User user = userRepository.findByName(name);
  39 +
  40 + userRepository.delete(user);*/
  41 +
17 42 return false;
18 43 }
19 44  
20 45 @Override
21 46 public User findByName(String name) {
  47 + /*if (name == null) {
  48 + throw new IllegalArgumentException("name cannot be null");
  49 + }
  50 + return userRepository.findByName(name);*/
22 51 return null;
23 52 }
24 53  
25 54 @Override
26 55 public boolean isUserHasRight(String userName, Right right) {
  56 + /*if(userName == null)
  57 + throw new IllegalArgumentException("name cannot be null");
  58 + if(right == null)
  59 + throw new IllegalArgumentException("Right cannot be null");
  60 + */
27 61 return false;
28 62 }
29 63 }
... ...
src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java
1 1 package fr.plil.sio.persistence.jpa;
2 2  
3 3 import fr.plil.sio.persistence.api.*;
  4 +import javax.transaction.Transactional;
4 5 import org.junit.Before;
5 6 import org.junit.Test;
6 7 import org.junit.runner.RunWith;
... ... @@ -10,6 +11,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 11  
11 12 import static org.junit.Assert.*;
12 13  
  14 +@Transactional
13 15 @RunWith(SpringJUnit4ClassRunner.class)
14 16 @SpringBootTest
15 17 public class GroupServiceTest {
... ...
src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java
... ... @@ -2,6 +2,7 @@ package fr.plil.sio.persistence.jpa;
2 2  
3 3 import fr.plil.sio.persistence.api.Right;
4 4 import fr.plil.sio.persistence.api.RightService;
  5 +import javax.transaction.Transactional;
5 6 import org.junit.Test;
6 7 import org.junit.runner.RunWith;
7 8 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -10,6 +11,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 11  
11 12 import static org.junit.Assert.*;
12 13  
  14 +@Transactional
13 15 @RunWith(SpringJUnit4ClassRunner.class)
14 16 @SpringBootTest
15 17 public class RightServiceTest {
... ...
src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java
1 1 package fr.plil.sio.persistence.jpa;
2 2  
3 3 import fr.plil.sio.persistence.api.*;
  4 +import javax.transaction.Transactional;
4 5 import org.junit.Before;
5 6 import org.junit.Test;
6 7 import org.junit.runner.RunWith;
... ... @@ -10,6 +11,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 11  
11 12 import static org.junit.Assert.*;
12 13  
  14 +@Transactional
13 15 @RunWith(SpringJUnit4ClassRunner.class)
14 16 @SpringBootTest
15 17 public class UserServiceTest {
... ...