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();
}
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));
}
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));
}
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));
}
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);
}
Aggregations