use of org.sagebionetworks.bridge.file.InMemoryFileHelper in project BridgeServer2 by Sage-Bionetworks.
the class UploadFileHelperFindValueTest method before.
@BeforeMethod
public void before() throws Exception {
// Spy file helper, so we can check to see how many times we read the disk later. Also make a dummy temp dir,
// as an in-memory place we can put files into.
inMemoryFileHelper = spy(new InMemoryFileHelper());
tmpDir = inMemoryFileHelper.createTempDir();
// Mock dependencies.
DigestUtils mockMd5DigestUtils = mock(DigestUtils.class);
when(mockMd5DigestUtils.digest(any(File.class))).thenReturn(TestConstants.MOCK_MD5);
when(mockMd5DigestUtils.digest(any(byte[].class))).thenReturn(TestConstants.MOCK_MD5);
mockS3Helper = mock(S3Helper.class);
metadataCaptor = ArgumentCaptor.forClass(ObjectMetadata.class);
// Create UploadFileHelper.
uploadFileHelper = new UploadFileHelper();
uploadFileHelper.setFileHelper(inMemoryFileHelper);
uploadFileHelper.setMd5DigestUtils(mockMd5DigestUtils);
uploadFileHelper.setS3Helper(mockS3Helper);
}
use of org.sagebionetworks.bridge.file.InMemoryFileHelper in project BridgeServer2 by Sage-Bionetworks.
the class UploadHandlersEndToEndTest method before.
@BeforeMethod
public void before() throws Exception {
// Reset all member vars, because JUnit doesn't.
inMemoryFileHelper = new InMemoryFileHelper();
mockHealthDataService = mock(HealthDataService.class);
mockUploadDao = mock(UploadDao.class);
mockS3UploadHelper = mock(S3Helper.class);
savedRecord = null;
uploadedFileContentMap = new HashMap<>();
metadataCaptor = ArgumentCaptor.forClass(ObjectMetadata.class);
ObjectNode metadataFromRequest = (ObjectNode) BridgeObjectMapper.get().readTree("{\"meta-request-key\":\"meta-request-value\"}");
upload = Upload.create();
upload.setFilename(UPLOAD_FILENAME);
upload.setHealthCode(HEALTH_CODE);
upload.setMetadata(metadataFromRequest);
upload.setUploadId(UPLOAD_ID);
// Mock HealthDataService.createOrUpdateRecord()
when(mockHealthDataService.createOrUpdateRecord(any(HealthDataRecord.class))).thenAnswer(invocation -> {
// save record
savedRecord = invocation.getArgument(0);
return savedRecord.getId();
});
when(mockHealthDataService.getRecordById(any())).thenAnswer(invocation -> {
String recordId = invocation.getArgument(0);
if (savedRecord == null || !savedRecord.getId().equals(recordId)) {
return null;
} else {
return savedRecord;
}
});
// Mock S3 upload helper. We need to save the file contents, since we delete all files at the end of execution.
doAnswer(invocation -> {
String s3Key = invocation.getArgument(1);
File uploadedFile = invocation.getArgument(2);
byte[] uploadedFileContent = inMemoryFileHelper.getBytes(uploadedFile);
uploadedFileContentMap.put(s3Key, uploadedFileContent);
// Required return
return null;
}).when(mockS3UploadHelper).writeFileToS3(any(), any(), any(), any());
doAnswer(invocation -> {
String s3Key = invocation.getArgument(1);
byte[] uploadedFileContent = invocation.getArgument(2);
uploadedFileContentMap.put(s3Key, uploadedFileContent);
// Required return
return null;
}).when(mockS3UploadHelper).writeBytesToS3(any(), any(), any(), any());
}
use of org.sagebionetworks.bridge.file.InMemoryFileHelper in project BridgeServer2 by Sage-Bionetworks.
the class UploadValidationTaskTest method setup.
@BeforeMethod
public void setup() throws IOException {
// Mock health data service
HealthDataRecord testRecord = makeRecordWithId(RECORD_ID);
HealthDataService healthDataService = mock(HealthDataService.class);
when(healthDataService.getRecordById(eq(RECORD_ID))).thenReturn(testRecord);
// Set up context
App app = TestUtils.getValidApp(UploadValidationTaskTest.class);
upload = Upload.create();
upload.setUploadId("test-upload");
ctx = new UploadValidationContext();
ctx.setAppId(app.getIdentifier());
ctx.setUpload(upload);
// Set up other pre-reqs
inMemoryFileHelper = new InMemoryFileHelper();
mockDao = mock(UploadDao.class);
// Set up task. Spy so we can verify some calls.
task = spy(new UploadValidationTask(ctx));
task.setFileHelper(inMemoryFileHelper);
task.setHandlerList(handlerList);
task.setHealthDataService(healthDataService);
task.setUploadDao(mockDao);
}
use of org.sagebionetworks.bridge.file.InMemoryFileHelper in project BridgeServer2 by Sage-Bionetworks.
the class DecryptHandlerTest method before.
@BeforeMethod
public void before() {
MockitoAnnotations.initMocks(this);
// Set up file helper.
fileHelper = new InMemoryFileHelper();
handler.setFileHelper(fileHelper);
File tmpDir = fileHelper.createTempDir();
dataFile = fileHelper.newFile(tmpDir, "data-file");
fileHelper.writeBytes(dataFile, DATA_FILE_CONTENT);
// inputs
upload = Upload.create();
ctx = new UploadValidationContext();
ctx.setAppId(TEST_APP_ID);
ctx.setUpload(upload);
ctx.setTempDir(tmpDir);
ctx.setDataFile(dataFile);
// mock UploadArchiveService
when(mockSvc.decrypt(eq(TEST_APP_ID), any(InputStream.class))).thenReturn(new ByteArrayInputStream("decrypted test data".getBytes(Charsets.UTF_8)));
// Don't actually buffer the input stream, as this breaks the test.
doAnswer(invocation -> invocation.getArgument(0)).when(handler).getBufferedInputStream(any());
}
use of org.sagebionetworks.bridge.file.InMemoryFileHelper in project BridgeServer2 by Sage-Bionetworks.
the class GenericUploadFormatHandlerTest method setup.
@BeforeMethod
public void setup() throws Exception {
// Set up InMemoryFileHelper with tmp dir
inMemoryFileHelper = new InMemoryFileHelper();
tmpDir = inMemoryFileHelper.createTempDir();
// Mock dependencies
mockUploadFileHelper = mock(UploadFileHelper.class);
when(mockUploadFileHelper.findValueForField(any(), any(), any(), any())).thenReturn(TextNode.valueOf(ATTACHMENT_ID));
mockSchemaService = mock(UploadSchemaService.class);
// Set up handler
handler = new GenericUploadFormatHandler();
handler.setFileHelper(inMemoryFileHelper);
handler.setUploadFileHelper(mockUploadFileHelper);
handler.setUploadSchemaService(mockSchemaService);
}
Aggregations