From 4e3e24c9a6011960fb04d9409a9333c25532aadc Mon Sep 17 00:00:00 2001 From: msahmane Date: Mon, 26 Sep 2016 12:28:26 +0200 Subject: [PATCH] Amélioration des tests de Group --- src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java | 35 ++++++++++++++++++++++++++++++++--- src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java | 9 +++++++++ src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java index ea6f764..895dc23 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java @@ -11,6 +11,7 @@ import javax.sql.DataSource; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.List; @Repository public class GroupRepositoryJdbc implements GroupRepository { @@ -63,7 +64,7 @@ public class GroupRepositoryJdbc implements GroupRepository { stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')", Statement.RETURN_GENERATED_KEYS); rs = stmt.getGeneratedKeys(); - if (rs.next()) { + if (rs.next()) { group.setId(rs.getLong(1)); } else { throw new UnsupportedOperationException("default in key access"); @@ -113,8 +114,36 @@ public class GroupRepositoryJdbc implements GroupRepository { @Override public boolean addRight(String groupName, Right right){ - - return false; + Statement stmt = null; + ResultSet rs = null; + try{ + stmt = dataSource.getConnection().createStatement(); + if(this.findByName(groupName) != null && right.getId() != null) + stmt.executeUpdate("INSERT INTO GROUP_RIGHTS VALUES("+this.findByName(groupName).getId()+","+right.getId()+")", + Statement.RETURN_GENERATED_KEYS); + rs = stmt.getGeneratedKeys(); + if(rs.next()){ + List rights = this.findByName(groupName).getRights(); + rights.add(right); + this.findByName(groupName).setRights(rights); + return true; + }else{ + return false; + } + }catch(SQLException e){ + throw new UnsupportedOperationException("sql exception", e); + }finally{ + try{ + if(rs != null){ + rs.close(); + } + if(stmt != null){ + stmt.close(); + } + }catch(SQLException e){ + throw new UnsupportedOperationException("sql exception during close", e); + } + } } public boolean removeRight(String groupName, Right right){ diff --git a/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java b/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java index 532e73b..f11829b 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java @@ -24,11 +24,18 @@ public abstract class AbstractServiceSupport { private static final String create_table_user = "CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, "+ "NAME_U VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID), GROUP_U INT, FOREIGN KEY(GROUP_U) REFERENCES GROUP_T(GROUP_ID)) "; + private static final String create_table_rg = "CREATE TABLE GROUP_RIGHTS (G_ID INT NOT NULL, R_ID INT NOT NULL,"+ + "PRIMARY KEY(G_ID,R_ID),"+ + "FOREIGN KEY(G_ID) REFERENCES GROUP_T(GROUP_ID),"+ + "FOREIGN KEY(R_ID) REFERENCES RIGHT_T(RIGHT_ID))"; + private static final String drop_table_group = "DROP TABLE GROUP_T"; private static final String drop_table_right = "DROP TABLE RIGHT_T"; private static final String drop_table_user = "DROP TABLE USER_T"; + + private static final String drop_table_rg = "DROP TABLE GROUP_RIGHTS"; @Autowired private DataSource dataSource; @@ -44,6 +51,7 @@ public abstract class AbstractServiceSupport { stmt.executeUpdate(create_table_group); stmt.executeUpdate(create_table_right); stmt.executeUpdate(create_table_user); + stmt.executeUpdate(create_table_rg); closeConnection(); } @@ -54,6 +62,7 @@ public abstract class AbstractServiceSupport { stmt.executeUpdate(drop_table_group); stmt.executeUpdate(drop_table_right); stmt.executeUpdate(drop_table_user); + stmt.executeUpdate(drop_table_rg); closeConnection(); } diff --git a/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java b/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java index 25b4f61..1d21052 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java @@ -81,7 +81,7 @@ public class GroupServiceTest extends AbstractServiceSupport { public void deleteGroupDoesDeleteUsers() { logger.info("deleteGroupDoesDeleteUsers"); userService.create("user1", "group"); - userService.create("user1", "group"); + userService.create("user2", "group"); assertNotNull(userService.findByName("user1")); assertNotNull(userService.findByName("user2")); groupService.delete("group"); -- libgit2 0.21.2