diff --git a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java index 686df5d2..c54e6e35 100644 --- a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java +++ b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java @@ -13,6 +13,7 @@ import edu.harvard.hms.dbmi.avillach.hpds.data.query.ResultType; import edu.harvard.hms.dbmi.avillach.hpds.service.filesharing.FileSharingService; +import edu.harvard.hms.dbmi.avillach.hpds.service.filesharing.TestDataService; import edu.harvard.hms.dbmi.avillach.hpds.service.util.Paginator; import edu.harvard.hms.dbmi.avillach.hpds.service.util.QueryDecorator; import org.apache.http.entity.ContentType; @@ -45,9 +46,9 @@ public class PicSureService implements IResourceRS { @Autowired public PicSureService(QueryService queryService, TimelineProcessor timelineProcessor, CountProcessor countProcessor, - VariantListProcessor variantListProcessor, AbstractProcessor abstractProcessor, - Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator - ) { + VariantListProcessor variantListProcessor, AbstractProcessor abstractProcessor, + Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator, TestDataService testDataService + ) { this.queryService = queryService; this.timelineProcessor = timelineProcessor; this.countProcessor = countProcessor; @@ -56,7 +57,8 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce this.paginator = paginator; this.fileSystemService = fileSystemService; this.queryDecorator = queryDecorator; - Crypto.loadDefaultKey(); + this.testDataService = testDataService; + Crypto.loadDefaultKey(); } private final QueryService queryService; @@ -79,6 +81,8 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce private final QueryDecorator queryDecorator; + private final TestDataService testDataService; + private static final String QUERY_METADATA_FIELD = "queryMetadata"; private static final int RESPONSE_CACHE_SIZE = 50; @@ -275,6 +279,10 @@ private Optional roundTripUUID(String uuid) { public Response writeQueryResult( @RequestBody() Query query, @PathParam("dataType") String datatype ) { + if ("test_upload".equals(datatype)) { + return testDataService.uploadTestFile(query.getPicSureId()) ? + Response.ok().build() : Response.status(500).build(); + } if (roundTripUUID(query.getPicSureId()).map(id -> !id.equalsIgnoreCase(query.getPicSureId())).orElse(false)) { return Response .status(400, "The query pic-sure ID is not a UUID") diff --git a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataService.java b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataService.java new file mode 100644 index 00000000..92b938f3 --- /dev/null +++ b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataService.java @@ -0,0 +1,34 @@ +package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.UUID; + +/** + * Writes test data to the directory shared with the uploader to test + * uploading features + */ +@Service +public class TestDataService { + + private static final Logger LOG = LoggerFactory.getLogger(TestDataService.class); + + private final FileSystemService fileSystemService; + + @Autowired + public TestDataService(FileSystemService fileSystemService) { + this.fileSystemService = fileSystemService; + } + + public boolean uploadTestFile(String uuid) { + LOG.info("Writing test file for uuid {}", uuid); + return fileSystemService.writeResultToFile( + "test_data.txt", + "This is a disposable test file", + uuid + ); + } +} diff --git a/service/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataServiceTest.java b/service/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataServiceTest.java new file mode 100644 index 00000000..c5e911c0 --- /dev/null +++ b/service/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataServiceTest.java @@ -0,0 +1,32 @@ +package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; + +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; + +import java.util.UUID; + +import static org.junit.Assert.*; + + +public class TestDataServiceTest { + FileSystemService fileSystemService; + + TestDataService subject; + + @Test + public void shouldCreateTestFileForUpload() { + fileSystemService = Mockito.mock(FileSystemService.class); + subject = new TestDataService(fileSystemService); + + String uuid = UUID.randomUUID().toString(); + Mockito.when(fileSystemService.writeResultToFile("test_data.txt", "This is a disposable test file", uuid.toString())) + .thenReturn(true); + + boolean success = subject.uploadTestFile(uuid); + assertTrue(success); + Mockito.verify(fileSystemService, Mockito.times(1)) + .writeResultToFile("test_data.txt", "This is a disposable test file", uuid.toString()); + } +} \ No newline at end of file