Search in sources :

Example 1 with InMemoryFileHelper

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);
}
Also used : S3Helper(org.sagebionetworks.bridge.s3.S3Helper) InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) DigestUtils(org.apache.commons.codec.digest.DigestUtils) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with InMemoryFileHelper

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());
}
Also used : S3Helper(org.sagebionetworks.bridge.s3.S3Helper) InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) HealthDataService(org.sagebionetworks.bridge.services.HealthDataService) UploadDao(org.sagebionetworks.bridge.dao.UploadDao) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) File(java.io.File) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with InMemoryFileHelper

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);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) HealthDataService(org.sagebionetworks.bridge.services.HealthDataService) UploadDao(org.sagebionetworks.bridge.dao.UploadDao) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with InMemoryFileHelper

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());
}
Also used : InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with InMemoryFileHelper

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);
}
Also used : InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) UploadSchemaService(org.sagebionetworks.bridge.services.UploadSchemaService) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

InMemoryFileHelper (org.sagebionetworks.bridge.file.InMemoryFileHelper)9 BeforeMethod (org.testng.annotations.BeforeMethod)8 File (java.io.File)5 HealthDataRecord (org.sagebionetworks.bridge.models.healthdata.HealthDataRecord)3 S3Helper (org.sagebionetworks.bridge.s3.S3Helper)3 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 UploadDao (org.sagebionetworks.bridge.dao.UploadDao)2 DynamoUpload2 (org.sagebionetworks.bridge.dynamodb.DynamoUpload2)2 HealthDataService (org.sagebionetworks.bridge.services.HealthDataService)2 UploadSchemaService (org.sagebionetworks.bridge.services.UploadSchemaService)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 DigestUtils (org.apache.commons.codec.digest.DigestUtils)1 App (org.sagebionetworks.bridge.models.apps.App)1 UploadFieldDefinition (org.sagebionetworks.bridge.models.upload.UploadFieldDefinition)1 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)1 Test (org.testng.annotations.Test)1