@Test
  public void testGetPayloadProperties_NullResource() throws Exception {

    Resource payloadRes = mock(Resource.class);
    Map<String, String> props = SendTemplatedEmailUtils.getPayloadProperties(payloadRes, sdf);
    assertEquals(0, props.size());
  }
  @Test
  public void testGetEmailAddrsNull() throws Exception {

    ResourceResolver resolver = mock(ResourceResolver.class);
    String userPath = "/doesnotexist";

    when(resolver.getResource(userPath)).thenReturn(null);
    String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, userPath);
    assertEquals(0, emails.length);
  }
  @Test
  public void testGetPayloadProperties_Asset() throws Exception {

    // set up jcr properties
    mockJcrProperties();

    Resource payloadRes = mock(Resource.class);
    Resource mdRes = mock(Resource.class);
    when(DamUtil.isAsset(payloadRes)).thenReturn(true);

    when(payloadRes.getChild(JcrConstants.JCR_CONTENT + "/" + DamConstants.METADATA_FOLDER))
        .thenReturn(mdRes);

    // mock valueMap
    when(mdRes.getValueMap()).thenReturn(vmap);
    Map<String, String> props = SendTemplatedEmailUtils.getPayloadProperties(payloadRes, sdf);

    assertEquals(props.get(PN_CALENDAR), CALENDAR_TOSTRING);
    assertEquals(props.get(PN_TITLE), STR_TOSTRING);
    assertEquals(props.get(PN_LONG), LONG_TOSTRING);
    assertEquals(props.get(PN_STR_ARRAY), STR_ARRAY_TOSTRING);
  }
  @Test
  public void testGetEmailAddrs_User() throws Exception {

    String userPath = "/home/users/a/admin";
    MockValue[] emailVal = new MockValue[] {new MockValue("*****@*****.**")};

    ResourceResolver resolver = mock(ResourceResolver.class);
    Resource userRes = mock(Resource.class);
    Authorizable adminUser = mock(Authorizable.class);

    when(resolver.getResource(userPath)).thenReturn(userRes);
    when(userRes.adaptTo(Authorizable.class)).thenReturn(adminUser);

    when(adminUser.isGroup()).thenReturn(false);
    when(adminUser.hasProperty(PN_EMAIL)).thenReturn(true);
    when(adminUser.getProperty(PN_EMAIL)).thenReturn(emailVal);

    String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, userPath);

    assertEquals(1, emails.length);
    assertEquals("*****@*****.**", emails[0]);
  }
  @Test
  public void testGetEmailAddrs_Group() throws Exception {

    // mock group and users
    String groupPath = "/home/users/g/group";
    List<Authorizable> groupMembers = new ArrayList<Authorizable>();

    Authorizable user1 = mock(Authorizable.class);
    Authorizable user2 = mock(Authorizable.class);

    when(user1.hasProperty(PN_EMAIL)).thenReturn(true);
    when(user1.getProperty(PN_EMAIL))
        .thenReturn(new MockValue[] {new MockValue("*****@*****.**")});

    when(user2.hasProperty(PN_EMAIL)).thenReturn(true);
    when(user2.getProperty(PN_EMAIL))
        .thenReturn(new MockValue[] {new MockValue("*****@*****.**")});

    groupMembers.add(user1);
    groupMembers.add(user2);

    ResourceResolver resolver = mock(ResourceResolver.class);
    Resource groupRes = mock(Resource.class);
    Authorizable groupAuth = mock(Authorizable.class);
    Group userGroup = mock(Group.class);

    when(resolver.getResource(groupPath)).thenReturn(groupRes);
    when(groupRes.adaptTo(Authorizable.class)).thenReturn(groupAuth);

    when(groupAuth.isGroup()).thenReturn(true);
    when(groupRes.adaptTo(Group.class)).thenReturn(userGroup);
    when(userGroup.getMembers()).thenReturn(groupMembers.iterator());

    String[] emails = SendTemplatedEmailUtils.getEmailAddrsFromUserPath(resolver, groupPath);
    assertEquals(2, emails.length);
    assertEquals("*****@*****.**", emails[0]);
    assertEquals("*****@*****.**", emails[1]);
  }
  @Test
  public void testGetPayloadProperties_Page() throws Exception {

    // set up jcr properties
    mockJcrProperties();

    Resource payloadRes = mock(Resource.class);
    Resource jcrRes = mock(Resource.class);
    when(DamUtil.isAsset(payloadRes)).thenReturn(false);

    Page payloadPage = mock(Page.class);
    when(payloadRes.adaptTo(Page.class)).thenReturn(payloadPage);
    when(payloadPage.getContentResource()).thenReturn(jcrRes);

    // mock valueMap
    when(jcrRes.getValueMap()).thenReturn(vmap);
    Map<String, String> props = SendTemplatedEmailUtils.getPayloadProperties(payloadRes, sdf);

    assertEquals(props.get(PN_CALENDAR), CALENDAR_TOSTRING);
    assertEquals(props.get(PN_TITLE), STR_TOSTRING);
    assertEquals(props.get(PN_LONG), LONG_TOSTRING);
    assertEquals(props.get(PN_STR_ARRAY), STR_ARRAY_TOSTRING);
  }