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