From 292aec6f6123cd2cc3584d721c788eae71b9cf37 Mon Sep 17 00:00:00 2001 From: Julien Iguchi-Cartigny Date: Sun, 11 Sep 2016 22:21:57 +0200 Subject: [PATCH] First version before first course --- src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java | 6 +++--- src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java | 10 +++++----- src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java | 8 ++++---- src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java | 65 +++++++++++++++++++++++++++++++++++++++-------------------------- src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java | 65 ++++++++++++++++++++++++++++++++++++++++++++++++----------------- src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 200 insertions(+), 56 deletions(-) create mode 100644 src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java 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 e02e4b5..971e8b1 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java @@ -29,7 +29,7 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean delete(String name) { - return false; + throw new IllegalStateException("not implemented !"); } @Override @@ -42,11 +42,11 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean addRight(String groupName, Right right) { - return false; + throw new IllegalStateException("not implemented !"); } @Override public boolean removeRight(String groupName, Right right) { - return false; + throw new IllegalStateException("not implemented !"); } } 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 a57b8c6..b1242a1 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; import java.util.List; @Service -public class RightServiceJdbc implements RightService{ +public class RightServiceJdbc implements RightService { @Override public Right create(String name) { return null; @@ -15,21 +15,21 @@ public class RightServiceJdbc implements RightService{ @Override public Right create(String name, Right parent) { - return null; + throw new IllegalStateException("not implemented !"); } @Override public boolean delete(Right right) { - return false; + throw new IllegalStateException("not implemented !"); } @Override public List findByName(String name) { - return null; + throw new IllegalStateException("not implemented !"); } @Override public Right findOne(Long id) { - return null; + throw new IllegalStateException("not implemented !"); } } 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 a93f868..e884a52 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java @@ -14,21 +14,21 @@ public class UserServiceJdbc implements UserService { @Override public User create(String name, String groupName) { - return null; + throw new IllegalStateException("not implemented !"); } @Override public boolean delete(String name) { - return false; + throw new IllegalStateException("not implemented !"); } @Override public User findByName(String name) { - return null; + throw new IllegalStateException("not implemented !"); } @Override public boolean isUserHasRight(String userName, Right right) { - return false; + throw new IllegalStateException("not implemented !"); } } diff --git a/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java b/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java new file mode 100644 index 0000000..1d4e1d4 --- /dev/null +++ b/src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java @@ -0,0 +1,49 @@ +package fr.plil.sio.persistence.jdbc; + +import org.junit.After; +import org.junit.Before; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +public abstract class AbstractServiceSupport { + + private static final String create_table_group = "CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, " + + "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))"; + + private static final String drop_table_group = "DROP TABLE GROUP_T"; + + @Autowired + private DataSource dataSource; + + private Connection connection; + + private Statement stmt; + + @Before + public void createTables() throws SQLException { + openConnection(); + stmt.executeUpdate(create_table_group); + closeConnection(); + } + + @After + public void cleanupDatabase() throws SQLException { + openConnection(); + stmt.executeUpdate(drop_table_group); + closeConnection(); + } + + private void closeConnection() throws SQLException { + stmt.close(); + connection.close(); + } + + private void openConnection() throws SQLException { + connection = dataSource.getConnection(); + stmt = connection.createStatement(); + } +} 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 8f63106..838f767 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java @@ -1,8 +1,6 @@ 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 fr.plil.sio.persistence.api.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -14,7 +12,7 @@ import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = JdbcApplication.class) -public class GroupServiceTest { +public class GroupServiceTest extends AbstractServiceSupport { @Autowired private GroupService groupService; @@ -22,8 +20,15 @@ public class GroupServiceTest { @Autowired private UserService userService; + @Autowired + private RightService rightService; + + private Right parent; + @Before public void before() { + parent = rightService.create("parent"); + rightService.create("sibling", parent); groupService.create("group"); } @@ -46,9 +51,14 @@ public class GroupServiceTest { public void testDeleteGroup() { assertTrue(groupService.delete("group")); assertNull(groupService.findByName("group")); - assertFalse(groupService.delete("unknown")); + assertFalse(groupService.delete("group")); } + public void testDeleteNotExistingGroup() { + assertFalse(groupService.delete("not-a-group")); + } + + @Test(expected = IllegalArgumentException.class) public void testDeleteGroupFailsIfNameNull() { groupService.delete(null); @@ -65,7 +75,7 @@ public class GroupServiceTest { assertNull(userService.findByName("user2")); } - public void testFindByNameIfUserNotFound() { + public void testFindByNameIfGroupNotFound() { assertNull(groupService.findByName("unknown")); } @@ -76,61 +86,64 @@ public class GroupServiceTest { @Test public void testAddRight() { - + assertTrue(groupService.addRight("group", parent)); + Group group = groupService.findByName("group"); + assertEquals(1, group.getRights().size()); + assertEquals("parent", group.getRights().get(0).getName()); + assertEquals(1, group.getRights().get(0).getSiblings().size()); + assertEquals("sibling", group.getRights().get(0).getSiblings().iterator().next().getName()); } @Test public void testAddRightIfAlreadyPresent() { - + assertTrue(groupService.addRight("group", parent)); + assertFalse(groupService.addRight("group", parent)); } @Test(expected = IllegalArgumentException.class) public void testAddRightFailsIfGroupNameNull() { - + groupService.addRight(null, parent); } @Test(expected = IllegalArgumentException.class) public void testAddRightFailsIfRightNull() { - + groupService.addRight("group", null); } @Test(expected = IllegalArgumentException.class) public void testAddRightFailsIfGroupNotInDatabase() { - - } - - @Test(expected = IllegalArgumentException.class) - public void testAddUserFailsIfRightNotInDatabase() { - + groupService.addRight("not-a-group", null); } @Test public void testRemoveRight() { - + assertTrue(groupService.addRight("group", parent)); + Group group = groupService.findByName("group"); + assertEquals(1, group.getRights().size()); + assertTrue(groupService.removeRight("group", parent)); + group = groupService.findByName("group"); + assertEquals(0, group.getRights().size()); } @Test public void testRemoveRightIfNotPresent() { - + Right right = new Right(); + right.setName("not-a-right"); + assertFalse(groupService.removeRight("group", right)); } @Test(expected = IllegalArgumentException.class) public void testRemoveRightFailsIfGroupNameNull() { - + groupService.removeRight(null, parent); } @Test(expected = IllegalArgumentException.class) public void testRemoveRightFailsIfRightNull() { - + groupService.removeRight("group", null); } @Test(expected = IllegalArgumentException.class) public void testRemoveRightFailsIfGroupNotInDatabase() { - - } - - @Test(expected = IllegalArgumentException.class) - public void testRemoveRightFailsIfRightNotInDatabase() { - + groupService.removeRight("not-a-group", null); } } 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 584c4d2..287d37d 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java @@ -1,95 +1,126 @@ package fr.plil.sio.persistence.jdbc; +import fr.plil.sio.persistence.api.Right; +import fr.plil.sio.persistence.api.RightService; 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 RightServiceTest { +public class RightServiceTest extends AbstractServiceSupport { + + @Autowired + private RightService rightService; @Test public void testCreateParentRightAndFindOne() { - + rightService.create("right"); + assertEquals(1, rightService.findByName("right").size()); } @Test public void testCreateTwoParentRightsWithSameNameAndFindByName() { - + rightService.create("right"); + rightService.create("right"); + assertEquals(2, rightService.findByName("right").size()); } @Test(expected = IllegalArgumentException.class) public void testCreateFailsIfNameNull() { - + rightService.create(null); } @Test public void testCreateSiblingRightAndFindOne() { - + Right parent = rightService.create("parent"); + rightService.create("sibling", parent); + assertEquals(1, rightService.findByName("sibling").size()); } @Test(expected = IllegalArgumentException.class) public void testCreateSiblingFailsIfNameNull() { - + Right parent = rightService.create("parent"); + rightService.create(null, parent); } @Test(expected = IllegalArgumentException.class) public void testCreateSiblingFailsIfParentNull() { - + rightService.create("parent"); + rightService.create("sibling", null); } @Test(expected = IllegalArgumentException.class) public void testCreateSiblingFailsIfParentNotInDatabase() { - + Right right = new Right(); + right.setName("not-a-right"); + rightService.create("sibling", right); } @Test public void testDeleteParentRight() { + Right right = rightService.create("right"); + assertEquals(1, rightService.findByName("right").size()); + rightService.delete(right); + assertEquals(0, rightService.findByName("right").size()); } @Test public void testDeleteSiblingRight() { + Right parent = rightService.create("parent"); + Right sibling = rightService.create("sibling", parent); + rightService.delete(sibling); + assertEquals(0, rightService.findByName("sibling").size()); } - @Test public void testDeleteParentAndSiblingRights() { + Right parent = rightService.create("parent"); + rightService.create("sibling", parent); + rightService.delete(parent); + assertEquals(0, rightService.findByName("sibling").size()); + assertEquals(0, rightService.findByName("parent").size()); } @Test public void testDeleteRightIfNotFound() { + Right right = new Right(); + right.setName("not-a-right"); + assertFalse(rightService.delete(right)); } @Test(expected = IllegalArgumentException.class) public void testDeleteRightFailsIfRightNull() { - } - - @Test - public void testFindByName() { - } - - @Test - public void testFindSeveralByName() { + rightService.delete(null); } @Test public void testFindByNameIfNameNotFound() { + assertEquals(0, rightService.findByName("no").size()); } @Test(expected = IllegalArgumentException.class) public void testFindByNameFailsIfNameNull() { + rightService.findByName(null); } @Test public void testFindOne() { + Right right = rightService.create("right"); + assertNotNull(rightService.findOne(right.getId())); } @Test public void testFindOneIfIdNotFound() { + assertNull(rightService.findOne(153463167809232L)); } @Test(expected = IllegalArgumentException.class) public void testFindOneFailsIfIdNull() { + rightService.findOne(null); } } diff --git a/src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java b/src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java index 09150c4..b2d0ea6 100644 --- a/src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java +++ b/src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java @@ -1,69 +1,120 @@ package fr.plil.sio.persistence.jdbc; +import fr.plil.sio.persistence.api.*; +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 UserServiceTest { +public class UserServiceTest extends AbstractServiceSupport { + + @Autowired + private GroupService groupService; + + @Autowired + private UserService userService; + + @Autowired + private RightService rightService; + + @Before + public void before() { + groupService.create("group"); + userService.create("user", "group"); + Right right = rightService.create("parent"); + rightService.create("sibling", right); + groupService.addRight("group", right); + rightService.create("not-for-me"); + } @Test public void testCreateAndFindByName() { + User user = userService.findByName("user"); + assertNotNull(user); + assertEquals("user", user.getName()); + Group group = user.getGroup(); + assertNotNull(group); + assertEquals("group", group.getName()); } @Test(expected = IllegalArgumentException.class) public void testCreateFailsWhenNameNull() { + userService.create(null, "group"); } @Test(expected = IllegalArgumentException.class) public void testCreateFailsWhenGroupNameNull() { + userService.create("user", null); } @Test(expected = IllegalArgumentException.class) public void testCreateFailsWhenGroupDoesNotExist() { + userService.create("user", "notGroup"); } @Test(expected = IllegalStateException.class) public void testCreateFailsWhenSameNameUserAlreadyPresent() { + userService.create("user", "group"); } public void testDeleteUser() { + assertTrue(userService.delete("user")); } public void testDeleteUserIfNotFound() { + assertFalse(userService.delete("user")); } @Test(expected = IllegalArgumentException.class) public void testDeleteUserFailsIfNameNull() { + userService.delete(null); } public void testFindUserByNameIfUserNotFound() { + assertNull(userService.findByName("blabla")); } @Test(expected = IllegalArgumentException.class) public void testFindUserByNameFailsIfNameNull() { + assertNull(userService.findByName(null)); } public void testIsUserHasExactRight() { + Right right = rightService.findByName("parent").get(0); + assertTrue(userService.isUserHasRight("user", right)); } public void testIsUserHasRightByParents() { + Right right = rightService.findByName("sibling").get(0); + assertTrue(userService.isUserHasRight("user", right)); } public void testIsUserHasNotTheExactRight() { + Right right = rightService.findByName("not-for-me").get(0); + assertFalse(userService.isUserHasRight("user", right)); } @Test(expected = IllegalArgumentException.class) public void testIsUserHasRightFailsWhenUsernameNull() { + Right right = rightService.findByName("parent").get(0); + userService.isUserHasRight(null, right); } @Test(expected = IllegalArgumentException.class) public void testIsUserHasRightFailsWhenRightNull() { + userService.isUserHasRight("user", null); } @Test(expected = IllegalArgumentException.class) public void testIsUserHasRightFailsWhenRightNotInDatabase() { + Right right = new Right(); + right.setName("dummy"); + userService.isUserHasRight("user", right); } } -- libgit2 0.21.2