Commit 292aec6f6123cd2cc3584d721c788eae71b9cf37

Authored by jcartign
1 parent 6fc5c241

First version before first course

src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... ... @@ -29,7 +29,7 @@ public class GroupServiceJdbc implements GroupService {
29 29  
30 30 @Override
31 31 public boolean delete(String name) {
32   - return false;
  32 + throw new IllegalStateException("not implemented !");
33 33 }
34 34  
35 35 @Override
... ... @@ -42,11 +42,11 @@ public class GroupServiceJdbc implements GroupService {
42 42  
43 43 @Override
44 44 public boolean addRight(String groupName, Right right) {
45   - return false;
  45 + throw new IllegalStateException("not implemented !");
46 46 }
47 47  
48 48 @Override
49 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 import java.util.List;
8 8  
9 9 @Service
10   -public class RightServiceJdbc implements RightService{
  10 +public class RightServiceJdbc implements RightService {
11 11 @Override
12 12 public Right create(String name) {
13 13 return null;
... ... @@ -15,21 +15,21 @@ public class RightServiceJdbc implements RightService{
15 15  
16 16 @Override
17 17 public Right create(String name, Right parent) {
18   - return null;
  18 + throw new IllegalStateException("not implemented !");
19 19 }
20 20  
21 21 @Override
22 22 public boolean delete(Right right) {
23   - return false;
  23 + throw new IllegalStateException("not implemented !");
24 24 }
25 25  
26 26 @Override
27 27 public List<Right> findByName(String name) {
28   - return null;
  28 + throw new IllegalStateException("not implemented !");
29 29 }
30 30  
31 31 @Override
32 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 14  
15 15 @Override
16 16 public User create(String name, String groupName) {
17   - return null;
  17 + throw new IllegalStateException("not implemented !");
18 18 }
19 19  
20 20 @Override
21 21 public boolean delete(String name) {
22   - return false;
  22 + throw new IllegalStateException("not implemented !");
23 23 }
24 24  
25 25 @Override
26 26 public User findByName(String name) {
27   - return null;
  27 + throw new IllegalStateException("not implemented !");
28 28 }
29 29  
30 30 @Override
31 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 @@
  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 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 4 import org.junit.Before;
7 5 import org.junit.Test;
8 6 import org.junit.runner.RunWith;
... ... @@ -14,7 +12,7 @@ import static org.junit.Assert.*;
14 12  
15 13 @RunWith(SpringJUnit4ClassRunner.class)
16 14 @SpringApplicationConfiguration(classes = JdbcApplication.class)
17   -public class GroupServiceTest {
  15 +public class GroupServiceTest extends AbstractServiceSupport {
18 16  
19 17 @Autowired
20 18 private GroupService groupService;
... ... @@ -22,8 +20,15 @@ public class GroupServiceTest {
22 20 @Autowired
23 21 private UserService userService;
24 22  
  23 + @Autowired
  24 + private RightService rightService;
  25 +
  26 + private Right parent;
  27 +
25 28 @Before
26 29 public void before() {
  30 + parent = rightService.create("parent");
  31 + rightService.create("sibling", parent);
27 32 groupService.create("group");
28 33 }
29 34  
... ... @@ -46,9 +51,14 @@ public class GroupServiceTest {
46 51 public void testDeleteGroup() {
47 52 assertTrue(groupService.delete("group"));
48 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 62 @Test(expected = IllegalArgumentException.class)
53 63 public void testDeleteGroupFailsIfNameNull() {
54 64 groupService.delete(null);
... ... @@ -65,7 +75,7 @@ public class GroupServiceTest {
65 75 assertNull(userService.findByName("user2"));
66 76 }
67 77  
68   - public void testFindByNameIfUserNotFound() {
  78 + public void testFindByNameIfGroupNotFound() {
69 79 assertNull(groupService.findByName("unknown"));
70 80 }
71 81  
... ... @@ -76,61 +86,64 @@ public class GroupServiceTest {
76 86  
77 87 @Test
78 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 97 @Test
83 98 public void testAddRightIfAlreadyPresent() {
84   -
  99 + assertTrue(groupService.addRight("group", parent));
  100 + assertFalse(groupService.addRight("group", parent));
85 101 }
86 102  
87 103 @Test(expected = IllegalArgumentException.class)
88 104 public void testAddRightFailsIfGroupNameNull() {
89   -
  105 + groupService.addRight(null, parent);
90 106 }
91 107  
92 108 @Test(expected = IllegalArgumentException.class)
93 109 public void testAddRightFailsIfRightNull() {
94   -
  110 + groupService.addRight("group", null);
95 111 }
96 112  
97 113 @Test(expected = IllegalArgumentException.class)
98 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 118 @Test
108 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 128 @Test
113 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 135 @Test(expected = IllegalArgumentException.class)
118 136 public void testRemoveRightFailsIfGroupNameNull() {
119   -
  137 + groupService.removeRight(null, parent);
120 138 }
121 139  
122 140 @Test(expected = IllegalArgumentException.class)
123 141 public void testRemoveRightFailsIfRightNull() {
124   -
  142 + groupService.removeRight("group", null);
125 143 }
126 144  
127 145 @Test(expected = IllegalArgumentException.class)
128 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 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 5 import org.junit.Test;
4 6 import org.junit.runner.RunWith;
  7 +import org.springframework.beans.factory.annotation.Autowired;
5 8 import org.springframework.boot.test.SpringApplicationConfiguration;
6 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7 10  
  11 +import static org.junit.Assert.*;
  12 +
8 13 @RunWith(SpringJUnit4ClassRunner.class)
9 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 20 @Test
13 21 public void testCreateParentRightAndFindOne() {
14   -
  22 + rightService.create("right");
  23 + assertEquals(1, rightService.findByName("right").size());
15 24 }
16 25  
17 26 @Test
18 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 33 @Test(expected = IllegalArgumentException.class)
23 34 public void testCreateFailsIfNameNull() {
24   -
  35 + rightService.create(null);
25 36 }
26 37  
27 38 @Test
28 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 45 @Test(expected = IllegalArgumentException.class)
33 46 public void testCreateSiblingFailsIfNameNull() {
34   -
  47 + Right parent = rightService.create("parent");
  48 + rightService.create(null, parent);
35 49 }
36 50  
37 51 @Test(expected = IllegalArgumentException.class)
38 52 public void testCreateSiblingFailsIfParentNull() {
39   -
  53 + rightService.create("parent");
  54 + rightService.create("sibling", null);
40 55 }
41 56  
42 57 @Test(expected = IllegalArgumentException.class)
43 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 64 @Test
48 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 72 @Test
52 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 80 @Test
57 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 89 @Test
61 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 96 @Test(expected = IllegalArgumentException.class)
65 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 101 @Test
77 102 public void testFindByNameIfNameNotFound() {
  103 + assertEquals(0, rightService.findByName("no").size());
78 104 }
79 105  
80 106 @Test(expected = IllegalArgumentException.class)
81 107 public void testFindByNameFailsIfNameNull() {
  108 + rightService.findByName(null);
82 109 }
83 110  
84 111 @Test
85 112 public void testFindOne() {
  113 + Right right = rightService.create("right");
  114 + assertNotNull(rightService.findOne(right.getId()));
86 115 }
87 116  
88 117 @Test
89 118 public void testFindOneIfIdNotFound() {
  119 + assertNull(rightService.findOne(153463167809232L));
90 120 }
91 121  
92 122 @Test(expected = IllegalArgumentException.class)
93 123 public void testFindOneFailsIfIdNull() {
  124 + rightService.findOne(null);
94 125 }
95 126 }
... ...
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
  3 +import fr.plil.sio.persistence.api.*;
  4 +import org.junit.Before;
3 5 import org.junit.Test;
4 6 import org.junit.runner.RunWith;
  7 +import org.springframework.beans.factory.annotation.Autowired;
5 8 import org.springframework.boot.test.SpringApplicationConfiguration;
6 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7 10  
  11 +import static org.junit.Assert.*;
  12 +
8 13 @RunWith(SpringJUnit4ClassRunner.class)
9 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 36 @Test
13 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 46 @Test(expected = IllegalArgumentException.class)
17 47 public void testCreateFailsWhenNameNull() {
  48 + userService.create(null, "group");
18 49 }
19 50  
20 51 @Test(expected = IllegalArgumentException.class)
21 52 public void testCreateFailsWhenGroupNameNull() {
  53 + userService.create("user", null);
22 54 }
23 55  
24 56 @Test(expected = IllegalArgumentException.class)
25 57 public void testCreateFailsWhenGroupDoesNotExist() {
  58 + userService.create("user", "notGroup");
26 59 }
27 60  
28 61 @Test(expected = IllegalStateException.class)
29 62 public void testCreateFailsWhenSameNameUserAlreadyPresent() {
  63 + userService.create("user", "group");
30 64 }
31 65  
32 66 public void testDeleteUser() {
  67 + assertTrue(userService.delete("user"));
33 68 }
34 69  
35 70 public void testDeleteUserIfNotFound() {
  71 + assertFalse(userService.delete("user"));
36 72 }
37 73  
38 74 @Test(expected = IllegalArgumentException.class)
39 75 public void testDeleteUserFailsIfNameNull() {
  76 + userService.delete(null);
40 77 }
41 78  
42 79 public void testFindUserByNameIfUserNotFound() {
  80 + assertNull(userService.findByName("blabla"));
43 81 }
44 82  
45 83 @Test(expected = IllegalArgumentException.class)
46 84 public void testFindUserByNameFailsIfNameNull() {
  85 + assertNull(userService.findByName(null));
47 86 }
48 87  
49 88 public void testIsUserHasExactRight() {
  89 + Right right = rightService.findByName("parent").get(0);
  90 + assertTrue(userService.isUserHasRight("user", right));
50 91 }
51 92  
52 93 public void testIsUserHasRightByParents() {
  94 + Right right = rightService.findByName("sibling").get(0);
  95 + assertTrue(userService.isUserHasRight("user", right));
53 96 }
54 97  
55 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 103 @Test(expected = IllegalArgumentException.class)
59 104 public void testIsUserHasRightFailsWhenUsernameNull() {
  105 + Right right = rightService.findByName("parent").get(0);
  106 + userService.isUserHasRight(null, right);
60 107 }
61 108  
62 109 @Test(expected = IllegalArgumentException.class)
63 110 public void testIsUserHasRightFailsWhenRightNull() {
  111 + userService.isUserHasRight("user", null);
64 112 }
65 113  
66 114 @Test(expected = IllegalArgumentException.class)
67 115 public void testIsUserHasRightFailsWhenRightNotInDatabase() {
  116 + Right right = new Right();
  117 + right.setName("dummy");
  118 + userService.isUserHasRight("user", right);
68 119 }
69 120 }
... ...