Commit e0494509 authored by Franck Desaize's avatar Franck Desaize
Browse files

Fixe DomainDAO and Test

parent 4b1614c4
Pipeline #273090 failed with stages
in 4 minutes and 42 seconds
......@@ -19,7 +19,7 @@ public interface DomainDAO {
* @param domainIdentifier Identifier of the domain to retrieve
* @return the domain from database that corresponds to the requested identifier
*/
HierarchicDesignatorDB retrieve(String domainIdentifier);
HierarchicDesignatorDB retrieve(String domainIdentifier) throws DomainSearchException;
/**
* Delete a domain from Database.
......
......@@ -45,9 +45,30 @@ public class DomainDAOImpl implements DomainDAO {
* {@inheritDoc}
*/
@Override
public void delete(String domainIdentifier) {
HierarchicDesignatorDB domain = retrieve(domainIdentifier);
entityManager.remove(domain);
public void createDomain(String domainIdentifier, String domainName) {
if(!exist(domainIdentifier)){
HierarchicDesignatorDB domain = new HierarchicDesignatorDB(domainName, domainIdentifier, "ISO", null);
domain.setUsage(DesignatorTypeDB.PATIENT_ID);
entityManager.persist(domain);
entityManager.flush();
log.info(String.format("Domain : %s created",domainIdentifier));
}
log.warn(String.format("Domain : %s already saved",domainIdentifier));
}
/**
* {@inheritDoc}
*/
@Override
public void delete(String domainIdentifier) {
HierarchicDesignatorDB domain = null;
try {
domain = retrieve(domainIdentifier);
entityManager.remove(domain);
log.info(String.format("Domain : %s deleted",domainIdentifier));
} catch (DomainSearchException exception) {
log.warn(String.format("Domain : %s not found or already deleted",domainIdentifier),exception);
}
}
/**
......@@ -61,9 +82,11 @@ public class DomainDAOImpl implements DomainDAO {
query.setParameter("givenUsage", DesignatorTypeDB.PATIENT_ID);
try {
query.getSingleResult();
} catch (NoResultException e) {
return false;
}
log.info(String.format("Domain : %s found",domainIdentifier));
return true;
}
......@@ -74,7 +97,7 @@ public class DomainDAOImpl implements DomainDAO {
* @return retrieved Domain if exists, or null.
*/
@Override
public HierarchicDesignatorDB retrieve(String domainIdentifier) {
public HierarchicDesignatorDB retrieve(String domainIdentifier) throws DomainSearchException {
TypedQuery<HierarchicDesignatorDB> query = entityManager.createNamedQuery("HierarchicDesignatorDB.findByUniversalID",
HierarchicDesignatorDB.class);
query.setParameter("universalID", domainIdentifier);
......@@ -82,36 +105,18 @@ public class DomainDAOImpl implements DomainDAO {
HierarchicDesignatorDB domain;
try {
domain = query.getSingleResult();
} catch (NoResultException e) {
return null;
} catch (NoResultException exception) {
throw new DomainSearchException("System does not exist",exception);
} catch (NonUniqueResultException exception) {
throw new DomainSearchException(String.format("Duplicated System for value : %s", domainIdentifier),exception);
}
log.info(String.format("Domain : %s found",domainIdentifier));
return domain;
}
/**
* {@inheritDoc}
*/
@Override
public void createDomain(String domainIdentifier, String domainName) {
HierarchicDesignatorDB domain = new HierarchicDesignatorDB(domainName, domainIdentifier, "ISO", null);
domain.setUsage(DesignatorTypeDB.PATIENT_ID);
entityManager.persist(domain);
entityManager.flush();
}
@Override
public Integer searchForHierarchicDesignator(String systemIdentifier) throws DomainSearchException {
try {
TypedQuery<HierarchicDesignatorDB> queryForAPatient = entityManager.createNamedQuery("HierarchicDesignatorDB.findByUniversalID",
HierarchicDesignatorDB.class);
queryForAPatient.setParameter("universalID", systemIdentifier);
queryForAPatient.setParameter("givenUsage", DesignatorTypeDB.PATIENT_ID);
return queryForAPatient.getSingleResult().getId();
} catch (NoResultException exception) {
throw new DomainSearchException("System does not exit");
} catch (NonUniqueResultException exception) {
throw new DomainSearchException(String.format("Duplicated System for value : %s", systemIdentifier));
}
return retrieve(systemIdentifier).getId();
}
}
package net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service;
import static org.junit.jupiter.api.Assertions.*;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.DesignatorTypeDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.HierarchicDesignatorDB;
import net.ihe.gazelle.app.patientregistryservice.application.exceptions.DomainSearchException;
import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import java.text.ParseException;
import static org.junit.jupiter.api.Assertions.assertThrows;
@Feature("DomainDAO")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DomainDAOImplTest {
private static final String PERSISTENCE_UNIT_NAME_CONST = "PersistenceUnitDomainTest";
private DomainDAO domainDAO;
private EntityManager entityManager;
/**
* database init
*/
@BeforeAll
public void initializeDatabase() throws ParseException {
entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME_CONST).createEntityManager();
domainDAO = new DomainDAOImpl(entityManager);
entityManager.getTransaction().begin();
}
/**
* Close the connection to the database after all tests are executed.
*/
@AfterAll
public void closeDatabase() {
entityManager.clear();
entityManager.close();
}
@Test
@Description("Test on constructor to generated an exception")
void constructorWithNullEntity() {
assertThrows(IllegalArgumentException.class, () -> new DomainDAOImpl(null));
}
@Test
@Description("Create a new domain")
void createNewDomain() throws DomainSearchException {
domainDAO.createDomain("domain1", "Domain1Name");
HierarchicDesignatorDB hierarchicDesignatorDB = domainDAO.retrieve("domain1");
Assert.assertNotNull("An id shall be generated", hierarchicDesignatorDB.getId());
Assert.assertEquals("the domainid shall be identical", "domain1", hierarchicDesignatorDB.getUniversalID());
domainDAO.delete("domain1");
}
@Test
@Description("Test on the function isExist()")
void isExist() throws DomainSearchException {
Assert.assertFalse(domainDAO.exist("domainExist"));
domainDAO.createDomain("domain1", "Domain1Name");
Assert.assertTrue(domainDAO.exist("domain1"));
domainDAO.delete("domain1");
}
@Test
@Description("Test on the function delete()")
void deleteDomain() throws DomainSearchException {
domainDAO.createDomain("domain1", "Domain1Name");
Assert.assertTrue(domainDAO.exist("domain1"));
domainDAO.delete("domain1");
Assert.assertFalse(domainDAO.exist("domain1"));
}
@Test
@Description("Test on the function retrieve() and return the HierarchicDesignatorDB entity from the DB")
void retrieveHierarchicDesignatorDB() throws DomainSearchException {
domainDAO.createDomain("domain1", "Domain1Name");
Assert.assertTrue(domainDAO.exist("domain1"));
HierarchicDesignatorDB hierarchicDesignatorDB = domainDAO.retrieve("domain1");
Assert.assertNotNull("An id shall be generated", hierarchicDesignatorDB.getId());
Assert.assertEquals("the domainid shall be identical", "domain1", hierarchicDesignatorDB.getUniversalID());
domainDAO.delete("domain1");
}
@Test
@Description("Test on the function retrieve() and excepted a DomainSearchException with NoResultException ")
void retrieveHierarchicDesignatorDBExceptionNoResult() {
DomainSearchException exception = assertThrows(DomainSearchException.class, () -> domainDAO.retrieve("Test1"));
Assert.assertEquals("The cause shall be", NoResultException.class, exception.getCause().getClass());
Assert.assertEquals("System does not exist", exception.getMessage());
}
@Test
@Description("Test on the function retrieve() and excepted a DomainSearchException with NonUniqueResultException ")
void retrieveHierarchicDesignatorDBExceptionNonUniqueResultException() throws DomainSearchException {
domainDAO.createDomain("Test", "Domain1Name");
HierarchicDesignatorDB domain = new HierarchicDesignatorDB("Domain1Name", "Test", "ISO", null);
domain.setUsage(DesignatorTypeDB.PATIENT_ID);
HierarchicDesignatorDB hierarchicDesignatorDB1 = entityManager.merge(domain);
DomainSearchException exception = assertThrows(DomainSearchException.class, () -> domainDAO.retrieve("Test"));
Assert.assertEquals("The cause shall be", NonUniqueResultException.class, exception.getCause().getClass());
Assert.assertEquals("Duplicated System for value : Test", exception.getMessage());
}
@Test
@Description("Test on the function retrieve() and return the id entity from the DB")
void retrieveHierarchicDesignatorDBID() throws DomainSearchException {
domainDAO.createDomain("domain1", "Domain1Name");
Assert.assertTrue(domainDAO.exist("domain1"));
Integer hierarchicDesignatorDB = domainDAO.searchForHierarchicDesignator("domain1");
Assert.assertNotNull("the domainid shall not be null", hierarchicDesignatorDB);
domainDAO.delete("domain1");
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment