From db6d8092f2c9c19265d618df995d36b2c5ddda8d Mon Sep 17 00:00:00 2001 From: TynerGjs <132295567+TynerGjs@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:49:06 -0400 Subject: [PATCH] =?UTF-8?q?modified=20MdmLinkDaoJpaImpl.convertToLongIds?= =?UTF-8?q?=20so=20that=20it=20returns=20all=20pi=E2=80=A6=20(#5244)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modified MdmLinkDaoJpaImpl.convertToLongIds so that it returns all pid that is related to a forced id. added tests for above changes * joined helper methods * joined helper methods --- .../fhir/jpa/dao/mdm/MdmLinkDaoJpaImpl.java | 5 +- .../ca/uhn/fhir/jpa/mdm/BaseMdmR4Test.java | 30 +++++++---- .../fhir/jpa/mdm/dao/MdmLinkDaoSvcTest.java | 50 +++++++++++++++---- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/mdm/MdmLinkDaoJpaImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/mdm/MdmLinkDaoJpaImpl.java index 7935fb9e8c8..6d924e04291 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/mdm/MdmLinkDaoJpaImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/mdm/MdmLinkDaoJpaImpl.java @@ -411,8 +411,9 @@ public class MdmLinkDaoJpaImpl implements IMdmLinkDao { @Nonnull private List convertToLongIds(List theMdmHistorySearchParameters) { - return theMdmHistorySearchParameters.stream() - .map(id -> myIdHelperService.getPidOrThrowException(RequestPartitionId.allPartitions(), id)) + return myIdHelperService + .getPidsOrThrowException(RequestPartitionId.allPartitions(), theMdmHistorySearchParameters) + .stream() .map(JpaPid::getId) .collect(Collectors.toUnmodifiableList()); } diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/BaseMdmR4Test.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/BaseMdmR4Test.java index a19b7235604..659a7809393 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/BaseMdmR4Test.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/BaseMdmR4Test.java @@ -41,20 +41,13 @@ import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.api.server.SystemRequestDetails; import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; +import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.StringUtils; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.r4.model.CodeableConcept; -import org.hl7.fhir.r4.model.ContactPoint; -import org.hl7.fhir.r4.model.DateType; -import org.hl7.fhir.r4.model.Medication; -import org.hl7.fhir.r4.model.Observation; -import org.hl7.fhir.r4.model.Organization; -import org.hl7.fhir.r4.model.Patient; -import org.hl7.fhir.r4.model.Practitioner; -import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -662,4 +655,23 @@ abstract public class BaseMdmR4Test extends BaseJpaR4Test { return myMdmLinkDao.save(mdmLink); } + + protected IBaseResource createResourceWithId(IBaseResource theResource, String theId, Enumerations.ResourceType theResourceType){ + theResource.setId(theId); + DaoMethodOutcome daoMethodOutcome = null; + switch (theResourceType){ + case PATIENT: + ((Patient) theResource).setActive(true); + daoMethodOutcome = myPatientDao.update((Patient) theResource, new SystemRequestDetails()); + break; + case PRACTITIONER: + ((Practitioner) theResource).setActive(true); + daoMethodOutcome = myPractitionerDao.update((Practitioner) theResource, new SystemRequestDetails()); + break; + default: + throw new NotImplementedException("This method haven't been setup for: " + theResourceType); + } + theResource.setId(daoMethodOutcome.getId()); + return theResource; + } } diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/dao/MdmLinkDaoSvcTest.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/dao/MdmLinkDaoSvcTest.java index 6f0bb32afa3..5af53d34392 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/dao/MdmLinkDaoSvcTest.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/dao/MdmLinkDaoSvcTest.java @@ -14,7 +14,10 @@ import ca.uhn.fhir.mdm.api.MdmMatchResultEnum; import ca.uhn.fhir.mdm.model.MdmPidTuple; import ca.uhn.fhir.mdm.rules.json.MdmRulesJson; import org.hibernate.envers.RevisionType; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Enumerations; import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.Practitioner; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -214,6 +217,31 @@ public class MdmLinkDaoSvcTest extends BaseMdmR4Test { assertThrows(IllegalArgumentException.class, () -> myMdmLinkDaoSvc.findMdmLinkHistory(new MdmHistorySearchParameters())); } + @Test + public void testHistoryForIdWithMultipleMatches(){ + // setup + String commonId = "p123"; + + // Patient/p123 and its golden resource + Patient goldenPatient = createPatient(); + Patient sourcePatient = (Patient) createResourceWithId(new Patient(), commonId, Enumerations.ResourceType.PATIENT); + + MdmLink mdmPatientLink = linkGoldenAndSourceResource(MdmMatchResultEnum.MATCH, goldenPatient, sourcePatient); + JpaPid goldenPatientId = mdmPatientLink.getGoldenResourcePersistenceId(); + // Practitioner/p123 and its golden resource + Practitioner goldenPractitioner = createPractitioner(new Practitioner()); + Practitioner sourcePractitioner = (Practitioner) createResourceWithId(new Practitioner(), commonId, Enumerations.ResourceType.PRACTITIONER); + + linkGoldenAndSourceResource(MdmMatchResultEnum.MATCH, goldenPractitioner, sourcePractitioner); + + // execute + MdmHistorySearchParameters mdmHistorySearchParameters = new MdmHistorySearchParameters().setSourceIds(List.of(commonId)); + List> actualMdmLinkRevisions = myMdmLinkDaoSvc.findMdmLinkHistory(mdmHistorySearchParameters); + + // verify + assertEquals(2, actualMdmLinkRevisions.size(), "Both Patient/p123 and Practitioner/p123 should be returned"); + } + @Nonnull private static List getIdsFromMdmLinks(Function getIdFunction, MdmLink... mdmLinks) { return Arrays.stream(mdmLinks) @@ -271,16 +299,18 @@ public class MdmLinkDaoSvcTest extends BaseMdmR4Test { return IntStream.range(0, numTargetPatients).mapToObj(myInt -> { final Patient targetPatient = createPatient(); - - MdmLink mdmLink = (MdmLink) myMdmLinkDaoSvc.newMdmLink(); - mdmLink.setLinkSource(MdmLinkSourceEnum.MANUAL); - mdmLink.setMatchResult(theFirstMdmMatchResultEnum); - mdmLink.setCreated(new Date()); - mdmLink.setUpdated(new Date()); - mdmLink.setGoldenResourcePersistenceId(runInTransaction(() -> myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), goldenPatient))); - mdmLink.setSourcePersistenceId(runInTransaction(() -> myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), targetPatient))); - return myMdmLinkDao.save(mdmLink); - + return linkGoldenAndSourceResource(theFirstMdmMatchResultEnum, goldenPatient, targetPatient); }).toList(); } + + private MdmLink linkGoldenAndSourceResource(MdmMatchResultEnum theFirstMdmMatchResultEnum, IBaseResource theGoldenResource, IBaseResource theTargetResource) { + MdmLink mdmLink = (MdmLink) myMdmLinkDaoSvc.newMdmLink(); + mdmLink.setLinkSource(MdmLinkSourceEnum.MANUAL); + mdmLink.setMatchResult(theFirstMdmMatchResultEnum); + mdmLink.setCreated(new Date()); + mdmLink.setUpdated(new Date()); + mdmLink.setGoldenResourcePersistenceId(runInTransaction(() -> myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), theGoldenResource))); + mdmLink.setSourcePersistenceId(runInTransaction(() -> myIdHelperService.getPidOrNull(RequestPartitionId.allPartitions(), theTargetResource))); + return myMdmLinkDao.save(mdmLink); + } }