/**
 * 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.
 */

package org.apache.solr.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.common.params.*;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryRequestBase;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.util.AbstractSolrTestCase;


/**
 * TODO -- this needs to actually test the results/query etc
 */
public class MoreLikeThisHandlerTest extends AbstractSolrTestCase {

  @Override public String getSchemaFile() { return "schema.xml"; }
  @Override public String getSolrConfigFile() { return "solrconfig.xml"; }
  @Override public void setUp() throws Exception {
    super.setUp();
    lrf = h.getRequestFactory("standard", 0, 20 );
  }
  
  
  public void testInterface() throws Exception
  {
    SolrCore core = h.getCore();
    MoreLikeThisHandler mlt = new MoreLikeThisHandler();
    
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrQueryRequestBase req = new SolrQueryRequestBase( core, params) {};
    
    // requires 'q' or single content stream
    try {
      mlt.handleRequestBody( req, new SolrQueryResponse() );
    }
    catch( Exception ex ) {} // expected

    // requires 'q' or single content stream
    try {
      ArrayList<ContentStream> streams = new ArrayList<ContentStream>( 2 );
      streams.add( new ContentStreamBase.StringStream( "hello" ) );
      streams.add( new ContentStreamBase.StringStream( "there" ) );
      req.setContentStreams( streams );
      mlt.handleRequestBody( req, new SolrQueryResponse() );
    }
    catch( Exception ex ) {} // expected
    
    assertU(adoc("id","42","name","Tom Cruise","subword","Top Gun","subword","Risky Business","subword","The Color of Money","subword","Minority Report","subword", "Days of Thunder","subword", "Eyes Wide Shut","subword", "Far and Away", "foo_ti","10"));
    assertU(adoc("id","43","name","Tom Hanks","subword","The Green Mile","subword","Forest Gump","subword","Philadelphia Story","subword","Big","subword","Cast Away", "foo_ti","10"));
    assertU(adoc("id","44","name","Harrison Ford","subword","Star Wars","subword","Indiana Jones","subword","Patriot Games","subword","Regarding Henry"));
    assertU(adoc("id","45","name","George Harrison","subword","Yellow Submarine","subword","Help","subword","Magical Mystery Tour","subword","Sgt. Peppers Lonley Hearts Club Band"));
    assertU(adoc("id","46","name","Nicole Kidman","subword","Batman","subword","Days of Thunder","subword","Eyes Wide Shut","subword","Far and Away"));
    assertU(commit());

    params.set(CommonParams.Q, "id:42");
    params.set(MoreLikeThisParams.MLT, "true");
    params.set(MoreLikeThisParams.SIMILARITY_FIELDS, "name,subword,foo_ti");
    params.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
    params.set(MoreLikeThisParams.MIN_TERM_FREQ,"1");
    params.set(MoreLikeThisParams.MIN_DOC_FREQ,"1");
    params.set("indent","true");

    SolrQueryRequest mltreq = new LocalSolrQueryRequest( core, params);
    assertQ("morelikethis - tom cruise",mltreq
        ,"//result/doc[1]/int[@name='id'][.='46']"
        ,"//result/doc[2]/int[@name='id'][.='43']");
    
    params.set(CommonParams.Q, "id:44");
    assertQ("morelike this - harrison ford",mltreq
        ,"//result/doc[1]/int[@name='id'][.='45']");

    // test that qparser plugins work
    params.set(CommonParams.Q, "{!field f=id}44");
    assertQ(mltreq
        ,"//result/doc[1]/int[@name='id'][.='45']");

    params.set(CommonParams.Q, "id:42");
    params.set(MoreLikeThisParams.QF,"name^5.0 subword^0.1");
    assertQ("morelikethis with weights",mltreq
        ,"//result/doc[1]/int[@name='id'][.='43']"
        ,"//result/doc[2]/int[@name='id'][.='46']");


    // test that qparser plugins work w/ the MoreLikeThisHandler
    params.set(CommonParams.QT, "/mlt");
    params.set(CommonParams.Q, "{!field f=id}44");
    assertQ(mltreq
        ,"//result/doc[1]/int[@name='id'][.='45']");

    // test that debugging works
    params.set(CommonParams.QT, "/mlt");
    params.set("debugQuery", "true");
    assertQ(mltreq
        ,"//result/doc[1]/int[@name='id'][.='45']"
        ,"//lst[@name='debug']/lst[@name='explain']"
    );

    // params.put(MoreLikeThisParams.QF,new String[]{"foo_ti"});
    // String response = h.query(mltreq);
    // System.out.println(response);

  }
}

@@ -908,16 +908,55 @@
 l.Map;%0A%0A
+import org.apache.solr.SolrTestCaseJ4;%0A
 import o
@@ -1386,16 +1386,69 @@
 stCase;%0A
+import org.junit.BeforeClass;%0Aimport org.junit.Test;%0A
 %0A%0A/**%0A *
@@ -1555,24 +1555,16 @@
 extends 
-Abstract
 SolrTest
@@ -1571,221 +1571,138 @@
 Case
+J4
  %7B%0A%0A  @
-Override public String getSchemaFile() %7B return %22schema.xml%22; %7D%0A  @Override public String getSolrConfigFile() %7B return %22solrconfig.xml%22; %7D%0A  @Override public void setUp() throws Exception %7B%0A    super.setUp(
+BeforeClass%0A  public static void moreLikeThisBeforeClass() throws Exception %7B%0A    initCore(%22solrconfig.xml%22, %22schema.xml%22
 );%0A 
@@ -1755,21 +1755,24 @@
  );%0A  %7D%0A
-  
 %0A  
+@Test
 %0A  publi
@@ -2631,16 +2631,55 @@
 xpected%0A
+    finally %7B%0A      req.close();%0A    %7D%0A
     %0A   

fix resource leak