Search in sources :

Example 1 with SurveyService

use of org.sagebionetworks.bridge.services.SurveyService in project BridgeServer2 by Sage-Bionetworks.

the class IosSchemaValidationHandler2GetSchemaTest method surveyWithNoSchemaRev.

@Test
public void surveyWithNoSchemaRev() {
    // mock survey service
    DynamoSurvey survey = new DynamoSurvey();
    survey.setIdentifier("test-survey");
    SurveyService mockSurveyService = mock(SurveyService.class);
    when(mockSurveyService.getSurvey(eq(TEST_APP_ID), eq(new GuidCreatedOnVersionHolderImpl("test-guid", TEST_SURVEY_CREATED_ON_MILLIS)), eq(false), eq(true))).thenReturn(survey);
    // set up test handler
    IosSchemaValidationHandler2 handler = new IosSchemaValidationHandler2();
    handler.setDefaultSchemaRevisionMap(DEFAULT_SCHEMA_REV_MAP);
    handler.setSurveyService(mockSurveyService);
    // make input
    ObjectNode infoJson = BridgeObjectMapper.get().createObjectNode();
    infoJson.put("surveyGuid", "test-guid");
    infoJson.put("surveyCreatedOn", TEST_SURVEY_CREATED_ON_STRING);
    // Execute. Returns null.
    UploadSchema retVal = handler.getUploadSchema(TEST_APP_ID, infoJson);
    assertNull(retVal);
}
Also used : GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamoSurvey(org.sagebionetworks.bridge.dynamodb.DynamoSurvey) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) SurveyService(org.sagebionetworks.bridge.services.SurveyService) Test(org.testng.annotations.Test)

Example 2 with SurveyService

use of org.sagebionetworks.bridge.services.SurveyService in project BridgeServer2 by Sage-Bionetworks.

the class IosSchemaValidationHandler2GetSchemaTest method surveySchemaNotFound.

@Test
public void surveySchemaNotFound() {
    // Mock survey service.
    DynamoSurvey survey = new DynamoSurvey();
    survey.setIdentifier("test-survey");
    survey.setSchemaRevision(4);
    SurveyService mockSurveyService = mock(SurveyService.class);
    when(mockSurveyService.getSurvey(eq(TEST_APP_ID), eq(new GuidCreatedOnVersionHolderImpl("test-guid", TEST_SURVEY_CREATED_ON_MILLIS)), eq(false), eq(true))).thenReturn(survey);
    // Set up test handler
    IosSchemaValidationHandler2 handler = new IosSchemaValidationHandler2();
    handler.setDefaultSchemaRevisionMap(DEFAULT_SCHEMA_REV_MAP);
    handler.setSurveyService(mockSurveyService);
    handler.setUploadSchemaService(mock(UploadSchemaService.class));
    // Make input.
    ObjectNode infoJson = BridgeObjectMapper.get().createObjectNode();
    infoJson.put("surveyGuid", "test-guid");
    infoJson.put("surveyCreatedOn", TEST_SURVEY_CREATED_ON_STRING);
    // Execute. Returns null.
    UploadSchema retVal = handler.getUploadSchema(TEST_APP_ID, infoJson);
    assertNull(retVal);
}
Also used : GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamoSurvey(org.sagebionetworks.bridge.dynamodb.DynamoSurvey) UploadSchemaService(org.sagebionetworks.bridge.services.UploadSchemaService) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) SurveyService(org.sagebionetworks.bridge.services.SurveyService) Test(org.testng.annotations.Test)

Example 3 with SurveyService

use of org.sagebionetworks.bridge.services.SurveyService in project BridgeServer2 by Sage-Bionetworks.

the class IosSchemaValidationHandler2GetSchemaTest method survey.

