From 7940aa2284e3b83e74d346fd60fbf5e8b8113a2f Mon Sep 17 00:00:00 2001 From: Julien Iguchi-Cartigny Date: Mon, 18 Jul 2016 14:21:01 +0200 Subject: [PATCH] Finishing API javadocs --- src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java | 12 ++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java | 32 ++++++++++++++++++++------------ src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java | 1 + src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java | 12 ------------ src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java | 91 ------------------------------------------------------------------------------------------- src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java | 2 +- src/main/resources/schema.sql | 1 - src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java | 48 +++++++++++++++++++++++++++++++++++------------- src/test/resources/application.properties | 4 +++- 10 files changed, 163 insertions(+), 131 deletions(-) create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java delete mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java delete mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java new file mode 100644 index 0000000..d13f520 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java @@ -0,0 +1,12 @@ +package fr.plil.sio.persistence.jdbc; + +import fr.plil.sio.persistence.api.Group; + +public interface GroupRepository { + + Group findByName(String name); + + void delete(Long id); + + void save(Group group); +} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java new file mode 100644 index 0000000..d121550 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java @@ -0,0 +1,91 @@ +package fr.plil.sio.persistence.jdbc; + +import fr.plil.sio.persistence.api.Group; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +@Repository +public class GroupRepositoryJdbc implements GroupRepository { + + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); + + @Autowired + private DataSource dataSource; + + @Override + public Group findByName(String name) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'"); + if (rs.next()) { + logger.debug("found group " + name); + Group group = new Group(); + group.setId(rs.getLong("GROUP_ID")); + group.setName(rs.getString("NAME_C")); + return group; + } else { + logger.debug("not found " + name); + 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); + + } + } + } + + @Override + public void save(Group group) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')", + Statement.RETURN_GENERATED_KEYS); + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + group.setId(rs.getLong(1)); + } else { + throw new UnsupportedOperationException("default in key access"); + } + } 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); + } + } + + } + + @Override + public void delete(Long id) { + throw new IllegalStateException("not implemented !"); + } +} 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 3908935..e02e4b5 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java @@ -3,13 +3,28 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.GroupService; import fr.plil.sio.persistence.api.Right; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class GroupServiceJdbc implements GroupService { + + @Autowired + private GroupRepository groupRepository; + @Override public Group create(String name) { - return null; + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + Group group = groupRepository.findByName(name); + if (group != null) { + throw new IllegalStateException("a group with the same name already exists"); + } + group = new Group(); + group.setName(name); + groupRepository.save(group); + return group; } @Override @@ -19,17 +34,10 @@ public class GroupServiceJdbc implements GroupService { @Override public Group findByName(String name) { - return null; - } - - @Override - public boolean addUser(String groupName, String userName) { - return false; - } - - @Override - public boolean removeUser(String groupName, String userName) { - return false; + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + return groupRepository.findByName(name); } @Override diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java b/src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java index 4feac18..e44da59 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java @@ -4,4 +4,5 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JdbcApplication { + } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java deleted file mode 100644 index 77b11dd..0000000 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package fr.plil.sio.persistence.jdbc; - -import fr.plil.sio.persistence.api.User; - -public interface UserRepository { - - User findByName(String name); - - void delete(Long id); - - void save(User user); -} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java deleted file mode 100644 index 3733904..0000000 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java +++ /dev/null @@ -1,91 +0,0 @@ -package fr.plil.sio.persistence.jdbc; - -import fr.plil.sio.persistence.api.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import javax.sql.DataSource; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -@Repository -public class UserRepositoryJdbc implements UserRepository { - - private static final Logger logger = LoggerFactory.getLogger(UserRepositoryJdbc.class); - - @Autowired - private DataSource dataSource; - - @Override - public User findByName(String name) { - Statement stmt = null; - ResultSet rs = null; - try { - stmt = dataSource.getConnection().createStatement(); - rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'"); - if (rs.next()) { - logger.debug("found user " + name); - User user = new User(); - user.setId(rs.getLong("GROUP_ID")); - user.setName(rs.getString("NAME_C")); - return user; - } else { - logger.debug("not found " + name); - 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); - - } - } - } - - @Override - public void delete(Long id) { - throw new IllegalStateException("not implemented !"); - } - - @Override - public void save(User user) { - Statement stmt = null; - ResultSet rs = null; - try { - stmt = dataSource.getConnection().createStatement(); - stmt.executeUpdate("INSERT INTO USER_T (NAME_C) VALUES (\'" + user.getName() + "\')", - Statement.RETURN_GENERATED_KEYS); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - user.setId(rs.getLong(1)); - } else { - throw new UnsupportedOperationException("default in key access"); - } - } 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/UserServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java index 1ec0396..a93f868 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java @@ -10,7 +10,7 @@ import org.springframework.stereotype.Service; public class UserServiceJdbc implements UserService { @Autowired - private UserRepository userRepository; + private GroupRepository userRepository; @Override public User create(String name, String groupName) { diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index d25299c..c127354 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,2 +1 @@ -CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID)); CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID)); 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 8f7a19c..8f63106 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java @@ -1,45 +1,77 @@ package fr.plil.sio.persistence.jdbc; +import fr.plil.sio.persistence.api.Group; +import fr.plil.sio.persistence.api.GroupService; +import fr.plil.sio.persistence.api.UserService; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static org.junit.Assert.*; + @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = JdbcApplication.class) public class GroupServiceTest { + @Autowired + private GroupService groupService; + + @Autowired + private UserService userService; + + @Before + public void before() { + groupService.create("group"); + } + @Test public void testCreateGroupAndFindByName() { + Group group = groupService.findByName("group"); + assertEquals("group", group.getName()); } @Test(expected = IllegalArgumentException.class) public void testCreateGroupFailsWhenNameNull() { + groupService.create(null); } @Test(expected = IllegalStateException.class) public void testCreateFailsWhenSameGroupUserAlreadyPresent() { + groupService.create("group"); } public void testDeleteGroup() { - } - - public void testDeleteGroupIfNotFound() { + assertTrue(groupService.delete("group")); + assertNull(groupService.findByName("group")); + assertFalse(groupService.delete("unknown")); } @Test(expected = IllegalArgumentException.class) public void testDeleteGroupFailsIfNameNull() { + groupService.delete(null); } @Test public void deleteGroupDoesDeleteUsers() { + userService.create("user1", "group"); + userService.create("user1", "group"); + assertNotNull(userService.findByName("user1")); + assertNotNull(userService.findByName("user2")); + groupService.delete("group"); + assertNull(userService.findByName("user1")); + assertNull(userService.findByName("user2")); } public void testFindByNameIfUserNotFound() { + assertNull(groupService.findByName("unknown")); } @Test(expected = IllegalArgumentException.class) public void testFindByNameFailsIfNameNull() { + groupService.findByName(null); } @Test @@ -72,16 +104,6 @@ public class GroupServiceTest { } - - /** - * Remove a right associated with a group. - * - * @param groupName the name of the group. - * @param right the right to remove - * @return true if right is removed from the group, false if teh right was not present in the group. - * @throws IllegalArgumentException if groupName or right is null, or if group or right cannot be found. - */ - @Test public void testRemoveRight() { diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 72a0e8b..683cf54 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,3 +1,5 @@ logging.level.org.springframework=INFO logging.level.org.hibernate.SQL=DEBUG -logging.level.org.hibernate=INFO \ No newline at end of file +logging.level.org.hibernate=INFO +#spring.datasource.default-auto-commit=false +#spring.datasource.auto-commit=false \ No newline at end of file -- libgit2 0.21.2