Commit 292aec6f6123cd2cc3584d721c788eae71b9cf37
1 parent
6fc5c241
First version before first course
Showing
7 changed files
with
200 additions
and
56 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
@@ -29,7 +29,7 @@ public class GroupServiceJdbc implements GroupService { | @@ -29,7 +29,7 @@ public class GroupServiceJdbc implements GroupService { | ||
29 | 29 | ||
30 | @Override | 30 | @Override |
31 | public boolean delete(String name) { | 31 | public boolean delete(String name) { |
32 | - return false; | 32 | + throw new IllegalStateException("not implemented !"); |
33 | } | 33 | } |
34 | 34 | ||
35 | @Override | 35 | @Override |
@@ -42,11 +42,11 @@ public class GroupServiceJdbc implements GroupService { | @@ -42,11 +42,11 @@ public class GroupServiceJdbc implements GroupService { | ||
42 | 42 | ||
43 | @Override | 43 | @Override |
44 | public boolean addRight(String groupName, Right right) { | 44 | public boolean addRight(String groupName, Right right) { |
45 | - return false; | 45 | + throw new IllegalStateException("not implemented !"); |
46 | } | 46 | } |
47 | 47 | ||
48 | @Override | 48 | @Override |
49 | public boolean removeRight(String groupName, Right right) { | 49 | public boolean removeRight(String groupName, Right right) { |
50 | - return false; | 50 | + throw new IllegalStateException("not implemented !"); |
51 | } | 51 | } |
52 | } | 52 | } |
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; | @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; | ||
7 | import java.util.List; | 7 | import java.util.List; |
8 | 8 | ||
9 | @Service | 9 | @Service |
10 | -public class RightServiceJdbc implements RightService{ | 10 | +public class RightServiceJdbc implements RightService { |
11 | @Override | 11 | @Override |
12 | public Right create(String name) { | 12 | public Right create(String name) { |
13 | return null; | 13 | return null; |
@@ -15,21 +15,21 @@ public class RightServiceJdbc implements RightService{ | @@ -15,21 +15,21 @@ public class RightServiceJdbc implements RightService{ | ||
15 | 15 | ||
16 | @Override | 16 | @Override |
17 | public Right create(String name, Right parent) { | 17 | public Right create(String name, Right parent) { |
18 | - return null; | 18 | + throw new IllegalStateException("not implemented !"); |
19 | } | 19 | } |
20 | 20 | ||
21 | @Override | 21 | @Override |
22 | public boolean delete(Right right) { | 22 | public boolean delete(Right right) { |
23 | - return false; | 23 | + throw new IllegalStateException("not implemented !"); |
24 | } | 24 | } |
25 | 25 | ||
26 | @Override | 26 | @Override |
27 | public List<Right> findByName(String name) { | 27 | public List<Right> findByName(String name) { |
28 | - return null; | 28 | + throw new IllegalStateException("not implemented !"); |
29 | } | 29 | } |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public Right findOne(Long id) { | 32 | public Right findOne(Long id) { |
33 | - return null; | 33 | + throw new IllegalStateException("not implemented !"); |
34 | } | 34 | } |
35 | } | 35 | } |
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
@@ -14,21 +14,21 @@ public class UserServiceJdbc implements UserService { | @@ -14,21 +14,21 @@ public class UserServiceJdbc implements UserService { | ||
14 | 14 | ||
15 | @Override | 15 | @Override |
16 | public User create(String name, String groupName) { | 16 | public User create(String name, String groupName) { |
17 | - return null; | 17 | + throw new IllegalStateException("not implemented !"); |
18 | } | 18 | } |
19 | 19 | ||
20 | @Override | 20 | @Override |
21 | public boolean delete(String name) { | 21 | public boolean delete(String name) { |
22 | - return false; | 22 | + throw new IllegalStateException("not implemented !"); |
23 | } | 23 | } |
24 | 24 | ||
25 | @Override | 25 | @Override |
26 | public User findByName(String name) { | 26 | public User findByName(String name) { |
27 | - return null; | 27 | + throw new IllegalStateException("not implemented !"); |
28 | } | 28 | } |
29 | 29 | ||
30 | @Override | 30 | @Override |
31 | public boolean isUserHasRight(String userName, Right right) { | 31 | public boolean isUserHasRight(String userName, Right right) { |
32 | - return false; | 32 | + throw new IllegalStateException("not implemented !"); |
33 | } | 33 | } |
34 | } | 34 | } |
src/test/java/fr/plil/sio/persistence/jdbc/AbstractServiceSupport.java
0 โ 100644
@@ -0,0 +1,49 @@ | @@ -0,0 +1,49 @@ | ||
1 | +package fr.plil.sio.persistence.jdbc; | ||
2 | + | ||
3 | +import org.junit.After; | ||
4 | +import org.junit.Before; | ||
5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
6 | + | ||
7 | +import javax.sql.DataSource; | ||
8 | +import java.sql.Connection; | ||
9 | +import java.sql.SQLException; | ||
10 | +import java.sql.Statement; | ||
11 | + | ||
12 | +public abstract class AbstractServiceSupport { | ||
13 | + | ||
14 | + private static final String create_table_group = "CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, " + | ||
15 | + "NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID))"; | ||
16 | + | ||
17 | + private static final String drop_table_group = "DROP TABLE GROUP_T"; | ||
18 | + | ||
19 | + @Autowired | ||
20 | + private DataSource dataSource; | ||
21 | + | ||
22 | + private Connection connection; | ||
23 | + | ||
24 | + private Statement stmt; | ||
25 | + | ||
26 | + @Before | ||
27 | + public void createTables() throws SQLException { | ||
28 | + openConnection(); | ||
29 | + stmt.executeUpdate(create_table_group); | ||
30 | + closeConnection(); | ||
31 | + } | ||
32 | + | ||
33 | + @After | ||
34 | + public void cleanupDatabase() throws SQLException { | ||
35 | + openConnection(); | ||
36 | + stmt.executeUpdate(drop_table_group); | ||
37 | + closeConnection(); | ||
38 | + } | ||
39 | + | ||
40 | + private void closeConnection() throws SQLException { | ||
41 | + stmt.close(); | ||
42 | + connection.close(); | ||
43 | + } | ||
44 | + | ||
45 | + private void openConnection() throws SQLException { | ||
46 | + connection = dataSource.getConnection(); | ||
47 | + stmt = connection.createStatement(); | ||
48 | + } | ||
49 | +} |
src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java
1 | package fr.plil.sio.persistence.jdbc; | 1 | package fr.plil.sio.persistence.jdbc; |
2 | 2 | ||
3 | -import fr.plil.sio.persistence.api.Group; | ||
4 | -import fr.plil.sio.persistence.api.GroupService; | ||
5 | -import fr.plil.sio.persistence.api.UserService; | 3 | +import fr.plil.sio.persistence.api.*; |
6 | import org.junit.Before; | 4 | import org.junit.Before; |
7 | import org.junit.Test; | 5 | import org.junit.Test; |
8 | import org.junit.runner.RunWith; | 6 | import org.junit.runner.RunWith; |
@@ -14,7 +12,7 @@ import static org.junit.Assert.*; | @@ -14,7 +12,7 @@ import static org.junit.Assert.*; | ||
14 | 12 | ||
15 | @RunWith(SpringJUnit4ClassRunner.class) | 13 | @RunWith(SpringJUnit4ClassRunner.class) |
16 | @SpringApplicationConfiguration(classes = JdbcApplication.class) | 14 | @SpringApplicationConfiguration(classes = JdbcApplication.class) |
17 | -public class GroupServiceTest { | 15 | +public class GroupServiceTest extends AbstractServiceSupport { |
18 | 16 | ||
19 | @Autowired | 17 | @Autowired |
20 | private GroupService groupService; | 18 | private GroupService groupService; |
@@ -22,8 +20,15 @@ public class GroupServiceTest { | @@ -22,8 +20,15 @@ public class GroupServiceTest { | ||
22 | @Autowired | 20 | @Autowired |
23 | private UserService userService; | 21 | private UserService userService; |
24 | 22 | ||
23 | + @Autowired | ||
24 | + private RightService rightService; | ||
25 | + | ||
26 | + private Right parent; | ||
27 | + | ||
25 | @Before | 28 | @Before |
26 | public void before() { | 29 | public void before() { |
30 | + parent = rightService.create("parent"); | ||
31 | + rightService.create("sibling", parent); | ||
27 | groupService.create("group"); | 32 | groupService.create("group"); |
28 | } | 33 | } |
29 | 34 | ||
@@ -46,9 +51,14 @@ public class GroupServiceTest { | @@ -46,9 +51,14 @@ public class GroupServiceTest { | ||
46 | public void testDeleteGroup() { | 51 | public void testDeleteGroup() { |
47 | assertTrue(groupService.delete("group")); | 52 | assertTrue(groupService.delete("group")); |
48 | assertNull(groupService.findByName("group")); | 53 | assertNull(groupService.findByName("group")); |
49 | - assertFalse(groupService.delete("unknown")); | 54 | + assertFalse(groupService.delete("group")); |
50 | } | 55 | } |
51 | 56 | ||
57 | + public void testDeleteNotExistingGroup() { | ||
58 | + assertFalse(groupService.delete("not-a-group")); | ||
59 | + } | ||
60 | + | ||
61 | + | ||
52 | @Test(expected = IllegalArgumentException.class) | 62 | @Test(expected = IllegalArgumentException.class) |
53 | public void testDeleteGroupFailsIfNameNull() { | 63 | public void testDeleteGroupFailsIfNameNull() { |
54 | groupService.delete(null); | 64 | groupService.delete(null); |
@@ -65,7 +75,7 @@ public class GroupServiceTest { | @@ -65,7 +75,7 @@ public class GroupServiceTest { | ||
65 | assertNull(userService.findByName("user2")); | 75 | assertNull(userService.findByName("user2")); |
66 | } | 76 | } |
67 | 77 | ||
68 | - public void testFindByNameIfUserNotFound() { | 78 | + public void testFindByNameIfGroupNotFound() { |
69 | assertNull(groupService.findByName("unknown")); | 79 | assertNull(groupService.findByName("unknown")); |
70 | } | 80 | } |
71 | 81 | ||
@@ -76,61 +86,64 @@ public class GroupServiceTest { | @@ -76,61 +86,64 @@ public class GroupServiceTest { | ||
76 | 86 | ||
77 | @Test | 87 | @Test |
78 | public void testAddRight() { | 88 | public void testAddRight() { |
79 | - | 89 | + assertTrue(groupService.addRight("group", parent)); |
90 | + Group group = groupService.findByName("group"); | ||
91 | + assertEquals(1, group.getRights().size()); | ||
92 | + assertEquals("parent", group.getRights().get(0).getName()); | ||
93 | + assertEquals(1, group.getRights().get(0).getSiblings().size()); | ||
94 | + assertEquals("sibling", group.getRights().get(0).getSiblings().iterator().next().getName()); | ||
80 | } | 95 | } |
81 | 96 | ||
82 | @Test | 97 | @Test |
83 | public void testAddRightIfAlreadyPresent() { | 98 | public void testAddRightIfAlreadyPresent() { |
84 | - | 99 | + assertTrue(groupService.addRight("group", parent)); |
100 | + assertFalse(groupService.addRight("group", parent)); | ||
85 | } | 101 | } |
86 | 102 | ||
87 | @Test(expected = IllegalArgumentException.class) | 103 | @Test(expected = IllegalArgumentException.class) |
88 | public void testAddRightFailsIfGroupNameNull() { | 104 | public void testAddRightFailsIfGroupNameNull() { |
89 | - | 105 | + groupService.addRight(null, parent); |
90 | } | 106 | } |
91 | 107 | ||
92 | @Test(expected = IllegalArgumentException.class) | 108 | @Test(expected = IllegalArgumentException.class) |
93 | public void testAddRightFailsIfRightNull() { | 109 | public void testAddRightFailsIfRightNull() { |
94 | - | 110 | + groupService.addRight("group", null); |
95 | } | 111 | } |
96 | 112 | ||
97 | @Test(expected = IllegalArgumentException.class) | 113 | @Test(expected = IllegalArgumentException.class) |
98 | public void testAddRightFailsIfGroupNotInDatabase() { | 114 | public void testAddRightFailsIfGroupNotInDatabase() { |
99 | - | ||
100 | - } | ||
101 | - | ||
102 | - @Test(expected = IllegalArgumentException.class) | ||
103 | - public void testAddUserFailsIfRightNotInDatabase() { | ||
104 | - | 115 | + groupService.addRight("not-a-group", null); |
105 | } | 116 | } |
106 | 117 | ||
107 | @Test | 118 | @Test |
108 | public void testRemoveRight() { | 119 | public void testRemoveRight() { |
109 | - | 120 | + assertTrue(groupService.addRight("group", parent)); |
121 | + Group group = groupService.findByName("group"); | ||
122 | + assertEquals(1, group.getRights().size()); | ||
123 | + assertTrue(groupService.removeRight("group", parent)); | ||
124 | + group = groupService.findByName("group"); | ||
125 | + assertEquals(0, group.getRights().size()); | ||
110 | } | 126 | } |
111 | 127 | ||
112 | @Test | 128 | @Test |
113 | public void testRemoveRightIfNotPresent() { | 129 | public void testRemoveRightIfNotPresent() { |
114 | - | 130 | + Right right = new Right(); |
131 | + right.setName("not-a-right"); | ||
132 | + assertFalse(groupService.removeRight("group", right)); | ||
115 | } | 133 | } |
116 | 134 | ||
117 | @Test(expected = IllegalArgumentException.class) | 135 | @Test(expected = IllegalArgumentException.class) |
118 | public void testRemoveRightFailsIfGroupNameNull() { | 136 | public void testRemoveRightFailsIfGroupNameNull() { |
119 | - | 137 | + groupService.removeRight(null, parent); |
120 | } | 138 | } |
121 | 139 | ||
122 | @Test(expected = IllegalArgumentException.class) | 140 | @Test(expected = IllegalArgumentException.class) |
123 | public void testRemoveRightFailsIfRightNull() { | 141 | public void testRemoveRightFailsIfRightNull() { |
124 | - | 142 | + groupService.removeRight("group", null); |
125 | } | 143 | } |
126 | 144 | ||
127 | @Test(expected = IllegalArgumentException.class) | 145 | @Test(expected = IllegalArgumentException.class) |
128 | public void testRemoveRightFailsIfGroupNotInDatabase() { | 146 | public void testRemoveRightFailsIfGroupNotInDatabase() { |
129 | - | ||
130 | - } | ||
131 | - | ||
132 | - @Test(expected = IllegalArgumentException.class) | ||
133 | - public void testRemoveRightFailsIfRightNotInDatabase() { | ||
134 | - | 147 | + groupService.removeRight("not-a-group", null); |
135 | } | 148 | } |
136 | } | 149 | } |
src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java
1 | package fr.plil.sio.persistence.jdbc; | 1 | package fr.plil.sio.persistence.jdbc; |
2 | 2 | ||
3 | +import fr.plil.sio.persistence.api.Right; | ||
4 | +import fr.plil.sio.persistence.api.RightService; | ||
3 | import org.junit.Test; | 5 | import org.junit.Test; |
4 | import org.junit.runner.RunWith; | 6 | import org.junit.runner.RunWith; |
7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
5 | import org.springframework.boot.test.SpringApplicationConfiguration; | 8 | import org.springframework.boot.test.SpringApplicationConfiguration; |
6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
7 | 10 | ||
11 | +import static org.junit.Assert.*; | ||
12 | + | ||
8 | @RunWith(SpringJUnit4ClassRunner.class) | 13 | @RunWith(SpringJUnit4ClassRunner.class) |
9 | @SpringApplicationConfiguration(classes = JdbcApplication.class) | 14 | @SpringApplicationConfiguration(classes = JdbcApplication.class) |
10 | -public class RightServiceTest { | 15 | +public class RightServiceTest extends AbstractServiceSupport { |
16 | + | ||
17 | + @Autowired | ||
18 | + private RightService rightService; | ||
11 | 19 | ||
12 | @Test | 20 | @Test |
13 | public void testCreateParentRightAndFindOne() { | 21 | public void testCreateParentRightAndFindOne() { |
14 | - | 22 | + rightService.create("right"); |
23 | + assertEquals(1, rightService.findByName("right").size()); | ||
15 | } | 24 | } |
16 | 25 | ||
17 | @Test | 26 | @Test |
18 | public void testCreateTwoParentRightsWithSameNameAndFindByName() { | 27 | public void testCreateTwoParentRightsWithSameNameAndFindByName() { |
19 | - | 28 | + rightService.create("right"); |
29 | + rightService.create("right"); | ||
30 | + assertEquals(2, rightService.findByName("right").size()); | ||
20 | } | 31 | } |
21 | 32 | ||
22 | @Test(expected = IllegalArgumentException.class) | 33 | @Test(expected = IllegalArgumentException.class) |
23 | public void testCreateFailsIfNameNull() { | 34 | public void testCreateFailsIfNameNull() { |
24 | - | 35 | + rightService.create(null); |
25 | } | 36 | } |
26 | 37 | ||
27 | @Test | 38 | @Test |
28 | public void testCreateSiblingRightAndFindOne() { | 39 | public void testCreateSiblingRightAndFindOne() { |
29 | - | 40 | + Right parent = rightService.create("parent"); |
41 | + rightService.create("sibling", parent); | ||
42 | + assertEquals(1, rightService.findByName("sibling").size()); | ||
30 | } | 43 | } |
31 | 44 | ||
32 | @Test(expected = IllegalArgumentException.class) | 45 | @Test(expected = IllegalArgumentException.class) |
33 | public void testCreateSiblingFailsIfNameNull() { | 46 | public void testCreateSiblingFailsIfNameNull() { |
34 | - | 47 | + Right parent = rightService.create("parent"); |
48 | + rightService.create(null, parent); | ||
35 | } | 49 | } |
36 | 50 | ||
37 | @Test(expected = IllegalArgumentException.class) | 51 | @Test(expected = IllegalArgumentException.class) |
38 | public void testCreateSiblingFailsIfParentNull() { | 52 | public void testCreateSiblingFailsIfParentNull() { |
39 | - | 53 | + rightService.create("parent"); |
54 | + rightService.create("sibling", null); | ||
40 | } | 55 | } |
41 | 56 | ||
42 | @Test(expected = IllegalArgumentException.class) | 57 | @Test(expected = IllegalArgumentException.class) |
43 | public void testCreateSiblingFailsIfParentNotInDatabase() { | 58 | public void testCreateSiblingFailsIfParentNotInDatabase() { |
44 | - | 59 | + Right right = new Right(); |
60 | + right.setName("not-a-right"); | ||
61 | + rightService.create("sibling", right); | ||
45 | } | 62 | } |
46 | 63 | ||
47 | @Test | 64 | @Test |
48 | public void testDeleteParentRight() { | 65 | public void testDeleteParentRight() { |
66 | + Right right = rightService.create("right"); | ||
67 | + assertEquals(1, rightService.findByName("right").size()); | ||
68 | + rightService.delete(right); | ||
69 | + assertEquals(0, rightService.findByName("right").size()); | ||
49 | } | 70 | } |
50 | 71 | ||
51 | @Test | 72 | @Test |
52 | public void testDeleteSiblingRight() { | 73 | public void testDeleteSiblingRight() { |
74 | + Right parent = rightService.create("parent"); | ||
75 | + Right sibling = rightService.create("sibling", parent); | ||
76 | + rightService.delete(sibling); | ||
77 | + assertEquals(0, rightService.findByName("sibling").size()); | ||
53 | } | 78 | } |
54 | 79 | ||
55 | - | ||
56 | @Test | 80 | @Test |
57 | public void testDeleteParentAndSiblingRights() { | 81 | public void testDeleteParentAndSiblingRights() { |
82 | + Right parent = rightService.create("parent"); | ||
83 | + rightService.create("sibling", parent); | ||
84 | + rightService.delete(parent); | ||
85 | + assertEquals(0, rightService.findByName("sibling").size()); | ||
86 | + assertEquals(0, rightService.findByName("parent").size()); | ||
58 | } | 87 | } |
59 | 88 | ||
60 | @Test | 89 | @Test |
61 | public void testDeleteRightIfNotFound() { | 90 | public void testDeleteRightIfNotFound() { |
91 | + Right right = new Right(); | ||
92 | + right.setName("not-a-right"); | ||
93 | + assertFalse(rightService.delete(right)); | ||
62 | } | 94 | } |
63 | 95 | ||
64 | @Test(expected = IllegalArgumentException.class) | 96 | @Test(expected = IllegalArgumentException.class) |
65 | public void testDeleteRightFailsIfRightNull() { | 97 | public void testDeleteRightFailsIfRightNull() { |
66 | - } | ||
67 | - | ||
68 | - @Test | ||
69 | - public void testFindByName() { | ||
70 | - } | ||
71 | - | ||
72 | - @Test | ||
73 | - public void testFindSeveralByName() { | 98 | + rightService.delete(null); |
74 | } | 99 | } |
75 | 100 | ||
76 | @Test | 101 | @Test |
77 | public void testFindByNameIfNameNotFound() { | 102 | public void testFindByNameIfNameNotFound() { |
103 | + assertEquals(0, rightService.findByName("no").size()); | ||
78 | } | 104 | } |
79 | 105 | ||
80 | @Test(expected = IllegalArgumentException.class) | 106 | @Test(expected = IllegalArgumentException.class) |
81 | public void testFindByNameFailsIfNameNull() { | 107 | public void testFindByNameFailsIfNameNull() { |
108 | + rightService.findByName(null); | ||
82 | } | 109 | } |
83 | 110 | ||
84 | @Test | 111 | @Test |
85 | public void testFindOne() { | 112 | public void testFindOne() { |
113 | + Right right = rightService.create("right"); | ||
114 | + assertNotNull(rightService.findOne(right.getId())); | ||
86 | } | 115 | } |
87 | 116 | ||
88 | @Test | 117 | @Test |
89 | public void testFindOneIfIdNotFound() { | 118 | public void testFindOneIfIdNotFound() { |
119 | + assertNull(rightService.findOne(153463167809232L)); | ||
90 | } | 120 | } |
91 | 121 | ||
92 | @Test(expected = IllegalArgumentException.class) | 122 | @Test(expected = IllegalArgumentException.class) |
93 | public void testFindOneFailsIfIdNull() { | 123 | public void testFindOneFailsIfIdNull() { |
124 | + rightService.findOne(null); | ||
94 | } | 125 | } |
95 | } | 126 | } |
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
1 | package fr.plil.sio.persistence.jdbc; | 1 | package fr.plil.sio.persistence.jdbc; |
2 | 2 | ||
3 | +import fr.plil.sio.persistence.api.*; | ||
4 | +import org.junit.Before; | ||
3 | import org.junit.Test; | 5 | import org.junit.Test; |
4 | import org.junit.runner.RunWith; | 6 | import org.junit.runner.RunWith; |
7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
5 | import org.springframework.boot.test.SpringApplicationConfiguration; | 8 | import org.springframework.boot.test.SpringApplicationConfiguration; |
6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
7 | 10 | ||
11 | +import static org.junit.Assert.*; | ||
12 | + | ||
8 | @RunWith(SpringJUnit4ClassRunner.class) | 13 | @RunWith(SpringJUnit4ClassRunner.class) |
9 | @SpringApplicationConfiguration(classes = JdbcApplication.class) | 14 | @SpringApplicationConfiguration(classes = JdbcApplication.class) |
10 | -public class UserServiceTest { | 15 | +public class UserServiceTest extends AbstractServiceSupport { |
16 | + | ||
17 | + @Autowired | ||
18 | + private GroupService groupService; | ||
19 | + | ||
20 | + @Autowired | ||
21 | + private UserService userService; | ||
22 | + | ||
23 | + @Autowired | ||
24 | + private RightService rightService; | ||
25 | + | ||
26 | + @Before | ||
27 | + public void before() { | ||
28 | + groupService.create("group"); | ||
29 | + userService.create("user", "group"); | ||
30 | + Right right = rightService.create("parent"); | ||
31 | + rightService.create("sibling", right); | ||
32 | + groupService.addRight("group", right); | ||
33 | + rightService.create("not-for-me"); | ||
34 | + } | ||
11 | 35 | ||
12 | @Test | 36 | @Test |
13 | public void testCreateAndFindByName() { | 37 | public void testCreateAndFindByName() { |
38 | + User user = userService.findByName("user"); | ||
39 | + assertNotNull(user); | ||
40 | + assertEquals("user", user.getName()); | ||
41 | + Group group = user.getGroup(); | ||
42 | + assertNotNull(group); | ||
43 | + assertEquals("group", group.getName()); | ||
14 | } | 44 | } |
15 | 45 | ||
16 | @Test(expected = IllegalArgumentException.class) | 46 | @Test(expected = IllegalArgumentException.class) |
17 | public void testCreateFailsWhenNameNull() { | 47 | public void testCreateFailsWhenNameNull() { |
48 | + userService.create(null, "group"); | ||
18 | } | 49 | } |
19 | 50 | ||
20 | @Test(expected = IllegalArgumentException.class) | 51 | @Test(expected = IllegalArgumentException.class) |
21 | public void testCreateFailsWhenGroupNameNull() { | 52 | public void testCreateFailsWhenGroupNameNull() { |
53 | + userService.create("user", null); | ||
22 | } | 54 | } |
23 | 55 | ||
24 | @Test(expected = IllegalArgumentException.class) | 56 | @Test(expected = IllegalArgumentException.class) |
25 | public void testCreateFailsWhenGroupDoesNotExist() { | 57 | public void testCreateFailsWhenGroupDoesNotExist() { |
58 | + userService.create("user", "notGroup"); | ||
26 | } | 59 | } |
27 | 60 | ||
28 | @Test(expected = IllegalStateException.class) | 61 | @Test(expected = IllegalStateException.class) |
29 | public void testCreateFailsWhenSameNameUserAlreadyPresent() { | 62 | public void testCreateFailsWhenSameNameUserAlreadyPresent() { |
63 | + userService.create("user", "group"); | ||
30 | } | 64 | } |
31 | 65 | ||
32 | public void testDeleteUser() { | 66 | public void testDeleteUser() { |
67 | + assertTrue(userService.delete("user")); | ||
33 | } | 68 | } |
34 | 69 | ||
35 | public void testDeleteUserIfNotFound() { | 70 | public void testDeleteUserIfNotFound() { |
71 | + assertFalse(userService.delete("user")); | ||
36 | } | 72 | } |
37 | 73 | ||
38 | @Test(expected = IllegalArgumentException.class) | 74 | @Test(expected = IllegalArgumentException.class) |
39 | public void testDeleteUserFailsIfNameNull() { | 75 | public void testDeleteUserFailsIfNameNull() { |
76 | + userService.delete(null); | ||
40 | } | 77 | } |
41 | 78 | ||
42 | public void testFindUserByNameIfUserNotFound() { | 79 | public void testFindUserByNameIfUserNotFound() { |
80 | + assertNull(userService.findByName("blabla")); | ||
43 | } | 81 | } |
44 | 82 | ||
45 | @Test(expected = IllegalArgumentException.class) | 83 | @Test(expected = IllegalArgumentException.class) |
46 | public void testFindUserByNameFailsIfNameNull() { | 84 | public void testFindUserByNameFailsIfNameNull() { |
85 | + assertNull(userService.findByName(null)); | ||
47 | } | 86 | } |
48 | 87 | ||
49 | public void testIsUserHasExactRight() { | 88 | public void testIsUserHasExactRight() { |
89 | + Right right = rightService.findByName("parent").get(0); | ||
90 | + assertTrue(userService.isUserHasRight("user", right)); | ||
50 | } | 91 | } |
51 | 92 | ||
52 | public void testIsUserHasRightByParents() { | 93 | public void testIsUserHasRightByParents() { |
94 | + Right right = rightService.findByName("sibling").get(0); | ||
95 | + assertTrue(userService.isUserHasRight("user", right)); | ||
53 | } | 96 | } |
54 | 97 | ||
55 | public void testIsUserHasNotTheExactRight() { | 98 | public void testIsUserHasNotTheExactRight() { |
99 | + Right right = rightService.findByName("not-for-me").get(0); | ||
100 | + assertFalse(userService.isUserHasRight("user", right)); | ||
56 | } | 101 | } |
57 | 102 | ||
58 | @Test(expected = IllegalArgumentException.class) | 103 | @Test(expected = IllegalArgumentException.class) |
59 | public void testIsUserHasRightFailsWhenUsernameNull() { | 104 | public void testIsUserHasRightFailsWhenUsernameNull() { |
105 | + Right right = rightService.findByName("parent").get(0); | ||
106 | + userService.isUserHasRight(null, right); | ||
60 | } | 107 | } |
61 | 108 | ||
62 | @Test(expected = IllegalArgumentException.class) | 109 | @Test(expected = IllegalArgumentException.class) |
63 | public void testIsUserHasRightFailsWhenRightNull() { | 110 | public void testIsUserHasRightFailsWhenRightNull() { |
111 | + userService.isUserHasRight("user", null); | ||
64 | } | 112 | } |
65 | 113 | ||
66 | @Test(expected = IllegalArgumentException.class) | 114 | @Test(expected = IllegalArgumentException.class) |
67 | public void testIsUserHasRightFailsWhenRightNotInDatabase() { | 115 | public void testIsUserHasRightFailsWhenRightNotInDatabase() { |
116 | + Right right = new Right(); | ||
117 | + right.setName("dummy"); | ||
118 | + userService.isUserHasRight("user", right); | ||
68 | } | 119 | } |
69 | } | 120 | } |