@Test
public void survey() {
    // mock survey service
    DynamoSurvey survey = new DynamoSurvey();
    survey.setIdentifier("test-survey");
    survey.setSchemaRevision(4);
    SurveyService mockSurveyService = mock(SurveyService.class);
    when(mockSurveyService.getSurvey(eq(TEST_APP_ID), eq(new GuidCreatedOnVersionHolderImpl("test-guid", TEST_SURVEY_CREATED_ON_MILLIS)), eq(false), eq(true))).thenReturn(survey);
    // mock upload schema service
    UploadSchema dummySchema = UploadSchema.create();
    UploadSchemaService mockSchemaSvc = mock(UploadSchemaService.class);
    when(mockSchemaSvc.getUploadSchemaByIdAndRevNoThrow(TEST_APP_ID, "test-survey", 4)).thenReturn(dummySchema);
    // set up test handler
    IosSchemaValidationHandler2 handler = new IosSchemaValidationHandler2();
    handler.setDefaultSchemaRevisionMap(DEFAULT_SCHEMA_REV_MAP);
    handler.setSurveyService(mockSurveyService);
    handler.setUploadSchemaService(mockSchemaSvc);
    // make input
    ObjectNode infoJson = BridgeObjectMapper.get().createObjectNode();
    infoJson.put("surveyGuid", "test-guid");
    infoJson.put("surveyCreatedOn", TEST_SURVEY_CREATED_ON_STRING);
    // execute and validate
    UploadSchema retVal = handler.getUploadSchema(TEST_APP_ID, infoJson);
    assertSame(retVal, dummySchema);
}
Also used : GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamoSurvey(org.sagebionetworks.bridge.dynamodb.DynamoSurvey) UploadSchemaService(org.sagebionetworks.bridge.services.UploadSchemaService) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) SurveyService(org.sagebionetworks.bridge.services.SurveyService) Test(org.testng.annotations.Test)

Example 4 with SurveyService

use of org.sagebionetworks.bridge.services.SurveyService in project BridgeServer2 by Sage-Bionetworks.

the class UploadHandlersEndToEndTest method test.

