Search in sources :

Example 1 with EntityAlreadyExistsException

use of org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppDao method createApp.

@Override
public App createApp(App app) {
    checkNotNull(app);
    checkArgument(app.getVersion() == null, "%s has a version; may not be new", "app");
    try {
        mapper.save(app);
    } catch (ConditionalCheckFailedException e) {
        // in the create scenario, this should be a hash key clash.
        throw new EntityAlreadyExistsException(App.class, "identifier", app.getIdentifier());
    }
    return app;
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) EntityAlreadyExistsException(org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 2 with EntityAlreadyExistsException

use of org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException in project BridgeServer2 by Sage-Bionetworks.

the class ConsentServiceTest method noConsentIfAlreadyConsented.

@Test
public void noConsentIfAlreadyConsented() {
    ConsentSignature sig = new ConsentSignature.Builder().withConsentCreatedOn(CONSENT_CREATED_ON).withSignedOn(DateTime.now().getMillis()).withName("A Name").withBirthdate("1960-10-10").build();
    account.setConsentSignatureHistory(SUBPOP_GUID, ImmutableList.of(sig));
    try {
        consentService.consentToResearch(app, SUBPOP_GUID, PARTICIPANT, CONSENT_SIGNATURE, SharingScope.NO_SHARING, false);
        fail("Exception expected.");
    } catch (EntityAlreadyExistsException e) {
        verify(accountService).getAccount(any());
        verifyNoMoreInteractions(accountService);
    }
}
Also used : ConsentSignature(org.sagebionetworks.bridge.models.subpopulations.ConsentSignature) EntityAlreadyExistsException(org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException) Test(org.testng.annotations.Test)

Example 3 with EntityAlreadyExistsException

use of org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException in project BridgeServer2 by Sage-Bionetworks.

the class SurveyService method createSurvey.

/**
 * Create a survey.
 */
public Survey createSurvey(Survey survey) {
    checkNotNull(survey, "Survey cannot be null");
    // Check survey ID uniqueness.
    if (isNotBlank(survey.getAppId()) && isNotBlank(survey.getIdentifier())) {
        String appId = survey.getAppId();
        String existingSurveyGuid = surveyDao.getSurveyGuidForIdentifier(appId, survey.getIdentifier());
        if (existingSurveyGuid != null) {
            String errMsg = "Survey identifier " + survey.getIdentifier() + " is already used by survey " + existingSurveyGuid;
            Map<String, Object> entityKeyMap = ImmutableMap.of(KEY_IDENTIFIER, survey.getIdentifier());
            throw new EntityAlreadyExistsException(Survey.class, errMsg, entityKeyMap);
        }
    }
    // Validate survey.
    survey.setGuid(BridgeUtils.generateGuid());
    for (SurveyElement element : survey.getElements()) {
        element.setGuid(BridgeUtils.generateGuid());
    }
    Set<String> dataGroups = Collections.emptySet();
    if (survey.getAppId() != null) {
        App app = appService.getApp(survey.getAppId());
        dataGroups = app.getDataGroups();
    }
    Validate.entityThrowingException(new SurveySaveValidator(dataGroups), survey);
    return surveyDao.createSurvey(survey);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) SurveySaveValidator(org.sagebionetworks.bridge.validators.SurveySaveValidator) EntityAlreadyExistsException(org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException) SurveyElement(org.sagebionetworks.bridge.models.surveys.SurveyElement)

Example 4 with EntityAlreadyExistsException

use of org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException in project BridgeServer2 by Sage-Bionetworks.

the class AuthenticationServiceTest method signUpExistingAccount.

@Test
public void signUpExistingAccount() {
    app.setPasswordPolicy(PasswordPolicy.DEFAULT_PASSWORD_POLICY);
    StudyParticipant participant = new StudyParticipant.Builder().withEmail(RECIPIENT_EMAIL).withPassword(PASSWORD).build();
    doThrow(new EntityAlreadyExistsException(Account.class, "userId", "user-id")).when(participantService).createParticipant(app, participant, true);
    service.signUp(app, participant);
    verify(participantService).createParticipant(eq(app), any(), eq(true));
    verify(accountWorkflowService).notifyAccountExists(eq(app), accountIdCaptor.capture());
    AccountId captured = accountIdCaptor.getValue();
    assertEquals(captured.getId(), "user-id");
    assertEquals(captured.getAppId(), TEST_APP_ID);
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) EntityAlreadyExistsException(org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 5 with EntityAlreadyExistsException

use of org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException in project BridgeServer2 by Sage-Bionetworks.

the class AuthenticationServiceTest method signUpExistingExternalId.

@Test
public void signUpExistingExternalId() {
    app.setPasswordPolicy(PasswordPolicy.DEFAULT_PASSWORD_POLICY);
    StudyParticipant participant = new StudyParticipant.Builder().withExternalId(EXTERNAL_ID).build();
    doThrow(new EntityAlreadyExistsException(ExternalIdentifier.class, "identifier", EXTERNAL_ID)).when(participantService).createParticipant(app, participant, true);
    service.signUp(app, participant);
    verify(participantService).createParticipant(eq(app), any(), eq(true));
    verify(accountWorkflowService).notifyAccountExists(eq(app), accountIdCaptor.capture());
    AccountId captured = accountIdCaptor.getValue();
    assertEquals(captured.getExternalId(), EXTERNAL_ID);
    assertEquals(captured.getAppId(), TEST_APP_ID);
}
Also used : AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) EntityAlreadyExistsException(org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException) ExternalIdentifier(org.sagebionetworks.bridge.models.accounts.ExternalIdentifier) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Aggregations

EntityAlreadyExistsException (org.sagebionetworks.bridge.exceptions.EntityAlreadyExistsException)23 Test (org.testng.annotations.Test)11 Account (org.sagebionetworks.bridge.models.accounts.Account)9 ConstraintViolationException (org.sagebionetworks.bridge.exceptions.ConstraintViolationException)7 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)6 PersistenceException (javax.persistence.PersistenceException)6 Enrollment (org.sagebionetworks.bridge.models.studies.Enrollment)4 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)3 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 DateTime (org.joda.time.DateTime)2 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)2 App (org.sagebionetworks.bridge.models.apps.App)2 Organization (org.sagebionetworks.bridge.models.organizations.Organization)2 Study (org.sagebionetworks.bridge.models.studies.Study)2 ConsentSignature (org.sagebionetworks.bridge.models.subpopulations.ConsentSignature)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OptimisticLockException (javax.persistence.OptimisticLockException)1