<commit_before>package org.sagebionetworks.bridge.play.controllers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.bridge.Roles.RESEARCHER;
import static org.sagebionetworks.bridge.Roles.ADMIN;

import java.net.URL;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import play.mvc.Result;
import play.test.Helpers;

import org.sagebionetworks.bridge.Roles;
import org.sagebionetworks.bridge.TestUtils;
import org.sagebionetworks.bridge.cache.CacheProvider;
import org.sagebionetworks.bridge.dao.HealthCodeDao;
import org.sagebionetworks.bridge.dynamodb.DynamoHealthDataRecord;
import org.sagebionetworks.bridge.dynamodb.DynamoUpload2;
import org.sagebionetworks.bridge.exceptions.EntityNotFoundException;
import org.sagebionetworks.bridge.exceptions.UnauthorizedException;
import org.sagebionetworks.bridge.json.BridgeObjectMapper;
import org.sagebionetworks.bridge.models.Metrics;
import org.sagebionetworks.bridge.models.RequestInfo;
import org.sagebionetworks.bridge.models.accounts.StudyParticipant;
import org.sagebionetworks.bridge.models.accounts.UserSession;
import org.sagebionetworks.bridge.models.healthdata.HealthDataRecord;
import org.sagebionetworks.bridge.models.studies.StudyIdentifierImpl;
import org.sagebionetworks.bridge.models.upload.Upload;
import org.sagebionetworks.bridge.models.upload.UploadCompletionClient;
import org.sagebionetworks.bridge.models.upload.UploadSession;
import org.sagebionetworks.bridge.models.upload.UploadStatus;
import org.sagebionetworks.bridge.models.upload.UploadValidationStatus;
import org.sagebionetworks.bridge.models.upload.UploadView;
import org.sagebionetworks.bridge.services.HealthDataService;
import org.sagebionetworks.bridge.services.UploadService;

@RunWith(MockitoJUnitRunner.class)
public class UploadControllerTest {
    private static final String HEALTH_CODE = "health-code";
    private static final String RECORD_ID = "record-id";
    private static final String UPLOAD_ID = "upload-id";
    private static final String VALIDATION_ERROR_MESSAGE = "There was a validation error";

    @Spy
    private UploadController controller;
    
    @Mock
    private UploadService uploadService;
    
    @Mock
    private HealthCodeDao healthCodeDao;
    
    @Mock
    private HealthDataService healthDataService;
    
    @Mock
    private UserSession workerSession;
    
    @Mock
    private UserSession consentedUserSession;
    
    @Mock
    private UserSession otherUserSession;
    
    @Mock
    private UserSession researcherSession;
    
    @Mock
    private CacheProvider cacheProvider;
    
    @Mock 
    private Metrics metrics;
    
    @Captor
    private ArgumentCaptor<Upload> uploadCaptor;
    
    @Captor
    private ArgumentCaptor<RequestInfo> requestInfoCaptor;
    
