Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
277 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/TransactionDeleteR4Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package ca.uhn.fhir.jpa.dao.r4; | ||
|
||
import ca.uhn.fhir.jpa.config.TestR4Config; | ||
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException; | ||
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException; | ||
import org.hl7.fhir.instance.model.api.IIdType; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.r4.model.DiagnosticReport; | ||
import org.hl7.fhir.r4.model.Observation; | ||
import org.hl7.fhir.r4.model.Reference; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.matchesPattern; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = {TestR4Config.class}) | ||
public class TransactionDeleteR4Test extends BaseJpaR4SystemTest { | ||
|
||
@After | ||
public void after() { | ||
myDaoConfig.setEnforceReferentialIntegrityOnDelete(true); | ||
} | ||
|
||
|
||
@Test | ||
public void testDeleteInTransactionShouldFailWhenReferencesExist() { | ||
final Observation obs1 = new Observation(); | ||
obs1.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs1id = myObservationDao.create(obs1).getId().toUnqualifiedVersionless(); | ||
|
||
final Observation obs2 = new Observation(); | ||
obs2.setStatus(Observation.ObservationStatus.FINAL); | ||
org.hl7.fhir.instance.model.api.IIdType obs2id = myObservationDao.create(obs2).getId().toUnqualifiedVersionless(); | ||
|
||
final DiagnosticReport rpt = new DiagnosticReport(); | ||
rpt.addResult(new Reference(obs2id)); | ||
IIdType rptId = myDiagnosticReportDao.create(rpt).getId().toUnqualifiedVersionless(); | ||
|
||
myObservationDao.read(obs1id); | ||
myObservationDao.read(obs2id); | ||
myDiagnosticReportDao.read(rptId); | ||
|
||
Bundle b = new Bundle(); | ||
b.addEntry().getRequest().setMethod(Bundle.HTTPVerb.DELETE).setUrl(obs2id.getValue()); | ||
|
||
try { | ||
mySystemDao.transaction(mySrd, b); | ||
fail(); | ||
} catch (ResourceVersionConflictException e) { | ||
// good, transaction should not succeed because DiagnosticReport has a reference to the obs2 | ||
} | ||
} | ||
|
||
@Test | ||
public void testDeleteInTransactionShouldSucceedWhenReferencesAreAlsoRemoved() { | ||
final Observation obs1 = new Observation(); | ||
obs1.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs1id = myObservationDao.create(obs1).getId().toUnqualifiedVersionless(); | ||
|
||
final Observation obs2 = new Observation(); | ||
obs2.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs2id = myObservationDao.create(obs2).getId().toUnqualifiedVersionless(); | ||
|
||
final DiagnosticReport rpt = new DiagnosticReport(); | ||
rpt.addResult(new Reference(obs2id)); | ||
IIdType rptId = myDiagnosticReportDao.create(rpt).getId().toUnqualifiedVersionless(); | ||
|
||
myObservationDao.read(obs1id); | ||
myObservationDao.read(obs2id); | ||
myDiagnosticReportDao.read(rptId); | ||
|
||
Bundle b = new Bundle(); | ||
b.addEntry().getRequest().setMethod(Bundle.HTTPVerb.DELETE).setUrl(rptId.getValue()); | ||
b.addEntry().getRequest().setMethod(Bundle.HTTPVerb.DELETE).setUrl(obs2id.getValue()); | ||
|
||
try { | ||
// transaction should succeed because the DiagnosticReport which references obs2 is also deleted | ||
mySystemDao.transaction(mySrd, b); | ||
} catch (ResourceVersionConflictException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testDeleteWithHas_TargetModifiedToNoLongerIncludeReference() { | ||
|
||
Observation obs1 = new Observation(); | ||
obs1.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs1id = myObservationDao.create(obs1).getId().toUnqualifiedVersionless(); | ||
|
||
Observation obs2 = new Observation(); | ||
obs2.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs2id = myObservationDao.create(obs2).getId().toUnqualifiedVersionless(); | ||
|
||
DiagnosticReport rpt = new DiagnosticReport(); | ||
rpt.addIdentifier().setSystem("foo").setValue("IDENTIFIER"); | ||
rpt.addResult(new Reference(obs2id)); | ||
IIdType rptId = myDiagnosticReportDao.create(rpt).getId().toUnqualifiedVersionless(); | ||
|
||
myObservationDao.read(obs1id); | ||
myObservationDao.read(obs2id); | ||
|
||
rpt = new DiagnosticReport(); | ||
rpt.addIdentifier().setSystem("foo").setValue("IDENTIFIER"); | ||
|
||
Bundle b = new Bundle(); | ||
b.addEntry().getRequest().setMethod(Bundle.HTTPVerb.DELETE).setUrl("Observation?_has:DiagnosticReport:result:identifier=foo|IDENTIFIER"); | ||
b.addEntry().setResource(rpt).getRequest().setMethod(Bundle.HTTPVerb.PUT).setUrl("DiagnosticReport?identifier=foo|IDENTIFIER"); | ||
mySystemDao.transaction(mySrd, b); | ||
|
||
myObservationDao.read(obs1id); | ||
try { | ||
myObservationDao.read(obs2id); | ||
fail(); | ||
} catch (ResourceGoneException e) { | ||
// good | ||
} | ||
|
||
rpt = myDiagnosticReportDao.read(rptId); | ||
assertThat(rpt.getResult(), empty()); | ||
} | ||
|
||
|
||
@Test | ||
public void testDeleteWithHas_TargetModifiedToStillIncludeReference() { | ||
|
||
Observation obs1 = new Observation(); | ||
obs1.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs1id = myObservationDao.create(obs1).getId().toUnqualifiedVersionless(); | ||
|
||
Observation obs2 = new Observation(); | ||
obs2.setStatus(Observation.ObservationStatus.FINAL); | ||
IIdType obs2id = myObservationDao.create(obs2).getId().toUnqualifiedVersionless(); | ||
|
||
DiagnosticReport rpt = new DiagnosticReport(); | ||
rpt.addIdentifier().setSystem("foo").setValue("IDENTIFIER"); | ||
rpt.addResult(new Reference(obs2id)); | ||
IIdType rptId = myDiagnosticReportDao.create(rpt).getId().toUnqualifiedVersionless(); | ||
|
||
myObservationDao.read(obs1id); | ||
myObservationDao.read(obs2id); | ||
|
||
rpt = new DiagnosticReport(); | ||
rpt.addIdentifier().setSystem("foo").setValue("IDENTIFIER"); | ||
rpt.addResult(new Reference(obs2id)); | ||
|
||
Bundle b = new Bundle(); | ||
b.addEntry().getRequest().setMethod(Bundle.HTTPVerb.DELETE).setUrl("Observation?_has:DiagnosticReport:result:identifier=foo|IDENTIFIER"); | ||
b.addEntry().setResource(rpt).getRequest().setMethod(Bundle.HTTPVerb.PUT).setUrl("DiagnosticReport?identifier=foo|IDENTIFIER"); | ||
try { | ||
mySystemDao.transaction(mySrd, b); | ||
fail(); | ||
} catch (ResourceVersionConflictException e ) { | ||
assertThat(e.getMessage(), matchesPattern("Unable to delete Observation/[0-9]+ because at least one resource has a reference to this resource. First reference found was resource DiagnosticReport/[0-9]+ in path DiagnosticReport.result")); | ||
} | ||
|
||
myObservationDao.read(obs1id); | ||
myObservationDao.read(obs2id); | ||
myDiagnosticReportDao.read(rptId); | ||
} | ||
} |
Oops, something went wrong.