Commit 7940aa2284e3b83e74d346fd60fbf5e8b8113a2f

Authored by jcartign
1 parent bda41d02

Finishing API javadocs

src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.Group;
  4 +
  5 +public interface GroupRepository {
  6 +
  7 + Group findByName(String name);
  8 +
  9 + void delete(Long id);
  10 +
  11 + void save(Group group);
  12 +}
... ...
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java renamed to src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
3   -import fr.plil.sio.persistence.api.User;
  3 +import fr.plil.sio.persistence.api.Group;
4 4 import org.slf4j.Logger;
5 5 import org.slf4j.LoggerFactory;
6 6 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -12,26 +12,26 @@ import java.sql.SQLException;
12 12 import java.sql.Statement;
13 13  
14 14 @Repository
15   -public class UserRepositoryJdbc implements UserRepository {
  15 +public class GroupRepositoryJdbc implements GroupRepository {
16 16  
17   - private static final Logger logger = LoggerFactory.getLogger(UserRepositoryJdbc.class);
  17 + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
18 18  
19 19 @Autowired
20 20 private DataSource dataSource;
21 21  
22 22 @Override
23   - public User findByName(String name) {
  23 + public Group findByName(String name) {
24 24 Statement stmt = null;
25 25 ResultSet rs = null;
26 26 try {
27 27 stmt = dataSource.getConnection().createStatement();
28 28 rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'");
29 29 if (rs.next()) {
30   - logger.debug("found user " + name);
31   - User user = new User();
32   - user.setId(rs.getLong("GROUP_ID"));
33   - user.setName(rs.getString("NAME_C"));
34   - return user;
  30 + logger.debug("found group " + name);
  31 + Group group = new Group();
  32 + group.setId(rs.getLong("GROUP_ID"));
  33 + group.setName(rs.getString("NAME_C"));
  34 + return group;
35 35 } else {
36 36 logger.debug("not found " + name);
37 37 return null;
... ... @@ -54,21 +54,16 @@ public class UserRepositoryJdbc implements UserRepository {
54 54 }
55 55  
56 56 @Override
57   - public void delete(Long id) {
58   - throw new IllegalStateException("not implemented !");
59   - }
60   -
61   - @Override
62   - public void save(User user) {
  57 + public void save(Group group) {
63 58 Statement stmt = null;
64 59 ResultSet rs = null;
65 60 try {
66 61 stmt = dataSource.getConnection().createStatement();
67   - stmt.executeUpdate("INSERT INTO USER_T (NAME_C) VALUES (\'" + user.getName() + ")",
  62 + stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + ")",
68 63 Statement.RETURN_GENERATED_KEYS);
69 64 rs = stmt.getGeneratedKeys();
70 65 if (rs.next()) {
71   - user.setId(rs.getLong(1));
  66 + group.setId(rs.getLong(1));
72 67 } else {
73 68 throw new UnsupportedOperationException("default in key access");
74 69 }
... ... @@ -88,4 +83,9 @@ public class UserRepositoryJdbc implements UserRepository {
88 83 }
89 84  
90 85 }
  86 +
  87 + @Override
  88 + public void delete(Long id) {
  89 + throw new IllegalStateException("not implemented !");
  90 + }
91 91 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... ... @@ -3,13 +3,28 @@ package fr.plil.sio.persistence.jdbc;
3 3 import fr.plil.sio.persistence.api.Group;
4 4 import fr.plil.sio.persistence.api.GroupService;
5 5 import fr.plil.sio.persistence.api.Right;
  6 +import org.springframework.beans.factory.annotation.Autowired;
6 7 import org.springframework.stereotype.Service;
7 8  
8 9 @Service
9 10 public class GroupServiceJdbc implements GroupService {
  11 +
  12 + @Autowired
  13 + private GroupRepository groupRepository;
  14 +
10 15 @Override
11 16 public Group create(String name) {
12   - return null;
  17 + if (name == null) {
  18 + throw new IllegalArgumentException("name cannot be null");
  19 + }
  20 + Group group = groupRepository.findByName(name);
  21 + if (group != null) {
  22 + throw new IllegalStateException("a group with the same name already exists");
  23 + }
  24 + group = new Group();
  25 + group.setName(name);
  26 + groupRepository.save(group);
  27 + return group;
13 28 }
14 29  
15 30 @Override
... ... @@ -19,17 +34,10 @@ public class GroupServiceJdbc implements GroupService {
19 34  
20 35 @Override
21 36 public Group findByName(String name) {
22   - return null;
23   - }
24   -
25   - @Override
26   - public boolean addUser(String groupName, String userName) {
27   - return false;
28   - }
29   -
30   - @Override
31   - public boolean removeUser(String groupName, String userName) {
32   - return false;
  37 + if (name == null) {
  38 + throw new IllegalArgumentException("name cannot be null");
  39 + }
  40 + return groupRepository.findByName(name);
33 41 }
34 42  
35 43 @Override
... ...
src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java
... ... @@ -4,4 +4,5 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
4 4  
5 5 @SpringBootApplication
6 6 public class JdbcApplication {
  7 +
7 8 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java deleted
... ... @@ -1,12 +0,0 @@
1   -package fr.plil.sio.persistence.jdbc;
2   -
3   -import fr.plil.sio.persistence.api.User;
4   -
5   -public interface UserRepository {
6   -
7   - User findByName(String name);
8   -
9   - void delete(Long id);
10   -
11   - void save(User user);
12   -}
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
... ... @@ -10,7 +10,7 @@ import org.springframework.stereotype.Service;
10 10 public class UserServiceJdbc implements UserService {
11 11  
12 12 @Autowired
13   - private UserRepository userRepository;
  13 + private GroupRepository userRepository;
14 14  
15 15 @Override
16 16 public User create(String name, String groupName) {
... ...
src/main/resources/schema.sql
1   -CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID));
2 1 CREATE TABLE GROUP_T (GROUP_ID INT NOT NULL AUTO_INCREMENT, NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(GROUP_ID));
... ...
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;
  6 +import org.junit.Before;
3 7 import org.junit.Test;
4 8 import org.junit.runner.RunWith;
  9 +import org.springframework.beans.factory.annotation.Autowired;
5 10 import org.springframework.boot.test.SpringApplicationConfiguration;
6 11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7 12  
  13 +import static org.junit.Assert.*;
  14 +
8 15 @RunWith(SpringJUnit4ClassRunner.class)
9 16 @SpringApplicationConfiguration(classes = JdbcApplication.class)
10 17 public class GroupServiceTest {
11 18  
  19 + @Autowired
  20 + private GroupService groupService;
  21 +
  22 + @Autowired
  23 + private UserService userService;
  24 +
  25 + @Before
  26 + public void before() {
  27 + groupService.create("group");
  28 + }
  29 +
12 30 @Test
13 31 public void testCreateGroupAndFindByName() {
  32 + Group group = groupService.findByName("group");
  33 + assertEquals("group", group.getName());
14 34 }
15 35  
16 36 @Test(expected = IllegalArgumentException.class)
17 37 public void testCreateGroupFailsWhenNameNull() {
  38 + groupService.create(null);
18 39 }
19 40  
20 41 @Test(expected = IllegalStateException.class)
21 42 public void testCreateFailsWhenSameGroupUserAlreadyPresent() {
  43 + groupService.create("group");
22 44 }
23 45  
24 46 public void testDeleteGroup() {
25   - }
26   -
27   - public void testDeleteGroupIfNotFound() {
  47 + assertTrue(groupService.delete("group"));
  48 + assertNull(groupService.findByName("group"));
  49 + assertFalse(groupService.delete("unknown"));
28 50 }
29 51  
30 52 @Test(expected = IllegalArgumentException.class)
31 53 public void testDeleteGroupFailsIfNameNull() {
  54 + groupService.delete(null);
32 55 }
33 56  
34 57 @Test
35 58 public void deleteGroupDoesDeleteUsers() {
  59 + userService.create("user1", "group");
  60 + userService.create("user1", "group");
  61 + assertNotNull(userService.findByName("user1"));
  62 + assertNotNull(userService.findByName("user2"));
  63 + groupService.delete("group");
  64 + assertNull(userService.findByName("user1"));
  65 + assertNull(userService.findByName("user2"));
36 66 }
37 67  
38 68 public void testFindByNameIfUserNotFound() {
  69 + assertNull(groupService.findByName("unknown"));
39 70 }
40 71  
41 72 @Test(expected = IllegalArgumentException.class)
42 73 public void testFindByNameFailsIfNameNull() {
  74 + groupService.findByName(null);
43 75 }
44 76  
45 77 @Test
... ... @@ -72,16 +104,6 @@ public class GroupServiceTest {
72 104  
73 105 }
74 106  
75   -
76   - /**
77   - * Remove a right associated with a group.
78   - *
79   - * @param groupName the name of the group.
80   - * @param right the right to remove
81   - * @return true if right is removed from the group, false if teh right was not present in the group.
82   - * @throws IllegalArgumentException if groupName or right is null, or if group or right cannot be found.
83   - */
84   -
85 107 @Test
86 108 public void testRemoveRight() {
87 109  
... ...
src/test/resources/application.properties
1 1 logging.level.org.springframework=INFO
2 2 logging.level.org.hibernate.SQL=DEBUG
3   -logging.level.org.hibernate=INFO
4 3 \ No newline at end of file
  4 +logging.level.org.hibernate=INFO
  5 +#spring.datasource.default-auto-commit=false
  6 +#spring.datasource.auto-commit=false
5 7 \ No newline at end of file
... ...