@WithMockUser(username = "******", password = "******", roles = "ADMIN")
  @Test
  public void testUpdateProjectUser() throws ProjectWithoutOwnerException {
    Project project = projectService.read(1L);
    User user = userService.read(1L);

    projectService.updateUserProjectRole(project, user, ProjectRole.PROJECT_USER);

    Page<ProjectEvent> eventsForProject =
        projectEventService.getEventsForProject(project, new PageRequest(0, 10));

    assertEquals(1, eventsForProject.getTotalElements());
    ProjectEvent event = eventsForProject.iterator().next();

    assertTrue(event instanceof UserRoleSetProjectEvent);
    UserRoleSetProjectEvent userEvent = (UserRoleSetProjectEvent) event;
    assertEquals(project, userEvent.getProject());
    assertEquals(user, userEvent.getUser());
  }
  @WithMockUser(username = "******", password = "******", roles = "ADMIN")
  @Test
  public void testErrorThrownNoEvent() {
    Project project = projectService.read(1L);

    Sample sample = sampleService.read(1L);

    try {
      projectService.addSampleToProject(project, sample);
      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());
  }
  @WithMockUser(username = "******", password = "******", roles = "ADMIN")
  @Test
  public void testAddProjectSample() {
    Project project = projectService.read(1L);

    Sample sample = sampleService.read(2L);

    projectService.addSampleToProject(project, sample);

    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());
  }
  /**
   * Get recent {@link ProjectEvent}s for the given {@link Project}
   *
   * @param projectId The ID of the {@link Project} to get events for
   * @param model Model for the view. Contains a list named "events". This will be a map which will
   *     contain "name" which is the name of the view fragment to use, and "event" which is a
   *     reference to the event itself
   * @param size Number of events to show
   * @return The name of the events view
   */
  @RequestMapping("/project/{projectId}")
  public String getRecentEventsForProject(
      @PathVariable Long projectId,
      Model model,
      @RequestParam(required = false, defaultValue = DEFAULT_PAGE_SIZE) Integer size) {
    Project project = projectService.read(projectId);

    Page<ProjectEvent> events =
        eventService.getEventsForProject(
            project, new PageRequest(0, size, Direction.DESC, "createdDate"));
    List<Map<String, Object>> eventInfo = buildEventsListFromPage(events);

    model.addAttribute("events", eventInfo);

    return EVENTS_VIEW;
  }
  @WithMockUser(username = "******", password = "******", roles = "ADMIN")
  @Test
  public void testGetEventsForProject() {
    Project project1 = projectService.read(1L);
    Project project3 = projectService.read(3L);

    Page<ProjectEvent> eventsForProject1 =
        projectEventService.getEventsForProject(project1, new PageRequest(0, 10));
    Page<ProjectEvent> eventsForProject2 =
        projectEventService.getEventsForProject(project3, new PageRequest(0, 10));

    assertEquals(0L, eventsForProject1.getTotalElements());
    assertEquals(1L, eventsForProject2.getTotalElements());

    ProjectEvent event2 = eventsForProject2.iterator().next();

    assertEquals(project3, event2.getProject());
  }
  /**
   * Update the subscription status on a {@link Project} for a {@link User}
   *
   * @param userId The {@link User} id to update
   * @param projectId the {@link Project} to subscribe to
   * @param subscribe boolean whether to be subscribed to the project or not
   * @param locale locale of the request
   * @return Map success message if the subscription status was updated
   */
  @RequestMapping(value = "/projects/{projectId}/subscribe/{userId}", method = RequestMethod.POST)
  public Map<String, String> addSubscription(
      @PathVariable Long userId,
      @PathVariable Long projectId,
      @RequestParam boolean subscribe,
      Locale locale) {
    User user = userService.read(userId);
    Project project = projectService.read(projectId);

    userService.updateEmailSubscription(user, project, subscribe);

    String message;
    if (subscribe) {
      message =
          messageSource.getMessage(
              "user.projects.subscriptions.added", new Object[] {project.getLabel()}, locale);
    } else {
      message =
          messageSource.getMessage(
              "user.projects.subscriptions.removed", new Object[] {project.getLabel()}, locale);
    }

    return ImmutableMap.of("success", "true", "message", message);
  }