Commit 4e3e24c9a6011960fb04d9409a9333c25532aadc
1 parent
60e63100
Amélioration des tests de Group
Showing
3 changed files
with
42 additions
and
4 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
@@ -11,6 +11,7 @@ import javax.sql.DataSource; | @@ -11,6 +11,7 @@ import javax.sql.DataSource; | ||
11 | import java.sql.ResultSet; | 11 | import java.sql.ResultSet; |
12 | import java.sql.SQLException; | 12 | import java.sql.SQLException; |
13 | import java.sql.Statement; | 13 | import java.sql.Statement; |
14 | +import java.util.List; | ||
14 | 15 | ||
15 | @Repository | 16 | @Repository |
16 | public class GroupRepositoryJdbc implements GroupRepository { | 17 | public class GroupRepositoryJdbc implements GroupRepository { |
@@ -63,7 +64,7 @@ public class GroupRepositoryJdbc implements GroupRepository { | @@ -63,7 +64,7 @@ public class GroupRepositoryJdbc implements GroupRepository { | ||
63 | stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')", | 64 | stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')", |
64 | Statement.RETURN_GENERATED_KEYS); | 65 | Statement.RETURN_GENERATED_KEYS); |
65 | rs = stmt.getGeneratedKeys(); | 66 | rs = stmt.getGeneratedKeys(); |
66 | - if (rs.next()) { | 67 | + if (rs.next()) { |
67 | group.setId(rs.getLong(1)); | 68 | group.setId(rs.getLong(1)); |
68 | } else { | 69 | } else { |
69 | throw new UnsupportedOperationException("default in key access"); | 70 | throw new UnsupportedOperationException("default in key access"); |
@@ -113,8 +114,36 @@ public class GroupRepositoryJdbc implements GroupRepository { | @@ -113,8 +114,36 @@ public class GroupRepositoryJdbc implements GroupRepository { | ||
113 | 114 | ||
114 | @Override | 115 | @Override |
115 | public boolean addRight(String groupName, Right right){ | 116 | public boolean addRight(String groupName, Right right){ |
116 | - | ||
117 | - return false; | 117 | + Statement stmt = null; |
118 | + ResultSet rs = null; | ||
119 | + try{ | ||
120 | + stmt = dataSource.getConnection().createStatement(); | ||
121 | + if(this.findByName(groupName) != null && right.getId() != null) | ||
122 | + stmt.executeUpdate("INSERT INTO GROUP_RIGHTS VALUES("+this.findByName(groupName).getId()+","+right.getId()+")", | ||
123 | + Statement.RETURN_GENERATED_KEYS); | ||
124 | + rs = stmt.getGeneratedKeys(); | ||
125 | + if(rs.next()){ | ||
126 | + List<Right> rights = this.findByName(groupName).getRights(); | ||
127 | + rights.add(right); | ||
128 | + this.findByName(groupName).setRights(rights); | ||
129 | + return true; | ||
130 | + }else{ | ||
131 | + return false; | ||
132 | + } | ||
133 | + }catch(SQLException e){ | ||
134 | + throw new UnsupportedOperationException("sql exception", e); | ||
135 | + }finally{ | ||
136 | + try{ | ||
137 | + if(rs != null){ | ||
138 | + rs.close(); | ||
139 | + } | ||
140 | + if(stmt != null){ | ||
141 | + stmt.close(); | ||
142 | + } | ||
143 | + }catch(SQLException e){ | ||
144 | + throw new UnsupportedOperationException("sql exception during close", e); | ||
145 | + } | ||
146 | + } | ||
118 | } | 147 | } |
119 | 148 | ||
120 | public boolean removeRight(String groupName, Right right){ | 149 | public boolean removeRight(String groupName, Right right){ |
src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java
@@ -24,11 +24,18 @@ public abstract class AbstractServiceSupport { | @@ -24,11 +24,18 @@ public abstract class AbstractServiceSupport { | ||
24 | private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, "+ | 24 | private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, "+ |
25 | "NAME_U VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID), GROUP_U INT, FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID)) "; | 25 | "NAME_U VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID), GROUP_U INT, FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID)) "; |
26 | 26 | ||
27 | + private static final String create_table_rg = "CREATE TABLE GROUP_RIGHTS (G_ID INT NOT NULL, R_ID INT NOT NULL,"+ | ||
28 | + "PRIMARY KEY(G_ID,R_ID),"+ | ||
29 | + "FOREIGN KEY(G_ID) REFERENCES GROUP_T(GROUP_ID),"+ | ||
30 | + "FOREIGN KEY(R_ID) REFERENCES RIGHT_T(RIGHT_ID))"; | ||
31 | + | ||
27 | private static final String drop_table_group = "DROP TABLE GROUP_T"; | 32 | private static final String drop_table_group = "DROP TABLE GROUP_T"; |
28 | 33 | ||
29 | private static final String drop_table_right = "DROP TABLE RIGHT_T"; | 34 | private static final String drop_table_right = "DROP TABLE RIGHT_T"; |
30 | 35 | ||
31 | private static final String drop_table_user = "DROP TABLE USER_T"; | 36 | private static final String drop_table_user = "DROP TABLE USER_T"; |
37 | + | ||
38 | + private static final String drop_table_rg = "DROP TABLE GROUP_RIGHTS"; | ||
32 | 39 | ||
33 | @Autowired | 40 | @Autowired |
34 | private DataSource dataSource; | 41 | private DataSource dataSource; |
@@ -44,6 +51,7 @@ public abstract class AbstractServiceSupport { | @@ -44,6 +51,7 @@ public abstract class AbstractServiceSupport { | ||
44 | stmt.executeUpdate(create_table_group); | 51 | stmt.executeUpdate(create_table_group); |
45 | stmt.executeUpdate(create_table_right); | 52 | stmt.executeUpdate(create_table_right); |
46 | stmt.executeUpdate(create_table_user); | 53 | stmt.executeUpdate(create_table_user); |
54 | + stmt.executeUpdate(create_table_rg); | ||
47 | closeConnection(); | 55 | closeConnection(); |
48 | } | 56 | } |
49 | 57 | ||
@@ -54,6 +62,7 @@ public abstract class AbstractServiceSupport { | @@ -54,6 +62,7 @@ public abstract class AbstractServiceSupport { | ||
54 | stmt.executeUpdate(drop_table_group); | 62 | stmt.executeUpdate(drop_table_group); |
55 | stmt.executeUpdate(drop_table_right); | 63 | stmt.executeUpdate(drop_table_right); |
56 | stmt.executeUpdate(drop_table_user); | 64 | stmt.executeUpdate(drop_table_user); |
65 | + stmt.executeUpdate(drop_table_rg); | ||
57 | closeConnection(); | 66 | closeConnection(); |
58 | } | 67 | } |
59 | 68 |
src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java
@@ -81,7 +81,7 @@ public class GroupServiceTest extends AbstractServiceSupport { | @@ -81,7 +81,7 @@ public class GroupServiceTest extends AbstractServiceSupport { | ||
81 | public void deleteGroupDoesDeleteUsers() { | 81 | public void deleteGroupDoesDeleteUsers() { |
82 | logger.info("deleteGroupDoesDeleteUsers"); | 82 | logger.info("deleteGroupDoesDeleteUsers"); |
83 | userService.create("user1", "group"); | 83 | userService.create("user1", "group"); |
84 | - userService.create("user1", "group"); | 84 | + userService.create("user2", "group"); |
85 | assertNotNull(userService.findByName("user1")); | 85 | assertNotNull(userService.findByName("user1")); |
86 | assertNotNull(userService.findByName("user2")); | 86 | assertNotNull(userService.findByName("user2")); |
87 | groupService.delete("group"); | 87 | groupService.delete("group"); |