@Test
 public void testVerifyVotesRejectsMissingCategory() {
   assertFalse(1 == firstEvent.getVoteCategories().size());
   List<Long> voteList = Lists.newArrayList();
   voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
   try {
     voteService.verifyVotes(firstEvent, voteList);
     fail("should not succeed");
   } catch (Exception e) {
     assertTrue(e.getMessage().contains("Invalid voting"));
   }
 }
  @Test
  public void testVerifyVotesRejectsVotesForOtherEvent() {
    assertEquals(2, firstEvent.getVoteCategories().size());

    List<Long> voteList = Lists.newArrayList();
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(secondEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    try {
      voteService.verifyVotes(firstEvent, voteList);
      fail("should not succeed");
    } catch (Exception e) {
      assertTrue(e.getMessage().contains("Must vote in all categories"));
    }
  }
  @Test
  public void testVoteEventDoesntExist() {
    Event e = new Event(null, null, "", "", EventType.PUBLIC, DateTime.now(), DateTime.now());
    e.setId((long) 10000);

    List<Long> voteList = Lists.newArrayList();
    voteList.add((long) 1);
    try {
      voteService.verifyVotes(e, voteList);
      fail("should not succeed");
    } catch (Exception e1) {
      assertTrue(e1.getMessage() != null);
    }
  }
  @Test
  public void testVerifyVotes() {
    assertEquals(2, firstEvent.getVoteCategories().size());

    List<Long> voteList = Lists.newArrayList();
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(0).getId());

    try {
      voteService.verifyVotes(firstEvent, voteList);
      return;
    } catch (Exception e) {
      // ignore
    }
    fail("should not succeed");
  }
  @Test
  public void testRepeatCategoriesRejectsAll() {
    List<Long> voteList = Lists.newArrayList();
    // repeat all categories or one category
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(1).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(0).getId());

    try {
      voteService.addVotes(firstEvent, voteList, voter);
      fail("should not succeed");
    } catch (Exception e) {
      assertTrue(e.getMessage().contains("Invalid voting"));
    }
  }
  /**
   * Validate event update.
   *
   * @param event the event
   * @throws ValidationException the validation exception
   */
  public static void validateEventUpdate(final Event event) throws ValidationException {
    final ValidationException validationException = new ValidationException();

    try {
      validateEventCreation(event);
    } catch (final ValidationException e) {
      validationException.addAllMessages(e.getErrorMessages());
    }

    if (event.getStartTime() != null && DateTime.now().isAfter(event.getStartTime())) {
      validationException.addMessage("Can not update event after start time");
    }

    if (validationException.hasErrorMessages()) {
      throw validationException;
    }
  }
  @Test
  public void testValidUserPrivateEvent() throws Exception {
    privateEvent.addEventUser(voter);

    List<Long> voteList = Lists.newArrayList();
    voteList.add(privateEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(privateEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

    voteService.addVotes(privateEvent, voteList, voter);

    VoteOption vo1o1 =
        voteOptionDao.find(privateEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    VoteOption vo2o1 =
        voteOptionDao.find(privateEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

    assertTrue(vo1o1.getVotes().size() == 1);
    assertTrue(vo2o1.getVotes().size() == 1);
  }
  @Test
  public void testVerifyAddVoteRecordsVote() throws Exception {
    // test firstEvent, secondEvent
    List<Long> voteList = Lists.newArrayList();
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());
    voteService.addVotes(firstEvent, voteList, voter);

    // eventDao.update(secondEvent);

    VoteOption vo1o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    VoteOption vo2o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

    assertTrue(vo1o1.getVotes().size() == 1);
    assertTrue(vo2o1.getVotes().size() == 1);
  }
  @Test
  public void testInvalidUserPrivateEvent() throws Exception {

    try {
      List<Long> voteList = Lists.newArrayList();
      voteList.add(privateEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
      voteList.add(privateEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

      voteService.addVotes(privateEvent, voteList, voter);

      VoteOption vo1o1 =
          voteOptionDao.find(
              privateEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
      VoteOption vo2o1 =
          voteOptionDao.find(
              privateEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

      assertTrue(vo1o1.getVotes().size() == 1);
      assertTrue(vo2o1.getVotes().size() == 1);

    } catch (Exception e) {
      assertTrue(e.getMessage().contains("Invalid event for user"));
    }
  }
  @Test
  public void testVerifyAddVoteReplacesExistingVote() throws Exception {
    List<Long> voteList = Lists.newArrayList();
    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(0).getId());
    voteService.addVotes(firstEvent, voteList, voter);
    // eventDao.update(firstEvent);

    VoteOption vo1o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    VoteOption vo2o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(1).getVoteOptions().get(0).getId());

    assertTrue(vo1o1.getVotes().size() == 1);
    assertTrue(vo2o1.getVotes().size() == 1);

    voteList.clear();

    voteList.add(firstEvent.getVoteCategories().get(0).getVoteOptions().get(1).getId());
    voteList.add(firstEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

    voteService.addVotes(firstEvent, voteList, voter);

    vo1o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(0).getVoteOptions().get(0).getId());
    vo2o1 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(1).getVoteOptions().get(0).getId());
    VoteOption vo1o2 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(0).getVoteOptions().get(1).getId());
    VoteOption vo2o2 =
        voteOptionDao.find(firstEvent.getVoteCategories().get(1).getVoteOptions().get(1).getId());

    assertTrue(vo1o1.getVotes().size() == 0);
    assertTrue(vo2o1.getVotes().size() == 0);
    assertTrue(vo1o2.getVotes().size() == 1);
    assertTrue(vo2o2.getVotes().size() == 1);
  }
  /**
   * Validate event creation.
   *
   * @param event the event
   * @throws ValidationException the validation exception
   */
  public static void validateEventCreation(final Event event) throws ValidationException {

    if (event == null) {
      return;
    }

    final ValidationException validationException = new ValidationException();

    if (StringUtils.isEmpty(StringUtils.trimToEmpty(event.getName()))) {
      validationException.addMessage("Event must have a name.");
    }

    if (event.getEventType() == null) {
      validationException.addMessage("EventType can not be empty.");
    }

    if (event.getStartTime() == null) {
      validationException.addMessage("Start date must be supplied.");
    }

    if (event.getEndTime() == null) {
      validationException.addMessage("End date must be supplied.");
    }

    if (event.getStartTime() != null
        && event.getEndTime() != null
        && (!event.getStartTime().isBefore(event.getEndTime()))) {
      validationException.addMessage("Start date must be before end date.");
    }

    if (event.getVoteCategories() == null || event.getVoteCategories().isEmpty()) {
      validationException.addMessage("At least one voting category must be supplied.");
    } else {

      for (final VoteCategory category : event.getVoteCategories()) {
        try {
          validateVoteCategory(category);
        } catch (final ValidationException e) {
          validationException.addAllMessages(e.getErrorMessages());
        }
      }
    }

    if (validationException.hasErrorMessages()) {
      throw validationException;
    }
  }