Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added resourceId for deferredBinaryTarget to avoid adding binary appl… #5438

Open
wants to merge 1 commit into
base: rel_6_10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added resourceId for deferredBinaryTarget to avoid adding binary appl…
…ications multiple times in one transaction
  • Loading branch information
kateryna-mironova committed Nov 7, 2023
commit 2a8ef9ac1d1868de56e081780fef9dbcf65b472d
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
type: fix
issue: 5437
jira: SMILE-7588
title: "Previously, when adding several binary resources in one bundle in a transaction, the EntityExistsException error appeared. This is now fixed."
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,40 @@
content.getAttachment().setData(theData);
}

@Test
public void testCreateBundleWithMultipleBinaryAttachments_createdSuccessfully() {

Check notice on line 562 in hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/BinaryStorageInterceptorR4Test.java#L562

The JUnit 5 test method name 'testCreateBundleWithMultipleBinaryAttachments_createdSuccessfully' doesn't match '[a-z][a-zA-Z0-9]*'
Binary binary = new Binary();
binary.setContentType("application/octet-stream");
binary.setData(SOME_BYTES);

DocumentReference docRef = new DocumentReference();
addDocumentAttachmentData(docRef, SOME_BYTES_2);

// Prepare the bundle
Bundle bundle = new Bundle();
bundle.setType(Bundle.BundleType.TRANSACTION);
addBundleEntry(bundle, binary, "Binary");
addBundleEntry(bundle, docRef, "DocumentReference");

// Execute transaction
Bundle output = myClient.transaction().withBundle(bundle).execute();
ourLog.debug(myFhirContext.newXmlParser().setPrettyPrint(true).encodeResourceToString(output));

// Verify bundle response
assertEquals(2, output.getEntry().size());
output.getEntry().forEach(entry -> assertEquals("201 Created", entry.getResponse().getStatus()));

// Verify Binary and attachment
IIdType binaryId = new IdType(output.getEntry().get(0).getResponse().getLocation());
Binary actualBinary = myBinaryDao.read(binaryId, mySrd);
assertEquals(binary.getContentType(), actualBinary.getContentType());
assertArrayEquals(SOME_BYTES, actualBinary.getData());

// Verify DocumentReference and attachments
IIdType docRefId = new IdType(output.getEntry().get(1).getResponse().getLocation());
DocumentReference actualDocRef = myDocumentReferenceDao.read(docRefId, mySrd);
assertEquals("application/octet-stream", actualDocRef.getContentFirstRep().getAttachment().getContentType());
assertArrayEquals(SOME_BYTES_2, actualDocRef.getContentFirstRep().getAttachment().getData());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private void extractLargeBinaries(
List<DeferredBinaryTarget> deferredBinaryTargets =
getOrCreateDeferredBinaryStorageList(theResource);
DeferredBinaryTarget newDeferredBinaryTarget =
new DeferredBinaryTarget(newBlobId, nextTarget, data);
new DeferredBinaryTarget(newBlobId, resourceId.getValue(), nextTarget, data);
deferredBinaryTargets.add(newDeferredBinaryTarget);
newDeferredBinaryTarget.setBlobIdPrefixHookApplied(true);
} else {
Expand Down Expand Up @@ -312,12 +312,14 @@ public void storeLargeBinariesBeforeCreatePersistence(
if (deferredBinaryTargetList != null) {
IIdType resourceId = theResource.getIdElement();
for (DeferredBinaryTarget next : (List<DeferredBinaryTarget>) deferredBinaryTargetList) {
String blobId = next.getBlobId();
IBinaryTarget target = next.getBinaryTarget();
InputStream dataStream = next.getDataStream();
String contentType = target.getContentType();
RequestDetails requestDetails = initRequestDetails(next);
myBinaryStorageSvc.storeBlob(resourceId, blobId, contentType, dataStream, requestDetails);
if (resourceId.getValueAsString().equals(next.getResourceId())) {
String blobId = next.getBlobId();
IBinaryTarget target = next.getBinaryTarget();
InputStream dataStream = next.getDataStream();
String contentType = target.getContentType();
RequestDetails requestDetails = initRequestDetails(next);
myBinaryStorageSvc.storeBlob(resourceId, blobId, contentType, dataStream, requestDetails);
}
}
}
}
Expand Down Expand Up @@ -422,12 +424,15 @@ public boolean isAllowAutoInflateBinaries() {

private static class DeferredBinaryTarget {
private final String myBlobId;
private final String myResourceId;
private final IBinaryTarget myBinaryTarget;
private final InputStream myDataStream;
private boolean myBlobIdPrefixHookApplied;

private DeferredBinaryTarget(String theBlobId, IBinaryTarget theBinaryTarget, byte[] theData) {
private DeferredBinaryTarget(
String theBlobId, String theResourceId, IBinaryTarget theBinaryTarget, byte[] theData) {
myBlobId = theBlobId;
myResourceId = theResourceId;
myBinaryTarget = theBinaryTarget;
myDataStream = new ByteArrayInputStream(theData);
}
Expand All @@ -436,6 +441,10 @@ String getBlobId() {
return myBlobId;
}

String getResourceId() {
return myResourceId;
}

IBinaryTarget getBinaryTarget() {
return myBinaryTarget;
}
Expand Down
Loading