diff --git a/pom.xml b/pom.xml index 41ddf45d28bb53c953c54ba19cdfc32d1e874ce7..a389b17e914814d8e742270b4505225ba717ceef 100644 --- a/pom.xml +++ b/pom.xml @@ -384,6 +384,12 @@ <artifactId>app.patient-registry-search-client</artifactId> <version>2.0.1-SNAPSHOT</version> </dependency> + + <dependency> + <groupId>net.ihe.gazelle</groupId> + <artifactId>app.patient-registry-service</artifactId> + <version>2.0.1-SNAPSHOT</version> + </dependency> <!-- Used for CORS support --> <dependency> diff --git a/src/main/java/net/ihe/gazelle/adapter/connector/BundleToPatientRegistryConverter.java b/src/main/java/net/ihe/gazelle/adapter/connector/BundleToPatientRegistryConverter.java index d2d1625abba4a73ca92094ad56b502da04939e59..e1d245ea71d7e9d1611c70549bee7ecb9ddeb622 100644 --- a/src/main/java/net/ihe/gazelle/adapter/connector/BundleToPatientRegistryConverter.java +++ b/src/main/java/net/ihe/gazelle/adapter/connector/BundleToPatientRegistryConverter.java @@ -48,26 +48,55 @@ public class BundleToPatientRegistryConverter { private static net.ihe.gazelle.app.patientregistryapi.business.Patient hl7PatientToPatientRegistry(Patient patient) throws ConversionException { net.ihe.gazelle.app.patientregistryapi.business.Patient convertedPatient = new net.ihe.gazelle.app.patientregistryapi.business.Patient(); - convertedPatient.setActive(patient.getActive()); + if (patient.hasActive()) { + convertedPatient.setActive(patient.getActive()); + } + + if (patient.hasIdentifier()) { + for (Identifier id : patient.getIdentifier()) { + addEntity(convertedPatient, id); + } + } + + if (patient.hasBirthDate()) { + convertedPatient.setDateOfBirth(patient.getBirthDate()); + } + else { + throw new ConversionException("Minimum Information not available"); + } - for (Identifier id : patient.getIdentifier()) { - addEntity(convertedPatient, id); + if (patient.hasDeceased()) { + convertedPatient.setDateOfDeath(patient.getDeceasedDateTimeType().getValue()); } - convertedPatient.setDateOfBirth(patient.getBirthDate()); - convertedPatient.setDateOfDeath(patient.getDeceasedDateTimeType().getValue()); - convertedPatient.setGender(convertGender(patient.getGender())); - convertedPatient.setMultipleBirthOrder(patient.getMultipleBirthIntegerType().getValue()); - convertedPatient.setUuid(patient.getId()); + if (patient.hasGender()) { + convertedPatient.setGender(convertGender(patient.getGender())); + } - for (Address address : patient.getAddress()) { - convertedPatient.addAddress(convertAddress(address)); + if (patient.hasMultipleBirth()) { + convertedPatient.setMultipleBirthOrder(patient.getMultipleBirthIntegerType().getValue()); } - for (ContactComponent contact : patient.getContact()) { - convertedPatient.addContact(convertContact(contact)); + + if (patient.hasId()) { + convertedPatient.setUuid(patient.getId()); } - for (HumanName name : patient.getName()) { - convertedPatient.addName(convertName(name)); + + if (patient.hasAddress()) { + for (Address address : patient.getAddress()) { + convertedPatient.addAddress(convertAddress(address)); + } + } + + if (patient.hasContact()) { + for (ContactComponent contact : patient.getContact()) { + convertedPatient.addContact(convertContact(contact)); + } + } + + if (patient.hasName()) { + for (HumanName name : patient.getName()) { + convertedPatient.addName(convertName(name)); + } } return null; @@ -75,8 +104,15 @@ public class BundleToPatientRegistryConverter { private static void addEntity(net.ihe.gazelle.app.patientregistryapi.business.Patient convertedPatient, Identifier id) { EntityIdentifier entityIdentifier = new EntityIdentifier(); - entityIdentifier.setSystemIdentifier(id.getSystem()); - entityIdentifier.setSystemName(id.getValue()); + + if (id.hasSystem()) { + entityIdentifier.setSystemIdentifier(id.getSystem()); + } + + if (id.hasValue()) { + entityIdentifier.setSystemName(id.getValue()); + } + convertedPatient.addIdentifier(entityIdentifier); } @@ -101,13 +137,31 @@ public class BundleToPatientRegistryConverter { private static net.ihe.gazelle.app.patientregistryapi.business.Address convertAddress(Address address) throws ConversionException { net.ihe.gazelle.app.patientregistryapi.business.Address convertedAddress = new net.ihe.gazelle.app.patientregistryapi.business.Address(); - convertedAddress.setCity(address.getCity()); - convertedAddress.setCountryIso3(address.getCountry()); - convertedAddress.setPostalCode(address.getPostalCode()); - convertedAddress.setState(address.getState()); - convertedAddress.setUse(convertAddressUse(address.getUse())); - for (StringType addressLine : address.getLine()) { - convertedAddress.addLine(addressLine.getValue()); + + if (address.hasCity()) { + convertedAddress.setCity(address.getCity()); + } + + if (address.hasCountry()) { + convertedAddress.setCountryIso3(address.getCountry()); + } + + if (address.hasPostalCode()) { + convertedAddress.setPostalCode(address.getPostalCode()); + } + + if (address.hasState()) { + convertedAddress.setState(address.getState()); + } + + if (address.hasUse()) { + convertedAddress.setUse(convertAddressUse(address.getUse())); + } + + if (address.hasLine()) { + for (StringType addressLine : address.getLine()) { + convertedAddress.addLine(addressLine.getValue()); + } } return convertedAddress; } @@ -137,11 +191,19 @@ public class BundleToPatientRegistryConverter { } private static Person convertContact(ContactComponent contact) throws ConversionException { - Person convertedContact = new Person(); - convertedContact.setGender(convertGender(contact.getGender())); - convertedContact.addAddress(convertAddress(contact.getAddress())); - convertedContact.addName(convertName(contact.getName())); + + if (contact.hasGender()) { + convertedContact.setGender(convertGender(contact.getGender())); + } + + if (contact.hasAddress()) { + convertedContact.addAddress(convertAddress(contact.getAddress())); + } + + if (contact.hasName()) { + convertedContact.addName(convertName(contact.getName())); + } //ntm les observations //convertedContact.addObservation(contact.getO); return convertedContact; @@ -149,17 +211,33 @@ public class BundleToPatientRegistryConverter { private static PersonName convertName(HumanName name) { PersonName convertedName = new PersonName(); - convertedName.setUse(name.getUse().toString()); - convertedName.setFamily(name.getFamily()); - for(StringType givenName : name.getGiven()) { - convertedName.addGiven(givenName.getValue()); + + if (name.hasUse()) { + convertedName.setUse(name.getUse().toString()); } - for(StringType prefixName : name.getPrefix()) { - convertedName.setPrefix(prefixName.getValue()); + + if (name.hasFamily()) { + convertedName.setFamily(name.getFamily()); } - for(StringType suffixName : name.getSuffix()) { - convertedName.setSuffix(suffixName.getValue()); + + if (name.hasGiven()) { + for(StringType givenName : name.getGiven()) { + convertedName.addGiven(givenName.getValue()); + } } + + if (name.hasPrefix()) { + for(StringType prefixName : name.getPrefix()) { + convertedName.setPrefix(prefixName.getValue()); + } + } + + if (name.hasSuffix()) { + for(StringType suffixName : name.getSuffix()) { + convertedName.setSuffix(suffixName.getValue()); + } + } + return convertedName; } } diff --git a/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java b/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java new file mode 100644 index 0000000000000000000000000000000000000000..ebf38cac95c43d9e22f48576888c5a7f4cc6bb46 --- /dev/null +++ b/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java @@ -0,0 +1,110 @@ +package net.ihe.gazelle.application; + +import java.util.UUID; + +import javax.inject.Inject; +import javax.inject.Named; + +import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import net.ihe.gazelle.app.patientregistryapi.application.PatientFeedException; +import net.ihe.gazelle.app.patientregistryservice.application.PatientFeedApplication; +import net.ihe.gazelle.app.patientregistryservice.application.dao.DomainDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.IdentifierDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.PatientCrossReferenceDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.PatientDAO; +import net.ihe.gazelle.framework.loggerservice.application.GazelleLogger; +import net.ihe.gazelle.framework.loggerservice.application.GazelleLoggerFactory; +import net.ihe.gazelle.lib.annotations.Package; + +@Named("PatientRegistryFeedClient") +public class PatientRegistryFeedClient { + + public static final String BAD_UUID = "Created Patient has an uuid that is not valid."; + public static final String NO_UUID = "No UUID was retrieved from the created Patient"; + public static final String NO_PATIENT_PARAMETER = "No Patient were given to create"; + + private static final GazelleLogger logger = GazelleLoggerFactory.getInstance().getLogger(PatientRegistrySearchClient.class); + private PatientFeedApplication client; + + public PatientRegistryFeedClient() { } + + /** + * Constructor with injection for the patient feed application + * @param patientDAO + * @param domainDAO + * @param patientCrossReferenceDAO + * @param identifierDAO + */ + @Inject + public PatientRegistryFeedClient(PatientDAO patientDAO, + DomainDAO domainDAO, + PatientCrossReferenceDAO patientCrossReferenceDAO, + IdentifierDAO identifierDAO) { + + this.client = new PatientFeedApplication(patientDAO, domainDAO, patientCrossReferenceDAO, identifierDAO); + } + + @Package + public void setClient(PatientFeedApplication client) { + this.client = client; + } + + + /** + * Method called to create a Patient in the Patient Registry Database + * @param patient : the patient to add in database, represented in patientregistry model + * @return a string corresponding to the uuid of the newly created patient in the database. + */ + public String createPatient(net.ihe.gazelle.app.patientregistryapi.business.Patient patient) { + + if (patient == null) { + throw new InvalidRequestException(NO_PATIENT_PARAMETER); + } + + /*if (client == null) { + logger.info("client not set"); + initializeClient(); + }*/ + + try { + String uuid = client.createPatient(patient); + + if (uuid == null || uuid.isBlank()) { + throw new InternalErrorException(NO_UUID); + } + + try { + UUID verification = UUID.fromString(uuid); + logger.info(verification.toString()); + } catch (IllegalArgumentException e) { + logger.error(BAD_UUID); + throw new InternalErrorException(BAD_UUID); + } + + return uuid; + + } catch (PatientFeedException e) { + switch (e.getCause().getMessage()) { + case "One patientIdentifier is malformed" : + throw new UnprocessableEntityException("", e); + case " one PatientIdentifier is duplicated id DB" : + throw new ResourceNotFoundException(""); + case "System not found" : + throw new ResourceNotFoundException("System Domain could not be found when creating Patient"); + case "Unexpected Exception persisting Patient !" : + throw new InternalErrorException("", e); + case "Null crossReference parameter for creation": + throw new InternalErrorException("", e); + case "Persistence Error": + throw new InternalErrorException("", e); + case "Exception performing the read operation for requested criteria !": + throw new InternalErrorException("", e); + default: + throw new InternalErrorException("", e); + } + } + } +} diff --git a/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java b/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java index e31f7551286b54d5dc795e9a1c3c38d10b917841..64973caa1edf2a9bc6c5bf896cfde02f51600a3e 100644 --- a/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java +++ b/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java @@ -7,16 +7,19 @@ import javax.inject.Inject; import javax.inject.Named; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Patient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ca.uhn.fhir.rest.annotation.Create; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Operation; import ca.uhn.fhir.rest.annotation.OperationParam; import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.param.StringAndListParam; import ca.uhn.fhir.rest.param.StringOrListParam; import ca.uhn.fhir.rest.param.StringParam; @@ -26,8 +29,12 @@ import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import net.ihe.gazelle.adapter.connector.BundleToPatientRegistryConverter; +import net.ihe.gazelle.adapter.connector.ConversionException; +import net.ihe.gazelle.app.patientregistryapi.application.PatientFeedException; import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException; import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier; +import net.ihe.gazelle.application.PatientRegistryFeedClient; import net.ihe.gazelle.application.PatientRegistrySearchClient; import net.ihe.gazelle.application.PatientRegistryXRefSearchClient; import net.ihe.gazelle.lib.annotations.Package; @@ -51,12 +58,16 @@ public class ChPatientResourceProvider implements IResourceProvider { public static final String TARGET_SYSTEM_NOT_FOUND = "targetSystem not found"; private static final Logger patientLogger = LoggerFactory.getLogger(ChPatientResourceProvider.class); public static final String NO_ID_PROVIDED = "No ID provided"; + public static final String NO_BUNDLE_PROVIDED = "No Bundle provided"; @Inject private PatientRegistryXRefSearchClient patientRegistryXRefSearchClient; @Inject private PatientRegistrySearchClient patientRegistrySearchClient; + + @Inject + private PatientRegistryFeedClient patientRegistryCreateClient; private ChPatientResourceProvider() { } @@ -65,6 +76,11 @@ public class ChPatientResourceProvider implements IResourceProvider { public ChPatientResourceProvider(PatientRegistryXRefSearchClient client) { this.patientRegistryXRefSearchClient = client; } + + @Package + public ChPatientResourceProvider(PatientRegistryFeedClient client) { + this.patientRegistryCreateClient = client; + } @Package public ChPatientResourceProvider(PatientRegistrySearchClient client) { @@ -97,6 +113,40 @@ public class ChPatientResourceProvider implements IResourceProvider { } } + + /** + * Method called to create a Patient + * @param iti93Bundle + * @return + */ + @Create + public Patient create(@ResourceParam Bundle iti93Bundle) { + if (iti93Bundle == null || iti93Bundle.getEntry().isEmpty()) { + patientLogger.error(NO_BUNDLE_PROVIDED); + throw new InvalidRequestException(NO_BUNDLE_PROVIDED); + } + + try { + net.ihe.gazelle.app.patientregistryapi.business.Patient patient = BundleToPatientRegistryConverter.iti93BundleToPatient(iti93Bundle); + //return patientRegistryCreateClient.createPatient(patient); + + } catch (ConversionException e) { + throw new InvalidRequestException("Bundle Could not be converted to HL7 Patient"); + } /* catch (PatientFeedException e) { + switch (e.getMessage()) { + case "Error feeding patient !": + break; + case "Error feeding patient ! SearchCrossReferenceException": + break; + case "Error feeding patient ! CreationCrossReferenceException": + break; + case "Error feeding patient ! CrossReferenceApplicationException": + break; + default: + } + } */ + return null; + } /** * Search method for a Patient using the source identifier required parameter diff --git a/src/test/java/net/ihe/gazelle/application/PatientFeedApplicationMock.java b/src/test/java/net/ihe/gazelle/application/PatientFeedApplicationMock.java new file mode 100644 index 0000000000000000000000000000000000000000..3e67600b8c3a4d576f137880f49abfd087a0b764 --- /dev/null +++ b/src/test/java/net/ihe/gazelle/application/PatientFeedApplicationMock.java @@ -0,0 +1,27 @@ +package net.ihe.gazelle.application; + +import net.ihe.gazelle.app.patientregistryapi.business.Patient; +import net.ihe.gazelle.app.patientregistryservice.application.PatientFeedApplication; +import net.ihe.gazelle.app.patientregistryservice.application.dao.DomainDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.IdentifierDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.PatientCrossReferenceDAO; +import net.ihe.gazelle.app.patientregistryservice.application.dao.PatientDAO; + +public class PatientFeedApplicationMock extends PatientFeedApplication{ + + public PatientFeedApplicationMock(PatientDAO patientDAO, + DomainDAO domainDAO, + PatientCrossReferenceDAO patientCrossReferenceDAO, + IdentifierDAO identifierDAO) { + + super(patientDAO, domainDAO, patientCrossReferenceDAO, identifierDAO); + } + + @Override + public String createPatient(Patient patient) { + + return null; + + } + +} diff --git a/src/test/java/net/ihe/gazelle/application/PatientFeedClientTest.java b/src/test/java/net/ihe/gazelle/application/PatientFeedClientTest.java new file mode 100644 index 0000000000000000000000000000000000000000..070c46f874c768e80c24ddf1069bb1ca5e58365f --- /dev/null +++ b/src/test/java/net/ihe/gazelle/application/PatientFeedClientTest.java @@ -0,0 +1,319 @@ +package net.ihe.gazelle.application; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Matchers.anyObject; + +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Date; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; +import io.qameta.allure.Description; +import io.qameta.allure.Feature; +import io.qameta.allure.Severity; +import io.qameta.allure.SeverityLevel; +import io.qameta.allure.Story; +import net.ihe.gazelle.app.patientregistryapi.application.PatientFeedException; +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.application.PatientFeedApplication; +import net.ihe.gazelle.app.patientregistryservice.application.exceptions.PatientCreationException; +import net.ihe.gazelle.framework.preferencesmodelapi.application.NamespaceException; +import net.ihe.gazelle.framework.preferencesmodelapi.application.PreferenceException; + +@Feature("PatientFeedClient") +public class PatientFeedClientTest { + + private static final String TEST_UUID = "123e4567-e89b-12d3-a456-426614174000"; + private static final String MALFORMED_UUID = "123e4567-e89b-12d3-a456-42661417400000000000000000000000000"; + + @Mock + static private PatientFeedApplication patientFeedApplicationMock; + @Mock + private PatientRegistryFeedClient patientRegistryFeedClient; + + @BeforeAll + static void initialize() { + patientFeedApplicationMock = Mockito.mock(PatientFeedApplication.class); + } + + /*@Test + @Description("Test on initialization with DAOs") + @Severity(SeverityLevel.MINOR) + @Story("initialization") + void TestInitialization() throws PreferenceException, NamespaceException, PatientFeedException { + + PatientDAOImpl patientDAO = new PatientDAOImpl(); + DomainDAOImpl domainDAO; + PatientCrossReferenceDAOImpl patientCrossReferenceDAO; + IdentifierDAOImpl identifierDAO; + + }*/ + + @Test + @Description("Test on create, nominal case") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestNominalCreation() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doReturn(TEST_UUID).when(patientFeedApplicationMock).createPatient(anyObject()); + assertEquals(TEST_UUID, patientRegistryFeedClient.createPatient(patient)); + } + + @Test + @Description("Test on create, null Patient") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestNullPatientException() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = null; + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + assertThrows(InvalidRequestException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InvalidRequestException e) { + assertEquals(PatientRegistryFeedClient.NO_PATIENT_PARAMETER, e.getMessage()); + } + } + + @Test + @Description("Test on create, when a blank uuid is returned") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestBlankUuidReturnedException() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doReturn("").when(patientFeedApplicationMock).createPatient(anyObject()); + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals(PatientRegistryFeedClient.NO_UUID, e.getMessage()); + } + } + + @Test + @Description("Test on create, when a Malformed UUID is returned") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestMalformedUuidReturnedException() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doReturn(MALFORMED_UUID).when(patientFeedApplicationMock).createPatient(anyObject()); + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals(PatientRegistryFeedClient.BAD_UUID, e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionMalformedId() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("One patientIdentifier is malformed"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(UnprocessableEntityException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (UnprocessableEntityException e) { + assertEquals("", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionDuplicatedId() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException(" one PatientIdentifier is duplicated id DB"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(ResourceNotFoundException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (ResourceNotFoundException e) { + assertEquals("", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionSystemNotFound() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("System not found"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(ResourceNotFoundException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (ResourceNotFoundException e) { + assertEquals("System Domain could not be found when creating Patient", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionPersistingPatient() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("Unexpected Exception persisting Patient !"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals("", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionNoCrossReferenceDB() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("Null crossReference parameter for creation"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals("", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionPersistanceError() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("Persistence Error"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals("", e.getMessage()); + } + } + + @Test + @Description("Test on create, for particular exceptions returned from PatientFeedApplication") + @Severity(SeverityLevel.CRITICAL) + @Story("create") + void TestFeedThrowsPatientFeedExceptionReadOperation() throws PreferenceException, NamespaceException, PatientFeedException { + + Patient patient = createPatient("","",LocalDate.of(1990, 06, 19), GenderCode.MALE); + + PatientCreationException embedException = new PatientCreationException("Exception performing the read operation for requested criteria !"); + PatientFeedException firstException = new PatientFeedException(embedException); + + patientRegistryFeedClient = new PatientRegistryFeedClient(); + patientRegistryFeedClient.setClient(patientFeedApplicationMock); + + Mockito.doThrow(firstException).when(patientFeedApplicationMock).createPatient(anyObject()); + + assertThrows(InternalErrorException.class, () -> patientRegistryFeedClient.createPatient(patient)); + try { + patientRegistryFeedClient.createPatient(patient); + } catch (InternalErrorException e) { + assertEquals("", e.getMessage()); + } + } + + private Patient createPatient(String familyName, String givenName, LocalDate birthDate, GenderCode gender) { + + Patient patient = new Patient(); + + PersonName name = new PersonName(); + name.addGiven(givenName); + name.setFamily(familyName); + patient.addName(name); + patient.setDateOfBirth(Date.from(birthDate.atStartOfDay(ZoneId.systemDefault()).toInstant())); + patient.setGender(gender); + return patient; + + } + +}