FhirSystemDaoDstu2 extends BaseFhirSystemDao<Bundle> { @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRED) @Override public Bundle transaction(Bundle theResources) { ourLog.info("Beginning transaction with {} resources", theResources.getEntry().size()); long start = System.currentTimeMillis(); Set<IdDt> allIds = new HashSet<IdDt>(); Map<IdDt, IdDt> idSubstitutions = new HashMap<IdDt, IdDt>(); Map<IdDt, DaoMethodOutcome> idToPersistedOutcome = new HashMap<IdDt, DaoMethodOutcome>(); Bundle response = new Bundle(); OperationOutcome oo = new OperationOutcome(); response.addEntry().setResource(oo); for (int i = 0; i < theResources.getEntry().size(); i++) { Entry nextEntry = theResources.getEntry().get(i); IResource res = nextEntry.getResource(); IdDt nextResourceId = null; if (res != null) { nextResourceId = res.getId(); if (nextResourceId.hasIdPart() && !nextResourceId.hasResourceType()) { nextResourceId = new IdDt(toResourceName(res.getClass()), nextResourceId.getIdPart()); res.setId(nextResourceId); } if (nextResourceId.hasResourceType() && nextResourceId.hasIdPart()) { IdDt nextId = nextResourceId.toUnqualifiedVersionless(); if (!allIds.add(nextId)) { throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseFhirSystemDao.class, "transactionContainsMultipleWithDuplicateId", nextId)); } } } HTTPVerbEnum verb = nextEntry.getTransaction().getMethodElement().getValueAsEnum(); if (verb == null) { throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseFhirSystemDao.class, "transactionEntryHasInvalidVerb", nextEntry.getTransaction().getMethod())); } switch (verb) { case POST: { @SuppressWarnings("rawtypes") IFhirResourceDao resourceDao = getDao(res.getClass()); res.setId(null); DaoMethodOutcome outcome; Entry newEntry = response.addEntry(); outcome = resourceDao.create(res, nextEntry.getTransaction().getIfNoneExist(), false); handleTransactionCreateOrUpdateOutcome(idSubstitutions, idToPersistedOutcome, nextResourceId, outcome, newEntry); break; } case DELETE: { Entry newEntry = response.addEntry(); String url = extractTransactionUrlOrThrowException(nextEntry, verb); UrlParts parts = parseUrl(verb.getCode(), url); if (parts.getResourceId() != null) { parts.getDao().delete(new IdDt(parts.getResourceType(), parts.getResourceId())); } else { parts.getDao().deleteByUrl(parts.getResourceType() + '?' + parts.getParams()); } newEntry.getTransactionResponse().setStatus(Integer.toString(Constants.STATUS_HTTP_204_NO_CONTENT)); break; } case PUT: { @SuppressWarnings("rawtypes") IFhirResourceDao resourceDao = getDao(res.getClass()); DaoMethodOutcome outcome; Entry newEntry = response.addEntry(); String url = extractTransactionUrlOrThrowException(nextEntry, verb); UrlParts parts = parseUrl(verb.getCode(), url); if (parts.getResourceId() != null) { res.setId(new IdDt(parts.getResourceType(), parts.getResourceId())); outcome = resourceDao.update(res, null, false); } else { res.setId(null); outcome = resourceDao.update(res, parts.getResourceType() + '?' + parts.getParams(), false); } handleTransactionCreateOrUpdateOutcome(idSubstitutions, idToPersistedOutcome, nextResourceId, outcome, newEntry); break; } case GET: { String url = extractTransactionUrlOrThrowException(nextEntry, verb); UrlParts parts = parseUrl(verb.getCode(), url); @SuppressWarnings("rawtypes") IFhirResourceDao resourceDao = parts.getDao(); if (parts.getResourceId() != null && parts.getParams() == null) { IResource found; if (parts.getVersionId() != null) { found = resourceDao.read(new IdDt(parts.getResourceType(), parts.getResourceId(), parts.getVersionId())); } else { found = resourceDao.read(new IdDt(parts.getResourceType(), parts.getResourceId())); } EntryTransactionResponse resp = response.addEntry().setResource(found).getTransactionResponse(); resp.setLocation(found.getId().toUnqualified().getValue()); resp.setEtag(found.getId().getVersionIdPart()); } else if (parts.getParams() != null) { RuntimeResourceDefinition def = getContext().getResourceDefinition(parts.getDao().getResourceType()); SearchParameterMap params = translateMatchUrl(url, def); IBundleProvider bundle = parts.getDao().search(params); Bundle searchBundle = new Bundle(); searchBundle.setTotal(bundle.size()); int configuredMax = 100; if (bundle.size() > configuredMax) { oo.addIssue().setSeverity(IssueSeverityEnum.WARNING).setDetails("Search nested within transaction found more than " + configuredMax + " matches, but paging is not supported in nested transactions"); } List<IResource> resourcesToAdd = bundle.getResources(0, Math.min(bundle.size(), configuredMax)); for (IResource next : resourcesToAdd) { searchBundle.addEntry().setResource(next); } response.addEntry().setResource(searchBundle); } } } } FhirTerser terser = getContext().newTerser(); for (DaoMethodOutcome nextOutcome : idToPersistedOutcome.values()) { IResource nextResource = nextOutcome.getResource(); if (nextResource == null) { continue; } List<BaseResourceReferenceDt> allRefs = terser.getAllPopulatedChildElementsOfType(nextResource, BaseResourceReferenceDt.class); for (BaseResourceReferenceDt nextRef : allRefs) { IdDt nextId = nextRef.getReference(); if (idSubstitutions.containsKey(nextId)) { IdDt newId = idSubstitutions.get(nextId); ourLog.info(" * Replacing resource ref {} with {}", nextId, newId); nextRef.setReference(newId); } else { ourLog.debug(" * Reference [{}] does not exist in bundle", nextId); } } InstantDt deletedInstantOrNull = ResourceMetadataKeyEnum.DELETED_AT.get(nextResource); Date deletedTimestampOrNull = deletedInstantOrNull != null ? deletedInstantOrNull.getValue() : null; updateEntity(nextResource, nextOutcome.getEntity(), false, deletedTimestampOrNull, true, false); } long delay = System.currentTimeMillis() - start; ourLog.info("Transaction completed in {}ms", new Object[]{delay}); oo.addIssue().setSeverity(IssueSeverityEnum.INFORMATION).setDetails("Transaction completed in " + delay + "ms"); notifyWriteCompleted(); return response; }  @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRED) @Override Bundle transaction(Bundle theResources); @Override MetaDt metaGetOperation();  }
@Test public void testTransactionDeleteMatchUrlWithTwoMatch() { String methodName = "testTransactionDeleteMatchUrlWithTwoMatch"; Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); try { ourSystemDao.transaction(request); fail(); } catch (ResourceNotFoundException e) { assertThat(e.getMessage(), containsString("resource with match URL \"Patient?")); } }
@Test public void testTransactionDeleteByResourceId() { String methodName = "testTransactionDeleteByResourceId"; Patient p1 = new Patient(); p1.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id1 = ourPatientDao.create(p1).getId(); ourLog.info("Created patient, got it: {}", id1); Patient p2 = new Patient(); p2.addIdentifier().setSystem("urn:system").setValue(methodName); p2.setId("Patient/" + methodName); IdDt id2 = ourPatientDao.update(p2).getId(); ourLog.info("Created patient, got it: {}", id2); Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient/" + id1.getIdPart()); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient/" + id2.getIdPart()); ourPatientDao.read(id1.toVersionless()); ourPatientDao.read(id2.toVersionless()); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); assertEquals("204", resp.getEntry().get(1).getTransactionResponse().getStatus()); assertEquals("204", resp.getEntry().get(2).getTransactionResponse().getStatus()); try { ourPatientDao.read(id1.toVersionless()); fail(); } catch (ResourceGoneException e) { } try { ourPatientDao.read(id2.toVersionless()); fail(); } catch (ResourceGoneException e) { } }
@Test public void testTransactionDeleteMatchUrlWithZeroMatch() { String methodName = "testTransactionDeleteMatchUrlWithZeroMatch"; Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); try { ourSystemDao.transaction(request); fail(); } catch (ResourceNotFoundException e) { assertThat(e.getMessage(), containsString("resource matching URL \"Patient?")); } }
@Test public void testTransactionDeleteNoMatchUrl() { String methodName = "testTransactionDeleteNoMatchUrl"; Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.setId("Patient/" + methodName); IdDt id = ourPatientDao.update(p).getId(); ourLog.info("Created patient, got it: {}", id); Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Bundle res = ourSystemDao.transaction(request); assertEquals(2, res.getEntry().size()); assertEquals(Constants.STATUS_HTTP_204_NO_CONTENT + "", res.getEntry().get(1).getTransactionResponse().getStatus()); try { ourPatientDao.read(id.toVersionless()); fail(); } catch (ResourceGoneException e) { } }
@Test(expected = InvalidRequestException.class) public void testTransactionFailsWithDuplicateIds() { Bundle request = new Bundle(); Patient patient1 = new Patient(); patient1.setId(new IdDt("Patient/testTransactionFailsWithDusplicateIds")); patient1.addIdentifier().setSystem("urn:system").setValue("testPersistWithSimpleLinkP01"); request.addEntry().setResource(patient1).getTransaction().setMethod(HTTPVerbEnum.POST); Patient patient2 = new Patient(); patient2.setId(new IdDt("Patient/testTransactionFailsWithDusplicateIds")); patient2.addIdentifier().setSystem("urn:system").setValue("testPersistWithSimpleLinkP02"); request.addEntry().setResource(patient2).getTransaction().setMethod(HTTPVerbEnum.POST); ourSystemDao.transaction(request); }
@Test public void testTransactionUpdateMatchUrlWithOneMatch() { String methodName = "testTransactionUpdateMatchUrlWithOneMatch"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.PUT).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); Entry nextEntry = resp.getEntry().get(1); assertEquals("200", nextEntry.getTransactionResponse().getStatus()); assertThat(nextEntry.getTransactionResponse().getLocation(), not(containsString("test"))); assertEquals(id.toVersionless(), p.getId().toVersionless()); assertNotEquals(id, p.getId()); assertThat(p.getId().toString(), endsWith("/_history/2")); nextEntry = resp.getEntry().get(1); assertEquals("" + Constants.STATUS_HTTP_200_OK, nextEntry.getTransactionResponse().getStatus()); assertThat(nextEntry.getTransactionResponse().getLocation(), not(emptyString())); nextEntry = resp.getEntry().get(2); o = ourObservationDao.read(new IdDt(nextEntry.getTransactionResponse().getLocation())); assertEquals(id.toVersionless(), o.getSubject().getReference()); }
@Test public void testTransactionUpdateMatchUrlWithTwoMatch() { String methodName = "testTransactionUpdateMatchUrlWithTwoMatch"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.PUT).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); try { ourSystemDao.transaction(request); fail(); } catch (PreconditionFailedException e) { assertThat(e.getMessage(), containsString("with match URL \"Patient")); } }
@Test public void testTransactionUpdateMatchUrlWithZeroMatch() { String methodName = "testTransactionUpdateMatchUrlWithZeroMatch"; Bundle request = new Bundle(); Patient p = new Patient(); p.addName().addFamily("Hello"); IdDt id = ourPatientDao.create(p).getId(); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("urn:"+methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.PUT).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/urn:"+methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); Entry nextEntry = resp.getEntry().get(1); assertEquals(Constants.STATUS_HTTP_201_CREATED + "", nextEntry.getTransactionResponse().getStatus()); IdDt patientId = new IdDt(nextEntry.getTransactionResponse().getLocation()); assertThat(nextEntry.getTransactionResponse().getLocation(), not(containsString("test"))); assertNotEquals(id.toVersionless(), patientId.toVersionless()); assertThat(patientId.getValue(), endsWith("/_history/1")); nextEntry = resp.getEntry().get(2); o = ourObservationDao.read(new IdDt(nextEntry.getTransactionResponse().getLocation())); assertEquals(patientId.toVersionless(), o.getSubject().getReference()); }
@Test public void testTransactionUpdateNoMatchUrl() { String methodName = "testTransactionUpdateNoMatchUrl"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.setId("Patient/" + methodName); IdDt id = ourPatientDao.update(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.PUT).setUrl("Patient/" + id.getIdPart()); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); Entry nextEntry = resp.getEntry().get(1); assertEquals("200", nextEntry.getTransactionResponse().getStatus()); assertThat(nextEntry.getTransactionResponse().getLocation(), (containsString("test"))); assertEquals(id.toVersionless(), new IdDt(nextEntry.getTransactionResponse().getLocation()).toVersionless()); assertNotEquals(id, new IdDt(nextEntry.getTransactionResponse().getLocation())); assertThat(nextEntry.getTransactionResponse().getLocation(), endsWith("/_history/2")); nextEntry = resp.getEntry().get(2); assertEquals("" + Constants.STATUS_HTTP_201_CREATED, nextEntry.getTransactionResponse().getStatus()); o = ourObservationDao.read(new IdDt(resp.getEntry().get(2).getTransactionResponse().getLocation())); assertEquals(id.toVersionless(), o.getSubject().getReference()); }
@Test public void testTransactionCreateMatchUrlWithOneMatch() { String methodName = "testTransactionCreateMatchUrlWithOneMatch"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.setId("Patient/" + methodName); IdDt id = ourPatientDao.update(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.POST).setIfNoneExist("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); Entry respEntry = resp.getEntry().get(1); assertEquals(Constants.STATUS_HTTP_200_OK + "", respEntry.getTransactionResponse().getStatus()); assertThat(respEntry.getTransactionResponse().getLocation(), endsWith("Patient/" + id.getIdPart() + "/_history/1")); assertEquals("1", respEntry.getTransactionResponse().getEtag()); respEntry = resp.getEntry().get(2); assertEquals(Constants.STATUS_HTTP_201_CREATED + "", respEntry.getTransactionResponse().getStatus()); assertThat(respEntry.getTransactionResponse().getLocation(), containsString("Observation/")); assertThat(respEntry.getTransactionResponse().getLocation(), endsWith("/_history/1")); assertEquals("1", respEntry.getTransactionResponse().getEtag()); o = (Observation) ourObservationDao.read(new IdDt(respEntry.getTransactionResponse().getLocationElement())); assertEquals(id.toVersionless(), o.getSubject().getReference()); assertEquals("1", o.getId().getVersionIdPart()); }
@Test public void testTransactionFromBundle() throws Exception { InputStream bundleRes = SystemProviderTest.class.getResourceAsStream("/transaction_link_patient_eve.xml"); String bundleStr = IOUtils.toString(bundleRes); Bundle bundle = ourFhirContext.newXmlParser().parseResource(Bundle.class, bundleStr); Bundle resp = ourSystemDao.transaction(bundle); ourLog.info(ourFhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(resp)); OperationOutcome oo = (OperationOutcome) resp.getEntry().get(0).getResource(); assertThat(oo.getIssue().get(0).getDetailsElement().getValue(), containsString("Transaction completed")); assertThat(resp.getEntry().get(1).getTransactionResponse().getLocation(), startsWith("Patient/a555-44-4444/_history/")); assertThat(resp.getEntry().get(2).getTransactionResponse().getLocation(), startsWith("Patient/temp6789/_history/")); assertThat(resp.getEntry().get(3).getTransactionResponse().getLocation(), startsWith("Organization/GHH/_history/")); Patient p = ourPatientDao.read(new IdDt("Patient/a555-44-4444/_history/1")); assertEquals("Patient/temp6789", p.getLink().get(0).getOther().getReference().getValue()); }
@Test public void testTransactionReadAndSearch() { String methodName = "testTransactionReadAndSearch"; Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.setId("Patient/" + methodName); IdDt idv1 = ourPatientDao.update(p).getId(); ourLog.info("Created patient, got id: {}", idv1); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Family Name"); p.setId("Patient/" + methodName); IdDt idv2 = ourPatientDao.update(p).getId(); ourLog.info("Updated patient, got id: {}", idv2); Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.GET).setUrl(idv1.toUnqualifiedVersionless().getValue()); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.GET).setUrl(idv1.toUnqualified().getValue()); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.GET).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Bundle resp = ourSystemDao.transaction(request); assertEquals(4, resp.getEntry().size()); Entry nextEntry; nextEntry = resp.getEntry().get(1); assertEquals(Patient.class, nextEntry.getResource().getClass()); assertEquals(idv2.toUnqualified(), nextEntry.getResource().getId().toUnqualified()); nextEntry = resp.getEntry().get(2); assertEquals(Patient.class, nextEntry.getResource().getClass()); assertEquals(idv1.toUnqualified(), nextEntry.getResource().getId().toUnqualified()); nextEntry = resp.getEntry().get(3); assertEquals(Bundle.class, nextEntry.getResource().getClass()); Bundle respBundle = (Bundle) nextEntry.getResource(); assertEquals(1, respBundle.getTotal().intValue()); }
@Test public void testTransactionCreateMatchUrlWithTwoMatch() { String methodName = "testTransactionCreateMatchUrlWithTwoMatch"; Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); Bundle request = new Bundle(); p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.POST).setIfNoneExist("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); try { ourSystemDao.transaction(request); fail(); } catch (PreconditionFailedException e) { assertThat(e.getMessage(), containsString("with match URL \"Patient")); } }
@Test public void testTransactionCreateMatchUrlWithZeroMatch() { String methodName = "testTransactionCreateMatchUrlWithZeroMatch"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.addName().addFamily("Hello"); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.POST).setIfNoneExist("Patient?identifier=urn%3Asystem%7C" + methodName); Observation o = new Observation(); o.getCode().setText("Some Observation"); o.getSubject().setReference("Patient/" + methodName); request.addEntry().setResource(o).getTransaction().setMethod(HTTPVerbEnum.POST); Bundle resp = ourSystemDao.transaction(request); assertEquals(3, resp.getEntry().size()); Entry respEntry = resp.getEntry().get(1); assertEquals(Constants.STATUS_HTTP_201_CREATED + "", respEntry.getTransactionResponse().getStatus()); String patientId = respEntry.getTransactionResponse().getLocation(); assertThat(patientId, not(endsWith("Patient/" + methodName + "/_history/1"))); assertThat(patientId, (endsWith("/_history/1"))); assertThat(patientId, (containsString("Patient/"))); assertEquals("1", respEntry.getTransactionResponse().getEtag()); respEntry = resp.getEntry().get(2); assertEquals(Constants.STATUS_HTTP_201_CREATED + "", respEntry.getTransactionResponse().getStatus()); assertThat(respEntry.getTransactionResponse().getLocation(), containsString("Observation/")); assertThat(respEntry.getTransactionResponse().getLocation(), endsWith("/_history/1")); assertEquals("1", respEntry.getTransactionResponse().getEtag()); o = (Observation) ourObservationDao.read(new IdDt(respEntry.getTransactionResponse().getLocationElement())); assertEquals(new IdDt(patientId).toUnqualifiedVersionless(), o.getSubject().getReference()); }
@Test public void testTransactionCreateNoMatchUrl() { String methodName = "testTransactionCreateNoMatchUrl"; Bundle request = new Bundle(); Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); p.setId("Patient/" + methodName); request.addEntry().setResource(p).getTransaction().setMethod(HTTPVerbEnum.POST).setIfNoneExist("Patient?identifier=urn%3Asystem%7C" + methodName); Bundle resp = ourSystemDao.transaction(request); assertEquals(2, resp.getEntry().size()); Entry respEntry = resp.getEntry().get(1); assertEquals(Constants.STATUS_HTTP_201_CREATED + "", respEntry.getTransactionResponse().getStatus()); String patientId = respEntry.getTransactionResponse().getLocation(); assertThat(patientId, not(containsString("test"))); }
@Test public void testTransactionDeleteMatchUrlWithOneMatch() { String methodName = "testTransactionDeleteMatchUrlWithOneMatch"; Patient p = new Patient(); p.addIdentifier().setSystem("urn:system").setValue(methodName); IdDt id = ourPatientDao.create(p).getId(); ourLog.info("Created patient, got it: {}", id); Bundle request = new Bundle(); request.addEntry().getTransaction().setMethod(HTTPVerbEnum.DELETE).setUrl("Patient?identifier=urn%3Asystem%7C" + methodName); Bundle resp = ourSystemDao.transaction(request); assertEquals(2, resp.getEntry().size()); Entry nextEntry = resp.getEntry().get(1); assertEquals(Constants.STATUS_HTTP_204_NO_CONTENT + "", nextEntry.getTransactionResponse().getStatus()); try { ourPatientDao.read(id.toVersionless()); fail(); } catch (ResourceGoneException e) { } try { ourPatientDao.read(new IdDt("Patient/" + methodName)); fail(); } catch (ResourceNotFoundException e) { } IBundleProvider history = ourPatientDao.history(id, null); assertEquals(2, history.size()); assertNotNull(ResourceMetadataKeyEnum.DELETED_AT.get(history.getResources(0, 0).get(0))); assertNotNull(ResourceMetadataKeyEnum.DELETED_AT.get(history.getResources(0, 0).get(0)).getValue()); assertNull(ResourceMetadataKeyEnum.DELETED_AT.get(history.getResources(1, 1).get(0))); }