Commit a800fde0ee3b7c25491f95621e374b113ea1300f

Authored by jcartign
0 parents

First version, include basic tests.

.gitignore 0 → 100644
  1 +++ a/.gitignore
@@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
  1 +*.iml
  2 +*/target/
  3 +.idea/
  4 +*/.idea/
pom.xml 0 → 100644
  1 +++ a/pom.xml
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  2 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4 + http://maven.apache.org/maven-v4_0_0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 +
  7 + <groupId>fr.plil.sio.persistence</groupId>
  8 + <artifactId>spring-jdbc</artifactId>
  9 + <version>0.1</version>
  10 +
  11 + <parent>
  12 + <groupId>org.springframework.boot</groupId>
  13 + <artifactId>spring-boot-starter-parent</artifactId>
  14 + <version>1.3.6.RELEASE</version>
  15 + </parent>
  16 +
  17 + <dependencies>
  18 + <dependency>
  19 + <groupId>org.springframework.boot</groupId>
  20 + <artifactId>spring-boot-starter-jdbc</artifactId>
  21 + </dependency>
  22 + <dependency>
  23 + <groupId>com.h2database</groupId>
  24 + <artifactId>h2</artifactId>
  25 + </dependency>
  26 + <dependency>
  27 + <groupId>org.springframework.boot</groupId>
  28 + <artifactId>spring-boot-starter-test</artifactId>
  29 + <scope>test</scope>
  30 + </dependency>
  31 + </dependencies>
  32 +
  33 + <properties>
  34 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  35 + <java.version>1.8</java.version>
  36 + </properties>
  37 +
  38 +</project>
src/main/java/fr/plil/sio/persistence/api/Group.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/Group.java
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +import java.util.Set;
  4 +import java.util.TreeSet;
  5 +
  6 +public class Group {
  7 +
  8 + private Long id;
  9 +
  10 + private String name;
  11 +
  12 + private Set<User> users = new TreeSet<>();
  13 +
  14 + public Long getId() {
  15 + return id;
  16 + }
  17 +
  18 + public void setId(Long id) {
  19 + this.id = id;
  20 + }
  21 +
  22 + public String getName() {
  23 + return name;
  24 + }
  25 +
  26 + public void setName(String name) {
  27 + this.name = name;
  28 + }
  29 +
  30 + public Set<User> getUsers() {
  31 + return users;
  32 + }
  33 +
  34 + public void setUsers(Set<User> users) {
  35 + this.users = users;
  36 + }
  37 +}
src/main/java/fr/plil/sio/persistence/api/GroupService.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/GroupService.java
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +public interface GroupService {
  4 +
  5 + Group create(String name);
  6 +
  7 + boolean delete(Group group);
  8 +
  9 + Group findByName(String name);
  10 +
  11 + boolean addUser(Group group, User user);
  12 +
  13 + boolean removeUser(Group group, User user);
  14 +
  15 + boolean addRight(Group group, Right right);
  16 +
  17 + boolean removeRight(Group group, Right right);
  18 +}
src/main/java/fr/plil/sio/persistence/api/Right.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/Right.java
@@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +import java.util.Set;
  4 +
  5 +public class Right {
  6 +
  7 + private Long id;
  8 +
  9 + private String name;
  10 +
  11 + private Right parent;
  12 +
  13 + private Set<Right> siblings;
  14 +
  15 + public Set<Right> getSiblings() {
  16 + return siblings;
  17 + }
  18 +
  19 + public void setSiblings(Set<Right> siblings) {
  20 + this.siblings = siblings;
  21 + }
  22 +
  23 + public Long getId() {
  24 + return id;
  25 + }
  26 +
  27 + public void setId(Long id) {
  28 + this.id = id;
  29 + }
  30 +
  31 + public String getName() {
  32 + return name;
  33 + }
  34 +
  35 + public void setName(String name) {
  36 + this.name = name;
  37 + }
  38 +
  39 + public Right getParent() {
  40 + return parent;
  41 + }
  42 +
  43 + public void setParent(Right parent) {
  44 + this.parent = parent;
  45 + }
  46 +}
