package org.apache.solr.uima.processor;

/**
 * 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.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.MultiMapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.XmlUpdateRequestHandler;
import org.apache.solr.request.SolrQueryRequestBase;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.processor.UpdateRequestProcessorChain;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * TestCase for {@link UIMAUpdateRequestProcessor}
 * 
 * @version $Id$
 */
public class UIMAUpdateRequestProcessorTest extends SolrTestCaseJ4 {

  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore("solrconfig.xml", "schema.xml", "solr-uima");
  }

  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();
    clearIndex();
    assertU(commit());
  }

  @Test
  public void testProcessorConfiguration() {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain chained = core.getUpdateProcessingChain("uima");
    assertNotNull(chained);
    UIMAUpdateRequestProcessorFactory factory = (UIMAUpdateRequestProcessorFactory) chained
            .getFactories()[0];
    assertNotNull(factory);
  }

  @Test
  public void testProcessing() throws Exception {
    // this test requires an internet connection (e.g. opencalais api)
    checkInternetConnection();

    addDoc(adoc(
            "id",
            "2312312321312",
            "text",
            "SpellCheckComponent got improvement related to recent Lucene changes. \n  "
                    + "Add support for specifying Spelling SuggestWord Comparator to Lucene spell "
                    + "checkers for SpellCheckComponent. Issue SOLR-2053 is already fixed, patch is"
                    + " attached if you need it, but it is also committed to trunk and 3_x branch."
                    + " Last Lucene European Conference has been held in Prague."));
    assertU(commit());
    assertQ(req("language:english"), "//*[@numFound='1']");
  }

  @Test
  public void testTwoUpdates() {
    // this test requires an internet connection (e.g. opencalais api)
    checkInternetConnection();

    try {
      addDoc(adoc("id", "1", "text", "The Apache Software Foundation is happy to announce "
              + "BarCampApache Sydney, Australia, the first ASF-backed event in the Southern "
              + "Hemisphere!"));
      assertU(commit());
      assertQ(req("language:english"), "//*[@numFound='1']");

      addDoc(adoc("id", "2", "text", "Taking place 11th December 2010 at the University "
              + "of Sydney's Darlington Centre, the BarCampApache \"unconference\" will be"
              + " attendee-driven, facilitated by members of the Apache community and will "
              + "focus on the Apache..."));
      assertU(commit());
      assertQ(req("language:english"), "//*[@numFound='2']");

    } catch (Exception e) {
      assumeNoException("Multiple updates on same instance didn't work", e);
    }
  }

  private void addDoc(String doc) throws Exception {
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put(UpdateParams.UPDATE_PROCESSOR, new String[] { "uima" });
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), (SolrParams) mmparams) {
    };

    XmlUpdateRequestHandler handler = new XmlUpdateRequestHandler();
    handler.init(null);
    ArrayList<ContentStream> streams = new ArrayList<ContentStream>(2);
    streams.add(new ContentStreamBase.StringStream(doc));
    req.setContentStreams(streams);
    handler.handleRequestBody(req, new SolrQueryResponse());
  }

  private void checkInternetConnection() {
    try {
      URLConnection conn = new URL("http://www.apache.org/").openConnection();
      conn.setConnectTimeout(5000);
      conn.setReadTimeout(5000);
      conn.connect();
    } catch (Exception ex) {
      assumeNoException("This test requires an internet connection", ex);
    }
  }
}

@@ -1589,16 +1589,41 @@
 eClass;%0A
+import org.junit.Ignore;%0A
 import o
@@ -2401,32 +2401,107 @@
 );%0A  %7D%0A%0A  @Test%0A
+  @Ignore(%22unstable: see https://issues.apache.org/jira/browse/SOLR-2387%22)%0A
   public void te

disable test