Search in sources :

Example 21 with ProjectEvent

use of ca.corefacility.bioinformatics.irida.model.event.ProjectEvent in project irida by phac-nml.

the class ProjectEventHandlerTest method testDelegateUserRole.

@Test
public void testDelegateUserRole() {
    Class<? extends ProjectEvent> clazz = UserRoleSetProjectEvent.class;
    Project project = new Project();
    User user = new User();
    ProjectUserJoin returnValue = new ProjectUserJoin(project, user, ProjectRole.PROJECT_USER);
    Object[] args = { project, user, ProjectRole.PROJECT_USER };
    MethodEvent methodEvent = new MethodEvent(clazz, returnValue, args);
    when(eventRepository.save(any(ProjectEvent.class))).thenReturn(new UserRoleSetProjectEvent(returnValue));
    handler.delegate(methodEvent);
    ArgumentCaptor<ProjectEvent> captor = ArgumentCaptor.forClass(ProjectEvent.class);
    verify(eventRepository).save(captor.capture());
    ProjectEvent event = captor.getValue();
    assertTrue(event instanceof UserRoleSetProjectEvent);
    verify(projectRepository).save(any(Project.class));
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) User(ca.corefacility.bioinformatics.irida.model.user.User) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Example 22 with ProjectEvent

use of ca.corefacility.bioinformatics.irida.model.event.ProjectEvent in project irida by phac-nml.

the class ProjectEventHandler method delegate.

/**
 * Handle translation of a {@link MethodEvent} into the correct
 * {@link ProjectEvent} subclass
 *
 * @param methodEvent
 *            Information from a method return annotated with
 *            {@link LaunchesProjectEvent}
 */
public void delegate(MethodEvent methodEvent) {
    Class<? extends ProjectEvent> eventClass = methodEvent.getEventClass();
    final Date eventDate = new Date();
    Collection<ProjectEvent> events = new ArrayList<>();
    if (eventClass.equals(SampleAddedProjectEvent.class)) {
        events.addAll(handleSampleAddedProjectEvent(methodEvent));
    } else if (eventClass.equals(UserRemovedProjectEvent.class)) {
        events.add(handleUserRemovedEvent(methodEvent));
    } else if (eventClass.equals(UserRoleSetProjectEvent.class)) {
        events.add(handleUserRoleSetProjectEvent(methodEvent));
    } else if (eventClass.equals(DataAddedToSampleProjectEvent.class)) {
        final Collection<DataAddedToSampleProjectEvent> dataAddedEvents = handleSequenceFileAddedEvent(methodEvent);
        /*
			 * We want the sample to show modification when these events are
			 * added, so update mod date
			 */
        for (final DataAddedToSampleProjectEvent e : dataAddedEvents) {
            final Sample s = e.getSample();
            s.setModifiedDate(eventDate);
            sampleRepository.save(s);
        }
        events.addAll(dataAddedEvents);
    } else if (eventClass.equals(UserGroupRoleSetProjectEvent.class)) {
        events.add(handleUserGroupRoleSetProjectEvent(methodEvent));
    } else if (eventClass.equals(UserGroupRemovedProjectEvent.class)) {
        events.add(handleUserGroupRemovedEvent(methodEvent));
    } else if (eventClass.equals(SampleRemovedProjectEvent.class)) {
        events.addAll(handleSampleRemovedEvent(methodEvent));
    } else {
        logger.warn("No handler found for event class " + eventClass.getName());
    }
    for (ProjectEvent e : events) {
        Project project = e.getProject();
        project.setModifiedDate(eventDate);
        projectRepository.save(project);
    }
}
Also used : UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) Date(java.util.Date) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) SampleRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleRemovedProjectEvent) UserGroupRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent)

Example 23 with ProjectEvent

use of ca.corefacility.bioinformatics.irida.model.event.ProjectEvent in project irida by phac-nml.

the class ProjectEventHandler method handleUserGroupRemovedEvent.

/**
 * Create a {@link UserRemovedProjectEvent}. The method arguments must
 * contain a {@link Project} and {@link User}
 *
 * @param event
 *            The {@link MethodEvent} that this event is being launched from
 */
