/**
   * Tests execute with a notifier exception (for coverage).
   *
   * @throws Exception Shouldn't.
   */
  @Test
  @SuppressWarnings("unchecked")
  public void testExecuteNotifierError() throws Exception {
    final List<Long> recipients = Collections.singletonList(4L);
    final NotificationDTO notification =
        new NotificationDTO(
            recipients, NotificationType.FOLLOW_PERSON, 1L, 2L, EntityType.PERSON, 3L);

    context.checking(
        new Expectations() {
          {
            oneOf(followerTranslator).translate(1, 2, 3);
            will(returnValue(Collections.singletonList(notification)));

            allowing(personMapper).execute(with(equal(4L)));
            will(returnValue(person));

            oneOf(populator).populate(with(same(notification)));

            oneOf(preferencesMapper).execute(with(any(List.class)));
            will(returnValue(new ArrayList<NotificationFilterPreferenceDTO>()));

            oneOf(applicationNotifier).notify(with(any(NotificationDTO.class)));
            will(returnValue(null));
            oneOf(emailNotifier).notify(with(any(NotificationDTO.class)));
            will(throwException(new Exception("BAD")));
          }
        });

    CreateNotificationsRequest request =
        new CreateNotificationsRequest(RequestType.FOLLOWER, 1, 2, 3);
    sut.execute(TestContextCreator.createTaskHandlerAsyncContext(request));
    context.assertIsSatisfied();
  }
  /**
   * Tests execute where one recipient gets filtered.
   *
   * @throws Exception Shouldn't.
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testExecuteWithFilteredRecipient() throws Exception {
    final List<Long> recipients = Arrays.asList(4L, 5L);
    final NotificationDTO notification =
        new NotificationDTO(
            recipients, NotificationType.FOLLOW_PERSON, 1L, 2L, EntityType.PERSON, 3L);

    context.checking(
        new Expectations() {
          {
            oneOf(followerTranslator).translate(1, 2, 3);
            will(returnValue(Collections.singletonList(notification)));

            allowing(personMapper).execute(with(equal(4L)));
            will(returnValue(person));

            allowing(personMapper).execute(with(equal(5L)));
            will(returnValue(person2));

            allowing(filterEmail).shouldFilter(person2, notification, "EMAIL");
            will(returnValue(true));

            oneOf(preferencesMapper).execute(with(any(List.class)));
            will(returnValue(Collections.EMPTY_LIST));

            oneOf(populator).populate(with(same(notification)));

            oneOf(applicationNotifier)
                .notify(
                    with(
                        new EasyMatcher<NotificationDTO>() {
                          @Override
                          protected boolean isMatch(final NotificationDTO inTestObject) {
                            return inTestObject
                                .getRecipientIds()
                                .containsAll(Arrays.asList(4L, 5L));
                          }
                        }));
            will(returnValue(null));
            oneOf(emailNotifier)
                .notify(
                    with(
                        new EasyMatcher<NotificationDTO>() {
                          @Override
                          protected boolean isMatch(final NotificationDTO inTestObject) {
                            return inTestObject.getRecipientIds().contains(4L)
                                && !inTestObject.getRecipientIds().contains(5L);
                          }
                        }));
          }
        });

    CreateNotificationsRequest request =
        new CreateNotificationsRequest(RequestType.FOLLOWER, 1, 2, 3);
    sut.execute(TestContextCreator.createTaskHandlerAsyncContext(request));
    context.assertIsSatisfied();
  }
  /** Tests when there is no translator for the request type. */
  @Test
  public void testExecuteUnlistedRequestType() {
    CreateNotificationsRequest request =
        new CreateNotificationsRequest(RequestType.FLAG_ACTIVITY, 1, 2, 3);
    assertEquals(
        Boolean.FALSE, sut.execute(TestContextCreator.createTaskHandlerAsyncContext(request)));

    context.assertIsSatisfied();
  }
  /** Tests when the translator returns an empty list. */
  @Test
  public void testExecuteTranslateNone() {
    context.checking(
        new Expectations() {
          {
            oneOf(followerTranslator).translate(1, 2, 3);
            will(returnValue(Collections.EMPTY_LIST));
          }
        });

    CreateNotificationsRequest request =
        new CreateNotificationsRequest(RequestType.FOLLOWER, 1, 2, 3);
    Serializable result = sut.execute(TestContextCreator.createTaskHandlerAsyncContext(request));
    context.assertIsSatisfied();

    assertEquals(Boolean.TRUE, result);
  }
  /**
   * Tests execute where users have notification filter preferences for this event type. Insures the
   * preferences don't interact between notifiers.
   *
   * @throws Exception Shouldn't.
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testExecuteWithPrefernceFilteredResults() throws Exception {
    final List<Long> recipients = Arrays.asList(4L, 5L);
    final NotificationDTO notification =
        new NotificationDTO(
            recipients, NotificationType.COMMENT_TO_COMMENTED_POST, 1L, 2L, EntityType.PERSON, 3L);

    final List<NotificationFilterPreferenceDTO> prefs =
        new ArrayList<NotificationFilterPreferenceDTO>();
    prefs.add(new NotificationFilterPreferenceDTO(4L, "EMAIL", Category.COMMENT));
    prefs.add(new NotificationFilterPreferenceDTO(5L, "APP_ALERT", Category.COMMENT));

    context.checking(
        new Expectations() {
          {
            oneOf(commentTranslator).translate(1, 2, 3);
            will(returnValue(Collections.singletonList(notification)));

            allowing(personMapper).execute(with(equal(4L)));
            will(returnValue(person));

            allowing(personMapper).execute(with(equal(5L)));
            will(returnValue(person2));

            oneOf(preferencesMapper).execute(with(any(List.class)));
            will(returnValue(prefs));

            oneOf(populator).populate(with(same(notification)));

            allowing(filterEmail)
                .shouldFilter(
                    with(any(PersonModelView.class)),
                    with(any(NotificationDTO.class)),
                    with(any(String.class)));
            will(returnValue(false));

            oneOf(applicationNotifier)
                .notify(
                    with(
                        new EasyMatcher<NotificationDTO>() {
                          @Override
                          protected boolean isMatch(final NotificationDTO inTestObject) {
                            return inTestObject.getRecipientIds().contains(4L)
                                && !inTestObject.getRecipientIds().contains(5L);
                          }
                        }));
            will(returnValue(null));
            oneOf(emailNotifier)
                .notify(
                    with(
                        new EasyMatcher<NotificationDTO>() {
                          @Override
                          protected boolean isMatch(final NotificationDTO inTestObject) {
                            return inTestObject.getRecipientIds().contains(5L)
                                && !inTestObject.getRecipientIds().contains(4L);
                          }
                        }));
          }
        });

    CreateNotificationsRequest request =
        new CreateNotificationsRequest(RequestType.COMMENT, 1, 2, 3);
    sut.execute(TestContextCreator.createTaskHandlerAsyncContext(request));
    context.assertIsSatisfied();
  }