Search in sources :

Example 1 with DynamoUpload2

use of org.sagebionetworks.bridge.dynamodb.DynamoUpload2 in project BridgeServer2 by Sage-Bionetworks.

the class UploadValidationContextTest method shallowCopy.

@Test
public void shallowCopy() {
    // dummy objects to test against
    App app = TestUtils.getValidApp(UploadValidationContextTest.class);
    Upload upload = new DynamoUpload2();
    File tempDir = mock(File.class);
    File dataFile = mock(File.class);
    File decryptedDataFile = mock(File.class);
    Map<String, File> unzippedDataFileMap = ImmutableMap.<String, File>builder().put("foo", mock(File.class)).put("bar", mock(File.class)).put("baz", mock(File.class)).build();
    JsonNode infoJsonNode = BridgeObjectMapper.get().createObjectNode();
    HealthDataRecord record = HealthDataRecord.create();
    // create original
    UploadValidationContext original = new UploadValidationContext();
    original.setHealthCode(HEALTH_CODE);
    original.setAppId(app.getIdentifier());
    original.setUpload(upload);
    original.setSuccess(false);
    original.addMessage("common message");
    original.setTempDir(tempDir);
    original.setDataFile(dataFile);
    original.setDecryptedDataFile(decryptedDataFile);
    original.setUnzippedDataFileMap(unzippedDataFileMap);
    original.setInfoJsonNode(infoJsonNode);
    original.setHealthDataRecord(record);
    original.setRecordId("test-record");
    // copy and validate
    UploadValidationContext copy = original.shallowCopy();
    assertEquals(copy.getHealthCode(), HEALTH_CODE);
    assertSame(copy.getAppId(), app.getIdentifier());
    assertSame(copy.getUpload(), upload);
    assertFalse(copy.getSuccess());
    assertSame(copy.getTempDir(), tempDir);
    assertSame(copy.getDataFile(), dataFile);
    assertSame(copy.getDecryptedDataFile(), decryptedDataFile);
    assertEquals(copy.getUnzippedDataFileMap(), unzippedDataFileMap);
    assertSame(copy.getInfoJsonNode(), infoJsonNode);
    assertSame(copy.getHealthDataRecord(), record);
    assertEquals(copy.getRecordId(), "test-record");
    assertEquals(copy.getMessageList().size(), 1);
    assertEquals(copy.getMessageList().get(0), "common message");
    // modify original and validate copy unchanged
    original.setHealthCode("new-health-code");
    original.addMessage("original message");
    assertEquals(copy.getHealthCode(), HEALTH_CODE);
    assertEquals(copy.getMessageList().size(), 1);
    assertEquals(copy.getMessageList().get(0), "common message");
    // modify copy and validate original unchanged
    copy.setRecordId("new-record-id");
    copy.addMessage("copy message");
    assertEquals(original.getRecordId(), "test-record");
    assertEquals(original.getMessageList().size(), 2);
    assertEquals(original.getMessageList().get(0), "common message");
    assertEquals(original.getMessageList().get(1), "original message");
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) Upload(org.sagebionetworks.bridge.models.upload.Upload) JsonNode(com.fasterxml.jackson.databind.JsonNode) File(java.io.File) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) Test(org.testng.annotations.Test)

Example 2 with DynamoUpload2

use of org.sagebionetworks.bridge.dynamodb.DynamoUpload2 in project BridgeServer2 by Sage-Bionetworks.

the class IosSchemaValidationHandler2Test method setup.

