Search in sources :

Example 6 with EntityExistsException

use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.

the class ProjectEventServiceImplIT method testErrorThrownNoEvent.

@WithMockUser(username = "tom", password = "password1", roles = "ADMIN")
@Test
public void testErrorThrownNoEvent() {
    Project project = projectService.read(1L);
    Sample sample = sampleService.read(1L);
    try {
        projectService.addSampleToProject(project, sample, true);
        fail("EntityExistsException should have been thrown");
    } catch (EntityExistsException ex) {
    // it's all good
    }
    Page<ProjectEvent> eventsForProject = projectEventService.getEventsForProject(project, new PageRequest(0, 10));
    assertEquals("No event should be created", 0, eventsForProject.getTotalElements());
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) PageRequest(org.springframework.data.domain.PageRequest) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 7 with EntityExistsException

use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.

the class ProjectServiceImpl method addSampleToProject.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SEQUENCER') or (hasPermission(#project, 'isProjectOwner'))")
public ProjectSampleJoin addSampleToProject(Project project, Sample sample, boolean owner) {
    logger.trace("Adding sample to project.");
    // project already
    if (sampleRepository.getSampleBySampleName(project, sample.getSampleName()) != null) {
        throw new EntityExistsException("Sample with sequencer id '" + sample.getSampleName() + "' already exists in project " + project.getId());
    }
    // the relationshipRepository.
    if (sample.getId() == null) {
        logger.trace("Going to validate and persist sample prior to creating relationship.");
        // validate the sample, then persist it:
        Set<ConstraintViolation<Sample>> constraintViolations = validator.validate(sample);
        if (constraintViolations.isEmpty()) {
            sample = sampleRepository.save(sample);
        } else {
            throw new ConstraintViolationException(constraintViolations);
        }
    }
    ProjectSampleJoin join = new ProjectSampleJoin(project, sample, owner);
    try {
        return psjRepository.save(join);
    } catch (DataIntegrityViolationException e) {
        throw new EntityExistsException("Sample [" + sample.getId() + "] has already been added to project [" + project.getId() + "]");
    }
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with EntityExistsException

use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.

the class UserServiceImpl method translateConstraintViolationException.

/**
 * Translate {@link ConstraintViolationException} errors into an appropriate
 * {@link EntityExistsException}.
 *
 * @param e
 *            the exception to translate.
 * @return the translated exception.
 */
private RuntimeException translateConstraintViolationException(org.hibernate.exception.ConstraintViolationException e) {
    final EntityExistsException UNABLE_TO_PARSE = new EntityExistsException("Could not create user as a duplicate fields exists; however the duplicate field was not included in " + "the ConstraintViolationException, the original cause is included.", e);
    String constraintName = e.getConstraintName();
    if (StringUtils.isEmpty(constraintName)) {
        return UNABLE_TO_PARSE;
    }
    Matcher matcher = USER_CONSTRAINT_PATTERN.matcher(constraintName);
    if (matcher.groupCount() != 1) {
        throw new IllegalArgumentException("The pattern must have capture groups to parse the constraint name.");
    }
    if (!matcher.find()) {
        return UNABLE_TO_PARSE;
    }
    String fieldName = matcher.group(1).toLowerCase();
    return new EntityExistsException("Could not create user as a duplicate field exists: " + fieldName, fieldName);
}
Also used : Matcher(java.util.regex.Matcher) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)

Example 9 with EntityExistsException

use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.

the class ProjectSamplesControllerTest method testShareSampleToProjectSampleExists.

@SuppressWarnings("unchecked")
@Test
public void testShareSampleToProjectSampleExists() {
    Long projectId = 1L;
    List<Long> sampleIds = ImmutableList.of(2L, 3L);
    Long newProjectId = 4L;
    boolean removeFromOriginal = false;
    Project oldProject = new Project("oldProject");
    Project newProject = new Project("newProject");
    Sample s2 = new Sample("s2");
    Sample s3 = new Sample("s3");
    ArrayList<Sample> sampleList = Lists.newArrayList(s2, s3);
    boolean owner = true;
    when(projectService.read(projectId)).thenReturn(oldProject);
    when(projectService.read(newProjectId)).thenReturn(newProject);
    when(sampleService.readMultiple(any(Iterable.class))).thenReturn(sampleList);
    when(projectService.shareSamples(oldProject, newProject, sampleList, owner)).thenThrow(new EntityExistsException("that sample exists in the project"));
    Map<String, Object> copySampleToProject = controller.shareSampleToProject(projectId, sampleIds, newProjectId, removeFromOriginal, true, Locale.US);
    assertTrue(copySampleToProject.containsKey("warnings"));
    verify(projectService).read(projectId);
    verify(projectService).read(newProjectId);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) Matchers.anyLong(org.mockito.Matchers.anyLong) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) Test(org.junit.Test)

Example 10 with EntityExistsException

use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.

the class UsersControllerTest method testSubmitUsernameExists.

@Test
public void testSubmitUsernameExists() {
    EntityExistsException ex = new EntityExistsException("username exists", "username");
    createWithException(ex, "username");
    verify(emailController, times(1)).isMailConfigured();
    verifyNoMoreInteractions(emailController);
}
Also used : EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) Test(org.junit.Test)

Aggregations

EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)12 Test (org.junit.Test)6 Project (ca.corefacility.bioinformatics.irida.model.project.Project)4 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)4 ConstraintViolation (javax.validation.ConstraintViolation)3 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)2 User (ca.corefacility.bioinformatics.irida.model.user.User)2 HashMap (java.util.HashMap)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 LaunchesProjectEvent (ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent)1 PasswordReusedException (ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException)1 Announcement (ca.corefacility.bioinformatics.irida.model.announcements.Announcement)1 ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)1 SampleAddedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent)1 UserRemovedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent)1 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)1