src/main/java/fr/plil/sio/persistence/api/RightService.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/RightService.java
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +public interface RightService {
  4 +
  5 + Right create(String name);
  6 +
  7 + Right create(String name, Right parent);
  8 +
  9 + boolean delete(Right group);
  10 +
  11 + Right findByName(String name);
  12 +}
src/main/java/fr/plil/sio/persistence/api/User.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/User.java
@@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +import javax.annotation.Generated;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.GeneratedValue;
  6 +import javax.persistence.GenerationType;
  7 +import javax.persistence.Id;
  8 +
  9 +@Entity
  10 +public class User {
  11 +
  12 + @Id
  13 + @GeneratedValue(strategy = GenerationType.AUTO)
  14 + private Long id;
  15 +
  16 + private String name;
  17 +
  18 + private Group group;
  19 +
  20 + public Long getId() {
  21 + return id;
  22 + }
  23 +
  24 + public void setId(Long id) {
  25 + this.id = id;
  26 + }
  27 +
  28 + public String getName() {
  29 + return name;
  30 + }
  31 +
  32 + public void setName(String name) {
  33 + this.name = name;
  34 + }
  35 +
  36 + public Group getGroup() {
  37 + return group;
  38 + }
  39 +
  40 + public void setGroup(Group group) {
  41 + this.group = group;
  42 + }
  43 +}
src/main/java/fr/plil/sio/persistence/api/UserService.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/api/UserService.java
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +package fr.plil.sio.persistence.api;
  2 +
  3 +public interface UserService {
  4 +
  5 + User create(String name);
  6 +
  7 + boolean delete(User user);
  8 +
  9 + User findByName(String name);
  10 +
  11 + boolean isUserHasRight(User user, Right right);
  12 +}
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
@@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.GroupService;
  5 +import fr.plil.sio.persistence.api.Right;
  6 +import fr.plil.sio.persistence.api.User;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +@Service
  10 +public class GroupServiceJdbc implements GroupService {
  11 + @Override
  12 + public Group create(String name) {
  13 + return null;
  14 + }
  15 +
  16 + @Override
  17 + public boolean delete(Group group) {
  18 + return false;
  19 + }
  20 +
  21 + @Override
  22 + public Group findByName(String name) {
  23 + return null;
  24 + }
  25 +
  26 + @Override
  27 + public boolean addUser(Group group, User user) {
  28 + return false;
  29 + }
  30 +
  31 + @Override
  32 + public boolean removeUser(Group group, User user) {
  33 + return false;
  34 + }
  35 +
  36 + @Override
  37 + public boolean addRight(Group group, Right right) {
  38 + return false;
  39 +
  40 + }
  41 +
  42 + @Override
  43 + public boolean removeRight(Group group, Right right) {
  44 + return false;
  45 + }
  46 +}
src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/JdbcApplication.java
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import org.springframework.boot.autoconfigure.SpringBootApplication;
  4 +
  5 +@SpringBootApplication
  6 +public class JdbcApplication {
  7 +}
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.Right;
  4 +import fr.plil.sio.persistence.api.RightService;
  5 +import org.springframework.stereotype.Service;
  6 +
  7 +@Service
  8 +public class RightServiceJdbc implements RightService{
  9 + @Override
  10 + public Right create(String name) {
  11 + return null;
  12 + }
  13 +
  14 + @Override
  15 + public Right create(String name, Right parent) {
  16 + return null;
  17 + }
  18 +
  19 + @Override
  20 + public boolean delete(Right group) {
  21 + return false;
  22 + }
  23 +
  24 + @Override
  25 + public Right findByName(String name) {
  26 + return null;
  27 + }
  28 +}