@BeforeMethod
public void setup() {
    DateTimeUtils.setCurrentMillisFixed(MOCK_NOW.getMillis());
    // set up common params for test context
    // For upload, we need uploadId, healthCode, and uploadDate
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setUploadId(TEST_UPLOAD_ID);
    upload.setHealthCode(TEST_HEALTHCODE);
    upload.setUploadDate(LocalDate.parse(TEST_UPLOAD_DATE_STRING));
    // Create health data record with a blank data map.
    HealthDataRecord record = HealthDataRecord.create();
    record.setData(BridgeObjectMapper.get().createObjectNode());
    context = new UploadValidationContext();
    context.setAppId(TEST_APP_ID);
    context.setUpload(upload);
    context.setHealthDataRecord(record);
    // Init fileHelper and tmpDir
    inMemoryFileHelper = new InMemoryFileHelper();
    tmpDir = inMemoryFileHelper.createTempDir();
    // set up test schemas
    // To test backwards compatibility, survey schema should include both the old style fields and the new
    // "answers" field.
    UploadSchema surveySchema = UploadSchema.create();
    surveySchema.setAppId(TEST_APP_ID);
    surveySchema.setSchemaId("test-survey");
    surveySchema.setRevision(1);
    surveySchema.setName("iOS Survey");
    surveySchema.setSchemaType(UploadSchemaType.IOS_SURVEY);
    surveySchema.setFieldDefinitions(ImmutableList.of(UploadUtil.ANSWERS_FIELD_DEF, new UploadFieldDefinition.Builder().withName("foo").withType(UploadFieldType.STRING).build(), new UploadFieldDefinition.Builder().withName("bar").withType(UploadFieldType.INT).build(), new UploadFieldDefinition.Builder().withName("bar_unit").withType(UploadFieldType.STRING).build(), new UploadFieldDefinition.Builder().withName("baz").withType(UploadFieldType.ATTACHMENT_JSON_BLOB).build(), new UploadFieldDefinition.Builder().withName("calendar-date").withType(UploadFieldType.CALENDAR_DATE).build(), new UploadFieldDefinition.Builder().withName("time-without-date").withType(UploadFieldType.TIME_V2).build(), new UploadFieldDefinition.Builder().withName("legacy-date-time").withType(UploadFieldType.TIMESTAMP).build(), new UploadFieldDefinition.Builder().withName("new-date-time").withType(UploadFieldType.TIMESTAMP).build(), new UploadFieldDefinition.Builder().withName("int-as-string").withType(UploadFieldType.STRING).build(), new UploadFieldDefinition.Builder().withName("timestamp-as-date").withType(UploadFieldType.CALENDAR_DATE).build(), new UploadFieldDefinition.Builder().withName("inline-json-blob").withType(UploadFieldType.INLINE_JSON_BLOB).build(), new UploadFieldDefinition.Builder().withName("optional").withRequired(false).withType(UploadFieldType.STRING).build(), new UploadFieldDefinition.Builder().withName("optional_attachment").withRequired(false).withType(UploadFieldType.ATTACHMENT_JSON_BLOB).build()));
    UploadSchema nonSurveySchema = UploadSchema.create();
    nonSurveySchema.setAppId(TEST_APP_ID);
    nonSurveySchema.setSchemaId("non-survey");
    nonSurveySchema.setRevision(1);
    nonSurveySchema.setName("Non-Survey");
    nonSurveySchema.setSchemaType(UploadSchemaType.IOS_DATA);
    nonSurveySchema.setFieldDefinitions(ImmutableList.of(new UploadFieldDefinition.Builder().withName("sanitize____attachment.txt").withType(UploadFieldType.ATTACHMENT_V2).withFileExtension(".txt").withMimeType("text/plain").build()));
    // mock upload schema service
    UploadSchemaService mockSchemaService = mock(UploadSchemaService.class);
    when(mockSchemaService.getUploadSchemaByIdAndRevNoThrow(TEST_APP_ID, "test-survey", 1)).thenReturn(surveySchema);
    when(mockSchemaService.getUploadSchemaByIdAndRevNoThrow(TEST_APP_ID, "non-survey", 1)).thenReturn(nonSurveySchema);
    // mock upload file helper
    mockUploadFileHelper = mock(UploadFileHelper.class);
    // set up handler
    handler = new IosSchemaValidationHandler2();
    handler.setDefaultSchemaRevisionMap(DEFAULT_SCHEMA_REV_MAP);
    handler.setFileHelper(inMemoryFileHelper);
    handler.setUploadSchemaService(mockSchemaService);
    handler.setUploadFileHelper(mockUploadFileHelper);
}
Also used : UploadFieldDefinition(org.sagebionetworks.bridge.models.upload.UploadFieldDefinition) InMemoryFileHelper(org.sagebionetworks.bridge.file.InMemoryFileHelper) UploadSchemaService(org.sagebionetworks.bridge.services.UploadSchemaService) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with DynamoUpload2

use of org.sagebionetworks.bridge.dynamodb.DynamoUpload2 in project BridgeServer2 by Sage-Bionetworks.

the class StrictValidationHandlerTest method setup.

@BeforeMethod
public void setup() {
    handler = new StrictValidationHandler();
    // Set up common context attributes.
    context = new UploadValidationContext();
    context.setAppId(TEST_APP_ID);
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setUploadId("test-upload");
    context.setUpload(upload);
}
Also used : DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with DynamoUpload2

