Commit 78b011fe authored by Matthieu Bonnouvrier's avatar Matthieu Bonnouvrier
Browse files

Refactoring to list of string

parent 4aae0e25
Pipeline #263900 failed with stages
in 8 minutes and 8 seconds
package net.ihe.gazelle.app.patientregistryservice.application;
import net.ihe.gazelle.app.patientregistryapi.application.DomainService;
import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceSearch;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.application.UnrecognizedDomainException;
......@@ -10,20 +11,24 @@ import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.conve
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.converter.PatientConverter;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.references.CrossReferenceDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.DomainDAO;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.PatientCrossReferenceDAO;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.NoResultException;
import java.util.List;
import java.util.stream.Collectors;
public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSearch {
private PatientCrossReferenceDAO patientCrossReferenceDAO;
private DomainService domainService;
@Inject
public PatientCrossReferenceSearchImpl(
@Named("PatientCrossReferenceDAO") PatientCrossReferenceDAO patientCrossReferenceDAO) {
@Named("PatientCrossReferenceDAO") PatientCrossReferenceDAO patientCrossReferenceDAO, DomainService domainService) {
this.patientCrossReferenceDAO = patientCrossReferenceDAO;
this.domainService = domainService;
}
/**
......@@ -32,15 +37,17 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
* Verify that the source identifier is valid
* Call the DAO to get a list of patient alias
* Merge those patient aliases into one
* Any patient from that patient alias that do not match the list of target domain will be deleted
*
* @param sourceIdentifier the search key
* @return
* @throws SearchCrossReferenceException
*/
@Override
public PatientAliases search(EntityIdentifier sourceIdentifier,List<String>targetDomains) throws SearchCrossReferenceException {
public PatientAliases search(EntityIdentifier sourceIdentifier, List<String> targetDomains) throws SearchCrossReferenceException {
checkSourceIdentifier(sourceIdentifier);
boolean targetDomainUsable = isTargetDomainsUsable(targetDomains);
PatientDB patientDB = null;
PatientAliases patientAlias = null;
try {
......@@ -59,24 +66,32 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
if (searchCrossReferenceException.getMessage().equals("Search error : No X-ref for given Patient")) {
return convertPatientDBToPatientAliases(patientDB);
}
// }catch (UnrecognizedDomainException exception) {
// throw new SearchCrossReferenceException("An Error occurred during CrossReference Search",exception);
}catch(Exception exception) {
throw new SearchCrossReferenceException("An Error occurred during CrossReference Search", exception);
if(searchCrossReferenceException.getMessage().equals("The current target domain could not be found")){
throw new SearchCrossReferenceException("An error in the target domain list : one does not exist");
}
} catch (Exception exception) {
throw new SearchCrossReferenceException("An error occurred during CrossReference Search", exception);
}
if(targetDomainUsable) {
return removePatientFromPatientAliasWithNonMatchingTargetDomain(patientAlias, targetDomains);
}else{
return patientAlias;
}
return patientAlias;
}
/**
* Delete every patient (of a patient aliases) where its uuid does not match the system identifier of any currently in the list
*
* @param patientAlias
* @param entityIdentifiers
* @param targetDomains
* @return
*/
private PatientAliases removePatientWithNonMatchingTargetDomain(PatientAliases patientAlias, List<EntityIdentifier> entityIdentifiers) {
private PatientAliases removePatientFromPatientAliasWithNonMatchingTargetDomain(PatientAliases patientAlias, List<String> targetDomains) {
List<Patient> patients = patientAlias.getMembers().stream()
.filter(patient -> entityIdentifiers.stream().anyMatch(
targetDomain -> targetDomain.getSystemIdentifier().equals(patient.getUuid())))
.filter(patient -> targetDomains.stream().anyMatch(
targetDomain -> targetDomain.equals(patient.getUuid())))
.collect(Collectors.toList());
patientAlias.setMembers(patients);
return patientAlias;
......@@ -84,17 +99,20 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
/**
* Check if the list of target domain contains the current source identifier
*
* @param sourceIdentifier
* @param entityIdentifiers
* Return true if the list is not empty, false otherwise
* @param targetDomains
* @throws UnrecognizedDomainException
*/
private void checkTargetDomains(EntityIdentifier sourceIdentifier, List<EntityIdentifier> entityIdentifiers) throws UnrecognizedDomainException {
EntityIdentifier entityIdentifier = entityIdentifiers.stream().filter(
internalEntityIdentifier -> sourceIdentifier.getSystemIdentifier()
.equals(internalEntityIdentifier.getSystemIdentifier())).findAny().orElse(null);
if (entityIdentifier == null) {
throw new UnrecognizedDomainException("The current target domain could not be found");
private boolean isTargetDomainsUsable(List<String> targetDomains) throws SearchCrossReferenceException {
if (!targetDomains.isEmpty()) {
try {
targetDomains.forEach(targetDomain -> domainService.exist(targetDomain));
} catch (NoResultException e) {
throw new SearchCrossReferenceException("The current target domain could not be found");
}
return true;
}else{
return false;
}
}
......
package net.ihe.gazelle.app.patientregistryservice.application;
import net.ihe.gazelle.app.patientregistryapi.application.DomainService;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.HierarchicDesignatorDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.DomainDAO;
public class DomainServiceMock implements DomainDAO, DomainService {
@Override
public HierarchicDesignatorDB retrieve(String domainIdentifier) {
return null;
}
public class DomainServiceMock implements DomainService {
@Override
public boolean exist(String domainIdentifier) {
return domainIdentifier.equals("3b19b902-cfe8-11ea-87d0-0242ac130003");
if(domainIdentifier == null){
return false;
}
if(domainIdentifier.equals("valid")){
return true;
}
return false;
}
@Override
public void createDomain(String domainIdentifier, String domainName) {
//Not used here
}
}
......@@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.fail;
public class PatientCrossReferenceSearchImplTest {
private PatientCrossReferenceSearch patientCrossReferenceSearch = new PatientCrossReferenceSearchImpl(new PatientCrossReferenceDAOMock());
private PatientCrossReferenceSearch patientCrossReferenceSearch = new PatientCrossReferenceSearchImpl(new PatientCrossReferenceDAOMock(), new DomainServiceMock());
/**
* Test to search with a null parameter
......@@ -94,7 +94,7 @@ public class PatientCrossReferenceSearchImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceSearch.search(sourceIdentifier,targetDomains));
assertEquals("An Error occurred during CrossReference Search", exception.getMessage());
assertEquals("An error occurred during CrossReference Search", exception.getMessage());
}
@Test
......@@ -168,7 +168,7 @@ public class PatientCrossReferenceSearchImplTest {
}
@Test
public void searchWithZeroTargetDomainMatching() throws SearchCrossReferenceException, UnrecognizedDomainException {
public void searchWithZeroTargetDomainMatching() throws SearchCrossReferenceException {
EntityIdentifier sourceIdentifier1 = new EntityIdentifier();
sourceIdentifier1.setSystemIdentifier("toto");
sourceIdentifier1.setValue("searchTargetDomainFound");
......@@ -181,17 +181,16 @@ public class PatientCrossReferenceSearchImplTest {
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(sourceIdentifier1);
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
List<String> targetDomains = new ArrayList<>();
targetDomains.add("2222");
targetDomains.add("3333");
PatientAliases patientAliases = patientCrossReferenceSearch.search(sourceIdentifier1, targetDomains);
assert (patientAliases.getMembers().size() == 0);
}
@Test
public void searchWithOneTargetDomainMatching() throws SearchCrossReferenceException, UnrecognizedDomainException {
public void searchWithOneTargetDomainMatching() throws SearchCrossReferenceException {
EntityIdentifier sourceIdentifier1 = new EntityIdentifier();
sourceIdentifier1.setSystemIdentifier("Retrieved11");
sourceIdentifier1.setValue("searchTargetDomainFound");
......@@ -204,11 +203,11 @@ public class PatientCrossReferenceSearchImplTest {
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(sourceIdentifier1);
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
List<String> targetDomains = new ArrayList<>();
targetDomains.add("Retrieved11");
targetDomains.add("2222");
targetDomains.add("3333");
PatientAliases patientAliases = patientCrossReferenceSearch.search(sourceIdentifier1, targetDomains);
assert (patientAliases.getMembers().size() == 1);
}
......@@ -227,13 +226,12 @@ public class PatientCrossReferenceSearchImplTest {
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
List<String> targetDomains = new ArrayList<>();
UnrecognizedDomainException exception = assertThrows(UnrecognizedDomainException.class,
targetDomains.add("2222");
targetDomains.add("3333");
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceSearch.search(entityIdentifier, targetDomains));
assertEquals("The current target domain could not be found", exception.getMessage());
assertEquals("An error in the target domain list : one does not exist", exception.getMessage());
}
}
\ 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