From a11c03a0cae37e6734357e202c4ca2fdda37fa40 Mon Sep 17 00:00:00 2001 From: Luke Sikina Date: Wed, 27 Nov 2024 15:57:50 -0500 Subject: [PATCH] [ALS-7539] Test file creation for uploader (cherry picked from commit 60354827199a5b10ca6ecc8222d0ae9a182b6cd6) --- .../avillach/hpds/service/PicSureService.java | 12 ++++++- .../service/filesharing/TestDataService.java | 34 +++++++++++++++++++ .../filesharing/TestDataServiceTest.java | 30 ++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataService.java create mode 100644 service/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataServiceTest.java 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 391d4ca8..749c5a36 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 @@ -11,6 +11,7 @@ import edu.harvard.hms.dbmi.avillach.hpds.data.query.ResultType; import edu.harvard.hms.dbmi.avillach.hpds.processing.upload.SignUrlService; 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; @@ -48,7 +49,8 @@ public class PicSureService { @Autowired public PicSureService(QueryService queryService, TimelineProcessor timelineProcessor, CountProcessor countProcessor, VariantListProcessor variantListProcessor, AbstractProcessor abstractProcessor, Paginator paginator, - SignUrlService signUrlService, FileSharingService fileSystemService, QueryDecorator queryDecorator + SignUrlService signUrlService, FileSharingService fileSystemService, QueryDecorator queryDecorator, + TestDataService testDataService ) { this.queryService = queryService; this.timelineProcessor = timelineProcessor; @@ -60,6 +62,8 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce this.queryDecorator = queryDecorator; this.signUrlService = signUrlService; Crypto.loadDefaultKey(); + this.testDataService = testDataService; + Crypto.loadDefaultKey(); } private final QueryService queryService; @@ -84,6 +88,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; @@ -252,6 +258,10 @@ private Optional roundTripUUID(String uuid) { public ResponseEntity writeQueryResult( @RequestBody() Query query, @PathVariable("dataType") String datatype ) { + if ("test_upload".equals(datatype)) { + return testDataService.uploadTestFile(query.getPicSureId()) ? + ResponseEntity.ok().build() : ResponseEntity.status(500).build(); + } if (roundTripUUID(query.getPicSureId()).map(id -> !id.equalsIgnoreCase(query.getPicSureId())).orElse(false)) { return ResponseEntity .status(400) 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..1123fa17 --- /dev/null +++ b/service/src/test/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/TestDataServiceTest.java @@ -0,0 +1,30 @@ +package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertTrue; + + +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