Search in sources :

Example 6 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException in project workbench by all-of-us.

the class CohortAnnotationDefinitionControllerTest method updateCohortAnnotationDefinition_NameConflict.

@Test
public void updateCohortAnnotationDefinition_NameConflict() throws Exception {
    String namespace = "aou-test";
    String name = "test";
    long cohortId = 1;
    long workspaceId = 1;
    long annotationDefinitionId = 1;
    final String columnName = "new-name";
    Cohort cohort = createCohort(workspaceId);
    Workspace workspace = createWorkspace(namespace, name, workspaceId);
    ModifyCohortAnnotationDefinitionRequest request = new ModifyCohortAnnotationDefinitionRequest();
    request.setColumnName(columnName);
    org.pmiops.workbench.db.model.CohortAnnotationDefinition definition = createDBCohortAnnotationDefinition(cohortId, annotationDefinitionId, AnnotationType.STRING, "name1");
    WorkspaceAccessLevel owner = WorkspaceAccessLevel.OWNER;
    when(workspaceService.enforceWorkspaceAccessLevel(namespace, name, WorkspaceAccessLevel.WRITER)).thenReturn(owner);
    when(cohortDao.findOne(cohortId)).thenReturn(cohort);
    when(workspaceService.getRequired(namespace, name)).thenReturn(workspace);
    when(cohortAnnotationDefinitionDao.findByCohortIdAndCohortAnnotationDefinitionId(cohortId, annotationDefinitionId)).thenReturn(definition);
    when(cohortAnnotationDefinitionDao.findByCohortIdAndColumnName(cohortId, columnName)).thenReturn(definition);
    try {
        cohortAnnotationDefinitionController.updateCohortAnnotationDefinition(namespace, name, cohortId, annotationDefinitionId, request);
        fail("Should have thrown a ConflictException!");
    } catch (ConflictException e) {
        assertEquals("Conflict: Cohort Annotation Definition name exists for: " + columnName, e.getMessage());
    }
    verify(cohortDao, times(1)).findOne(cohortId);
    verify(workspaceService, times(1)).getRequired(namespace, name);
    verify(cohortAnnotationDefinitionDao, times(1)).findByCohortIdAndCohortAnnotationDefinitionId(cohortId, annotationDefinitionId);
    verify(cohortAnnotationDefinitionDao, times(1)).findByCohortIdAndColumnName(cohortId, columnName);
    verify(workspaceService).enforceWorkspaceAccessLevel(namespace, name, WorkspaceAccessLevel.WRITER);
    verifyNoMoreMockInteractions();
}
Also used : ModifyCohortAnnotationDefinitionRequest(org.pmiops.workbench.model.ModifyCohortAnnotationDefinitionRequest) Cohort(org.pmiops.workbench.db.model.Cohort) ConflictException(org.pmiops.workbench.exceptions.ConflictException) WorkspaceAccessLevel(org.pmiops.workbench.model.WorkspaceAccessLevel) Workspace(org.pmiops.workbench.db.model.Workspace) Test(org.junit.Test)

Example 7 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException in project workbench by all-of-us.

the class CohortAnnotationDefinitionController method createCohortAnnotationDefinition.

@Override
public ResponseEntity<CohortAnnotationDefinition> createCohortAnnotationDefinition(String workspaceNamespace, String workspaceId, Long cohortId, CohortAnnotationDefinition request) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.WRITER);
    Cohort cohort = findCohort(cohortId);
    // this validates that the user is in the proper workspace
    validateMatchingWorkspace(workspaceNamespace, workspaceId, cohort.getWorkspaceId());
    request.setCohortId(cohortId);
    org.pmiops.workbench.db.model.CohortAnnotationDefinition cohortAnnotationDefinition = FROM_CLIENT_COHORT_ANNOTATION_DEFINITION.apply(request);
    org.pmiops.workbench.db.model.CohortAnnotationDefinition existingDefinition = cohortAnnotationDefinitionDao.findByCohortIdAndColumnName(cohortId, request.getColumnName());
    if (existingDefinition != null) {
        throw new ConflictException(String.format("Conflict: Cohort Annotation Definition name exists for: %s", request.getColumnName()));
    }
    cohortAnnotationDefinition = cohortAnnotationDefinitionDao.save(cohortAnnotationDefinition);
    return ResponseEntity.ok(TO_CLIENT_COHORT_ANNOTATION_DEFINITION.apply(cohortAnnotationDefinition));
}
Also used : Cohort(org.pmiops.workbench.db.model.Cohort) ConflictException(org.pmiops.workbench.exceptions.ConflictException)