private void test(UploadSchema schema, Survey survey, Map<String, String> fileMap, String nonZippedFileContent) throws Exception {
    // Set up zip service.
    UploadArchiveService unzipService = new UploadArchiveService();
    if (upload.isZipped()) {
        // fileMap is in strings. Convert to bytes so we can use the Zipper.
        Map<String, byte[]> fileBytesMap = new HashMap<>();
        for (Map.Entry<String, String> oneFile : fileMap.entrySet()) {
            String filename = oneFile.getKey();
            String fileString = oneFile.getValue();
            fileBytesMap.put(filename, fileString.getBytes(Charsets.UTF_8));
        }
        // Add metadata.json to the fileBytesMap.
        fileBytesMap.put("metadata.json", METADATA_JSON_CONTENT);
        // For zipping, we use the real service.
        unzipService.setMaxNumZipEntries(1000000);
        unzipService.setMaxZipEntrySize(1000000);
        rawFile = unzipService.zip(fileBytesMap);
    } else {
        // If it's not zipped, use the nonZippedFileContent as the raw file.
        rawFile = nonZippedFileContent.getBytes(Charsets.UTF_8);
    }
    // Set up UploadFileHelper
    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);
    UploadFileHelper uploadFileHelper = new UploadFileHelper();
    uploadFileHelper.setFileHelper(inMemoryFileHelper);
    uploadFileHelper.setMd5DigestUtils(mockMd5DigestUtils);
    uploadFileHelper.setS3Helper(mockS3UploadHelper);
    // set up S3DownloadHandler - mock S3 Helper
    // "S3" returns file unencrypted for simplicity of testing
    S3Helper mockS3DownloadHelper = mock(S3Helper.class);
    doAnswer(invocation -> {
        File destFile = invocation.getArgument(2);
        inMemoryFileHelper.writeBytes(destFile, rawFile);
        // Required return
        return null;
    }).when(mockS3DownloadHelper).downloadS3File(eq(TestConstants.UPLOAD_BUCKET), eq(UPLOAD_ID), any());
    S3DownloadHandler s3DownloadHandler = new S3DownloadHandler();
    s3DownloadHandler.setFileHelper(inMemoryFileHelper);
    s3DownloadHandler.setS3Helper(mockS3DownloadHelper);
    // set up DecryptHandler - For ease of tests, this will just return the input verbatim.
    UploadArchiveService mockUploadArchiveService = mock(UploadArchiveService.class);
    when(mockUploadArchiveService.decrypt(eq(TEST_APP_ID), any(InputStream.class))).thenAnswer(invocation -> invocation.getArgument(1));
    DecryptHandler decryptHandler = new DecryptHandler();
    decryptHandler.setFileHelper(inMemoryFileHelper);
    decryptHandler.setUploadArchiveService(mockUploadArchiveService);
    // Set up UnzipHandler
    UnzipHandler unzipHandler = new UnzipHandler();
    unzipHandler.setFileHelper(inMemoryFileHelper);
    unzipHandler.setUploadArchiveService(unzipService);
    // Set up InitRecordHandler
    InitRecordHandler initRecordHandler = new InitRecordHandler();
    initRecordHandler.setFileHelper(inMemoryFileHelper);
    // mock schema service
    UploadSchemaService mockUploadSchemaService = mock(UploadSchemaService.class);
    if (schema != null) {
        when(mockUploadSchemaService.getUploadSchemaByIdAndRev(TEST_APP_ID, schema.getSchemaId(), schema.getRevision())).thenReturn(schema);
        when(mockUploadSchemaService.getUploadSchemaByIdAndRevNoThrow(TEST_APP_ID, schema.getSchemaId(), schema.getRevision())).thenReturn(schema);
    }
    // mock survey service
    SurveyService mockSurveyService = mock(SurveyService.class);
    if (survey != null) {
        when(mockSurveyService.getSurvey(TEST_APP_ID, new GuidCreatedOnVersionHolderImpl(survey.getGuid(), survey.getCreatedOn()), false, true)).thenReturn(survey);
    }
    // set up IosSchemaValidationHandler
    IosSchemaValidationHandler2 iosSchemaValidationHandler = new IosSchemaValidationHandler2();
    iosSchemaValidationHandler.setFileHelper(inMemoryFileHelper);
    iosSchemaValidationHandler.setUploadFileHelper(uploadFileHelper);
    iosSchemaValidationHandler.setUploadSchemaService(mockUploadSchemaService);
    iosSchemaValidationHandler.setSurveyService(mockSurveyService);
    // set up GenericUploadFormatHandler
    GenericUploadFormatHandler genericUploadFormatHandler = new GenericUploadFormatHandler();
    genericUploadFormatHandler.setFileHelper(inMemoryFileHelper);
    genericUploadFormatHandler.setUploadFileHelper(uploadFileHelper);
    genericUploadFormatHandler.setUploadSchemaService(mockUploadSchemaService);
    genericUploadFormatHandler.setSurveyService(mockSurveyService);
    // set up UploadFormatHandler
    UploadFormatHandler uploadFormatHandler = new UploadFormatHandler();
    uploadFormatHandler.setV1LegacyHandler(iosSchemaValidationHandler);
    uploadFormatHandler.setV2GenericHandler(genericUploadFormatHandler);
    // set up StrictValidationHandler
    StrictValidationHandler strictValidationHandler = new StrictValidationHandler();
    strictValidationHandler.setUploadSchemaService(mockUploadSchemaService);
    AppService mockAppService = mock(AppService.class);
    when(mockAppService.getApp(TEST_APP_ID)).thenReturn(APP);
    strictValidationHandler.setAppService(mockAppService);
    Enrollment enrollment = Enrollment.create(TEST_APP_ID, "test-study", "userId", EXTERNAL_ID);
    // set up TranscribeConsentHandler
    Account account = Account.create();
    account.setDataGroups(ImmutableSet.of("parkinson", "test_user"));
    account.setSharingScope(SharingScope.SPONSORS_AND_PARTNERS);
    account.setEnrollments(ImmutableSet.of(enrollment));
    AccountService mockAccountService = mock(AccountService.class);
    when(mockAccountService.getAccount(any())).thenReturn(Optional.of(account));
    ParticipantService mockParticipantService = mock(ParticipantService.class);
    when(mockParticipantService.getStudyStartTime(any())).thenReturn(STUDY_START_TIME);
    TranscribeConsentHandler transcribeConsentHandler = new TranscribeConsentHandler();
    transcribeConsentHandler.setAccountService(mockAccountService);
    transcribeConsentHandler.setParticipantService(mockParticipantService);
    // Set up UploadRawZipHandler.
    UploadRawZipHandler uploadRawZipHandler = new UploadRawZipHandler();
    uploadRawZipHandler.setUploadFileHelper(uploadFileHelper);
    // set up UploadArtifactsHandler
    UploadArtifactsHandler uploadArtifactsHandler = new UploadArtifactsHandler();
    uploadArtifactsHandler.setHealthDataService(mockHealthDataService);
    // set up task factory
    List<UploadValidationHandler> handlerList = ImmutableList.of(s3DownloadHandler, decryptHandler, unzipHandler, initRecordHandler, uploadFormatHandler, strictValidationHandler, transcribeConsentHandler, uploadRawZipHandler, uploadArtifactsHandler);
    UploadValidationTaskFactory taskFactory = new UploadValidationTaskFactory();
    taskFactory.setFileHelper(inMemoryFileHelper);
    taskFactory.setHandlerList(handlerList);
    taskFactory.setUploadDao(mockUploadDao);
    taskFactory.setHealthDataService(mockHealthDataService);
    // create task, execute
    UploadValidationTask task = taskFactory.newTask(TEST_APP_ID, upload);
    task.run();
}
Also used : GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) Account(org.sagebionetworks.bridge.models.accounts.Account) ParticipantService(org.sagebionetworks.bridge.services.ParticipantService) HashMap(java.util.HashMap) Enrollment(org.sagebionetworks.bridge.models.studies.Enrollment) SurveyService(org.sagebionetworks.bridge.services.SurveyService) UploadArchiveService(org.sagebionetworks.bridge.services.UploadArchiveService) AppService(org.sagebionetworks.bridge.services.AppService) UploadSchemaService(org.sagebionetworks.bridge.services.UploadSchemaService) InputStream(java.io.InputStream) DigestUtils(org.apache.commons.codec.digest.DigestUtils) S3Helper(org.sagebionetworks.bridge.s3.S3Helper) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) File(java.io.File) AccountService(org.sagebionetworks.bridge.services.AccountService)