    @Before
    public void before() {
        controller.setUploadService(uploadService);
        controller.setHealthCodeDao(healthCodeDao);
        controller.setCacheProvider(cacheProvider);
        controller.setHealthDataService(healthDataService);

        // mock uploadService.getUpload()
        DynamoUpload2 upload = new DynamoUpload2();
        upload.setHealthCode("consented-user-health-code");
        upload.setUploadId("upload-id");
        doReturn(upload).when(uploadService).getUpload("upload-id");

        // mock uploadService.get/pollUploadValidationStatus()
        // mock UploadService with validation status
        HealthDataRecord record = HealthDataRecord.create();
        record.setId(RECORD_ID);
        record.setHealthCode(HEALTH_CODE);

        UploadValidationStatus status = new UploadValidationStatus.Builder()
                .withId(UPLOAD_ID)
                .withRecord(record)
                .withMessageList(Lists.newArrayList(VALIDATION_ERROR_MESSAGE))
                .withStatus(UploadStatus.VALIDATION_FAILED).build();

        doReturn(status).when(uploadService).getUploadValidationStatus(UPLOAD_ID);
        doReturn(status).when(uploadService).pollUploadValidationStatusUntilComplete(UPLOAD_ID);

        // mock metrics
        doReturn(metrics).when(controller).getMetrics();

        // mock sessions
        doReturn("worker-health-code").when(workerSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("worker-study-id")).when(workerSession).getStudyIdentifier();
        doReturn(true).when(workerSession).isInRole(Roles.WORKER);
        StudyParticipant participant = new StudyParticipant.Builder().withRoles(Sets.newHashSet(Roles.WORKER)).build();
        doReturn(participant).when(workerSession).getParticipant();
        
        doReturn("consented-user-health-code").when(consentedUserSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("consented-user-study-id")).when(consentedUserSession).getStudyIdentifier();
        doReturn(true).when(consentedUserSession).isAuthenticated();
        doReturn(true).when(consentedUserSession).doesConsent();
        doReturn("userId").when(consentedUserSession).getId();
        doReturn(new StudyParticipant.Builder().build()).when(consentedUserSession).getParticipant();

        doReturn("researcher-health-code").when(researcherSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("researcher-study-id")).when(researcherSession).getStudyIdentifier();
        doReturn(true).when(researcherSession).isInRole(Roles.RESEARCHER);
        doReturn(true).when(researcherSession).isAuthenticated();
        doReturn(false).when(researcherSession).doesConsent();

        doReturn("other-user-health-code").when(otherUserSession).getHealthCode();
        participant = new StudyParticipant.Builder().withRoles(Sets.newHashSet()).build();
        doReturn(participant).when(otherUserSession).getParticipant();
        doReturn(true).when(otherUserSession).doesConsent();

        // mock healthCodeDao
        doReturn("worker-study-id").when(healthCodeDao).getStudyIdentifier("worker-health-code");
        doReturn("consented-user-study-id").when(healthCodeDao).getStudyIdentifier("consented-user-health-code");
    }
    
