import java.io.*;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.*;

/**
 * Title:       GFChunkedEncodingTest
 * @author:     Ian Ibbotson (ian.ibbotson@k-int.com)
 * Company:     Knowledge Integration Ltd.
 * Description: Cause GlassFish v3 (build 74.2) to randomly throw either an "invalid chunk header" or "Unexpected EOF in prolog  at [row,col {unknown-source}]: [1,0]"
 */
public class GFChunkedEncodingTest extends TestCase {

  /**
   * Assembles and returns a test suite for all the test methods of this test case.
   * @return A non-null test suite.
   */
  public static Test suite() {
    TestSuite suite = new TestSuite(GFChunkedEncodingTest.class);
    return suite;
  }

  /**
   * Runs the test case.
   */
  public static void main(String args[]) {
    junit.textui.TestRunner.run(suite());
  }

  public GFChunkedEncodingTest(String name) {
    super(name);
  }

  protected void setUp() {
  }

  protected void tearDown() {
  }

  public void testChunkedEncoding() throws org.apache.solr.client.solrj.SolrServerException, java.io.IOException {
    try {
      // Address of solr 1.4.0 deployed on glassfish, with context name as solr rather than apache-solr-1.4.0
      String solr_server_url = "http://localhost:8080/solr";

      // Create a sold input document that will store the fields we want to send
      org.apache.solr.common.SolrInputDocument index_properties = null;
      UpdateResponse resp = null;

      // SOLR connection, uses chunked HTTP to send java objects over the http connection
      SolrServer solr = new CommonsHttpSolrServer(solr_server_url);

      // Step 1: Submit a document with an invlalid solr field, this will cause SOLR to error, and perhaps truncate the stream.

      // Add some fields
      try {
        index_properties = new org.apache.solr.common.SolrInputDocument();
        index_properties.addField("id","111111");
        index_properties.addField("tootle","the tootle field does not exist and will cause an error on the return stream");
        index_properties.addField("description","the description");
        // Send the update
        resp = solr.add(index_properties);
        System.out.println(resp);
      }
      catch ( Exception e ) {
        System.err.println("We expect this exception to be thrown because of the invalid field, all is OK");
      }

      // Step 2 - Add a real document that should work. Hopefully the chunked encoding will now be broken
      index_properties = new org.apache.solr.common.SolrInputDocument();
      index_properties.addField("id","111111");
      index_properties.addField("title","the tootle field does not exist and will cause an error on the return stream");
      index_properties.addField("description","the description");
      // Send the update
      resp = solr.add(index_properties);
      System.out.println(resp);

    }
    catch ( java.net.MalformedURLException murle ) {
      murle.printStackTrace();
    }
  }
}