src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  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/UserRepositoryJdbc.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
@@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.User;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Repository;
  8 +
  9 +import javax.sql.DataSource;
  10 +import java.sql.ResultSet;
  11 +import java.sql.SQLException;
  12 +import java.sql.Statement;
  13 +
  14 +@Repository
  15 +public class UserRepositoryJdbc implements UserRepository {
  16 +
  17 + private static final Logger logger = LoggerFactory.getLogger(UserRepositoryJdbc.class);
  18 +
  19 + @Autowired
  20 + private DataSource dataSource;
  21 +
  22 + @Override
  23 + public User findByName(String name) {
  24 + Statement stmt = null;
  25 + ResultSet rs = null;
  26 + try {
  27 + stmt = dataSource.getConnection().createStatement();
  28 + rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'");
  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;
  35 + } else {
  36 + logger.debug("not found " + name);
  37 + return null;
  38 + }
  39 + } catch (SQLException e) {
  40 + throw new UnsupportedOperationException("sql exception", e);
  41 + } finally {
  42 + try {
  43 + if (rs != null) {
  44 + rs.close();
  45 + }
  46 + if (stmt != null) {
  47 + stmt.close();
  48 + }
  49 + } catch (SQLException e) {
  50 + throw new UnsupportedOperationException("sql exception during close", e);
  51 +
  52 + }
  53 + }
  54 + }
  55 +
  56 + @Override
  57 + public void delete(Long id) {
  58 + throw new IllegalStateException("not implemented !");
  59 + }
  60 +
  61 + @Override
  62 + public void save(User user) {
  63 + Statement stmt = null;
  64 + ResultSet rs = null;
  65 + try {
  66 + stmt = dataSource.getConnection().createStatement();
  67 + stmt.executeUpdate("INSERT INTO USER_T (NAME_C) VALUES (\'" + user.getName() + "\')",
  68 + Statement.RETURN_GENERATED_KEYS);
  69 + rs = stmt.getGeneratedKeys();
  70 + if (rs.next()) {
  71 + user.setId(rs.getLong(1));
  72 + } else {
  73 + throw new UnsupportedOperationException("default in key access");
  74 + }
  75 + } catch (SQLException e) {
  76 + throw new UnsupportedOperationException("sql exception", e);
  77 + } finally {
  78 + try {
  79 + if (rs != null) {
  80 + rs.close();
  81 + }
  82 + if (stmt != null) {
  83 + stmt.close();
  84 + }
  85 + } catch (SQLException e) {
  86 + throw new UnsupportedOperationException("sql exception during close", e);
  87 + }
  88 + }
  89 +
  90 + }
  91 +}
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java 0 → 100644
  1 +++ a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.Right;
  4 +import fr.plil.sio.persistence.api.User;
  5 +import fr.plil.sio.persistence.api.UserService;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +@Service
  10 +public class UserServiceJdbc implements UserService {
  11 +
  12 + @Autowired
  13 + private UserRepository userRepository;
  14 +
  15 + @Override
  16 + public User create(String name) {
  17 + return null;
  18 + }
  19 +
  20 + @Override
  21 + public boolean delete(User user) {
  22 + return false;
  23 + }
  24 +
  25 + @Override
  26 + public boolean isUserHasRight(User user, Right right) {
  27 + return false;
  28 + }
  29 +
  30 + @Override
  31 + public User findByName(String name) {
  32 + return null;
  33 + }
  34 +
  35 + public void setUserRepository(UserRepository userRepository) {
  36 + this.userRepository = userRepository;
  37 + }
  38 +}
src/main/resources/schema.sql 0 → 100644
  1 +++ a/src/main/resources/schema.sql
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +CREATE TABLE USER_T (USER_ID INT NOT NULL AUTO_INCREMENT, NAME_C VARCHAR(50) UNIQUE NOT NULL, PRIMARY KEY(USER_ID));
  2 +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 0 → 100644
  1 +++ a/src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java
