/**
  * Create a new JCR node type definition from the given <code>QNodeTypeDefinition</code>.
  *
  * @param qNtd A SPI node type definition.
  * @return the corresponding JCR node type definition.
  * @throws RepositoryException if an error occurs.
  */
 @SuppressWarnings("unchecked")
 public NodeTypeDefinition create(QNodeTypeDefinition qNtd) throws RepositoryException {
   NodeTypeTemplate nt = ntMgr.createNodeTypeTemplate();
   nt.setName(getJCRName(qNtd.getName()));
   nt.setDeclaredSuperTypeNames(getJCRNames(qNtd.getSupertypes()));
   nt.setAbstract(qNtd.isAbstract());
   nt.setMixin(qNtd.isMixin());
   nt.setOrderableChildNodes(qNtd.hasOrderableChildNodes());
   nt.setPrimaryItemName(getJCRName(qNtd.getPrimaryItemName()));
   nt.setQueryable(qNtd.isQueryable());
   List nodeDefs = nt.getNodeDefinitionTemplates();
   for (QNodeDefinition qNd : qNtd.getChildNodeDefs()) {
     nodeDefs.add(create(qNd));
   }
   List propDefs = nt.getPropertyDefinitionTemplates();
   for (QPropertyDefinition qPd : qNtd.getPropertyDefs()) {
     propDefs.add(create(qPd));
   }
   return nt;
 }
  @Test
  public void testAggregateIndex()
      throws IOException, RepositoryException, InterruptedException, ClientPoolException,
          StorageClientException, AccessDeniedException, ClassNotFoundException {
    RepositoryBase repositoryBase = RepositoryBaseTest.getRepositoryBase();
    Repository repository = repositoryBase.getRepository();
    Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();

    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setDeclaredSuperTypeNames(new String[] {"nt:unstructured"});
    ntt.setName("sakai:user-home");

    ntm.registerNodeType(ntt, true);

    Node rootNode = session.getRootNode();
    Node homeNode = rootNode.addNode("iebhome", "sakai:user-home");
    homeNode.setProperty("sling:resourceType", "sakai/user-home");
    Node publicNode = homeNode.addNode("public", "nt:unstructured");
    Node publicSub = publicNode.addNode("sub", "nt:unstructured");
    publicSub.setProperty("test", "The brown fox");
    Node publicSub2 = publicNode.addNode("sub2", "nt:unstructured");
    publicSub2.setProperty("test", "The brown fox");
    Node childFileNode = publicSub.addNode("childNode", "nt:file");
    Node resourceNode = childFileNode.addNode("jcr:content", "nt:resource");
    ValueFactory valueFactory = session.getValueFactory();
    InputStream fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes("UTF-8"));
    Binary binary = valueFactory.createBinary(fileStream);
    resourceNode.setProperty("jcr:data", binary);
    session.save();
    session.logout();

    Thread.sleep(100);

    session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
    Node n = session.getNode("/iebhome/public/sub");
    Assert.assertEquals("The brown fox", n.getProperty("test").getString());

    QueryManager qm = session.getWorkspace().getQueryManager();
    {
      Query q =
          qm.createQuery(
              "//*[@sling:resourceType = 'sakai/user-home' and jcr:contains(.,'brown')]",
              Query.XPATH);
      QueryResult qr = q.execute();
      NodeIterator nodeIterator = qr.getNodes();
      Assert.assertTrue(nodeIterator.hasNext());
      Assert.assertEquals("/iebhome", nodeIterator.nextNode().getPath());
      Assert.assertFalse(nodeIterator.hasNext());
    }
    {
      Query q = qm.createQuery("//*[@sling:resourceType = 'sakai/user-home']", Query.XPATH);
      QueryResult qr = q.execute();
      NodeIterator nodeIterator = qr.getNodes();
      Assert.assertTrue(nodeIterator.hasNext());
      Assert.assertEquals("/iebhome", nodeIterator.nextNode().getPath());
      Assert.assertFalse(nodeIterator.hasNext());
    }
    {
      Query q = qm.createQuery("//*[jcr:contains(.,'brown')]", Query.XPATH);
      QueryResult qr = q.execute();
      NodeIterator nodeIterator = qr.getNodes();
      List<String> paths = new ArrayList<String>();
      while (nodeIterator.hasNext()) {
        paths.add(nodeIterator.nextNode().getPath());
      }

      Assert.assertTrue(paths.contains("/iebhome"));
      Assert.assertTrue(paths.contains("/iebhome/public/sub"));
      Assert.assertTrue(paths.contains("/iebhome/public/sub2"));
    }
    session.logout();
  }