use of org.sagebionetworks.bridge.dynamodb.DynamoUpload2 in project BridgeServer2 by Sage-Bionetworks.

the class InitRecordHandlerTest method notZipped.

@Test
public void notZipped() throws Exception {
    // Set up context with an upload that isn't zipped.
    UploadValidationContext context = new UploadValidationContext();
    context.setAppId(TEST_APP_ID);
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setHealthCode(HEALTH_CODE);
    upload.setUploadId(UPLOAD_ID);
    upload.setZipped(false);
    context.setUpload(upload);
    // Execute.
    handler.handle(context);
    // These attributes are still parsed in.
    HealthDataRecord record = context.getHealthDataRecord();
    assertEquals(record.getHealthCode(), HEALTH_CODE);
    assertEquals(record.getAppId(), TEST_APP_ID);
    assertEquals(record.getUploadDate(), MOCK_NOW_DATE);
    assertEquals(record.getUploadId(), UPLOAD_ID);
    assertEquals(record.getUploadedOn().longValue(), MOCK_NOW_MILLIS);
    // These attributes are not.
    assertNull(record.getAppVersion());
    assertNull(record.getPhoneInfo());
    assertNull(record.getUserMetadata());
    // Data and metadata both exist and are empty.
    assertTrue(record.getData().isObject());
    assertEquals(record.getData().size(), 0);
    assertTrue(record.getMetadata().isObject());
    assertEquals(record.getMetadata().size(), 0);
    // No messages.
    assertTrue(context.getMessageList().isEmpty());
}
Also used : HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) Test(org.testng.annotations.Test)

Example 5 with DynamoUpload2

use of org.sagebionetworks.bridge.dynamodb.DynamoUpload2 in project BridgeServer2 by Sage-Bionetworks.

the class UploadControllerTest method getUploadByRecordIdRejectsAppAdmin.

@Test(expectedExceptions = UnauthorizedException.class, expectedExceptionsMessageRegExp = ".*Caller does not have permission to access upload.*")
public void getUploadByRecordIdRejectsAppAdmin() throws Exception {
    doReturn(mockResearcherSession).when(controller).getAuthenticatedSession(DEVELOPER, WORKER);
    doReturn(true).when(mockResearcherSession).isInRole(ADMIN);
    doReturn(TEST_USER_ID).when(mockResearcherSession).getId();
    when(mockResearcherSession.getAppId()).thenReturn(TEST_APP_ID);
    HealthDataRecord record = HealthDataRecord.create();
    record.setAppId(TEST_APP_ID);
    record.setUploadId(UPLOAD_ID);
    record.setHealthCode(HEALTH_CODE);
    when(mockHealthDataService.getRecordById("record-id")).thenReturn(record);
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setAppId("researcher-app-id");
    upload.setCompletedBy(UploadCompletionClient.S3_WORKER);
    UploadView uploadView = new UploadView.Builder().withUpload(upload).withHealthDataRecord(record).build();
    when(mockUploadService.getUploadView(UPLOAD_ID)).thenReturn(uploadView);
    controller.getUpload("recordId:record-id");
}
Also used : UploadView(org.sagebionetworks.bridge.models.upload.UploadView) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) Test(org.testng.annotations.Test)

Aggregations

DynamoUpload2 (org.sagebionetworks.bridge.dynamodb.DynamoUpload2)48 Test (org.testng.annotations.Test)42 HealthDataRecord (org.sagebionetworks.bridge.models.healthdata.HealthDataRecord)16 UploadRequest (org.sagebionetworks.bridge.models.upload.UploadRequest)12 UploadView (org.sagebionetworks.bridge.models.upload.UploadView)10 Upload (org.sagebionetworks.bridge.models.upload.Upload)9 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)5 UploadValidationStatus (org.sagebionetworks.bridge.models.upload.UploadValidationStatus)5 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)3 File (java.io.File)3 URL (java.net.URL)3 InMemoryFileHelper (org.sagebionetworks.bridge.file.InMemoryFileHelper)3 App (org.sagebionetworks.bridge.models.apps.App)3 UploadSession (org.sagebionetworks.bridge.models.upload.UploadSession)3 GeneratePresignedUrlRequest (com.amazonaws.services.s3.model.GeneratePresignedUrlRequest)2 Date (java.util.Date)2 DateTime (org.joda.time.DateTime)2