@@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.GroupService;
  5 +import fr.plil.sio.persistence.api.User;
  6 +import fr.plil.sio.persistence.api.UserService;
  7 +import org.junit.Test;
  8 +import org.junit.runner.RunWith;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.boot.test.SpringApplicationConfiguration;
  11 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12 +
  13 +import static org.junit.Assert.assertEquals;
  14 +import static org.junit.Assert.assertNotNull;
  15 +
  16 +@RunWith(SpringJUnit4ClassRunner.class)
  17 +@SpringApplicationConfiguration(classes = JdbcApplication.class)
  18 +public class GroupServiceTest {
  19 +
  20 + @Autowired
  21 + private UserService userService;
  22 +
  23 + @Autowired
  24 + private GroupService groupService;
  25 +
  26 + @Test
  27 + public void testCreateAndFindByName() {
  28 + groupService.create("group1");
  29 + Group group2 = groupService.findByName("group1");
  30 + assertNotNull(group2);
  31 + assertNotNull(group2.getName());
  32 + assertEquals("group1", group2.getName());
  33 + }
  34 +
  35 + @Test
  36 + public void addUsersAndCheckIfPresent() {
  37 + Group group1 = groupService.create("group1");
  38 + User user1 = userService.create("user1");
  39 + User user2 = userService.create("user2");
  40 + groupService.addUser(group1, user1);
  41 + groupService.addUser(group1, user2);
  42 + Group group2 = groupService.findByName("group1");
  43 + assertNotNull(group2);
  44 + assertNotNull(group2.getName());
  45 + assertEquals("group1", group2.getName());
  46 + assertEquals(2, group2.getUsers().size());
  47 + }
  48 +
  49 + @Test
  50 + public void deleteGroupDoesNotDeleteUsers() {
  51 + Group group1 = groupService.create("group1");
  52 + User user1 = userService.create("user1");
  53 + User user2 = userService.create("user2");
  54 + groupService.addUser(group1, user1);
  55 + groupService.addUser(group1, user2);
  56 + groupService.delete(group1);
  57 + assertNotNull(userService.findByName("user1"));
  58 + assertNotNull(userService.findByName("user2"));
  59 + }
  60 +
  61 +}
src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java 0 → 100644
  1 +++ a/src/test/java/fr/plil/sio/persistence/jdbc/RightServiceTest.java
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import org.junit.runner.RunWith;
  4 +import org.springframework.boot.test.SpringApplicationConfiguration;
  5 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6 +
  7 +@RunWith(SpringJUnit4ClassRunner.class)
  8 +@SpringApplicationConfiguration(classes = JdbcApplication.class)
  9 +public class RightServiceTest {
  10 +}
src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java 0 → 100644
  1 +++ a/src/test/java/fr/plil/sio/persistence/jdbc/UserServiceTest.java
@@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
  1 +package fr.plil.sio.persistence.jdbc;
  2 +
  3 +import fr.plil.sio.persistence.api.User;
  4 +import fr.plil.sio.persistence.api.UserService;
  5 +import org.junit.Test;
  6 +import org.junit.runner.RunWith;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.boot.test.SpringApplicationConfiguration;
  9 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  10 +
  11 +import static org.junit.Assert.assertEquals;
  12 +import static org.junit.Assert.assertNotNull;
  13 +
  14 +@RunWith(SpringJUnit4ClassRunner.class)
  15 +@SpringApplicationConfiguration(classes = JdbcApplication.class)
  16 +public class UserServiceTest {
  17 +
  18 + @Autowired
  19 + private UserService userService;
  20 +
  21 + @Test
  22 + public void testCreateAndFindByName() {
  23 + final String name = "blabla";
  24 + User user1 = userService.create(name);
  25 + User user2 = userService.findByName(name);
  26 + assertNotNull(user1);
  27 + assertNotNull(user2);
  28 + assertNotNull(user1.getName());
  29 + assertNotNull(user2.getName());
  30 + assertNotNull(user1.getGroup());
  31 + assertNotNull(user2.getGroup());
  32 + assertEquals(user1.getName(), user2.getName());
  33 + assertEquals(user1.getGroup(), user2.getGroup());
  34 + }
  35 +}
src/test/resources/application.properties 0 → 100644
  1 +++ a/src/test/resources/application.properties
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +logging.level.org.springframework=INFO
  2 +logging.level.org.hibernate.SQL=DEBUG
  3 +logging.level.org.hibernate=INFO
0 \ No newline at end of file 4 \ No newline at end of file