From fd5e74ca92a7e060d47eefe56a26eb3299480bb2 Mon Sep 17 00:00:00 2001 From: jcartign Date: Thu, 15 Sep 2016 14:28:48 +0200 Subject: [PATCH] First student version --- .gitignore | 3 +++ pom.xml | 38 ++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/JpaApplication.java | 8 ++++++++ src/main/java/fr/plil/sio/persistence/api/Group.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/api/GroupService.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/api/Right.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/api/RightService.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/api/User.java | 34 ++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/api/UserService.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java | 9 +++++++++ src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java | 35 +++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java | 29 +++++++++++++++++++++++++++++ src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test/resources/application.properties | 4 ++++ 17 files changed, 868 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/fr/plil/sio/persistence/JpaApplication.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/Group.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/GroupService.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/Right.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/RightService.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/User.java create mode 100644 src/main/java/fr/plil/sio/persistence/api/UserService.java create mode 100644 src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java create mode 100644 src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java create mode 100644 src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java create mode 100644 src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java create mode 100644 src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java create mode 100644 src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java create mode 100644 src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java create mode 100644 src/test/resources/application.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a65bf38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.iml +target/ +.idea/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..194c1a0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + fr.plil.sio.persistence + spring-jpa + 0.1 + + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + UTF-8 + 1.8 + + + diff --git a/src/main/java/fr/plil/sio/persistence/JpaApplication.java b/src/main/java/fr/plil/sio/persistence/JpaApplication.java new file mode 100644 index 0000000..cf3dc12 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/JpaApplication.java @@ -0,0 +1,8 @@ +package fr.plil.sio.persistence; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JpaApplication { + +} diff --git a/src/main/java/fr/plil/sio/persistence/api/Group.java b/src/main/java/fr/plil/sio/persistence/api/Group.java new file mode 100644 index 0000000..d9cf671 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/Group.java @@ -0,0 +1,55 @@ +package fr.plil.sio.persistence.api; + +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class Group { + + private Long id; + + private String name; + + /** + * Users in the group. + */ + private Set users = new TreeSet<>(); + + /** + * List of rights. The list CANNOT contains duplicate rights. + */ + private List rights = new LinkedList<>(); + + public List getRights() { + return rights; + } + + public void setRights(List rights) { + this.rights = rights; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getUsers() { + return users; + } + + public void setUsers(Set users) { + this.users = users; + } +} diff --git a/src/main/java/fr/plil/sio/persistence/api/GroupService.java b/src/main/java/fr/plil/sio/persistence/api/GroupService.java new file mode 100644 index 0000000..72daae3 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/GroupService.java @@ -0,0 +1,53 @@ +package fr.plil.sio.persistence.api; + +public interface GroupService { + + /** + * Create a group with a specific name in the database. + * There is no two groups with the same name in the database. + * + * @param name the name of the group + * @return an instance of the group + * @throws IllegalArgumentException if name is null + * @throws IllegalStateException if a group with the same name is already present + */ + Group create(String name); + + /** + * Delete a group in the database. Remove all users in the group. + * + * @param name the name of the group to remove + * @return true if group has been deleted, false if group is not found in the database. + * @throws IllegalArgumentException if name is null + */ + boolean delete(String name); + + /** + * Find a group in the database based on its name. + * + * @param name the name of the group to search for. + * @return an instance of the group if found, else null. + * @throws IllegalArgumentException if name is null + */ + Group findByName(String name); + + /** + * Add a right in the group. Right is inserted at the end of rights list of the group. + * + * @param groupName the name of the group. + * @param right the right to add + * @return true if right is added in the group, false if right was already present. + * @throws IllegalArgumentException if groupName or right is null, or if group or right cannot be found. + */ + boolean addRight(String groupName, Right right); + + /** + * 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. + */ + boolean removeRight(String groupName, Right right); +} diff --git a/src/main/java/fr/plil/sio/persistence/api/Right.java b/src/main/java/fr/plil/sio/persistence/api/Right.java new file mode 100644 index 0000000..cc7ef4e --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/Right.java @@ -0,0 +1,49 @@ +package fr.plil.sio.persistence.api; + +import java.util.HashSet; +import java.util.Set; + +public class Right { + + private Long id; + + private String name; + + /// the parent group + private Right parent; + + /// the sibling group(s), eventually empty + private Set siblings = new HashSet<>(); + + public Set getSiblings() { + return siblings; + } + + public void setSiblings(Set siblings) { + this.siblings = siblings; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Right getParent() { + return parent; + } + + public void setParent(Right parent) { + this.parent = parent; + } +} diff --git a/src/main/java/fr/plil/sio/persistence/api/RightService.java b/src/main/java/fr/plil/sio/persistence/api/RightService.java new file mode 100644 index 0000000..469cdbc --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/RightService.java @@ -0,0 +1,52 @@ +package fr.plil.sio.persistence.api; + +import java.util.List; + +public interface RightService { + + /** + * Create a parent right with a specific name in the database. + * It is possible that two rights has the same name. + * + * @param name the name of the right + * @throws IllegalArgumentException if name is null + */ + Right create(String name); + + /** + * Create a sibling right attached to a parent right with a specific name in the database. + * It is possible that two rights has the same name. + * + * @param name the name of the right + * @param parent the parent right + * @throws IllegalArgumentException if name or parent is null, or if parent does not exist in the database. + */ + Right create(String name, Right parent); + + /** + * Delete a right in the database. Delete sibling rights if present. + * + * @param right the right to delete + * @return true if right has been deleted, false else. + * @throws IllegalArgumentException if right is null. + */ + boolean delete(Right right); + + /** + * Find a list of rights in the database based on their name. + * + * @param name the name of the rights to search for. + * @return A list of rights, eventually empty. + * @throws IllegalArgumentException if name is null + */ + List findByName(String name); + + /** + * Find a right in the database based on its id. + * + * @param id the name of the right to search for. + * @return an instance of the right if found, else null. + * @throws IllegalArgumentException if id is null + */ + Right findOne(Long id); +} diff --git a/src/main/java/fr/plil/sio/persistence/api/User.java b/src/main/java/fr/plil/sio/persistence/api/User.java new file mode 100644 index 0000000..cbc3f27 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/User.java @@ -0,0 +1,34 @@ +package fr.plil.sio.persistence.api; + +public class User { + + private Long id; + + private String name; + + private Group group; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; + } +} diff --git a/src/main/java/fr/plil/sio/persistence/api/UserService.java b/src/main/java/fr/plil/sio/persistence/api/UserService.java new file mode 100644 index 0000000..9bad49f --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/api/UserService.java @@ -0,0 +1,45 @@ +package fr.plil.sio.persistence.api; + +public interface UserService { + + /** + * Create an user with a specific name in the database and affected to an existing group. + * There is no two users with the same name in the database. + * + * @param name the name of the user + * @param groupName the name of the group + * @return an instance of the user + * @throws IllegalArgumentException if name or groupName is null, or if group does not exist. + * @throws IllegalStateException if an user with the same name is already present + */ + User create(String name, String groupName); + + /** + * Delete an user in the database. + * + * @param name the name of the user to remove + * @return true if user has been deleted, false if user is not found in the database. + * @throws IllegalArgumentException if name is null + */ + boolean delete(String name); + + /** + * Find an user in the database based on its name. + * + * @param name the name of the user to search for. + * @return an instance of the user if found, else null. + * @throws IllegalArgumentException if name is null + */ + User findByName(String name); + + /** + * Check method to control if an user has a specific right. The following rule is used: an user has a specific + * right if the group where the user is contains the specific right or one of its parents. + * + * @param userName the name of the user + * @param right the specific right + * @return true if the group where the user is contains the specific right or one of its parents, false else. + * @throws IllegalArgumentException if userName or right is null, or if the user or right does not exist in the db. + */ + boolean isUserHasRight(String userName, Right right); +} diff --git a/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java b/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java new file mode 100644 index 0000000..99fe2d9 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java @@ -0,0 +1,9 @@ +package fr.plil.sio.persistence.jpa; + +import fr.plil.sio.persistence.api.Group; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GroupRepository extends JpaRepository { + + Group findByName(String name); +} diff --git a/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java new file mode 100644 index 0000000..3302278 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java @@ -0,0 +1,52 @@ +package fr.plil.sio.persistence.jpa; + +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 GroupServiceJpa implements GroupService { + + @Autowired + private GroupRepository groupRepository; + + @Override + public Group create(String name) { + 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 + public boolean delete(String name) { + throw new IllegalStateException("not implemented !"); + } + + @Override + public Group findByName(String name) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + return groupRepository.findByName(name); + } + + @Override + public boolean addRight(String groupName, Right right) { + return false; + } + + @Override + public boolean removeRight(String groupName, Right right) { + return false; + } +} diff --git a/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java new file mode 100644 index 0000000..6b0f6d4 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java @@ -0,0 +1,35 @@ +package fr.plil.sio.persistence.jpa; + +import fr.plil.sio.persistence.api.Right; +import fr.plil.sio.persistence.api.RightService; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class RightServiceJpa implements RightService { + @Override + public Right create(String name) { + return null; + } + + @Override + public Right create(String name, Right parent) { + return null; + } + + @Override + public boolean delete(Right right) { + return false; + } + + @Override + public List findByName(String name) { + return null; + } + + @Override + public Right findOne(Long id) { + return null; + } +} diff --git a/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java new file mode 100644 index 0000000..f950761 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java @@ -0,0 +1,29 @@ +package fr.plil.sio.persistence.jpa; + +import fr.plil.sio.persistence.api.Right; +import fr.plil.sio.persistence.api.User; +import fr.plil.sio.persistence.api.UserService; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceJpa implements UserService { + @Override + public User create(String name, String groupName) { + return null; + } + + @Override + public boolean delete(String name) { + return false; + } + + @Override + public User findByName(String name) { + return null; + } + + @Override + public boolean isUserHasRight(String userName, Right right) { + return false; + } +} diff --git a/src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java b/src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java new file mode 100644 index 0000000..af67ef0 --- /dev/null +++ b/src/test/java/fr/plil/sio/persistence/jpa/GroupServiceTest.java @@ -0,0 +1,151 @@ +package fr.plil.sio.persistence.jpa; + +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.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class GroupServiceTest { + + @Autowired + private GroupService groupService; + + @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"); + } + + @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"); + } + + @Test + public void testDeleteGroup() { + assertTrue(groupService.delete("group")); + assertNull(groupService.findByName("group")); + assertFalse(groupService.delete("group")); + } + + @Test + public void testDeleteNotExistingGroup() { + assertFalse(groupService.delete("not-a-group")); + } + + + @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 testFindByNameIfGroupNotFound() { + assertNull(groupService.findByName("unknown")); + } + + @Test(expected = IllegalArgumentException.class) + public void testFindByNameFailsIfNameNull() { + groupService.findByName(null); + } + + @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() { + 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() { + groupService.removeRight("not-a-group", null); + } +} diff --git a/src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java b/src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java new file mode 100644 index 0000000..905ab69 --- /dev/null +++ b/src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java @@ -0,0 +1,126 @@ +package fr.plil.sio.persistence.jpa; + +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.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class RightServiceTest { + + @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() { + 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/jpa/UserServiceTest.java b/src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java new file mode 100644 index 0000000..89e94e6 --- /dev/null +++ b/src/test/java/fr/plil/sio/persistence/jpa/UserServiceTest.java @@ -0,0 +1,125 @@ +package fr.plil.sio.persistence.jpa; + +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.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class UserServiceTest { + + @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"); + } + + @Test + public void testDeleteUser() { + assertTrue(userService.delete("user")); + } + + @Test + 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)); + } + + @Test + public void testIsUserHasExactRight() { + Right right = rightService.findByName("parent").get(0); + assertTrue(userService.isUserHasRight("user", right)); + } + + @Test + public void testIsUserHasRightByParents() { + Right right = rightService.findByName("sibling").get(0); + assertTrue(userService.isUserHasRight("user", right)); + } + + @Test + 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); + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..6901e9c --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,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 \ No newline at end of file -- libgit2 0.21.2