<commit_before>package org.apache.solr.handler.dataimport;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter;
import org.apache.lucene.search.join.BitDocIdSetFilter;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.apache.solr.handler.dataimport.config.ConfigNameConstants;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.SolrIndexSearcher;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test for DocBuilder using the test harness. 
 * <b> Documents are hierarchical in this test, i.e. each document have nested children documents.</b>
 */
public class TestHierarchicalDocBuilder extends AbstractDataImportHandlerTestCase {

  private static final String FIELD_ID = "id";
  private int id = 0; //unique id
  private SolrQueryRequest req;
  
  private static class ContextHolder {
    int counter = 0;
    List<Hierarchy> hierarchies = new ArrayList<Hierarchy>();
  }
  
  private static class Hierarchy {
    String elementType;
    Map<String, Object> elementData = new HashMap<String,Object>();
    List<Hierarchy> elements = new ArrayList<Hierarchy>();
  }
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore("dataimport-solrconfig.xml", "dataimport-schema.xml");    
  }
  
  @Before
  public void before() {
    req = req("*:*"); // don't really care about query
    MockDataSource.clearCache();
  }
  
  @After
  public void after() {
    req.close();
    MockDataSource.clearCache();
  }

  @Test
  public void testThreeLevelHierarchy() throws Exception {
    int parentsNum = 3; //fixed for simplicity of test
    int childrenNum = 0;
    int grandChildrenNum = 0;
    
    final String parentType = "parent";
    final String childType = "child";
    final String grandChildType = "grand_child";

    List<String> parentIds = createDataIterator("select * from PARENT", parentType, parentType, parentsNum);
    Collections.shuffle(parentIds);
    String parentId1 = parentIds.get(0);
    String parentId2 = parentIds.get(1);
    
    //parent 1 children
    int firstParentChildrenNum = 3; //fixed for simplicity of test
    String select = "select * from CHILD where parent_id='" + parentId1 + "'";
    List<String> childrenIds = createDataIterator(select, childType, "child of first parent", firstParentChildrenNum);
    List<String> firstParentChildrenIds = new ArrayList<String>(childrenIds);
    childrenNum += childrenIds.size();
    
    // grand children of first parent first child
    String childId = childrenIds.get(0);
    String description = "grandchild of first parent, child of " + childId + " child";
    select = "select * from GRANDCHILD where parent_id='" + childId + "'";
    List<String> grandChildrenIds = createDataIterator(select, grandChildType, description, atLeast(2));<commit_msg>SOLR-5147 remove forbidden API<commit_after>package org.apache.solr.handler.dataimport;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter;
import org.apache.lucene.search.join.BitDocIdSetFilter;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.apache.solr.handler.dataimport.config.ConfigNameConstants;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.SolrIndexSearcher;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test for DocBuilder using the test harness. 
 * <b> Documents are hierarchical in this test, i.e. each document have nested children documents.</b>
 */
public class TestHierarchicalDocBuilder extends AbstractDataImportHandlerTestCase {

  private static final String FIELD_ID = "id";
  private int id = 0; //unique id
  private SolrQueryRequest req;
  
  private static class ContextHolder {
    int counter = 0;
    List<Hierarchy> hierarchies = new ArrayList<Hierarchy>();
  }
  
  private static class Hierarchy {
    String elementType;
    Map<String, Object> elementData = new HashMap<String,Object>();
    List<Hierarchy> elements = new ArrayList<Hierarchy>();
  }
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore("dataimport-solrconfig.xml", "dataimport-schema.xml");    
  }
  
  @Before
  public void before() {
    req = req("*:*"); // don't really care about query
    MockDataSource.clearCache();
  }
  
  @After
  public void after() {
    req.close();
    MockDataSource.clearCache();
  }

  @Test
  public void testThreeLevelHierarchy() throws Exception {
    int parentsNum = 3; //fixed for simplicity of test
    int childrenNum = 0;
    int grandChildrenNum = 0;
    
    final String parentType = "parent";
    final String childType = "child";
    final String grandChildType = "grand_child";

    List<String> parentIds = createDataIterator("select * from PARENT", parentType, parentType, parentsNum);
    Collections.shuffle(parentIds, random());
    String parentId1 = parentIds.get(0);
    String parentId2 = parentIds.get(1);
    
    //parent 1 children
    int firstParentChildrenNum = 3; //fixed for simplicity of test
    String select = "select * from CHILD where parent_id='" + parentId1 + "'";
    List<String> childrenIds = createDataIterator(select, childType, "child of first parent", firstParentChildrenNum);
    List<String> firstParentChildrenIds = new ArrayList<String>(childrenIds);
    childrenNum += childrenIds.size();
    
    // grand children of first parent first child
    String childId = childrenIds.get(0);
    String description = "grandchild of first parent, child of " + childId + " child";
    select = "select * from GRANDCHILD where parent_id='" + childId + "'";
    List<String> grandChildrenIds = createDataIterator(select, grandChildType, description, atLeast(2));
{'fraction_non_alphanumeric': 0.09647194910352805, 'fraction_numerical': 0.004626951995373048, 'mean_word_length': 4.284841075794621, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6993188', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2413, 'n_words': 755}