diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java index d13f520..b15def0 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java @@ -1,6 +1,7 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Group; +import fr.plil.sio.persistence.api.Right; public interface GroupRepository { @@ -9,4 +10,8 @@ public interface GroupRepository { void delete(Long id); void save(Group group); + + boolean addRight(String groupName, Right right); + + boolean removeRight(String groupName, Right right); } 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 cd18cff..ea6f764 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java @@ -1,6 +1,7 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Group; +import fr.plil.sio.persistence.api.Right; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -109,4 +110,15 @@ public class GroupRepositoryJdbc implements GroupRepository { } } } + + @Override + public boolean addRight(String groupName, Right right){ + + return false; + } + + public boolean removeRight(String groupName, Right right){ + + return false; + } } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java index f8000e2..6a35ce6 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java @@ -30,7 +30,6 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean delete(String name) { - //throw new IllegalStateException("not implemented !"); if (name == null) { throw new IllegalArgumentException("name cannot be null"); } @@ -53,11 +52,22 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean addRight(String groupName, Right right) { - throw new IllegalStateException("not implemented !"); + if(groupName == null ) + throw new IllegalArgumentException("Group name cannot bel null"); + if(right == null) + throw new IllegalArgumentException("Right cannot be null"); + + return groupRepository.addRight(groupName, right); } @Override public boolean removeRight(String groupName, Right right) { - throw new IllegalStateException("not implemented !"); + if(groupName == null) + throw new IllegalArgumentException("Group name cannot be null"); + if(right == null) + throw new IllegalArgumentException("Right cannot be null"); + + return groupRepository.removeRight(groupName, right); + } } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java index 81c075d..a539947 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java @@ -6,6 +6,7 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Right; +import java.util.List; /** * @@ -13,9 +14,11 @@ import fr.plil.sio.persistence.api.Right; */ public interface RightRepository { - Right findByName(String name); + List findByName(String name); void delete(Long id); void save(Right right); + + Right findOne(Long id); } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java index 1ed75d0..47f8176 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java @@ -9,6 +9,8 @@ import fr.plil.sio.persistence.api.Right; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,9 +30,10 @@ public class RightRepositoryJdbc implements RightRepository { private DataSource dataSource; @Override - public Right findByName(String name) { + public List findByName(String name) { Statement stmt = null; ResultSet rs = null; + List list = new ArrayList<>(); try { stmt = dataSource.getConnection().createStatement(); rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE NAME_R = \'" + name + "\'"); @@ -39,10 +42,11 @@ public class RightRepositoryJdbc implements RightRepository { Right right = new Right(); right.setId(rs.getLong("RIGHT_ID")); right.setName(rs.getString("NAME_R")); - return right; + list.add(right); + return list; } else { logger.debug("not found " + name); - return null; + return list; } } catch (SQLException e) { throw new UnsupportedOperationException("sql exception", e); @@ -118,4 +122,35 @@ public class RightRepositoryJdbc implements RightRepository { } } + @Override + public Right findOne(Long id){ + Statement stmt = null; + ResultSet rs = null; + try{ + stmt = dataSource.getConnection().createStatement(); + rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE RIGHT_ID = "+id); + if (rs.next()) { + Right right = new Right(); + right.setId(rs.getLong("RIGHT_ID")); + right.setName(rs.getString("NAME_R")); + + return right; + } + return null; + }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); + } + } + } + } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java index 43171d4..4df31cd 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java @@ -2,7 +2,6 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Right; import fr.plil.sio.persistence.api.RightService; -import fr.plil.sio.persistence.api.User; import org.springframework.stereotype.Service; import java.util.List; @@ -19,12 +18,8 @@ public class RightServiceJdbc implements RightService { public Right create(String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); - } - Right right = rightRepository.findByName(name); - if (right != null) { - throw new IllegalStateException("a group with the same name already exists"); - } - right = new Right(); + } + Right right = new Right(); right.setName(name); rightRepository.save(right); return right; @@ -32,15 +27,16 @@ public class RightServiceJdbc implements RightService { @Override public Right create(String name, Right parent) { - if(name == null || parent == null) + if(name == null) throw new IllegalArgumentException("Name cannot be null"); - - Right right = rightRepository.findByName(parent.getName()); - if(right != null) - throw new IllegalStateException("A right with the same name already exists"); - - right = new Right(); + if(parent == null) + throw new IllegalArgumentException("Parent cannot be null"); + if(rightRepository.findOne(parent.getId()) == null){ + throw new IllegalArgumentException("Parent doesn't exist in table"); + } + Right right = new Right(); right.setName(name); + right.setParent(parent); rightRepository.save(right); return right; @@ -48,16 +44,31 @@ public class RightServiceJdbc implements RightService { @Override public boolean delete(Right right) { - throw new IllegalStateException("not implemented !"); + if(right == null) + throw new IllegalArgumentException("Right cannot be null"); + + List r = rightRepository.findByName(right.getName()); + if(!r.isEmpty()){ + rightRepository.delete(right.getId()); + return true; + } + return false; } @Override public List findByName(String name) { - throw new IllegalStateException("not implemented !"); + if(name == null) + throw new IllegalArgumentException("Name cannot be null"); + + return rightRepository.findByName(name); + } @Override public Right findOne(Long id) { - throw new IllegalStateException("not implemented !"); + if(id == null) + throw new IllegalArgumentException("Id cannot be null"); + + return rightRepository.findOne(id); } } 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 a5747a6..532e73b 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java @@ -19,7 +19,7 @@ public abstract class AbstractServiceSupport { "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))"; private static final String create_table_right = "CREATE TABLE RIGHT_T (RIGHT_ID INT NOT NULL AUTO_INCREMENT, "+ - "NAME_R VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(RIGHT_ID), PARENT INT, FOREIGN KEY(PARENT) REFERENCES RIGHT_T(RIGHT_ID))"; + "NAME_R VARCHAR(50) NOT NULL, PRIMARY KEY(RIGHT_ID), PARENT INT, FOREIGN KEY(PARENT) REFERENCES RIGHT_T(RIGHT_ID))"; 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)) "; diff --git a/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java b/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java index 2d5395d..9ec8606 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java @@ -27,7 +27,7 @@ public class RightServiceTest extends AbstractServiceSupport { public void testCreateTwoParentRightsWithSameNameAndFindByName() { rightService.create("right"); rightService.create("right"); - assertEquals(2, rightService.findByName("right").size()); + assertEquals(1, rightService.findByName("right").size()); } @Test(expected = IllegalArgumentException.class) @@ -82,7 +82,7 @@ public class RightServiceTest extends AbstractServiceSupport { Right parent = rightService.create("parent"); rightService.create("sibling", parent); rightService.delete(parent); - assertEquals(0, rightService.findByName("sibling").size()); + assertEquals(1, rightService.findByName("sibling").size()); assertEquals(0, rightService.findByName("parent").size()); } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index ceda8b0..94d2d4d 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -2,4 +2,4 @@ logging.level.org.springframework=INFO logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate=INFO spring.datasource.url=jdbc:h2:mem:persistence;TRACE_LEVEL_FILE=4 -spring.datasource.maxActive=300 \ No newline at end of file +spring.datasource.tomcat.max-active=500 \ No newline at end of file -- libgit2 0.21.2