package org.apache.solr.search;

/*
 * 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 org.apache.solr.BaseDistributedSearchTestCase;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
import org.junit.BeforeClass;

/**
 * Test for QueryComponent's distributed querying
 *
 * @see org.apache.solr.handler.component.QueryComponent
 */
public class AnalyticsMergeStrategyTest extends BaseDistributedSearchTestCase {

  public AnalyticsMergeStrategyTest() {
    fixShardCount = true;
    shardCount = 3;
    stress = 0;
  }

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    initCore("solrconfig-analytics-query.xml", "schema15.xml");
  }

  @Override
  public void doTest() throws Exception {
    del("*:*");

    index_specific(0,"id","1", "sort_i", "5");
    index_specific(0,"id","2", "sort_i", "50");
    index_specific(1,"id","5", "sort_i", "4");
    index_specific(1,"id","6", "sort_i", "10");
    index_specific(0,"id","7", "sort_i", "1");
    index_specific(1,"id","8", "sort_i", "2");
    index_specific(2,"id","9", "sort_i", "1000");
    index_specific(2,"id","10", "sort_i", "1500");
    index_specific(2,"id","11", "sort_i", "1300");
    index_specific(1,"id","12", "sort_i", "15");
    index_specific(1,"id","13", "sort_i", "16");

    commit();

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("fq", "{!count}");
    setDistributedParams(params);
    QueryResponse rsp = queryServer(params);
    assertCount(rsp, 11);

    params = new ModifiableSolrParams();
    params.add("q", "id:(1 2 5 6)");
    params.add("fq", "{!count}");
    setDistributedParams(params);
    rsp = queryServer(params);
    assertCount(rsp, 4);
  }

  private void assertCount(QueryResponse rsp, int count) throws Exception {
    NamedList response = rsp.getResponse();
    NamedList analytics = (NamedList)response.get("analytics");
    Integer c = (Integer)analytics.get("mycount");
    if(c.intValue() != count) {
      throw new Exception("Count is not correct:"+count+":"+c.intValue());
    }

    long numFound = rsp.getResults().getNumFound();
    if(c.intValue() != numFound) {
      throw new Exception("Count does not equal numFound:"+c.intValue()+":"+numFound);
    }
  }
}