Example 8 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException in project workbench by all-of-us.

the class CohortAnnotationDefinitionController method updateCohortAnnotationDefinition.

@Override
public ResponseEntity<CohortAnnotationDefinition> updateCohortAnnotationDefinition(String workspaceNamespace, String workspaceId, Long cohortId, Long annotationDefinitionId, ModifyCohortAnnotationDefinitionRequest modifyCohortAnnotationDefinitionRequest) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.WRITER);
    String columnName = modifyCohortAnnotationDefinitionRequest.getColumnName();
    Cohort cohort = findCohort(cohortId);
    // this validates that the user is in the proper workspace
    validateMatchingWorkspace(workspaceNamespace, workspaceId, cohort.getWorkspaceId());
    org.pmiops.workbench.db.model.CohortAnnotationDefinition cohortAnnotationDefinition = findCohortAnnotationDefinition(cohortId, annotationDefinitionId);
    org.pmiops.workbench.db.model.CohortAnnotationDefinition existingDefinition = cohortAnnotationDefinitionDao.findByCohortIdAndColumnName(cohortId, columnName);
    if (existingDefinition != null) {
        throw new ConflictException(String.format("Conflict: Cohort Annotation Definition name exists for: %s", columnName));
    }
    cohortAnnotationDefinition.columnName(columnName);
    cohortAnnotationDefinition = cohortAnnotationDefinitionDao.save(cohortAnnotationDefinition);
    return ResponseEntity.ok(TO_CLIENT_COHORT_ANNOTATION_DEFINITION.apply(cohortAnnotationDefinition));
}
Also used : Cohort(org.pmiops.workbench.db.model.Cohort) ConflictException(org.pmiops.workbench.exceptions.ConflictException)

Example 9 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException in project workbench by all-of-us.

the class CohortsController method updateCohort.

@Override
public ResponseEntity<Cohort> updateCohort(String workspaceNamespace, String workspaceId, Long cohortId, Cohort cohort) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.WRITER);
    org.pmiops.workbench.db.model.Cohort dbCohort = getDbCohort(workspaceNamespace, workspaceId, cohortId);
    if (Strings.isNullOrEmpty(cohort.getEtag())) {
        throw new BadRequestException("missing required update field 'etag'");
    }
    int version = Etags.toVersion(cohort.getEtag());
    if (dbCohort.getVersion() != version) {
        throw new ConflictException("Attempted to modify outdated cohort version");
    }
    if (cohort.getType() != null) {
        dbCohort.setType(cohort.getType());
    }
    if (cohort.getName() != null) {
        dbCohort.setName(cohort.getName());
    }
    if (cohort.getDescription() != null) {
        dbCohort.setDescription(cohort.getDescription());
    }
    if (cohort.getCriteria() != null) {
        dbCohort.setCriteria(cohort.getCriteria());
    }
    Timestamp now = new Timestamp(clock.instant().toEpochMilli());
    dbCohort.setLastModifiedTime(now);
    try {
        // The version asserted on save is the same as the one we read via
        // getRequired() above, see RW-215 for details.
        dbCohort = cohortDao.save(dbCohort);
    } catch (OptimisticLockException e) {
        log.log(Level.WARNING, "version conflict for cohort update", e);
        throw new ConflictException("Failed due to concurrent cohort modification");
    }
    return ResponseEntity.ok(TO_CLIENT_COHORT.apply(dbCohort));
}
Also used : ConflictException(org.pmiops.workbench.exceptions.ConflictException) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) OptimisticLockException(javax.persistence.OptimisticLockException) Timestamp(java.sql.Timestamp)

