public void validateQueryOnIndexWithRegion(VM vm, final String query, final int resultSize,
      final String region) {
    vm.invoke(new CacheSerializableRunnable("Validate Query") {
      @Override
      public void run2() throws CacheException {
        LogWriterUtils.getLogWriter().info("### Validating Query. ###");
        QueryService qs = getCache().getQueryService();

        Query q = qs.newQuery(query);
        // Set the index observer
        QueryObserverImpl observer = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer);
        try {
          Object r = q.execute();
          if (r instanceof SelectResults) {
            int rSize = ((SelectResults) r).asSet().size();
            LogWriterUtils.getLogWriter().info("### Result Size is :" + rSize);

            if (region == null) {
              assertEquals(resultSize, rSize);
            } else {
              Region reg;
              if (region != null && (reg =
                  getCache().getRegion(SEPARATOR + "root" + SEPARATOR + region)) != null) {
                assertEquals(rSize, reg.size());
                for (Object value : reg.values()) {
                  if (!((SelectResults) r).asSet().contains(value)) {
                    fail("Query resultset mismatch with region values for value: " + value);
                  }
                }
              }
            }
          }
        } catch (Exception e) {
          Assert.fail("Failed to execute the query.", e);
        }
        if (!observer.isIndexesUsed) {
          fail("Index not used for query");
        }
      }
    });
  }
Validates a query result with client region values if region is not null, otherwise verifies
the size only.
geode-cq/src/distributedTest/java/org/apache/geode/cache/query/dunit/QueryIndexUpdateRIDUnitTest.java