@Test
 public void testExtractTwigMultipleInArrayObjectVariable()
     throws QueryNodeException, ProjectionException, IOException {
   String query =
       "{"
           + "\"twig\" : { "
           + "\"root\" : \"actors\","
           + "\"child\" : [{\"occur\":\"MUST\","
           // blank node
           + "\"twig\": {"
           + "\"child\":[{\"occur\":\"MUST\","
           + "\"twig\": {"
           + "\"root\":\"last_name\","
           + "\"child\":["
           + "{\"occur\":\"MUST\","
           + "\"node\" : { \"query\" : \"e*\" } "
           + "}"
           + "]"
           + "}"
           + "}"
           + "]"
           + "}"
           + "}"
           + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
           + "]"
           + "}"
           + "}";
   Query q = parser.parse(query, "");
   SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
   assertEquals(
       mapper.readTree(
           "[{\"last_name\":\"Eastwood\",\"first_name\":\"Clint\",\"birth_date\":1930,\"role\": \"William Munny\"},"
               + "{\"first_name\": \"Morgan\",\"last_name\": \"Ereeman\",\"birth_date\": 1937,\"role\": \"Ned Logan\"}]"),
       mapper.readTree(extractor.extractAsString(json, q)));
 }
 @Test
 public void testExtractTwigNoVariable() throws QueryNodeException, ProjectionException {
   String query =
       "{"
           + "\"twig\" : { "
           + "\"root\" : \"director\","
           + "\"child\" : [{\"occur\":\"MUST\","
           + "\"twig\": {"
           + "\"child\":[{\"occur\":\"MUST\","
           + "\"twig\": {"
           + "\"root\":\"first_name\","
           + "\"child\":["
           + "{\"occur\":\"MUST\","
           + "\"node\" : { \"query\" : \"Clint\" } "
           + "}"
           + "]"
           + "}"
           + "}"
           + "]"
           + "}"
           + "}"
           + "]           "
           + "}"
           + "}";
   Query q = parser.parse(query, "");
   SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
   assertEquals(json, extractor.extractAsString(json, q));
 }
 @Test
 public void testExtractTwigMultipleInArray() throws QueryNodeException, ProjectionException {
   String query =
       "{"
           + "\"twig\" : { "
           + "\"root\" : \"actors\","
           + "\"child\" : [{\"occur\":\"MUST\","
           // blank node
           + "\"twig\": {"
           + "\"child\":[{\"occur\":\"MUST\","
           + "\"twig\": {"
           + "\"root\":\"first_name\","
           + "\"child\":["
           + "{\"occur\":\"MUST\","
           + "\"node\" : { \"query\" : \"Clint\" } "
           + "}"
           + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
           + "]"
           + "}"
           + "}"
           + "]"
           + "}"
           + "}"
           + "]"
           + "}"
           + "}";
   Query q = parser.parse(query, "");
   SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
   assertEquals("[{\"first_name\":\"Clint\"}]", extractor.extractAsString(json, q));
 }
  @Test
  public void testSimpleNumericRangeNoVars()
      throws QueryNodeException, ProjectionException, IOException {
    String query =
        "{ \"twig\": "
            + "{ \"root\":\"year\","
            + "  \"child\": [{\"occur\":\"MUST\","
            + "               \"node\" : { \"query\" : \"http://www.w3.org/2001/XMLSchema#long([1991 TO 2000])\" }}]}}";

    Query q = parser.parse(query, "");
    SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
    assertEquals(mapper.readTree(json), mapper.readTree(extractor.extractAsString(json, q)));
  }
  @Test
  public void testTwoVariablesPrimitiveAndObject()
      throws QueryNodeException, ProjectionException, IOException {
    String twig0 =
        "\"twig\": {"
            + "\"root\":\"title\""
            + ",\"child\":["
            + "{\"occur\":\"MUST\""
            + ",\"node\" : { \"query\" : \"Unforgiven\" }"
            + "}"
            + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
            + "]"
            + "}";

    String twig1 =
        "\"twig\" : { "
            + "\"root\" : \"director\","
            + "\"child\" : [{\"occur\":\"MUST\","
            // blank node
            + "\"twig\": {"
            + "\"child\":[{\"occur\":\"MUST\","
            + "\"twig\": {"
            + "\"root\":\"first_name\","
            + "\"child\":["
            + "{\"occur\":\"MUST\","
            + "\"node\" : { \"query\" : \"Clint\" } "
            + "}"
            + "]"
            + "}"
            + "}"
            + "]"
            + "}"
            + "}"
            + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
            + "]"
            + "}";

    Query q =
        parser.parse(
            "{\"boolean\":{\"clause\":[{\"occur\":\"MUST\","
                + twig0
                + "},{\"occur\":\"MUST\","
                + twig1
                + "}]}}",
            "");
    SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
    assertEquals(
        mapper.readTree(
            "{\"title\":\"Unforgiven\",\"director\":{\"last_name\":\"Eastwood\",\"first_name\":\"Clint\",\"birth_date\":1930}}"),
        mapper.readTree(extractor.extractAsString(json, q)));
  }
 @Test
 public void testExtractNode() throws QueryNodeException, ProjectionException {
   final String query = "{ \"node\" : { \"query\" : \"Eastwood\" } }";
   Query q = parser.parse(query, "");
   // no variable whole json should be returned
   assertEquals(json, extractor.extractAsString(json, q));
 }
 @Test
 public void testSelectByNumericRangeFromArray()
     throws QueryNodeException, ProjectionException, IOException {
   String query =
       "{"
           + "\"twig\" : { "
           + "\"root\" : \"actors\","
           + "\"child\" : [{\"occur\":\"MUST\","
           // blank node
           + "\"twig\": {"
           + "\"child\":[{\"occur\":\"MUST\","
           + "\"twig\": {"
           + "\"root\":\"birth_date\","
           + "\"child\":["
           + "{\"occur\":\"MUST\","
           + "\"node\" : { \"query\" :  \"http://www.w3.org/2001/XMLSchema#long([* TO 1932])\" } "
           + "}"
           + "]"
           + "}"
           + "}"
           + "]"
           + "}"
           + "}"
           + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
           + "]"
           + "}"
           + "}";
   Query q = parser.parse(query, "");
   SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
   assertEquals(
       mapper.readTree(
           " ["
               + "{"
               + "\"first_name\": \"Clint\","
               + "\"last_name\": \"Eastwood\","
               + "\"birth_date\": 1930,"
               + "\"role\": \"William Munny\""
               + "},"
               + "{"
               + "\"first_name\": \"Gene\","
               + "\"last_name\": \"Hackman\","
               + "\"birth_date\": 1930,"
               + "\"role\": \"Little Bill Dagget\""
               + "}]"),
       mapper.readTree(extractor.extractAsString(json, q)));
 }
 @Test
 public void testBooleanAndTermQuery() throws ProjectionException {
   NodeBooleanQuery bq = new NodeBooleanQuery();
   NodePhraseQuery pq = new NodePhraseQuery();
   pq.add(new Term("summary", "Whisky"));
   pq.add(new Term("summary", "Big"));
   bq.add(pq, Occur.MUST);
   assertEquals(json, extractor.extractAsString(json, bq));
 }
  @Test
  public void testTwoPrimitiveVariables()
      throws QueryNodeException, ProjectionException, IOException {
    String twig0 =
        "\"twig\": {"
            + "\"root\":\"title\""
            + ",\"child\":["
            + "{\"occur\":\"MUST\""
            + ",\"node\" : { \"query\" : \"Unforgiven\" }"
            + "}"
            + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
            + "]"
            + "}";

    String twig1 =
        "\"twig\": {"
            + "\"root\":\"country\""
            + ",\"child\":["
            + "{\"occur\":\"MUST\""
            + ",\"node\" : { \"query\" : \"USA\" }"
            + "}"
            + ", {\"occur\" : \"MUST\", \"variable\" : {}}"
            + "]"
            + "}";
    Query q =
        parser.parse(
            "{\"boolean\":{\"clause\":[{\"occur\":\"MUST\","
                + twig0
                + "},{\"occur\":\"MUST\","
                + twig1
                + "}]}}",
            "");
    SimpleJsonByQueryExtractor extractor = new SimpleJsonByQueryExtractor();
    assertEquals(
        mapper.readTree("{\"country\":\"USA\",\"title\":\"Unforgiven\"}"),
        mapper.readTree(extractor.extractAsString(json, q)));
  }