Commit 7bcd8ffe authored by Franck Desaize's avatar Franck Desaize
Browse files

Start TUs DAO

parent 4161eaf2
Pipeline #262346 failed with stages
in 4 minutes and 31 seconds
......@@ -14,7 +14,7 @@ import java.util.List;
@Table(name = "pam_patient_identifier", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = {
"full_patient_id", "identifier_type_code" }))
@SequenceGenerator(name = "pam_patient_identifier_sequence", sequenceName = "pam_patient_identifier_id_seq", allocationSize = 1)
@NamedQuery(name = "PatientIdentifierDB.findByHierarchicDomain", query = "SELECT pi FROM PatientIdentifierDB pi WHERE pi.domain = :domain_id")
@NamedQuery(name = "PatientIdentifierDB.findByHierarchicDomainAndId", query = "SELECT pi FROM PatientIdentifierDB pi WHERE pi.domain = :domain_id and pi.identifier = :id_number" )
public class PatientIdentifierDB {
@Id
......
......@@ -60,10 +60,10 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
*/
@Override
public List<PatientAliases> searchForPatientAliasesInSourceIdentifier(EntityIdentifier sourceIdentifier) throws SearchCrossReferenceException {
String systemIndentifier = sourceIdentifier.getSystemIdentifier();
String systemIdentifier = sourceIdentifier.getSystemIdentifier();
String value = sourceIdentifier.getValue();
HierarchicDesignatorDB hierarchicDesignatorDB = searchForHierarchicDesignator(systemIndentifier);
HierarchicDesignatorDB hierarchicDesignatorDB = searchForHierarchicDesignator(systemIdentifier);
PatientIdentifierDB patientIdentifier = searchForPatientIdentifier(hierarchicDesignatorDB, value);
......@@ -78,40 +78,40 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
return PatientAliasesConverter.toListOfPatientAliases(crossReferences);
}
public HierarchicDesignatorDB searchForHierarchicDesignator(String systemIndentifier) {
private HierarchicDesignatorDB searchForHierarchicDesignator(String systemIdentifier) {
TypedQuery<HierarchicDesignatorDB> queryForAPatient = entityManager.createNamedQuery("HierarchicDesignatorDB.findByUniversalID", HierarchicDesignatorDB.class);
queryForAPatient.setParameter("universalID", systemIndentifier);
queryForAPatient.setParameter("universalID", systemIdentifier);
return queryForAPatient.getSingleResult();
}
public PatientIdentifierDB searchForPatientIdentifier(HierarchicDesignatorDB hierarchicDesignatorDB, String uuid) {
TypedQuery<PatientIdentifierDB> queryForAPatient = entityManager.createNamedQuery("PatientIdentifierDB.findByHierarchicDomain", PatientIdentifierDB.class);
// A garder pour le moment queryForAPatient.setParameter("id", uuid);
private PatientIdentifierDB searchForPatientIdentifier(HierarchicDesignatorDB hierarchicDesignatorDB, String idNumber) {
TypedQuery<PatientIdentifierDB> queryForAPatient = entityManager.createNamedQuery("PatientIdentifierDB.findByHierarchicDomainAndId", PatientIdentifierDB.class);
queryForAPatient.setParameter("domain_id", hierarchicDesignatorDB);
queryForAPatient.setParameter("id_number",idNumber);
return queryForAPatient.getSingleResult();
}
public PatientDB searchforPatient(PatientIdentifierDB patientIdentifier) {
private PatientDB searchforPatient(PatientIdentifierDB patientIdentifier) {
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByPatientIdentifier", PatientDB.class);
queryForAPatient.setParameter("patientIdentifier", patientIdentifier);
return queryForAPatient.getSingleResult();
}
public boolean isExistingPatientAliases(PatientDB patient) {
private boolean isExistingPatientAliases(PatientDB patient) {
TypedQuery<Long> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.countPatient", Long.class);
queryForPatientAliases.setParameter("patient", patient);
Long count = queryForPatientAliases.getSingleResult();
return count > 0;
}
public List<CrossReferenceDB> searchForPatientAliases(PatientDB patient) {
private List<CrossReferenceDB> searchForPatientAliases(PatientDB patient) {
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByPatient", CrossReferenceDB.class);
queryForPatientAliases.setParameter("patient", patient);
return queryForPatientAliases.getResultList();
}
public List<CrossReferenceDB> deleteKnownPatient(PatientDB patientDB, List<CrossReferenceDB> crossReferences) {
private List<CrossReferenceDB> deleteKnownPatient(PatientDB patientDB, List<CrossReferenceDB> crossReferences) {
List<CrossReferenceDB> crossReferencesCopy = new ArrayList<>(crossReferences);
for (CrossReferenceDB crossReference : crossReferencesCopy) {
if (crossReference.getPatients().size() > 1) {
......@@ -121,7 +121,7 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
return crossReferencesCopy;
}
public void createPatientAliases(PatientDB patientDB) {
private void createPatientAliases(PatientDB patientDB) {
CrossReferenceDB crossReference = new CrossReferenceDB(patientDB.getFirstName());
List<PatientDB> patients = new ArrayList<>();
patients.add(patientDB);
......
package net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.patient;
import net.ihe.gazelle.app.patientregistryapi.business.*;
import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceSearch;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.GenderCode;
import net.ihe.gazelle.app.patientregistryapi.business.Patient;
import net.ihe.gazelle.app.patientregistryapi.business.PersonName;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.converter.PatientConverter;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.GenderCodeDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.HierarchicDesignatorDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientIdentifierDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.references.CrossReferenceDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.PatientCrossReferenceDAO;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.PatientCrossReferenceDAOImpl;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
......@@ -22,13 +27,14 @@ import javax.persistence.TypedQuery;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RunWith(PowerMockRunner.class)
//@PrepareForTest({PatientCrossReferenceDAOImpl.class})
import static org.junit.jupiter.api.Assertions.assertThrows;
public class PatientCrossReferenceDAOImplTest {
private static final String PERSISTENCE_UNIT_NAME_CONST = "PersistenceUnitTest";
private PatientCrossReferenceDAO patientCrossReferenceDAO;
private EntityManager entityManager;
......@@ -36,25 +42,20 @@ public class PatientCrossReferenceDAOImplTest {
* database init
*/
@BeforeEach
public void initializeDatabase() throws ParseException {
public void initializeDatabase() {
entityManager.getTransaction().begin();
entityManager.getTransaction().commit();
}
@BeforeAll
public void startup()throws ParseException {
entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME_CONST).createEntityManager();
// PowerMockito.mock(PatientCrossReferenceDAOImpl.class);
patientCrossReferenceDAO = new PatientCrossReferenceDAOImpl(entityManager);
System.out.println("initializeEntityManager");
addTestEntitiesToDB(entityManager);
System.out.println("addTestEntitiesToDB");
entityManager.getTransaction().begin();
entityManager.getTransaction().commit();
}
// @BeforeEach
// public void initializeEntityManager() {
// System.out.println("initializeEntityManager");
// addTestEntitiesToDB(entityManager);
// System.out.println("addTestEntitiesToDB");
// entityManager.getTransaction().begin();
// }
/**
* Close the transaction corresponding to the test case when it is over.
*/
......@@ -70,6 +71,8 @@ public class PatientCrossReferenceDAOImplTest {
public void closeDatabase() {
entityManager.close();
}
/**
* search for a list of PatientAliases with an existing source identifier
*/
......@@ -94,10 +97,18 @@ public class PatientCrossReferenceDAOImplTest {
// sourceIdentifier.setValue("value");
//
// PatientCrossReferenceDAO patientCrossReference = new PatientCrossReferenceDAOImpl(entityManager);
// SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class, () -> patientCrossReference.searchForPatientAliasesInSourceIdentifier(sourceIdentifier));
// SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class, () -> patientCrossReference
// .searchForPatientAliasesInSourceIdentifier(sourceIdentifier));
//
// assertEquals("The source identifier is unknown", exception.getMessage());
// }
@Test
void testNoHierarchicDesignatorDB(){
EntityIdentifier entityIdentifier = new EntityIdentifier();
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class, () -> patientCrossReferenceDAO.searchForPatientAliasesInSourceIdentifier(entityIdentifier));
}
@Test
public void testEntityManager() {
// for (int i = 1; i < 20; i++) {
......@@ -110,15 +121,16 @@ public class PatientCrossReferenceDAOImplTest {
TypedQuery<CrossReferenceDB> queryForAPatient = entityManager.createNamedQuery("CrossReferenceDB.findById", CrossReferenceDB.class);
queryForAPatient.setParameter("id", 1);
Query query = entityManager.createQuery("select pi from " + PatientIdentifierDB.class.getSimpleName()+ " pi");
Query query = entityManager.createQuery("select pi from " + PatientIdentifierDB.class.getSimpleName() + " pi");
List<PatientIdentifierDB> patientIdentifierDBS = new ArrayList<PatientIdentifierDB>(query.getResultList());
List testPIDB = query.getResultList();
Query queryP = entityManager.createQuery("select p from " + PatientDB.class.getSimpleName() + " p");
List<PatientDB> patientDBS = queryP.getResultList();
CrossReferenceDB cross = queryForAPatient.getSingleResult();
System.out.println("toto");
}
private void addPatientIdentifierDBToDB(EntityManager entityManager){
private void addPatientIdentifierDBToDB(EntityManager entityManager) {
PatientIdentifierDB patientIdentifierDB = new PatientIdentifierDB();
patientIdentifierDB.setIdentifier("identifierTest");
patientIdentifierDB.setFullPatientId("patientID");
......@@ -127,12 +139,13 @@ public class PatientCrossReferenceDAOImplTest {
}
private void addTestEntitiesToDB(EntityManager entityManager) throws ParseException {
List<Patient> patientObjects = generatePatient();
List<PatientDB> patientDBObjects = convertPatientToPatientDB(patientObjects);
for (PatientDB patientDB : patientDBObjects) {
entityManager.merge(patientDB);
}
entityManager.persist(create());
initilizeDb(entityManager);
// List<Patient> patientObjects = generatePatient();
// List<PatientDB> patientDBObjects = convertPatientToPatientDB(patientObjects);
// for (PatientDB patientDB : patientDBObjects) {
// entityManager.merge(patientDB);
// }
// entityManager.persist(create());
// entityManager.flush();
}
// private void addTestPersonNameToDB(EntityManager entityManager) throws ParseException {
......@@ -144,7 +157,7 @@ public class PatientCrossReferenceDAOImplTest {
// entityManager.merge(create());
// }
private List<PersonName> generatePersonName(){
private List<PersonName> generatePersonName() {
PersonName personName = new PersonName();
personName.setFamily("Dupont");
personName.addGiven("Marie");
......@@ -239,6 +252,8 @@ public class PatientCrossReferenceDAOImplTest {
}
private List<PatientDB> convertPatientToPatientDB(List<Patient> patients) {
List<PatientDB> patientDBObjects = new ArrayList<>();
for (Patient patient : patients) {
PatientDB patientDB = PatientConverter.toPatientDB(patient);
......@@ -247,6 +262,64 @@ public class PatientCrossReferenceDAOImplTest {
return patientDBObjects;
}
private void initilizeDb(EntityManager entityManager) {
HierarchicDesignatorDB hierarchicDesignatorDB = new HierarchicDesignatorDB();
hierarchicDesignatorDB.setUniversalID("test");
hierarchicDesignatorDB.setUniversalIDType("ISO");
HierarchicDesignatorDB hierarchicDesignatorDB2 = entityManager.merge(hierarchicDesignatorDB);
HierarchicDesignatorDB hierarchicDesignatorDB3 = new HierarchicDesignatorDB();
hierarchicDesignatorDB.setUniversalID("test2");
hierarchicDesignatorDB.setUniversalIDType("ISO");
HierarchicDesignatorDB hierarchicDesignatorDB4 = entityManager.merge(hierarchicDesignatorDB3);
PatientIdentifierDB patientIdentifierDB = new PatientIdentifierDB();
patientIdentifierDB.setDomain(hierarchicDesignatorDB2);
patientIdentifierDB.setIdentifier("1234");
PatientIdentifierDB patientIdentifierDBmerged = entityManager.merge(patientIdentifierDB);
PatientIdentifierDB patientIdentifierDB2 = new PatientIdentifierDB();
patientIdentifierDB.setDomain(hierarchicDesignatorDB4);
patientIdentifierDB.setIdentifier("1234");
PatientIdentifierDB patientIdentifierDB2merged = entityManager.merge(patientIdentifierDB2);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
PatientDB patient1 = new PatientDB();
patient1.setUuid("testUuid1");
patient1.setStillActive(true);
patient1.setGenderCode(GenderCodeDB.F);
PersonName personName = new PersonName();
personName.setFamily("Dupont");
personName.addGiven("Marie");
patient1.setLastName("Dupont");
List<PatientIdentifierDB> patientIdentifierDBS = new ArrayList<>();
patientIdentifierDBS.add(patientIdentifierDBmerged);
patient1.setPatientIdentifiers(patientIdentifierDBS);
PatientDB patient1m = entityManager.merge(patient1);
Patient patient2 = new Patient();
patient2.setUuid("testUuid2");
patient2.setActive(false);
patient2.setGender(GenderCode.MALE);
PersonName personName2 = new PersonName();
personName2.setFamily("Martin");
personName2.addGiven("Pierre");
patient2.addName(personName2);
PatientDB patient2m = entityManager.merge(PatientConverter.toPatientDB(patient2));
List<PatientDB> patientDBS = new ArrayList<>();
patientDBS.add(patient1m);
patientDBS.add(patient2m);
CrossReferenceDB crossReferenceDB = new CrossReferenceDB();
crossReferenceDB.setLastModifier("fde");
crossReferenceDB.setPatients(patientDBS);
entityManager.persist(crossReferenceDB);
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
}
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