private Collection<Tag> collectResourceTags(Resource resource, boolean recurse) {
    if (resource == null) {
      return Collections.emptyList();
    }
    Set<Tag> treeTags = new HashSet<Tag>();
    Queue<Resource> searchResources = new LinkedList<Resource>();
    searchResources.add(resource);

    while (!searchResources.isEmpty()) {
      Resource searchResource = searchResources.poll();

      if (recurse) {
        CollectionUtils.addAll(searchResources, searchResource.listChildren());
      }

      String[] tags = resource.getValueMap().get(TagConstants.PN_TAGS, String[].class);
      if (tags == null) {
        continue;
      }

      for (String tagStr : tags) {
        Tag tag = resolve(tagStr);
        if (tag != null) {
          treeTags.add(tag);
        }
      }
    }
    return treeTags;
  }
  @Test
  public void testManyReferenceToSinglePages() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    String path = "/content/geometrixx/en";
    String path1 = "/content/geometrixx/en";
    map.put("path", path);
    map.put("path1", path1);
    ValueMap vm = new ValueMapDecorator(map);
    when(resource.getValueMap()).thenReturn(vm);
    when(resource.getResourceResolver()).thenReturn(resolver);
    when(resolver.getResource(path)).thenReturn(res);
    when(resolver.getResource(path1)).thenReturn(res1);
    when(res.adaptTo(Page.class)).thenReturn(referredpage);
    when(res1.adaptTo(Page.class)).thenReturn(referredpage);
    when(referredpage.getName()).thenReturn("geometrixx");
    when(referredpage1.getName()).thenReturn("geometrixx");
    when(referredpage1.getPath()).thenReturn(path1);
    when(manager.getContainingPage(path1)).thenReturn(referredpage1);
    Calendar cal = GregorianCalendar.getInstance();
    when(referredpage.getLastModified()).thenReturn(cal);
    when(referredpage1.getLastModified()).thenReturn(cal);
    List<Reference> actual = instance.findReferences(resource);

    assertNotNull(actual);
    assertEquals(1, actual.size());
    assertEquals("geometrixx (Page)", actual.get(0).getName());
  }
 private boolean isPrimaryType(final Resource resource, final String primaryType) {
   Node node = resource.adaptTo(Node.class);
   if (node != null) {
     // JCR-based resource resolver
     try {
       return StringUtils.equals(node.getPrimaryNodeType().getName(), primaryType);
     } catch (RepositoryException ex) {
       // ignore
       return false;
     }
   } else {
     // sling resource resolver mock
     ValueMap props = resource.getValueMap();
     return StringUtils.equals(props.get(JcrConstants.JCR_PRIMARYTYPE, String.class), primaryType);
   }
 }
 @Before
 public void setUp() throws Exception {
   Map<String, Object> map = new HashMap<String, Object>();
   String path = "/content/geometrixx/en";
   map.put("path", path);
   ValueMap vm = new ValueMapDecorator(map);
   when(resource.getValueMap()).thenReturn(vm);
   when(resource.getResourceResolver()).thenReturn(resolver);
   when(resolver.adaptTo(PageManager.class)).thenReturn(manager);
   when(manager.getContainingPage(path)).thenReturn(referredpage);
   when(referredpage.getPath()).thenReturn(path);
   when(resource.listChildren()).thenReturn(iter);
   when(iter.hasNext()).thenReturn(false);
   when(resolver.getResource(path)).thenReturn(res);
   when(res.adaptTo(Page.class)).thenReturn(referredpage);
   when(referredpage.getName()).thenReturn("geometrixx");
   Calendar cal = GregorianCalendar.getInstance();
   when(referredpage.getLastModified()).thenReturn(cal);
 }
  @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 testMultipleReferencesReferenceToPages() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    String path = "\"/content/geometrixx/en\",\"/content/geometrixx/en/toolbar\"";
    String path1 = "/content/geometrixx/en/toolbar";
    map.put("path", path);
    // map.put("path1", path1);
    ValueMap vm = new ValueMapDecorator(map);
    when(resource.getValueMap()).thenReturn(vm);
    when(resource.getResourceResolver()).thenReturn(resolver);
    when(resolver.getResource(path)).thenReturn(res);
    when(resolver.getResource(path1)).thenReturn(res1);
    when(res.adaptTo(Page.class)).thenReturn(referredpage);
    when(res1.adaptTo(Page.class)).thenReturn(referredpage1);
    when(referredpage.getName()).thenReturn("geometrixx");
    when(referredpage1.getName()).thenReturn("geometrixx1");
    when(referredpage1.getPath()).thenReturn(path1);
    when(manager.getContainingPage(path1)).thenReturn(referredpage1);
    Calendar cal = GregorianCalendar.getInstance();
    when(referredpage.getLastModified()).thenReturn(cal);
    when(referredpage1.getLastModified()).thenReturn(cal);
    List<Reference> actual = instance.findReferences(resource);

    assertNotNull(actual);
    assertEquals(2, actual.size());

    boolean geometrixxFound = false;
    boolean geometrixxOneFound = false;
    for (Reference ref : actual) {
      if (ref.getName().equals("geometrixx (Page)")) {
        geometrixxFound = true;
      } else if (ref.getName().equals("geometrixx1 (Page)")) {
        geometrixxOneFound = true;
      }
    }

    assertTrue(geometrixxFound);
    assertTrue(geometrixxOneFound);
  }
  @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);
  }
  @Override
  public RangeIterator<Resource> find(String basePath, String[] tagIDs, boolean oneMatchIsEnough) {
    Resource base = resourceResolver.getResource(basePath);
    if (base == null) {
      return new CollectionRangeIterator<Resource>(Collections.<Resource>emptyList());
    }

    Collection<String> tagPaths = new HashSet<String>(tagIDs.length);
    for (String tagID : tagIDs) {
      Tag tag = resolve(tagID);
      // clause - if tag does not exist, should return null.
      if (tag == null) {
        return null;
      }
      tagPaths.add(tag.adaptTo(Resource.class).getPath());
    }

    Queue<Resource> searchResources = new LinkedList<Resource>();
    searchResources.add(base);

    Collection<Resource> matchedResources = new ArrayList<Resource>();

    while (!searchResources.isEmpty()) {
      Resource resource = searchResources.poll();
      // add the children to search the entire tree
      CollectionUtils.addAll(searchResources, resource.listChildren());

      // now process the tags
      String[] resourceTags = resource.getValueMap().get(TagConstants.PN_TAGS, String[].class);
      if (resourceTags == null) {
        continue;
      }

      List<String> resourceTagPaths = new ArrayList<String>(resourceTags.length);
      try {
        for (String resourceTag : resourceTags) {
          resourceTagPaths.add(getPathFromID(resourceTag));
        }
      } catch (InvalidTagFormatException e) {
        log.error("invalid tag id encountered", e);
      }

      if (resourceTagPaths.isEmpty()) {
        continue;
      }

      boolean matches = false;
      if (oneMatchIsEnough) {
        // this is essentially an OR list, so break out on the first positive
        oneMatched:
        for (String tagPath : tagPaths) {
          for (String resourceTagPath : resourceTagPaths) {
            matches = doTagsMatch(resourceTagPath, tagPath);
            if (matches) {
              break oneMatched;
            }
          }
        }
      } else {
        // this is essentially an AND list, so break out on the first failure
        matches = true;
        for (String tagPath : tagPaths) {
          boolean tagMatched = false;
          for (Iterator<String> resourceTagPathIter = resourceTagPaths.iterator();
              !tagMatched && resourceTagPathIter.hasNext(); ) {
            String resourceTagPath = resourceTagPathIter.next();
            tagMatched = doTagsMatch(resourceTagPath, tagPath);
          }
          // if no tag on the resource matched the current search tag, it fails the search
          if (!tagMatched) {
            matches = false;
            break;
          }
        }
      }

      if (matches) {
        matchedResources.add(resource);
      }
    }

    return new CollectionRangeIterator<Resource>(matchedResources);
  }