Commit fa02d31b92e42f58a2b8ee4031835f4249e3fc10

Authored by rvangrev
1 parent af60b9d2

Commit avant le merge.

Debut des tests sur le user.
Creation nouvelle table pour les groupes et les droits.
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... ... @@ -64,17 +64,18 @@ public class GroupServiceJdbc implements GroupService {
64 64 throw new IllegalArgumentException("group cannot be null");
65 65 }
66 66  
67   - group.setName(groupName);
68   - List<Right> list = group.getRights();
  67 + Group g = groupRepository.findByName(groupName);
  68 + List<Right> list = g.getRights();
69 69 list.add(right);
70   - group.setRights(list);
  70 +// g.setRights(list);
  71 +
71 72 return true;
72 73 }
73 74  
74 75 @Override
75 76 public boolean removeRight(String groupName, Right right) {
76 77 if(right==null){
77   - throw new IllegalArgumentException("name cannot be null");
  78 + throw new IllegalArgumentException("right cannot be null");
78 79 }
79 80 Group group = groupRepository.findByName(groupName);
80 81 if (group != null) {
... ... @@ -82,4 +83,9 @@ public class GroupServiceJdbc implements GroupService {
82 83 }
83 84 return true;
84 85 }
  86 +
  87 + public List<Right> getListRight(String groupName){
  88 + Group g = groupRepository.findByName(groupName);
  89 + return g.getRights();
  90 + }
85 91 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java
... ... @@ -5,13 +5,16 @@
5 5 */
6 6 package fr.plil.sio.persistence.jdbc;
7 7  
  8 +import java.util.List;
8 9 import fr.plil.sio.persistence.api.Right;
9 10  
10 11 public interface RightRepository {
11 12  
12   - Right findByName(String name);
  13 + List<Right> findByName(String name);
13 14  
14   - void delete(Long id);
  15 + Right findOne(Long id);
  16 +
  17 + boolean delete(Long id);
15 18  
16   - void save(Right right);
  19 + void save(Right right, int parentId);
17 20 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
... ... @@ -5,6 +5,8 @@
5 5 */
6 6 package fr.plil.sio.persistence.jdbc;
7 7  
  8 +import java.util.List;
  9 +import java.util.ArrayList;
8 10 import fr.plil.sio.persistence.api.Group;
9 11 import java.sql.ResultSet;
10 12 import java.sql.SQLException;
... ... @@ -14,12 +16,14 @@ import org.slf4j.Logger;
14 16 import org.slf4j.LoggerFactory;
15 17 import org.springframework.beans.factory.annotation.Autowired;
16 18 import fr.plil.sio.persistence.api.Right;
  19 +import org.springframework.stereotype.Repository;
17 20  
18   -/**
19   - *
20   - * @author rvangrev
21   - */
22   -public class RightRepositoryJdbc implements RightRepository{
  21 +@Repository
  22 +public class RightRepositoryJdbc implements RightRepository {
  23 +
  24 +
  25 + private GroupRepository groupRepository;
  26 + private RightRepository rightRepository;
23 27  
24 28 private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
25 29  
... ... @@ -27,22 +31,56 @@ public class RightRepositoryJdbc implements RightRepository{
27 31 private DataSource dataSource;
28 32  
29 33 @Override
30   - public Right findByName(String name) {
  34 + public List<Right> findByName(String name) {
31 35 Statement stmt = null;
32 36 ResultSet rs = null;
33 37 try {
34 38 stmt = dataSource.getConnection().createStatement();
35   - rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'");
36   - if (rs.next()) {
  39 + rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE NAME_R = \'" + name + "\'");
  40 + ArrayList<Right> list = new ArrayList<Right>();
  41 + while (rs.next()) {
37 42 logger.debug("found group " + name);
38   -// Group group = new Group();
39   -// group.setId(rs.getLong("GROUP_ID"));
40   -// group.setName(rs.getString("NAME_C"));
41   -// return group;
42   - } else {
43   - logger.debug("not found " + name);
44   - return null;
  43 + Right right = new Right();
  44 + right.setId(rs.getLong("RIGHT_ID"));
  45 + right.setName(rs.getString("NAME_R"));
  46 + if(right!=null){
  47 + list.add(right);
  48 + }
  49 + }
  50 + return list;
  51 + } catch (SQLException e) {
  52 + throw new UnsupportedOperationException("sql exception", e);
  53 + } finally {
  54 + try {
  55 + if (rs != null) {
  56 + rs.close();
  57 + }
  58 + if (stmt != null) {
  59 + stmt.close();
  60 + }
  61 + } catch (SQLException e) {
  62 + throw new UnsupportedOperationException("sql exception during close", e);
  63 +
45 64 }
  65 + }
  66 + }
  67 +
  68 + @Override
  69 + public Right findOne(Long id){
  70 + Statement stmt = null;
  71 + ResultSet rs = null;
  72 + try {
  73 + stmt = dataSource.getConnection().createStatement();
  74 + rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' LIMIT 1");
  75 +
  76 + if (rs.next()) {
  77 + logger.debug("found id " + id);
  78 + Right right = new Right();
  79 + right.setId(rs.getLong("RIGHT_ID"));
  80 + right.setName(rs.getString("NAME_R"));
  81 + return right;
  82 + }
  83 +
46 84 } catch (SQLException e) {
47 85 throw new UnsupportedOperationException("sql exception", e);
48 86 } finally {
... ... @@ -62,14 +100,15 @@ public class RightRepositoryJdbc implements RightRepository{
62 100 }
63 101  
64 102 @Override
65   - public void save(Right right) {
  103 + public void save(Right right, int parentId) {
66 104 Statement stmt = null;
67 105 ResultSet rs = null;
68 106 try {
69 107 stmt = dataSource.getConnection().createStatement();
70   -
71   - stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C, PARENT) VALUES (\'" + right.getName() + "\', \'" + right.getParent().getId() + "\')",
  108 +
  109 + stmt.executeUpdate("INSERT INTO RIGHT_T (NAME_R, PARENT_ID) VALUES (\'" + right.getName() + "\', "+ parentId +")",
72 110 Statement.RETURN_GENERATED_KEYS);
  111 +
73 112 rs = stmt.getGeneratedKeys();
74 113 if (rs.next()) {
75 114 right.setId(rs.getLong(1));
... ... @@ -94,15 +133,18 @@ public class RightRepositoryJdbc implements RightRepository{
94 133 }
95 134  
96 135 @Override
97   - public void delete(Long id) {
  136 + public boolean delete(Long id) {
98 137 Statement stmt = null;
99 138 ResultSet rs = null;
100 139 if(id==null){
101   - throw new IllegalArgumentException();
  140 + return false;
  141 + }
  142 + if(findOne(id)==null){
  143 + return false;
102 144 }
103 145 try {
104 146 stmt = dataSource.getConnection().createStatement();
105   - stmt.execute("DELETE FROM RIGHT_T WHERE RIGHT_ID = " + id);
  147 + stmt.execute("DELETE FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' OR PARENT_ID = " + id + "");
106 148 } catch (SQLException e) {
107 149 throw new UnsupportedOperationException("sql exception", e);
108 150 } finally {
... ... @@ -117,5 +159,6 @@ public class RightRepositoryJdbc implements RightRepository{
117 159 throw new UnsupportedOperationException("sql exception during close", e);
118 160 }
119 161 }
  162 + return true;
120 163 }
121 164 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... ... @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.jdbc;
3 3 import fr.plil.sio.persistence.api.Right;
4 4 import fr.plil.sio.persistence.api.RightService;
5 5 import org.springframework.stereotype.Service;
  6 +import java.lang.Math;
6 7  
7 8 import java.util.List;
8 9 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -11,7 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired;
11 12 public class RightServiceJdbc implements RightService {
12 13  
13 14 @Autowired
14   - private GroupRepository groupRepository;
15 15 private RightRepository rightRepository;
16 16  
17 17 @Override
... ... @@ -21,25 +21,28 @@ public class RightServiceJdbc implements RightService {
21 21 }
22 22 Right right = new Right();
23 23 right.setName(name);
24   - if(right==null){
25   - throw new IllegalArgumentException("name cannot be null");
26   - }
27   - rightRepository.save(right);
  24 + rightRepository.save(right, 0);
  25 +
28 26 return right;
29 27 }
30 28  
31 29 @Override
32 30 public Right create(String name, Right parent) {
33   - if(name==null || parent.getId()==null){
  31 + if(name==null){
34 32 throw new IllegalArgumentException("name cannot be null");
35 33 }
  34 + if(parent==null){
  35 + throw new IllegalArgumentException("parent cannot be null");
  36 + }
  37 + if (parent.getId()==null){
  38 + throw new IllegalArgumentException("parent id cannot be null");
  39 + }
  40 +
36 41 Right right = new Right();
37 42 right.setName(name);
38 43 right.setParent(parent);
39   - if(right==null){
40   - throw new IllegalArgumentException("name cannot be null");
41   - }
42   - rightRepository.save(right);
  44 + int parentID = Math.toIntExact(parent.getId());
  45 + rightRepository.save(right, parentID);
43 46 return right;
44 47 }
45 48  
... ... @@ -49,13 +52,15 @@ public class RightServiceJdbc implements RightService {
49 52 throw new IllegalArgumentException("name cannot be null");
50 53 }
51 54  
52   - rightRepository.delete(right.getId());
53   - return true;
  55 + return rightRepository.delete(right.getId());
54 56 }
55 57  
56 58 @Override
57 59 public List<Right> findByName(String name) {
58   - throw new IllegalStateException("not implemented !");
  60 + if(name==null){
  61 + throw new IllegalArgumentException("name cannot be null");
  62 + }
  63 + return rightRepository.findByName(name);
59 64 }
60 65  
61 66 @Override
... ... @@ -63,8 +68,7 @@ public class RightServiceJdbc implements RightService {
63 68 if(id==null){
64 69 throw new IllegalArgumentException("name cannot be null");
65 70 }
66   - else{
67   - return null;
68   - }
  71 + return rightRepository.findOne(id);
  72 +
69 73 }
70 74 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
... ... @@ -22,6 +22,9 @@ public class UserRepositoryJdbc implements UserRepository{
22 22 private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
23 23  
24 24 @Autowired
  25 + private GroupRepository groupRepository;
  26 +
  27 + @Autowired
25 28 private DataSource dataSource;
26 29  
27 30 @Override
... ... @@ -30,15 +33,13 @@ public class UserRepositoryJdbc implements UserRepository{
30 33 ResultSet rs = null;
31 34 try {
32 35 stmt = dataSource.getConnection().createStatement();
33   - rs = stmt.executeQuery("SELECT U.USER_ID, U.NAME_U, U.GROUP_U, G.NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_U=G.GROUP_ID WHERE NAME_U = \'" + name + "\' ");
  36 + rs = stmt.executeQuery("SELECT USER_ID, NAME_U, U.GROUP_ID, NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_ID=G.GROUP_ID");
34 37 if (rs.next()) {
35 38 logger.debug("found group " + name);
36 39 User user = new User();
37   - user.setId(rs.getLong("T.USER_ID"));
38   - user.setName(rs.getString("T.NAME_U"));
39   -// Group group = new Group();
40   -// group = g.findByName(rs.getString("G.NAME_C"));
41   -// user.setGroup(group);
  40 + user.setId(rs.getLong("USER_ID"));
  41 + user.setName(rs.getString("NAME_U"));
  42 + user.setGroup(groupRepository.findByName("NAME_C"));
42 43 return user;
43 44 } else {
44 45 logger.debug("not found " + name);
... ... @@ -67,7 +68,12 @@ public class UserRepositoryJdbc implements UserRepository{
67 68 ResultSet rs = null;
68 69 try {
69 70 stmt = dataSource.getConnection().createStatement();
70   - stmt.executeUpdate("INSERT INTO USER_T (NAME_U, GROUP_U) VALUES (\'" + user.getName() + "\', \'" + group.getId() + "\')",
  71 +
  72 + int groupid=0;
  73 + if(group!=null){
  74 + groupid = Math.toIntExact(group.getId());
  75 + }
  76 + stmt.executeUpdate("INSERT INTO USER_T (NAME_U, GROUP_ID) VALUES (\'" + user.getName() + "\', " + groupid + ")",
71 77 Statement.RETURN_GENERATED_KEYS);
72 78 rs = stmt.getGeneratedKeys();
73 79 if (rs.next()) {
... ...
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
... ... @@ -12,6 +12,8 @@ public class UserServiceJdbc implements UserService {
12 12  
13 13 @Autowired
14 14 private UserRepository userRepository;
  15 +
  16 + @Autowired
15 17 private GroupRepository groupRepository;
16 18  
17 19 @Override
... ... @@ -21,13 +23,17 @@ public class UserServiceJdbc implements UserService {
21 23 }
22 24 User user = userRepository.findByName(name);
23 25 if(user!=null){
24   - throw new IllegalStateException("a user with the same name already exists");
  26 + throw new IllegalArgumentException("a user with the same name already exists");
25 27 }
26 28 user = new User();
27 29 user.setName(name);
28 30 Group group = new Group();
29   - group.setName(groupName);
30   -// Group group = groupRepository.findByName(groupName);
  31 + group = groupRepository.findByName(groupName);
  32 + if(group!=null)
  33 + user.setGroup(group);
  34 + else{
  35 + throw new IllegalArgumentException("group trouve avec le find by name null");
  36 + }
31 37 userRepository.save(user, group);
32 38 return user;
33 39 }
... ... @@ -35,7 +41,7 @@ public class UserServiceJdbc implements UserService {
35 41 @Override
36 42 public boolean delete(String name) {
37 43 if(name == null){
38   - throw new IllegalStateException("not implemented !");
  44 + throw new IllegalArgumentException("name cannot be null");
39 45 }
40 46 User user = findByName(name);
41 47 if(user==null){
... ... @@ -55,9 +61,12 @@ public class UserServiceJdbc implements UserService {
55 61  
56 62 @Override
57 63 public boolean isUserHasRight(String userName, Right right) {
58   - if(right==null){
  64 + if(userName==null){
59 65 throw new IllegalArgumentException("name cannot be null");
60 66 }
  67 + if(right==null){
  68 + throw new IllegalArgumentException("right cannot be null");
  69 + }
61 70 else{
62 71 return false;
63 72 }
... ...
src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java
... ... @@ -15,15 +15,18 @@ public abstract class AbstractServiceSupport {
15 15 "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))";
16 16  
17 17 private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, " +
18   - "NAME_R VARCHAR(50) UNIQUE NOT NULL, PARENT INT, PRIMARY KEY(RIGHT_ID))";
  18 + "NAME_R VARCHAR(50) NOT NULL, PARENT_ID INT, PRIMARY KEY(RIGHT_ID))";
19 19  
20 20 private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, " +
21   - "NAME_U VARCHAR(50) UNIQUE NOT NULL, GROUP_U INT, PRIMARY KEY(USER_ID), " +
22   - "FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID))";
  21 + "NAME_U VARCHAR(50) UNIQUE NOT NULL, GROUP_ID INT, PRIMARY KEY(USER_ID))";
  22 +
  23 + private static final String create_table_link = "CREATE TABLE LINK_T (RIGHT_ID INT NOT NULL, GROUP_ID INT NOT NULL, " +
  24 + "PRIMARY KEY(RIGHT_ID, GROUP_ID))";
23 25  
24 26 private static final String drop_table_group = "DROP TABLE GROUP_T";
25 27 private static final String drop_table_right = "DROP TABLE RIGHT_T";
26 28 private static final String drop_table_user = "DROP TABLE USER_T";
  29 + private static final String drop_table_link = "DROP TABLE LINK_T";
27 30  
28 31 @Autowired
29 32 private DataSource dataSource;
... ... @@ -38,6 +41,7 @@ public abstract class AbstractServiceSupport {
38 41 stmt.executeUpdate(create_table_group);
39 42 stmt.executeUpdate(create_table_right);
40 43 stmt.executeUpdate(create_table_user);
  44 + stmt.executeUpdate(create_table_link);
41 45 closeConnection();
42 46 }
43 47  
... ... @@ -47,6 +51,7 @@ public abstract class AbstractServiceSupport {
47 51 stmt.executeUpdate(drop_table_group);
48 52 stmt.executeUpdate(drop_table_right);
49 53 stmt.executeUpdate(drop_table_user);
  54 + stmt.executeUpdate(drop_table_link);
50 55 closeConnection();
51 56 }
52 57  
... ...
src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java
... ... @@ -119,7 +119,7 @@ public class RightServiceTest extends AbstractServiceSupport {
119 119 assertNull(rightService.findOne(153463167809232L));
120 120 }
121 121  
122   - @Test(expected = IllegalArgumentException.class)
  122 + @Test(expected = IllegalArgumentException.class)
123 123 public void testFindOneFailsIfIdNull() {
124 124 rightService.findOne(null);
125 125 }
... ...
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
... ... @@ -27,10 +27,10 @@ public class UserServiceTest extends AbstractServiceSupport {
27 27 public void before() {
28 28 groupService.create("group");
29 29 userService.create("user", "group");
30   -// Right right = rightService.create("parent");
31   -// rightService.create("sibling", right);
32   -// groupService.addRight("group", right);
33   -// rightService.create("not-for-me");
  30 + Right right = rightService.create("parent");
  31 + rightService.create("sibling", right);
  32 + groupService.addRight("group", right);
  33 + rightService.create("not-for-me");
34 34 }
35 35  
36 36 @Test
... ... @@ -70,6 +70,7 @@ public class UserServiceTest extends AbstractServiceSupport {
70 70  
71 71 @Test
72 72 public void testDeleteUserIfNotFound() {
  73 + userService.delete("user");
73 74 assertFalse(userService.delete("user"));
74 75 }
75 76  
... ...
src/test/resources/application.properties
... ... @@ -2,4 +2,5 @@ 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.maxActive=300
  7 +spring.datasource.tomcat.max-active=500
7 8 \ No newline at end of file
... ...