Skip to content
Merged
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
13 changes: 13 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@

<!-- End OpenMRS core -->

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byteBuddyVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${byteBuddyVersion}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,26 @@ public class ComplexObsSaver {
@Qualifier(AttachmentsConstants.COMPONENT_COMPLEXDATA_HELPER)
protected ComplexDataHelper complexDataHelper;

@Autowired
@Qualifier(AttachmentsConstants.COMPONENT_VISIT_COMPATIBILITY)
protected VisitCompatibility visitCompatibility;

protected Obs obs = new Obs();

protected ConceptComplex conceptComplex;

public Obs getObs() {
Comment thread
denniskigen marked this conversation as resolved.
Resolved
return obs;
}

protected void prepareComplexObs(Visit visit, Person person, Encounter encounter, String fileCaption,
String formFieldNamespace, String formFieldPath) {
obs = new Obs(person, conceptComplex,
protected Obs prepareComplexObs(Visit visit, Person person, Encounter encounter, String fileCaption,
String formFieldNamespace, String formFieldPath, ConceptComplex conceptComplex) {
Obs obs = new Obs(person, conceptComplex,
visit == null || visit.getStopDatetime() == null ? new Date() : visit.getStopDatetime(),
encounter != null ? encounter.getLocation() : null);
obs.setEncounter(encounter); // may be null
obs.setComment(fileCaption);
if (StringUtils.isNotBlank(formFieldNamespace) && StringUtils.isNotBlank(formFieldPath)) {
obs.setFormField(formFieldNamespace, formFieldPath);
}
return obs;
}

public Obs saveImageAttachment(Visit visit, Person person, Encounter encounter, String fileCaption,
MultipartFile multipartFile, String instructions, String formFieldNamespace, String formFieldPath)
throws IOException {

conceptComplex = context.getConceptComplex(ContentFamily.IMAGE);
prepareComplexObs(visit, person, encounter, fileCaption, formFieldNamespace, formFieldPath);
ConceptComplex conceptComplex = context.getConceptComplex(ContentFamily.IMAGE);
Obs obs = prepareComplexObs(visit, person, encounter, fileCaption, formFieldNamespace, formFieldPath,
Comment thread
denniskigen marked this conversation as resolved.
Resolved
conceptComplex);

Object image = multipartFile.getInputStream();
double compressionRatio = getCompressionRatio(multipartFile.getSize(),
Expand All @@ -94,8 +84,9 @@ public Obs saveImageAttachment(Visit visit, Person person, Encounter encounter,
public Obs saveOtherAttachment(Visit visit, Person person, Encounter encounter, String fileCaption,
MultipartFile multipartFile, String instructions, String formFieldNamespace, String formFieldPath)
throws IOException {
conceptComplex = context.getConceptComplex(ContentFamily.OTHER);
prepareComplexObs(visit, person, encounter, fileCaption, formFieldNamespace, formFieldPath);
ConceptComplex conceptComplex = context.getConceptComplex(ContentFamily.OTHER);
Obs obs = prepareComplexObs(visit, person, encounter, fileCaption, formFieldNamespace, formFieldPath,
conceptComplex);

obs.setComplexData(complexDataHelper.build(instructions,
replacePipeCharactersInFilenameWithUnderscores(multipartFile.getOriginalFilename()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.openmrs.module.attachments;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;
import org.openmrs.ConceptComplex;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.api.ObsService;
import org.openmrs.module.attachments.AttachmentsConstants.ContentFamily;
import org.openmrs.module.attachments.obs.AttachmentComplexData;
import org.openmrs.module.attachments.obs.ComplexDataHelper;
import org.openmrs.module.attachments.obs.ValueComplex;
import org.openmrs.obs.ComplexData;
import org.springframework.mock.web.MockMultipartFile;

public class ComplexObsSaverTest {

@Test
public void saveImageAttachment_shouldNotMixUpConcurrentUploads() throws Exception {
ComplexObsSaver saver = new ComplexObsSaver();
saver.context = mock(AttachmentsContext.class);
saver.complexDataHelper = mock(ComplexDataHelper.class);
ObsService obsService = mock(ObsService.class);

when(saver.context.getObsService()).thenReturn(obsService);
when(saver.context.getMaxStorageFileSize()).thenReturn(5.0);
when(saver.context.getConceptComplex(ContentFamily.IMAGE)).thenReturn(new ConceptComplex());
when(obsService.saveObs(any(Obs.class), anyString())).thenAnswer(i -> i.getArgument(0));

CyclicBarrier bothPrepared = new CyclicBarrier(2);
when(saver.complexDataHelper.build(anyString(), anyString(), any(), anyString())).thenAnswer(i -> {
bothPrepared.await();
AttachmentComplexData data = mock(AttachmentComplexData.class);
when(data.asComplexData()).thenReturn(new ComplexData(i.getArgument(1), i.getArgument(2)));
return data;
});

ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Obs> first = executor.submit(() -> save(saver, "first.png", "First caption"));
Future<Obs> second = executor.submit(() -> save(saver, "second.png", "Second caption"));
Obs firstObs = first.get();
Obs secondObs = second.get();
executor.shutdown();

assertNotSame(firstObs, secondObs);
assertEquals("First caption", firstObs.getComment());
assertEquals("first.png", firstObs.getComplexData().getTitle());
assertEquals("Second caption", secondObs.getComment());
assertEquals("second.png", secondObs.getComplexData().getTitle());
}

private Obs save(ComplexObsSaver saver, String fileName, String caption) throws Exception {
return saver.saveImageAttachment(null, new Patient(), null, caption,
new MockMultipartFile(fileName, fileName, "image/png", fileName.getBytes()),
ValueComplex.INSTRUCTIONS_DEFAULT, null, null);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<webservices.restVersion>3.0.0</webservices.restVersion>
<serialization.xstreamVersion>0.3.0</serialization.xstreamVersion>
<javaxVersion>4.0.1</javaxVersion>
<byteBuddyVersion>1.18.7</byteBuddyVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down