Example 10 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException in project workbench by all-of-us.

the class ProfileController method initializeUserIfNeeded.

private User initializeUserIfNeeded() {
    UserAuthentication userAuthentication = userAuthenticationProvider.get();
    User user = userAuthentication.getUser();
    if (userAuthentication.getUserType() == UserType.SERVICE_ACCOUNT) {
        // Service accounts don't need further initialization.
        return user;
    }
    // On first sign-in, create a FC user, billing project, and set the first sign in time.
    if (user.getFirstSignInTime() == null) {
        // instead use the freeTierBillingProjectStatus.
        if (user.getFreeTierBillingProjectName() == null) {
            String billingProjectName = createFirecloudUserAndBillingProject(user);
            user.setFreeTierBillingProjectName(billingProjectName);
            user.setFreeTierBillingProjectStatus(BillingProjectStatus.PENDING);
        }
        user.setFirstSignInTime(new Timestamp(clock.instant().toEpochMilli()));
        try {
            return userDao.save(user);
        } catch (ObjectOptimisticLockingFailureException e) {
            log.log(Level.WARNING, "version conflict for user update", e);
            throw new ConflictException("Failed due to concurrent modification");
        }
    }
    // Free tier billing project setup is complete; nothing to do.
    if (BillingProjectStatus.READY.equals(user.getFreeTierBillingProjectStatus())) {
        return user;
    }
    // On subsequent sign-ins to the first, attempt to complete the setup of the FC billing project
    // and mark the Workbench's project setup as completed. FC project creation is asynchronous, so
    // first confirm whether Firecloud claims the project setup is complete.
    BillingProjectStatus status = null;
    try {
        status = fireCloudService.getBillingProjectMemberships().stream().filter(m -> user.getFreeTierBillingProjectName().equals(m.getProjectName())).map(m -> fcToWorkbenchBillingMap.get(m.getCreationStatus())).findFirst().orElse(BillingProjectStatus.NONE);
    } catch (ApiException e) {
        log.log(Level.WARNING, "failed to retrieve billing projects, continuing", e);
        return user;
    }
    switch(status) {
        case NONE:
        case PENDING:
            log.log(Level.INFO, "free tier project is still initializing, continuing");
            return user;
        case ERROR:
            log.log(Level.SEVERE, String.format("free tier project %s failed to be created", user.getFreeTierBillingProjectName()));
            user.setFreeTierBillingProjectStatus(status);
            return userDao.save(user);
        case READY:
            break;
        default:
            log.log(Level.SEVERE, String.format("unrecognized status '%s'", status));
            return user;
    }
    // notebooks.
    try {
        fireCloudService.grantGoogleRoleToUser(user.getFreeTierBillingProjectName(), FireCloudService.BIGQUERY_JOB_USER_GOOGLE_ROLE, user.getEmail());
    } catch (ApiException e) {
        log.log(Level.WARNING, "granting BigQuery role on created free tier billing project failed", e);
        // Allow the user to continue, as most workbench functionality will still be usable.
        return user;
    }
    log.log(Level.INFO, "free tier project initialized and BigQuery role granted");
    user.setFreeTierBillingProjectStatus(BillingProjectStatus.READY);
    return userDao.save(user);
}
Also used : Message(javax.mail.Message) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException) IdVerificationListResponse(org.pmiops.workbench.model.IdVerificationListResponse) Provider(javax.inject.Provider) MailChimpService(org.pmiops.workbench.mailchimp.MailChimpService) FireCloudService(org.pmiops.workbench.firecloud.FireCloudService) MessagingException(javax.mail.MessagingException) Autowired(org.springframework.beans.factory.annotation.Autowired) EmailException(org.pmiops.workbench.exceptions.EmailException) DirectoryService(org.pmiops.workbench.google.DirectoryService) Authority(org.pmiops.workbench.model.Authority) CreateAccountRequest(org.pmiops.workbench.model.CreateAccountRequest) Map(java.util.Map) CloudStorageService(org.pmiops.workbench.google.CloudStorageService) User(org.pmiops.workbench.db.model.User) Profile(org.pmiops.workbench.model.Profile) UserService(org.pmiops.workbench.db.dao.UserService) UserDao(org.pmiops.workbench.db.dao.UserDao) Transport(javax.mail.Transport) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) AuthorityRequired(org.pmiops.workbench.annotations.AuthorityRequired) InvitationVerificationRequest(org.pmiops.workbench.model.InvitationVerificationRequest) ConflictException(org.pmiops.workbench.exceptions.ConflictException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) List(java.util.List) Address(com.blockscore.models.Address) BillingProjectStatus(org.pmiops.workbench.model.BillingProjectStatus) WorkbenchEnvironment(org.pmiops.workbench.config.WorkbenchEnvironment) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserType(org.pmiops.workbench.auth.UserAuthentication.UserType) BillingProjectMembership(org.pmiops.workbench.model.BillingProjectMembership) UsernameTakenResponse(org.pmiops.workbench.model.UsernameTakenResponse) ApiException(org.pmiops.workbench.firecloud.ApiException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) InternetAddress(javax.mail.internet.InternetAddress) ProfileService(org.pmiops.workbench.auth.ProfileService) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ExceptionUtils(org.pmiops.workbench.exceptions.ExceptionUtils) Properties(java.util.Properties) IdVerificationRequest(org.pmiops.workbench.model.IdVerificationRequest) IdVerificationReviewRequest(org.pmiops.workbench.model.IdVerificationReviewRequest) BlockscoreIdVerificationStatus(org.pmiops.workbench.model.BlockscoreIdVerificationStatus) IOException(java.io.IOException) MimeMessage(javax.mail.internet.MimeMessage) BlockscoreService(org.pmiops.workbench.blockscore.BlockscoreService) UserAuthentication(org.pmiops.workbench.auth.UserAuthentication) HttpStatus(org.springframework.http.HttpStatus) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) WorkbenchConfig(org.pmiops.workbench.config.WorkbenchConfig) Clock(java.time.Clock) Session(javax.mail.Session) EmailVerificationStatus(org.pmiops.workbench.model.EmailVerificationStatus) Person(com.blockscore.models.Person) ResponseEntity(org.springframework.http.ResponseEntity) CreationStatusEnum(org.pmiops.workbench.firecloud.model.BillingProjectMembership.CreationStatusEnum) BillingProjectStatus(org.pmiops.workbench.model.BillingProjectStatus) User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) UserAuthentication(org.pmiops.workbench.auth.UserAuthentication) Timestamp(java.sql.Timestamp) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException) ApiException(org.pmiops.workbench.firecloud.ApiException)

Aggregations

ConflictException (org.pmiops.workbench.exceptions.ConflictException)13 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)6 Workspace (org.pmiops.workbench.model.Workspace)5 Timestamp (java.sql.Timestamp)4 Test (org.junit.Test)4 Cohort (org.pmiops.workbench.db.model.Cohort)4 User (org.pmiops.workbench.db.model.User)4 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)3 ServerErrorException (org.pmiops.workbench.exceptions.ServerErrorException)2 ApiException (org.pmiops.workbench.firecloud.ApiException)2 UserRole (org.pmiops.workbench.model.UserRole)2 Address (com.blockscore.models.Address)1 Person (com.blockscore.models.Person)1 Blob (com.google.cloud.storage.Blob)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1