private ProjectEvent handleUserGroupRemovedEvent(MethodEvent event) {
    final Optional<Object> user = Arrays.stream(event.getArgs()).filter(e -> e instanceof UserGroup).findAny();
    final Optional<Object> project = Arrays.stream(event.getArgs()).filter(e -> e instanceof Project).findAny();
    if (!user.isPresent() || !project.isPresent()) {
        throw new IllegalArgumentException("Project or user group cannot be found on method annotated with @LaunchesProjectEvent(UserGroupRemovedProjectEvent.class)");
    }
    return eventRepository.save(new UserGroupRemovedProjectEvent((Project) project.get(), (UserGroup) user.get()));
}
Also used : ProjectSampleJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectSampleJoinRepository) Arrays(java.util.Arrays) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) ProjectEventRepository(ca.corefacility.bioinformatics.irida.repositories.ProjectEventRepository) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) SampleRepository(ca.corefacility.bioinformatics.irida.repositories.sample.SampleRepository) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ArrayList(java.util.ArrayList) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) ProjectRepository(ca.corefacility.bioinformatics.irida.repositories.ProjectRepository) Logger(org.slf4j.Logger) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Collection(java.util.Collection) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) SampleRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleRemovedProjectEvent) UserGroupRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent) Project(ca.corefacility.bioinformatics.irida.model.project.Project) List(java.util.List) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) Optional(java.util.Optional) User(ca.corefacility.bioinformatics.irida.model.user.User) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) Project(ca.corefacility.bioinformatics.irida.model.project.Project) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)

Example 24 with ProjectEvent

use of ca.corefacility.bioinformatics.irida.model.event.ProjectEvent in project irida by phac-nml.

the class ProjectEventsControllerTest method testUnknownEvent.

@Test
public void testUnknownEvent() {
    Long projectId = 1L;
    Project project = new Project();
    ExtendedModelMap model = new ExtendedModelMap();
    ProjectEvent event = new ProjectEvent() {

        @Override
        public String getLabel() {
            return "an unmapped event";
        }
    };
    Page<ProjectEvent> page = new PageImpl<>(Lists.newArrayList(event));
    when(projectService.read(projectId)).thenReturn(project);
    when(eventService.getEventsForProject(eq(project), any(Pageable.class))).thenReturn(page);
    String recentEventsForProject = controller.getRecentEventsForProject(projectId, model, 10);
    assertEquals(ProjectEventsController.EVENTS_VIEW, recentEventsForProject);
    assertTrue(model.containsAttribute("events"));
    @SuppressWarnings("unchecked") List<Map<String, Object>> events = (List<Map<String, Object>>) model.get("events");
    assertEquals(0, events.size());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Pageable(org.springframework.data.domain.Pageable) List(java.util.List) Map(java.util.Map) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) Test(org.junit.Test)

Example 25 with ProjectEvent

use of ca.corefacility.bioinformatics.irida.model.event.ProjectEvent in project irida by phac-nml.

the class ProjectEventServiceImplIT method testAddProjectSample.

@WithMockUser(username = "tom", password = "password1", roles = "ADMIN")
@Test
public void testAddProjectSample() {
    Project project = projectService.read(1L);
    Sample sample = sampleService.read(2L);
    projectService.addSampleToProject(project, sample, true);
    Page<ProjectEvent> eventsForProject = projectEventService.getEventsForProject(project, new PageRequest(0, 10));
    assertEquals(1, eventsForProject.getTotalElements());
    ProjectEvent event = eventsForProject.iterator().next();
    assertTrue(event instanceof SampleAddedProjectEvent);
    SampleAddedProjectEvent userEvent = (SampleAddedProjectEvent) event;
    assertEquals(project, userEvent.getProject());
    assertEquals(sample, userEvent.getSample());
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) PageRequest(org.springframework.data.domain.PageRequest) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) 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)

Aggregations

ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)30 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)29 SampleAddedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent)23 UserRemovedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent)23 Test (org.junit.Test)23 Project (ca.corefacility.bioinformatics.irida.model.project.Project)21 User (ca.corefacility.bioinformatics.irida.model.user.User)16 DataAddedToSampleProjectEvent (ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent)13 PageRequest (org.springframework.data.domain.PageRequest)10 WithMockUser (org.springframework.security.test.context.support.WithMockUser)10 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)8 Map (java.util.Map)8 Date (java.util.Date)7 List (java.util.List)7 UserGroupRemovedProjectEvent (ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent)5 SampleRemovedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.SampleRemovedProjectEvent)5 UserGroupRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent)5 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)5 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)5 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)4