@Test
  public void testIndexDrlRuleMultipleTypes() throws IOException, InterruptedException {
    // Add test files
    final Path path1 = basePath.resolve("drl3.rdrl");
    final String drl1 = loadText("drl3.rdrl");
    ioService().write(path1, drl1);
    final Path path2 = basePath.resolve("drl4.rdrl");
    final String drl2 = loadText("drl4.rdrl");
    ioService().write(path2, drl2);

    Thread.sleep(
        5000); // wait for events to be consumed from jgit -> (notify changes -> watcher -> index)
    // -> lucene index

    final Index index =
        getConfig()
            .getIndexManager()
            .get(org.kie.uberfire.metadata.io.KObjectUtil.toKCluster(basePath.getFileSystem()));

    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new QueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.rule.backend.server.indexing.classes.Applicant"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(2, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path1);
      assertContains(results, path2);

      ((LuceneIndex) index).nrtRelease(searcher);
    }

    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new QueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.rule.backend.server.indexing.classes.Mortgage"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(1, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path2);

      ((LuceneIndex) index).nrtRelease(searcher);
    }
  }
  @Test
  public void testIndexGuidedScoreCard() throws IOException, InterruptedException {
    // Add test files
    final Path path1 = basePath.resolve("scorecard1.scgd");
    final ScoreCardModel model1 =
        GuidedScoreCardFactory.makeScoreCardWithCharacteristics(
            "org.drools.workbench.screens.guided.scorecard.backend.server.indexing",
            new ArrayList<Import>() {
              {
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Applicant"));
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage"));
              }
            },
            "scorecard1");
    final String xml1 = GuidedScoreCardXMLPersistence.getInstance().marshal(model1);
    ioService().write(path1, xml1);
    final Path path2 = basePath.resolve("scorecard2.scgd");
    final ScoreCardModel model2 =
        GuidedScoreCardFactory.makeScoreCardWithoutCharacteristics(
            "org.drools.workbench.screens.guided.scorecard.backend.server.indexing",
            new ArrayList<Import>() {
              {
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Applicant"));
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage"));
              }
            },
            "scorecard2");
    final String xml2 = GuidedScoreCardXMLPersistence.getInstance().marshal(model2);
    ioService().write(path2, xml2);
    final Path path3 = basePath.resolve("scorecard3.scgd");
    final ScoreCardModel model3 =
        GuidedScoreCardFactory.makeEmptyScoreCard(
            "org.drools.workbench.screens.guided.scorecard.backend.server.indexing", "scorecard3");
    final String xml3 = GuidedScoreCardXMLPersistence.getInstance().marshal(model3);
    ioService().write(path3, xml3);

    Thread.sleep(
        5000); // wait for events to be consumed from jgit -> (notify changes -> watcher -> index)
    // -> lucene index

    final Index index =
        getConfig()
            .getIndexManager()
            .get(org.uberfire.ext.metadata.io.KObjectUtil.toKCluster(basePath.getFileSystem()));

    // Score Cards using
    // org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Applicant
    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new BasicQueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Applicant"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(2, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path1);
      assertContains(results, path2);

      ((LuceneIndex) index).nrtRelease(searcher);
    }

    // Score Cards using
    // org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage
    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new BasicQueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(1, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path1);

      ((LuceneIndex) index).nrtRelease(searcher);
    }

    // Score Cards using
    // org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage#amount
    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new BasicQueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.scorecard.backend.server.indexing.classes.Mortgage"))
              .addTerm(new ValueFieldIndexTerm("amount"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(1, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path1);

      ((LuceneIndex) index).nrtRelease(searcher);
    }

    // Score Cards using java.lang.Integer
    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new BasicQueryBuilder().addTerm(new ValueTypeIndexTerm("java.lang.Integer")).build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(2, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path1);
      assertContains(results, path2);

      ((LuceneIndex) index).nrtRelease(searcher);
    }
  }
  @Test
  public void testQueryInvalidIndexTerms() throws IOException, InterruptedException {
    final Instance<NamedQuery> namedQueriesProducer = mock(Instance.class);
    when(namedQueriesProducer.iterator()).thenReturn(queries.iterator());

    final RefactoringQueryServiceImpl service =
        new RefactoringQueryServiceImpl(getConfig(), namedQueriesProducer);
    service.init();

    // Add test files
    final Path path1 = basePath.resolve("drl1.drl");
    final String drl1 = loadText("drl1.drl");
    ioService().write(path1, drl1);

    Thread.sleep(
        5000); // wait for events to be consumed from jgit -> (notify changes -> watcher -> index)
               // -> lucene index

    {
      final RefactoringPageRequest request =
          new RefactoringPageRequest(
              "FindRulesByProjectQuery", new HashSet<ValueIndexTerm>(), 0, -1);

      try {
        final PageResponse<RefactoringPageRow> response = service.query(request);
        fail();
      } catch (IllegalArgumentException e) {
        // Swallow. Expected
      }
    }

    {
      final RefactoringPageRequest request =
          new RefactoringPageRequest(
              "FindRulesByProjectQuery",
              new HashSet<ValueIndexTerm>() {
                {
                  add(
                      new ValueTypeIndexTerm(
                          "org.kie.workbench.common.services.refactoring.backend.server.drl.classes.Applicant"));
                }
              },
              0,
              -1);

      try {
        final PageResponse<RefactoringPageRow> response = service.query(request);
        fail();
      } catch (IllegalArgumentException e) {
        // Swallow. Expected
      }
    }

    {
      final RefactoringPageRequest request =
          new RefactoringPageRequest(
              "FindRulesByProjectQuery",
              new HashSet<ValueIndexTerm>() {
                {
                  add(new ValueRuleIndexTerm("myRule"));
                  add(
                      new ValueTypeIndexTerm(
                          "org.kie.workbench.common.services.refactoring.backend.server.drl.classes.Applicant"));
                }
              },
              0,
              -1);

      try {
        final PageResponse<RefactoringPageRow> response = service.query(request);
        fail();
      } catch (IllegalArgumentException e) {
        // Swallow. Expected
      }
    }
  }
  @Test
  public void testIndexGuidedDecisionTableBRLFragmentActions()
      throws IOException, InterruptedException {
    // Add test files
    final Path path = basePath.resolve("dtable1.gdst");
    final GuidedDecisionTable52 model =
        GuidedDecisionTableFactory.makeTableWithBRLFragmentActionCol(
            "org.drools.workbench.screens.guided.dtable.backend.server.indexing",
            new ArrayList<Import>() {
              {
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.dtable.backend.server.indexing.classes.Applicant"));
                add(
                    new Import(
                        "org.drools.workbench.screens.guided.dtable.backend.server.indexing.classes.Mortgage"));
              }
            },
            "dtable1");
    final String xml = GuidedDTXMLPersistence.getInstance().marshal(model);
    ioService().write(path, xml);

    Thread.sleep(
        5000); // wait for events to be consumed from jgit -> (notify changes -> watcher -> index)
               // -> lucene index

    final Index index =
        getConfig()
            .getIndexManager()
            .get(org.kie.uberfire.metadata.io.KObjectUtil.toKCluster(basePath.getFileSystem()));

    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new QueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.dtable.backend.server.indexing.classes.Applicant"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(1, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path);

      ((LuceneIndex) index).nrtRelease(searcher);
    }

    {
      final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher();
      final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
      final Query query =
          new QueryBuilder()
              .addTerm(
                  new ValueTypeIndexTerm(
                      "org.drools.workbench.screens.guided.dtable.backend.server.indexing.classes.Mortgage"))
              .build();

      searcher.search(query, collector);
      final ScoreDoc[] hits = collector.topDocs().scoreDocs;
      assertEquals(1, hits.length);

      final List<KObject> results = new ArrayList<KObject>();
      for (int i = 0; i < hits.length; i++) {
        results.add(KObjectUtil.toKObject(searcher.doc(hits[i].doc)));
      }
      assertContains(results, path);

      ((LuceneIndex) index).nrtRelease(searcher);
    }
  }