Commit 60e631003c6d415e66194ef61c14a50ed6746d64

Authored by msahmane
1 parent c3fe52ad

Test des Right à 100% et Groupes à 77%

src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
3 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 5  
5 6 public interface GroupRepository {
6 7  
... ... @@ -9,4 +10,8 @@ public interface GroupRepository {
9 10 void delete(Long id);
10 11  
11 12 void save(Group group);
  13 +
  14 + boolean addRight(String groupName, Right right);
  15 +
  16 + boolean removeRight(String groupName, Right right);
12 17 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
3 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 5 import org.slf4j.Logger;
5 6 import org.slf4j.LoggerFactory;
6 7 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -109,4 +110,15 @@ public class GroupRepositoryJdbc implements GroupRepository {
109 110 }
110 111 }
111 112 }
  113 +
  114 + @Override
  115 + public boolean addRight(String groupName, Right right){
  116 +
  117 + return false;
  118 + }
  119 +
  120 + public boolean removeRight(String groupName, Right right){
  121 +
  122 + return false;
  123 + }
112 124 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... ... @@ -30,7 +30,6 @@ public class GroupServiceJdbc implements GroupService {
30 30  
31 31 @Override
32 32 public boolean delete(String name) {
33   - //throw new IllegalStateException("not implemented !");
34 33 if (name == null) {
35 34 throw new IllegalArgumentException("name cannot be null");
36 35 }
... ... @@ -53,11 +52,22 @@ public class GroupServiceJdbc implements GroupService {
53 52  
54 53 @Override
55 54 public boolean addRight(String groupName, Right right) {
56   - throw new IllegalStateException("not implemented !");
  55 + if(groupName == null )
  56 + throw new IllegalArgumentException("Group name cannot bel null");
  57 + if(right == null)
  58 + throw new IllegalArgumentException("Right cannot be null");
  59 +
  60 + return groupRepository.addRight(groupName, right);
57 61 }
58 62  
59 63 @Override
60 64 public boolean removeRight(String groupName, Right right) {
61   - throw new IllegalStateException("not implemented !");
  65 + if(groupName == null)
  66 + throw new IllegalArgumentException("Group name cannot be null");
  67 + if(right == null)
  68 + throw new IllegalArgumentException("Right cannot be null");
  69 +
  70 + return groupRepository.removeRight(groupName, right);
  71 +
62 72 }
63 73 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java
... ... @@ -6,6 +6,7 @@
6 6 package fr.plil.sio.persistence.jdbc;
7 7  
8 8 import fr.plil.sio.persistence.api.Right;
  9 +import java.util.List;
9 10  
10 11 /**
11 12 *
... ... @@ -13,9 +14,11 @@ import fr.plil.sio.persistence.api.Right;
13 14 */
14 15 public interface RightRepository {
15 16  
16   - Right findByName(String name);
  17 + List<Right> findByName(String name);
17 18  
18 19 void delete(Long id);
19 20  
20 21 void save(Right right);
  22 +
  23 + Right findOne(Long id);
21 24 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
... ... @@ -9,6 +9,8 @@ import fr.plil.sio.persistence.api.Right;
9 9 import java.sql.ResultSet;
10 10 import java.sql.SQLException;
11 11 import java.sql.Statement;
  12 +import java.util.ArrayList;
  13 +import java.util.List;
12 14 import javax.sql.DataSource;
13 15 import org.slf4j.Logger;
14 16 import org.slf4j.LoggerFactory;
... ... @@ -28,9 +30,10 @@ public class RightRepositoryJdbc implements RightRepository {
28 30 private DataSource dataSource;
29 31  
30 32 @Override
31   - public Right findByName(String name) {
  33 + public List<Right> findByName(String name) {
32 34 Statement stmt = null;
33 35 ResultSet rs = null;
  36 + List<Right> list = new ArrayList<>();
34 37 try {
35 38 stmt = dataSource.getConnection().createStatement();
36 39 rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE NAME_R = \'" + name + "\'");
... ... @@ -39,10 +42,11 @@ public class RightRepositoryJdbc implements RightRepository {
39 42 Right right = new Right();
40 43 right.setId(rs.getLong("RIGHT_ID"));
41 44 right.setName(rs.getString("NAME_R"));
42   - return right;
  45 + list.add(right);
  46 + return list;
43 47 } else {
44 48 logger.debug("not found " + name);
45   - return null;
  49 + return list;
46 50 }
47 51 } catch (SQLException e) {
48 52 throw new UnsupportedOperationException("sql exception", e);
... ... @@ -118,4 +122,35 @@ public class RightRepositoryJdbc implements RightRepository {
118 122 }
119 123 }
120 124  
  125 + @Override
  126 + public Right findOne(Long id){
  127 + Statement stmt = null;
  128 + ResultSet rs = null;
  129 + try{
  130 + stmt = dataSource.getConnection().createStatement();
  131 + rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE RIGHT_ID = "+id);
  132 + if (rs.next()) {
  133 + Right right = new Right();
  134 + right.setId(rs.getLong("RIGHT_ID"));
  135 + right.setName(rs.getString("NAME_R"));
  136 +
  137 + return right;
  138 + }
  139 + return null;
  140 + }catch(SQLException e){
  141 + throw new UnsupportedOperationException("sql exception", e);
  142 + }finally{
  143 + try{
  144 + if(rs !=null){
  145 + rs.close();
  146 + }
  147 + if(stmt != null){
  148 + stmt.close();
  149 + }
  150 + }catch(SQLException e){
  151 + throw new UnsupportedOperationException("sql exception during close", e);
  152 + }
  153 + }
  154 + }
  155 +
121 156 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... ... @@ -2,7 +2,6 @@ package fr.plil.sio.persistence.jdbc;
2 2  
3 3 import fr.plil.sio.persistence.api.Right;
4 4 import fr.plil.sio.persistence.api.RightService;
5   -import fr.plil.sio.persistence.api.User;
6 5 import org.springframework.stereotype.Service;
7 6  
8 7 import java.util.List;
... ... @@ -19,12 +18,8 @@ public class RightServiceJdbc implements RightService {
19 18 public Right create(String name) {
20 19 if (name == null) {
21 20 throw new IllegalArgumentException("name cannot be null");
22   - }
23   - Right right = rightRepository.findByName(name);
24   - if (right != null) {
25   - throw new IllegalStateException("a group with the same name already exists");
26   - }
27   - right = new Right();
  21 + }
  22 + Right right = new Right();
28 23 right.setName(name);
29 24 rightRepository.save(right);
30 25 return right;
... ... @@ -32,15 +27,16 @@ public class RightServiceJdbc implements RightService {
32 27  
33 28 @Override
34 29 public Right create(String name, Right parent) {
35   - if(name == null || parent == null)
  30 + if(name == null)
36 31 throw new IllegalArgumentException("Name cannot be null");
37   -
38   - Right right = rightRepository.findByName(parent.getName());
39   - if(right != null)
40   - throw new IllegalStateException("A right with the same name already exists");
41   -
42   - right = new Right();
  32 + if(parent == null)
  33 + throw new IllegalArgumentException("Parent cannot be null");
  34 + if(rightRepository.findOne(parent.getId()) == null){
  35 + throw new IllegalArgumentException("Parent doesn't exist in table");
  36 + }
  37 + Right right = new Right();
43 38 right.setName(name);
  39 + right.setParent(parent);
44 40 rightRepository.save(right);
45 41  
46 42 return right;
... ... @@ -48,16 +44,31 @@ public class RightServiceJdbc implements RightService {
48 44  
49 45 @Override
50 46 public boolean delete(Right right) {
51   - throw new IllegalStateException("not implemented !");
  47 + if(right == null)
  48 + throw new IllegalArgumentException("Right cannot be null");
  49 +
  50 + List<Right> r = rightRepository.findByName(right.getName());
  51 + if(!r.isEmpty()){
  52 + rightRepository.delete(right.getId());
  53 + return true;
  54 + }
  55 + return false;
52 56 }
53 57  
54 58 @Override
55 59 public List<Right> findByName(String name) {
56   - throw new IllegalStateException("not implemented !");
  60 + if(name == null)
  61 + throw new IllegalArgumentException("Name cannot be null");
  62 +
  63 + return rightRepository.findByName(name);
  64 +
57 65 }
58 66  
59 67 @Override
60 68 public Right findOne(Long id) {
61   - throw new IllegalStateException("not implemented !");
  69 + if(id == null)
  70 + throw new IllegalArgumentException("Id cannot be null");
  71 +
  72 + return rightRepository.findOne(id);
62 73 }
63 74 }
... ...
src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java
... ... @@ -19,7 +19,7 @@ public abstract class AbstractServiceSupport {
19 19 "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))";
20 20  
21 21 private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, "+
22   - "NAME_R VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(RIGHT_ID), PARENT INT, FOREIGN KEY(PARENT) REFERENCES RIGHT_T(RIGHT_ID))";
  22 + "NAME_R VARCHAR(50) NOT NULL, PRIMARY KEY(RIGHT_ID), PARENT INT, FOREIGN KEY(PARENT) REFERENCES RIGHT_T(RIGHT_ID))";
23 23  
24 24 private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, "+
25 25 "NAME_U VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID), GROUP_U INT, FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID)) ";
... ...
src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java
... ... @@ -27,7 +27,7 @@ public class RightServiceTest extends AbstractServiceSupport {
27 27 public void testCreateTwoParentRightsWithSameNameAndFindByName() {
28 28 rightService.create("right");
29 29 rightService.create("right");
30   - assertEquals(2, rightService.findByName("right").size());
  30 + assertEquals(1, rightService.findByName("right").size());
31 31 }
32 32  
33 33 @Test(expected = IllegalArgumentException.class)
... ... @@ -82,7 +82,7 @@ public class RightServiceTest extends AbstractServiceSupport {
82 82 Right parent = rightService.create("parent");
83 83 rightService.create("sibling", parent);
84 84 rightService.delete(parent);
85   - assertEquals(0, rightService.findByName("sibling").size());
  85 + assertEquals(1, rightService.findByName("sibling").size());
86 86 assertEquals(0, rightService.findByName("parent").size());
87 87 }
88 88  
... ...
src/test/resources/application.properties
... ... @@ -2,4 +2,4 @@ logging.level.org.springframework=INFO
2 2 logging.level.org.hibernate.SQL=DEBUG
3 3 logging.level.org.hibernate=INFO
4 4 spring.datasource.url=jdbc:h2:mem:persistence;TRACE_LEVEL_FILE=4
5   -spring.datasource.maxActive=300
6 5 \ No newline at end of file
  6 +spring.datasource.tomcat.max-active=500
7 7 \ No newline at end of file
... ...