Example 5 with SurveyService

use of org.sagebionetworks.bridge.services.SurveyService in project BridgeServer2 by Sage-Bionetworks.

the class IosSchemaValidationHandler2GetSchemaTest method surveyWithNoIdentifier.

@Test
public void surveyWithNoIdentifier() {
    // mock survey service
    DynamoSurvey survey = new DynamoSurvey();
    survey.setSchemaRevision(4);
    SurveyService mockSurveyService = mock(SurveyService.class);
    when(mockSurveyService.getSurvey(eq(TEST_APP_ID), eq(new GuidCreatedOnVersionHolderImpl("test-guid", TEST_SURVEY_CREATED_ON_MILLIS)), eq(false), eq(true))).thenReturn(survey);
    // set up test handler
    IosSchemaValidationHandler2 handler = new IosSchemaValidationHandler2();
    handler.setDefaultSchemaRevisionMap(DEFAULT_SCHEMA_REV_MAP);
    handler.setSurveyService(mockSurveyService);
    // make input
    ObjectNode infoJson = BridgeObjectMapper.get().createObjectNode();
    infoJson.put("surveyGuid", "test-guid");
    infoJson.put("surveyCreatedOn", TEST_SURVEY_CREATED_ON_STRING);
    // Execute. Returns null.
    UploadSchema retVal = handler.getUploadSchema(TEST_APP_ID, infoJson);
    assertNull(retVal);
}
Also used : GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DynamoSurvey(org.sagebionetworks.bridge.dynamodb.DynamoSurvey) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) SurveyService(org.sagebionetworks.bridge.services.SurveyService) Test(org.testng.annotations.Test)

Aggregations

GuidCreatedOnVersionHolderImpl (org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl)5 SurveyService (org.sagebionetworks.bridge.services.SurveyService)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 DynamoSurvey (org.sagebionetworks.bridge.dynamodb.DynamoSurvey)4 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)4 Test (org.testng.annotations.Test)4 UploadSchemaService (org.sagebionetworks.bridge.services.UploadSchemaService)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DigestUtils (org.apache.commons.codec.digest.DigestUtils)1 Account (org.sagebionetworks.bridge.models.accounts.Account)1 Enrollment (org.sagebionetworks.bridge.models.studies.Enrollment)1 S3Helper (org.sagebionetworks.bridge.s3.S3Helper)1 AccountService (org.sagebionetworks.bridge.services.AccountService)1 AppService (org.sagebionetworks.bridge.services.AppService)1 ParticipantService (org.sagebionetworks.bridge.services.ParticipantService)1 UploadArchiveService (org.sagebionetworks.bridge.services.UploadArchiveService)1