    @Test
    public void startingUploadRecordedInRequestInfo() throws Exception {
        doReturn(consentedUserSession).when(controller).getAuthenticatedAndConsentedSession();
        TestUtils.mockPlayContextWithJson(TestUtils.createJson(
            "{'name':'uploadName','contentLength':100,'contentMd5':'abc','contentType':'application/zip'}"));
        <commit_msg>Removing trivial whitespace change<commit_after>package org.sagebionetworks.bridge.play.controllers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.bridge.Roles.RESEARCHER;
import static org.sagebionetworks.bridge.Roles.ADMIN;

import java.net.URL;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import play.mvc.Result;
import play.test.Helpers;

import org.sagebionetworks.bridge.Roles;
import org.sagebionetworks.bridge.TestUtils;
import org.sagebionetworks.bridge.cache.CacheProvider;
import org.sagebionetworks.bridge.dao.HealthCodeDao;
import org.sagebionetworks.bridge.dynamodb.DynamoHealthDataRecord;
import org.sagebionetworks.bridge.dynamodb.DynamoUpload2;
import org.sagebionetworks.bridge.exceptions.EntityNotFoundException;
import org.sagebionetworks.bridge.exceptions.UnauthorizedException;
import org.sagebionetworks.bridge.json.BridgeObjectMapper;
import org.sagebionetworks.bridge.models.Metrics;
import org.sagebionetworks.bridge.models.RequestInfo;
import org.sagebionetworks.bridge.models.accounts.StudyParticipant;
import org.sagebionetworks.bridge.models.accounts.UserSession;
import org.sagebionetworks.bridge.models.healthdata.HealthDataRecord;
import org.sagebionetworks.bridge.models.studies.StudyIdentifierImpl;
import org.sagebionetworks.bridge.models.upload.Upload;
import org.sagebionetworks.bridge.models.upload.UploadCompletionClient;
import org.sagebionetworks.bridge.models.upload.UploadSession;
import org.sagebionetworks.bridge.models.upload.UploadStatus;
import org.sagebionetworks.bridge.models.upload.UploadValidationStatus;
import org.sagebionetworks.bridge.models.upload.UploadView;
import org.sagebionetworks.bridge.services.HealthDataService;
import org.sagebionetworks.bridge.services.UploadService;

@RunWith(MockitoJUnitRunner.class)
public class UploadControllerTest {
    private static final String HEALTH_CODE = "health-code";
    private static final String RECORD_ID = "record-id";
    private static final String UPLOAD_ID = "upload-id";
    private static final String VALIDATION_ERROR_MESSAGE = "There was a validation error";

    @Spy
    private UploadController controller;
    
    @Mock
    private UploadService uploadService;
    
    @Mock
    private HealthCodeDao healthCodeDao;
    
    @Mock
    private HealthDataService healthDataService;
    
    @Mock
    private UserSession workerSession;
    
    @Mock
    private UserSession consentedUserSession;
    
    @Mock
    private UserSession otherUserSession;
    
    @Mock
    private UserSession researcherSession;
    
    @Mock
    private CacheProvider cacheProvider;
    
    @Mock 
    private Metrics metrics;
    
    @Captor
    private ArgumentCaptor<Upload> uploadCaptor;
    
    @Captor
    private ArgumentCaptor<RequestInfo> requestInfoCaptor;
    
    @Before
    public void before() {
        controller.setUploadService(uploadService);
        controller.setHealthCodeDao(healthCodeDao);
        controller.setCacheProvider(cacheProvider);
        controller.setHealthDataService(healthDataService);

        // mock uploadService.getUpload()
        DynamoUpload2 upload = new DynamoUpload2();
        upload.setHealthCode("consented-user-health-code");
        upload.setUploadId("upload-id");
        doReturn(upload).when(uploadService).getUpload("upload-id");

        // mock uploadService.get/pollUploadValidationStatus()
        // mock UploadService with validation status
        HealthDataRecord record = HealthDataRecord.create();
        record.setId(RECORD_ID);
        record.setHealthCode(HEALTH_CODE);

        UploadValidationStatus status = new UploadValidationStatus.Builder()
                .withId(UPLOAD_ID)
                .withRecord(record)
                .withMessageList(Lists.newArrayList(VALIDATION_ERROR_MESSAGE))
                .withStatus(UploadStatus.VALIDATION_FAILED).build();

        doReturn(status).when(uploadService).getUploadValidationStatus(UPLOAD_ID);
        doReturn(status).when(uploadService).pollUploadValidationStatusUntilComplete(UPLOAD_ID);

        // mock metrics
        doReturn(metrics).when(controller).getMetrics();

        // mock sessions
        doReturn("worker-health-code").when(workerSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("worker-study-id")).when(workerSession).getStudyIdentifier();
        doReturn(true).when(workerSession).isInRole(Roles.WORKER);
        StudyParticipant participant = new StudyParticipant.Builder().withRoles(Sets.newHashSet(Roles.WORKER)).build();
        doReturn(participant).when(workerSession).getParticipant();
        
        doReturn("consented-user-health-code").when(consentedUserSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("consented-user-study-id")).when(consentedUserSession).getStudyIdentifier();
        doReturn(true).when(consentedUserSession).isAuthenticated();
        doReturn(true).when(consentedUserSession).doesConsent();
        doReturn("userId").when(consentedUserSession).getId();
        doReturn(new StudyParticipant.Builder().build()).when(consentedUserSession).getParticipant();
        
        doReturn("researcher-health-code").when(researcherSession).getHealthCode();
        doReturn(new StudyIdentifierImpl("researcher-study-id")).when(researcherSession).getStudyIdentifier();
        doReturn(true).when(researcherSession).isInRole(Roles.RESEARCHER);
        doReturn(true).when(researcherSession).isAuthenticated();
        doReturn(false).when(researcherSession).doesConsent();
        
        doReturn("other-user-health-code").when(otherUserSession).getHealthCode();
        participant = new StudyParticipant.Builder().withRoles(Sets.newHashSet()).build();
        doReturn(participant).when(otherUserSession).getParticipant();
        doReturn(true).when(otherUserSession).doesConsent();

        // mock healthCodeDao
        doReturn("worker-study-id").when(healthCodeDao).getStudyIdentifier("worker-health-code");
        doReturn("consented-user-study-id").when(healthCodeDao).getStudyIdentifier("consented-user-health-code");
    }
    
    @Test
    public void startingUploadRecordedInRequestInfo() throws Exception {
        doReturn(consentedUserSession).when(controller).getAuthenticatedAndConsentedSession();
        TestUtils.mockPlayContextWithJson(TestUtils.createJson(
            "{'name':'uploadName','contentLength':100,'contentMd5':'abc','contentType':'application/zip'}"));
        
{'fraction_non_alphanumeric': 0.10352314912096379, 'fraction_numerical': 0.0009805981648805771, 'mean_word_length': 4.555642023346303, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3635713', 'n_tokens_mistral': 4170, 'n_tokens_neox': 4097, 'n_words': 559}