diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 4dccc35039fe3e8a38b6d9f097d66a40ba67ad58..434d57ced2ae967958017da40b1e812b87a69f51 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,3 +2,27 @@ include:
- project: 'dhu.projets/ci-include'
ref: v1.0.3
file: 'ci-verify-deploy-release.yml'
+
+default:
+ before_script:
+ - |
+ sudo -u postgres psql << EOF
+ CREATE ROLE ${TEST_DB_USER} WITH login PASSWORD $(echo "'"${TEST_DB_PASSWORD}"'");
+ CREATE DATABASE "${TEST_DB_NAME}" OWNER ${TEST_DB_USER};
+ \l
+ EOF
+
+ - sudo echo "127.0.1.2 ${TEST_DB_HOST}" >> /etc/hosts
+ - cat /etc/hosts
+
+ - |
+ PGPASSWORD="${TEST_DB_PASSWORD}" psql -h ${TEST_DB_HOST} -p ${TEST_DB_PORT} -U "${TEST_DB_USER}" "${TEST_DB_NAME}" << EOF
+ \d
+ EOF
+
+ - |
+ export DATABASE_HOST=${TEST_DB_HOST}
+ export DATABASE_PORT=${TEST_DB_PORT}
+ export DATABASE_NAME=${TEST_DB_NAME}
+ export DATABASE_USER=${TEST_DB_USER}
+ export DATABASE_PASSWORD=${TEST_DB_PASSWORD}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index cf5af30ba8d15753276210a0fe4d0bc08e9e5305..1fa0f8984b4b74215cb3b4612bf826f1d2d65d55 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,12 @@
7.0
provided
+
+ org.postgresql
+ postgresql
+ 42.3.1
+
+
diff --git a/src/main/java/personne/datasource/DatabaseSetup.java b/src/main/java/personne/datasource/DatabaseSetup.java
new file mode 100644
index 0000000000000000000000000000000000000000..e3a8cc1e59b1473d037d93d15deb33dd543fc624
--- /dev/null
+++ b/src/main/java/personne/datasource/DatabaseSetup.java
@@ -0,0 +1,15 @@
+package personne.datasource;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin@rpn.ch)
+ */
+public interface DatabaseSetup {
+
+ void createTables() throws PersistenceException;
+
+ void dropTables() throws PersistenceException;
+
+ void insertData() throws PersistenceException;
+
+}
diff --git a/src/main/java/personne/datasource/EntiteInconnuePersistenceException.java b/src/main/java/personne/datasource/EntiteInconnuePersistenceException.java
new file mode 100644
index 0000000000000000000000000000000000000000..e0ff2d89a51605f28197dfb00519172c9899d96a
--- /dev/null
+++ b/src/main/java/personne/datasource/EntiteInconnuePersistenceException.java
@@ -0,0 +1,34 @@
+package personne.datasource;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class EntiteInconnuePersistenceException extends PersistenceException {
+
+ public EntiteInconnuePersistenceException() {
+ }
+
+ public EntiteInconnuePersistenceException(
+ final String message) {
+ super(message);
+ }
+
+ public EntiteInconnuePersistenceException(
+ final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ public EntiteInconnuePersistenceException(
+ final Throwable cause) {
+ super(cause);
+ }
+
+ public EntiteInconnuePersistenceException(
+ final String message,
+ final Throwable cause, final boolean enableSuppression,
+ final boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+
+}
diff --git a/src/main/java/personne/datasource/Mapper.java b/src/main/java/personne/datasource/Mapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..5105ac47f2515a4c419b4d628347d60d283aff34
--- /dev/null
+++ b/src/main/java/personne/datasource/Mapper.java
@@ -0,0 +1,20 @@
+package personne.datasource;
+
+import java.util.List;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin@rpn.ch)
+ */
+public interface Mapper {
+
+ E create(E entite) throws PersistenceException;
+
+ E retrieveByUUID(String uuid) throws PersistenceException;
+
+ List retrieve(String filtre) throws PersistenceException;
+
+ void update(E entite) throws PersistenceException;
+
+ void delete(E entite) throws PersistenceException;
+}
diff --git a/src/main/java/personne/datasource/MapperManager.java b/src/main/java/personne/datasource/MapperManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..8b937f690824cb389d3db6fba913f399b80d939c
--- /dev/null
+++ b/src/main/java/personne/datasource/MapperManager.java
@@ -0,0 +1,15 @@
+package personne.datasource;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin@rpn.ch)
+ */
+public interface MapperManager {
+
+ PersonneMapper getPersonneMapper()
+ throws PersistenceException;
+
+ DatabaseSetup getDatabaseSetup()
+ throws PersistenceException;
+
+}
diff --git a/src/main/java/personne/datasource/PersistenceException.java b/src/main/java/personne/datasource/PersistenceException.java
new file mode 100644
index 0000000000000000000000000000000000000000..506f5fe3b5c6547e37649d786831184add268314
--- /dev/null
+++ b/src/main/java/personne/datasource/PersistenceException.java
@@ -0,0 +1,35 @@
+package personne.datasource;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class PersistenceException extends Exception {
+
+ public PersistenceException() {
+ }
+
+ public PersistenceException(
+ final String message) {
+ super(message);
+ }
+
+ public PersistenceException(
+ final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ public PersistenceException(
+ final Throwable cause) {
+ super(cause);
+ }
+
+ public PersistenceException(
+ final String message,
+ final Throwable cause,
+ final boolean enableSuppression,
+ final boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+
+}
diff --git a/src/main/java/personne/datasource/PersonneMapper.java b/src/main/java/personne/datasource/PersonneMapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..f2d256cd4065a93dc991a3fc4784233f2d148b09
--- /dev/null
+++ b/src/main/java/personne/datasource/PersonneMapper.java
@@ -0,0 +1,11 @@
+package personne.datasource;
+
+import personne.domain.Personne;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public interface PersonneMapper extends Mapper {
+
+}
diff --git a/src/main/java/personne/datasource/db/DataSourceFactory.java b/src/main/java/personne/datasource/db/DataSourceFactory.java
new file mode 100644
index 0000000000000000000000000000000000000000..3ea03afcd16123d5a2acb3c3c336e2938277b020
--- /dev/null
+++ b/src/main/java/personne/datasource/db/DataSourceFactory.java
@@ -0,0 +1,33 @@
+package personne.datasource.db;
+
+import javax.sql.DataSource;
+import org.postgresql.ds.PGPoolingDataSource;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin@rpn.ch)
+ */
+public final class DataSourceFactory {
+
+ private DataSourceFactory() {
+ }
+
+ public static DataSource getPostgreSQLDataSource(
+ final String serverName,
+ final int portNumber,
+ final String databaseName,
+ final String user,
+ final String password) {
+
+ PGPoolingDataSource ds = new PGPoolingDataSource();
+ ds.setServerName(serverName);
+ ds.setPortNumber(portNumber);
+ ds.setDatabaseName(databaseName);
+ ds.setUser(user);
+ ds.setPassword(password);
+ ds.setMaxConnections(MAX_CONNECTION);
+
+ return ds;
+ }
+ private static final int MAX_CONNECTION = 10;
+}
diff --git a/src/main/java/personne/datasource/db/DatabaseSetupImpl.java b/src/main/java/personne/datasource/db/DatabaseSetupImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..386458477e8a640e519a93ae4fd2abb259342545
--- /dev/null
+++ b/src/main/java/personne/datasource/db/DatabaseSetupImpl.java
@@ -0,0 +1,90 @@
+package personne.datasource.db;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import personne.datasource.DatabaseSetup;
+import personne.datasource.PersistenceException;
+import personne.domain.DemoData;
+import personne.domain.Personne;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin@rpn.ch)
+ */
+public class DatabaseSetupImpl implements DatabaseSetup {
+//CHECKSTYLE.OFF: MagicNumber
+
+ private final DbMapperManagerImpl mapperManager;
+ private static final Logger LOG = Logger.getLogger(
+ DatabaseSetupImpl.class.getName());
+
+ public DatabaseSetupImpl(final DbMapperManagerImpl mm) {
+ this.mapperManager = mm;
+ }
+
+ @Override
+ public void createTables() throws PersistenceException {
+
+ try (Connection conn = mapperManager.getConnection()) {
+ try (Statement requete = conn.createStatement();) {
+ requete.addBatch(SQL.PERSONNES.CREATE_TABLE);
+
+ requete.executeBatch();
+ }
+ } catch (SQLException ex) {
+ LOG.log(Level.SEVERE, null, ex);
+ throw new PersistenceException(ex);
+ }
+
+ }
+
+ @Override
+ public void dropTables() throws PersistenceException {
+ try (Connection conn = mapperManager.getConnection()) {
+ try (Statement requete = conn.createStatement();) {
+
+ requete.addBatch(SQL.PERSONNES.DROP_TABLE);
+ requete.executeBatch();
+ }
+ } catch (SQLException ex) {
+ LOG.log(Level.SEVERE, null, ex);
+ throw new PersistenceException(ex);
+ }
+
+ }
+
+ @Override
+ public void insertData() throws PersistenceException {
+ DemoData data = new DemoData();
+ data.initialisation();
+ try (Connection conn
+ = this.mapperManager.getConnection()) {
+ conn.setAutoCommit(false);
+
+ try (PreparedStatement rc
+ = conn.prepareStatement(SQL.PERSONNES.INSERT)) {
+
+ for (Personne e : data.getPersonnes().values()) {
+
+ rc.setString(1, e.getUUID());
+ rc.setString(2, e.getNom());
+ rc.setString(3, e.getPrenom());
+ rc.addBatch();
+ }
+ rc.executeBatch();
+ }
+
+ conn.commit();
+
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+
+ }
+//CHECKSTYLE.ON: MagicNumber
+
+}
diff --git a/src/main/java/personne/datasource/db/DbMapperManagerImpl.java b/src/main/java/personne/datasource/db/DbMapperManagerImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..cabb9747f957830724d07a49f68468d29eed978c
--- /dev/null
+++ b/src/main/java/personne/datasource/db/DbMapperManagerImpl.java
@@ -0,0 +1,51 @@
+package personne.datasource.db;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import javax.sql.DataSource;
+import personne.datasource.DatabaseSetup;
+import personne.datasource.MapperManager;
+import personne.datasource.PersistenceException;
+import personne.datasource.PersonneMapper;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class DbMapperManagerImpl implements MapperManager {
+
+ private final DataSource datasource;
+ private DatabaseSetup databaseSetup;
+ private PersonneMapper personneMapper;
+
+ public DbMapperManagerImpl(final DataSource datasource) {
+ this.datasource = datasource;
+ }
+
+ Connection getConnection() throws PersistenceException {
+ try {
+ return this.datasource.getConnection();
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+ }
+
+ @Override
+ public PersonneMapper getPersonneMapper() {
+ if (this.personneMapper == null) {
+ this.personneMapper = new PersonneMapperImpl(this);
+ }
+ return this.personneMapper;
+ }
+
+
+ @Override
+ public DatabaseSetup getDatabaseSetup() {
+ if (this.databaseSetup == null) {
+ this.databaseSetup = new DatabaseSetupImpl(this);
+ }
+ return this.databaseSetup;
+
+ }
+
+}
diff --git a/src/main/java/personne/datasource/db/PersonneMapperImpl.java b/src/main/java/personne/datasource/db/PersonneMapperImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..d6aa068fb99e654979abe0f6ddd2f18da760ea05
--- /dev/null
+++ b/src/main/java/personne/datasource/db/PersonneMapperImpl.java
@@ -0,0 +1,247 @@
+package personne.datasource.db;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import personne.datasource.EntiteInconnuePersistenceException;
+import personne.datasource.PersistenceException;
+import personne.datasource.PersonneMapper;
+import personne.domain.Personne;
+import personne.domain.PersonneBase;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class PersonneMapperImpl implements PersonneMapper {
+//CHECKSTYLE.OFF: MagicNumber
+
+ private final DbMapperManagerImpl mapperManager;
+
+ public PersonneMapperImpl(final DbMapperManagerImpl mm) {
+ this.mapperManager = mm;
+ }
+
+ @Override
+ public Personne create(final Personne entite) throws PersistenceException {
+ if (entite == null) {
+ return null;
+ }
+ Personne nouvelleEntite = null;
+
+ String uuid = entite.getUUID();
+ if (uuid == null) {
+ uuid = UUID.randomUUID().toString();
+ }
+
+ try (Connection conn
+ = this.mapperManager.getConnection()) {
+ conn.setAutoCommit(false);
+
+ try (PreparedStatement ps
+ = conn.prepareStatement(SQL.PERSONNES.INSERT)) {
+ ps.setString(1,
+ uuid);
+ if (entite.getNom() != null) {
+ ps.setString(2,
+ entite.getNom());
+ } else {
+ ps.setNull(2,
+ Types.VARCHAR);
+ }
+
+ if (entite.getPrenom() != null) {
+ ps.setString(3,
+ entite.getPrenom());
+ } else {
+ ps.setNull(3,
+ Types.VARCHAR);
+ }
+
+ ps.executeUpdate();
+ }
+
+ nouvelleEntite = retrieveEntite(conn, uuid);
+
+ conn.commit();
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+
+ return nouvelleEntite;
+ }
+
+ @Override
+ public Personne retrieveByUUID(final String uuid) throws PersistenceException {
+ if (uuid == null) {
+ return null;
+ }
+ Personne entite = null;
+
+ try (Connection conn
+ = this.mapperManager.getConnection()) {
+ conn.setAutoCommit(false);
+
+ entite = retrieveEntite(conn, uuid);
+
+ conn.commit();
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+ return entite;
+ }
+
+ private Personne retrieveEntite(final Connection conn, final String uuid) throws SQLException {
+ Personne entite = null;
+ try (PreparedStatement ps
+ = conn.prepareStatement(SQL.PERSONNES.SELECT_BY_UUID)) {
+ ps.setString(1, uuid);
+ ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ entite = readEntite(rs);
+ }
+
+ }
+ return entite;
+ }
+
+ @Override
+ public List retrieve(final String filtre) throws PersistenceException {
+ List entites = new ArrayList<>();
+
+ if (filtre == null) {
+ return entites;
+ }
+ try (Connection conn
+ = this.mapperManager.getConnection()) {
+
+ conn.setAutoCommit(false);
+
+ try (PreparedStatement ps
+ = conn.prepareStatement(SQL.PERSONNES.SELECT_BY_FILTRE)) {
+ ps.setString(1,
+ filtre);
+ ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ Personne entite = readEntite(rs);
+ if (entite != null) {
+ entites.add(entite);
+ }
+ }
+
+ }
+
+ conn.commit();
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+ return entites;
+
+ }
+
+ @Override
+ public void update(final Personne entite) throws PersistenceException {
+ if (entite == null) {
+ return;
+ }
+ if (entite.getUUID() == null) {
+ return;
+ }
+ try (Connection conn
+ = this.mapperManager.getConnection()) {
+
+ conn.setAutoCommit(false);
+
+ Personne entiteTrouvee
+ = this.retrieveEntite(conn,
+ entite.getUUID());
+ if (entiteTrouvee == null) {
+ throw new EntiteInconnuePersistenceException(
+ String.format("Erreur: l'entitée (%s) "
+ + "n'est pas connue!",
+ entite.toString()));
+ }
+
+ try (PreparedStatement ps
+ = conn.prepareStatement(
+ SQL.PERSONNES.UPDATE)) {
+ if (entite.getNom() != null) {
+ ps.setString(1,
+ entite.getNom());
+ } else {
+ ps.setNull(1,
+ Types.VARCHAR);
+ }
+
+ if (entite.getPrenom() != null) {
+ ps.setString(2,
+ entite.getPrenom());
+ } else {
+ ps.setNull(2,
+ Types.VARCHAR);
+ }
+
+ ps.setString(3,
+ entite.getUUID());
+
+ ps.executeUpdate();
+ }
+
+ conn.commit();
+
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+
+ }
+
+ @Override
+ public void delete(final Personne entite) throws PersistenceException {
+ if (entite == null) {
+ return;
+ }
+ if (entite.getUUID() == null) {
+ return;
+ }
+ try (Connection conn = this.mapperManager.getConnection()) {
+ conn.setAutoCommit(false);
+ Personne entiteTrouvee
+ = this.retrieveEntite(conn, entite.getUUID());
+ if (entiteTrouvee == null) {
+ throw new EntiteInconnuePersistenceException(
+ String.format("Erreur: l'entitée (%s) "
+ + "n'est pas connue!", entite.toString()));
+ }
+ try (PreparedStatement ps = conn.prepareStatement(SQL.PERSONNES.DELETE_BY_UUID)) {
+ ps.setString(1, entite.getUUID());
+ ps.executeUpdate();
+ }
+ conn.commit();
+ } catch (SQLException ex) {
+ throw new PersistenceException(ex);
+ }
+
+ }
+
+ protected Personne readEntite(final ResultSet rs)
+ throws SQLException {
+ String uuid = rs.getString(SQL.PERSONNES.ATTRIBUTS.UUID);
+ String nom = rs.getString(SQL.PERSONNES.ATTRIBUTS.NOM);
+ String prenom = rs.getString(SQL.PERSONNES.ATTRIBUTS.PRENOM);
+
+ Personne entite = PersonneBase.builder()
+ .uuid(uuid)
+ .nom(nom)
+ .prenom(prenom)
+ .build();
+
+ return entite;
+ }
+//CHECKSTYLE.ON: MagicNumber
+
+}
diff --git a/src/main/java/personne/datasource/db/SQL.java b/src/main/java/personne/datasource/db/SQL.java
new file mode 100644
index 0000000000000000000000000000000000000000..9ab3debff6abe840cfca0bdd93498784233f04aa
--- /dev/null
+++ b/src/main/java/personne/datasource/db/SQL.java
@@ -0,0 +1,66 @@
+package personne.datasource.db;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public final class SQL {
+
+ public static final class PERSONNES {
+
+ public static final String CREATE_TABLE
+ = "CREATE TABLE IF NOT EXISTS personnes (\n"
+ + " uuid VARCHAR, -- aid\n"
+ + " nom TEXT NOT NULL,\n"
+ + " prenom TEXT NOT NULL,\n"
+ + "\n"
+ + " CONSTRAINT pk_personnes\n"
+ + " PRIMARY KEY (uuid)\n"
+ + ")";
+
+ public static final String DROP_TABLE
+ = "DROP TABLE IF EXISTS personnes CASCADE";
+
+ public static final String INSERT
+ = "INSERT INTO personnes (uuid, nom, prenom)\n"
+ + "VALUES (?,?,?)";
+
+ public static final String SELECTION
+ = "SELECT p.uuid,\n"
+ + " p.nom,\n"
+ + " p.prenom\n";
+
+ public static final String SELECT_BY_UUID
+ = SELECTION
+ + "FROM personnes p\n"
+ + "WHERE p.uuid = ?\n";
+
+ public static final String SELECT_BY_FILTRE
+ = SELECTION
+ + "FROM personnes p\n"
+ + "WHERE p.nom || ' ' || p. prenom ~* ?\n"
+ + "ORDER BY p.nom";
+
+ public static final String UPDATE
+ = "UPDATE personnes \n"
+ + "SET nom = ?,\n"
+ + " prenom = ?\n"
+ + "WHERE uuid = ?\n";
+
+ public static final String DELETE
+ = "DELETE FROM personnes\n";
+
+ public static final String DELETE_BY_UUID
+ = DELETE
+ + "WHERE uuid = ?\n";
+
+ public static final class ATTRIBUTS {
+
+ public static final String UUID = "uuid";
+ public static final String NOM = "nom";
+ public static final String PRENOM = "prenom";
+
+ }
+ }
+
+}
diff --git a/src/main/java/personne/domain/DemoData.java b/src/main/java/personne/domain/DemoData.java
index 8ee0517476f46b47abff6e0e90f49f241e6d234c..e2eb7ce7938ec1b505af5c40cd27d916993f583d 100644
--- a/src/main/java/personne/domain/DemoData.java
+++ b/src/main/java/personne/domain/DemoData.java
@@ -23,12 +23,12 @@ public class DemoData {
private void initPersonnes() {
- String uuid = "DEMO0000-0000-0000-0002-000000000001";
+ String uuid = PERNONNE_DEFRERE_UUID;
Personne p = PersonneBase.builder()
.uuid(uuid)
- .nom("DEFRERE")
- .prenom("Marc")
+ .nom(PERNONNE_DEFRERE_NOM)
+ .prenom(PERSONNE_DEFRERE_PRENOM)
.build();
this.personnes.put(uuid, p);
@@ -182,6 +182,9 @@ public class DemoData {
this.personnes.put(uuid, p);
}
+ public static final String PERSONNE_DEFRERE_PRENOM = "Marc";
+ public static final String PERNONNE_DEFRERE_NOM = "DEFRERE";
+ public static final String PERNONNE_DEFRERE_UUID = "DEMO0000-0000-0000-0002-000000000001";
public Map getPersonnes() {
return personnes;
diff --git a/src/test/java/personne/datasource/db/DatabaseSetupImplTest.java b/src/test/java/personne/datasource/db/DatabaseSetupImplTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..eb95ea2b9d3f5dc2a1ba3e848c2537de6913c590
--- /dev/null
+++ b/src/test/java/personne/datasource/db/DatabaseSetupImplTest.java
@@ -0,0 +1,55 @@
+package personne.datasource.db;
+
+import javax.sql.DataSource;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import personne.datasource.DatabaseSetup;
+import personne.datasource.MapperManager;
+import personne.domain.DemoData;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class DatabaseSetupImplTest {
+
+ private final MapperManager mapperManager;
+ private final DemoData demoData;
+
+ public DatabaseSetupImplTest() {
+ this.demoData = new DemoData();
+ this.demoData.initialisation();
+
+ DataSource datasource = TestDataSourceFactory.getInstance();
+ this.mapperManager = new DbMapperManagerImpl(datasource);
+
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testDropCreateTables() throws Exception {
+ DatabaseSetup setup = mapperManager.getDatabaseSetup();
+ setup.dropTables();
+ setup.createTables();
+ }
+
+ /**
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testInsertData() throws Exception {
+ DatabaseSetup setup = mapperManager.getDatabaseSetup();
+ setup.dropTables();
+ setup.createTables();
+ setup.insertData();
+ }
+}
diff --git a/src/test/java/personne/datasource/db/PersonneMapperImplTest.java b/src/test/java/personne/datasource/db/PersonneMapperImplTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..10d7f2d1c600f24de9d125ab896d913bdf3b7d52
--- /dev/null
+++ b/src/test/java/personne/datasource/db/PersonneMapperImplTest.java
@@ -0,0 +1,168 @@
+package personne.datasource.db;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.UUID;
+import java.util.regex.Pattern;
+import javax.sql.DataSource;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import personne.datasource.DatabaseSetup;
+import personne.datasource.EntiteInconnuePersistenceException;
+import personne.datasource.MapperManager;
+import personne.datasource.PersistenceException;
+import personne.domain.DemoData;
+import personne.domain.Personne;
+import personne.domain.PersonneBase;
+
+/**
+ *
+ * @author dominique huguenin (dominique.huguenin AT rpn.ch)
+ */
+public class PersonneMapperImplTest {
+
+ protected MapperManager mm;
+ protected DemoData demoData;
+ protected String uuidDrefrere;
+ protected Personne personneDefrere;
+ protected Personne nouvellePersonneRef;
+
+ public PersonneMapperImplTest() throws PersistenceException {
+ demoData = new DemoData();
+ demoData.initialisation();
+
+ DataSource datasource = TestDataSourceFactory.getInstance();
+ mm = new DbMapperManagerImpl(datasource);
+
+ DatabaseSetup setup = mm.getDatabaseSetup();
+ setup.dropTables();
+ setup.createTables();
+ setup.insertData();
+
+ }
+
+ @Before
+ public void setUp() {
+ uuidDrefrere = DemoData.PERNONNE_DEFRERE_UUID;
+ personneDefrere = PersonneBase.builder()
+ .uuid(uuidDrefrere)
+ .nom(DemoData.PERNONNE_DEFRERE_NOM)
+ .prenom(DemoData.PERSONNE_DEFRERE_PRENOM)
+ .build();
+ nouvellePersonneRef = PersonneBase.builder()
+ .nom("nouveau nom " + Instant.now().toString())
+ .prenom("nouveau prenom " + Instant.now().toString())
+ .build();
+ }
+
+ @Test
+ public void testRetrieve_Identifiant() throws Exception {
+ Personne entite = mm.getPersonneMapper().retrieveByUUID(uuidDrefrere);
+ Assert.assertNotNull(entite);
+ Assert.assertEquals(personneDefrere, entite);
+ Assert.assertEquals(personneDefrere.getNom(), entite.getNom());
+ Assert.assertEquals(personneDefrere.getPrenom(), entite.getPrenom());
+ }
+
+ @Test
+ public void testRetrieve_IdentifiantNull() throws Exception {
+ String uuid = null;
+ Personne entite = mm.getPersonneMapper().retrieveByUUID(uuid);
+ Assert.assertNull(entite);
+ }
+
+ @Test
+ public void testRetrieve_String() throws Exception {
+ String regex = "^.*(DEF).*$";
+ Pattern pattern = Pattern.compile(regex);
+ List entites1 = mm.getPersonneMapper().retrieve(regex);
+ Assert.assertEquals(5, entites1.size());
+ for (Personne i : entites1) {
+ Assert.assertTrue(
+ pattern.matcher(
+ String.format("%s %s", i.getNom(), i.getPrenom())
+ ).find());
+ }
+ }
+
+ @Test
+ public void testRetrieve_StringNull() throws Exception {
+ String regex = null;
+ List entites1 = mm.getPersonneMapper().retrieve(regex);
+ Assert.assertEquals(0, entites1.size());
+ }
+
+
+ @Test
+ public void testCreate() throws Exception {
+ Personne nouvelleEntite = mm.getPersonneMapper().create(nouvellePersonneRef);
+ Assert.assertNotNull(nouvelleEntite.getUUID());
+ Assert.assertEquals(nouvellePersonneRef.getNom(), nouvelleEntite.getNom());
+ Assert.assertEquals(nouvellePersonneRef.getPrenom(), nouvelleEntite.getPrenom());
+ Personne entite = mm.getPersonneMapper().retrieveByUUID(nouvelleEntite.getUUID());
+ Assert.assertNotNull(entite);
+ Assert.assertEquals(nouvelleEntite, entite);
+ Assert.assertNotSame(nouvelleEntite, entite);
+ Assert.assertEquals(nouvelleEntite.getNom(), entite.getNom());
+ Assert.assertEquals(nouvelleEntite.getNom(), entite.getNom());
+ }
+
+
+ @Test
+ public void testUpdate() throws Exception {
+ Personne nouvelleEntite = mm.getPersonneMapper().create(nouvellePersonneRef);
+ Personne entite = mm.getPersonneMapper().retrieveByUUID(nouvelleEntite.getUUID());
+ Assert.assertNotNull(nouvelleEntite.getUUID());
+ Assert.assertEquals(nouvellePersonneRef.getNom(), nouvelleEntite.getNom());
+ Assert.assertEquals(nouvellePersonneRef.getPrenom(), nouvelleEntite.getPrenom());
+ Personne entiteMod = PersonneBase.builder()
+ .personne(entite)
+ .build();
+ entiteMod.setNom(entite.getNom() + " update");
+ entiteMod.setPrenom(entite.getPrenom() + " update");
+ mm.getPersonneMapper().update(entiteMod);
+
+ Personne entiteModifie = mm.getPersonneMapper().retrieveByUUID(nouvelleEntite.getUUID());
+ Assert.assertEquals(entiteMod, entiteModifie);
+ Assert.assertEquals(entiteMod.getNom(), entiteModifie.getNom());
+ Assert.assertEquals(entiteMod.getPrenom(), entiteModifie.getPrenom());
+ }
+
+ /**
+ *
+ * @throws Exception
+ */
+ @Test(expected = EntiteInconnuePersistenceException.class)
+ public void testUpdateEntiteInconnu() throws Exception {
+ Personne entiteMod = PersonneBase.builder()
+ .personne(this.nouvellePersonneRef)
+ .uuid(UUID.randomUUID().toString())
+ .build();
+ mm.getPersonneMapper().update(entiteMod);
+ }
+
+
+ @Test
+ public void testDelete() throws Exception {
+ Personne nouvelleEntite = mm.getPersonneMapper().create(nouvellePersonneRef);
+ Personne entite = mm.getPersonneMapper()
+ .retrieveByUUID(nouvelleEntite.getUUID());
+ Assert.assertNotNull(entite);
+ mm.getPersonneMapper().delete(entite);
+ Personne entiteNull = mm.getPersonneMapper()
+ .retrieveByUUID(nouvelleEntite.getUUID());
+ Assert.assertNull(entiteNull);
+ }
+
+
+ @Test(expected = EntiteInconnuePersistenceException.class)
+ public void testDeleteEntiteInconnu() throws Exception {
+ Personne entite = PersonneBase.builder()
+ .personne(this.nouvellePersonneRef)
+ .uuid(UUID.randomUUID().toString())
+ .build();
+ mm.getPersonneMapper().delete(entite);
+ }
+
+}
diff --git a/src/test/java/personne/datasource/db/TestDataSourceFactory.java b/src/test/java/personne/datasource/db/TestDataSourceFactory.java
new file mode 100644
index 0000000000000000000000000000000000000000..b4f9f5003c5721593d95b04fa36d4e244f6b0c78
--- /dev/null
+++ b/src/test/java/personne/datasource/db/TestDataSourceFactory.java
@@ -0,0 +1,22 @@
+package personne.datasource.db;
+
+import javax.sql.DataSource;
+
+/**
+ *
+ * @author dominique huguenin
+ */
+public class TestDataSourceFactory {
+
+ /**
+ *
+ * @return
+ */
+ public static DataSource getInstance() {
+ return DataSourceFactory.getPostgreSQLDataSource(
+ "postgres", 5432,
+ "personneTestDB",
+ "personne", "personnepass");
+ }
+
+}