/*
 This software was produced for the U. S. Government
 under Contract No. W15P7T-11-C-F600, and is
 subject to the Rights in Noncommercial Computer Software
 and Noncommercial Computer Software Documentation
 Clause 252.227-7014 (JUN 1995)

 Copyright 2013 The MITRE Corporation. All Rights Reserved.

 Licensed 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.opensextant.solrtexttagger;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

/**
 * Tests that we can skip serialization of the documents when embedding
 * Solr.
 *
 * @author David Smiley - dsmiley@mitre.org
 */
public class EmbeddedSolrNoSerializeTest extends SolrTestCaseJ4 {

  EmbeddedSolrServer solrServer;

  @Before
  public void setUp() throws Exception {
    super.setUp();
    initCore("solrconfig.xml", "schema.xml");
    solrServer = new NoSerializeEmbeddedSolrServer(h.getCoreContainer(), null);
    //we don't need to close the EmbeddedSolrServer because SolrTestCaseJ4 closes the core
    clearIndex();
  }

  @Test
  public void testTag() throws SolrServerException, IOException {
    assertU(adoc("id", "9999", "name", "Boston"));
    assertU(commit());

    ModifiableSolrParams params = params();
    String input = "foo boston bar";//just one tag;
    QueryRequest req = new SolrTaggerRequest(params, input);
    req.setPath("/tag");

    QueryResponse rsp = req.process(solrServer);
    SolrDocumentList results= (SolrDocumentList) rsp.getResponse().get("matchingDocs");
    assertNotNull(rsp.getResponse().get("tags"));
    assertNotNull(results.get(0));
  }

  @SuppressWarnings("serial")
  public static class SolrTaggerRequest extends QueryRequest {

    private final String input;

    public SolrTaggerRequest(SolrParams p, String input) {
      super(p, METHOD.POST);
      this.input = input;
    }

    @Override
    public Collection<ContentStream> getContentStreams() {
      return Collections.singleton((ContentStream) new ContentStreamBase
          .StringStream(input));
    }
  }

  @Test
  public void testSearch() throws SolrServerException {
    assertU(adoc("id", "9999", "name", "Boston"));
    assertU(commit());

    QueryResponse rsp = solrServer.query(params("q", "name:Boston"));
    assertNotNull(rsp.getResults().get(0));
  }
}

@@ -1440,16 +1440,46 @@
 Before;%0A
+import org.junit.BeforeClass;%0A
 import o
@@ -1787,16 +1787,23 @@
 J4 %7B%0A%0A  
+static 
 Embedded
@@ -1835,16 +1835,21 @@
  @Before
+Class
 %0A  publi
@@ -1854,18 +1854,24 @@
 lic 
-void setUp
+static void init
 () t
@@ -1892,27 +1892,8 @@
 n %7B%0A
-    super.setUp();%0A
     
@@ -2111,36 +2111,20 @@
 e%0A  
-  clearIndex();%0A  %7D%0A%0A  @Test
+%7D%0A%0A  @Before
 %0A  p
@@ -2138,15 +2138,13 @@
 oid 
-testTag
+setUp
 () t
@@ -2153,42 +2153,56 @@
 ows 
-SolrServerException, IOException %7B
+Exception %7B%0A    super.setUp();%0A    clearIndex();
 %0A   
@@ -2268,24 +2268,102 @@
 (commit());%0A
+  %7D%0A%0A  @Test%0A  public void testTag() throws SolrServerException, IOException %7B
 %0A    Modifia
@@ -3267,83 +3267,8 @@
 n %7B%0A
-    assertU(adoc(%22id%22, %229999%22, %22name%22, %22Boston%22));%0A    assertU(commit());%0A%0A
     

Fix test; it seems initCore() should be done from a @BeforeClass or else multiple Tests will cause a hang