/*
 * 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.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

import org.apache.commons.io.FileUtils;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;

public class PingRequestHandlerTest extends SolrTestCaseJ4 {

  private final String fileName = this.getClass().getName() + ".server-enabled";
  private File healthcheckFile = null;
  private PingRequestHandler handler = null;
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore("solrconfig.xml", "schema.xml");
  }

  @Before
  public void before() throws IOException {

    // by default, use relative file in dataDir
    healthcheckFile = new File(dataDir, fileName);
    String fileNameParam = fileName;

    // sometimes randomly use an absolute File path instead 
    if (random().nextBoolean()) {
      healthcheckFile = new File(TEMP_DIR, fileName);
      fileNameParam = healthcheckFile.getAbsolutePath();
    } 
      
    if (healthcheckFile.exists()) FileUtils.forceDelete(healthcheckFile);

    handler = new PingRequestHandler();
    NamedList initParams = new NamedList();
    initParams.add(PingRequestHandler.HEALTHCHECK_FILE_PARAM,
                   fileNameParam);
    handler.init(initParams);
    handler.inform(h.getCore());
  }
  
  public void testPingWithNoHealthCheck() throws Exception {
    
    // for this test, we don't want any healthcheck file configured at all
    handler = new PingRequestHandler();
    handler.init(new NamedList());
    handler.inform(h.getCore());

    SolrQueryResponse rsp = null;
    
    rsp = makeRequest(handler, req());
    assertEquals("OK", rsp.getValues().get("status"));
    
    rsp = makeRequest(handler, req("action","ping"));
    assertEquals("OK", rsp.getValues().get("status")); 

  }
  @Ignore
  public void testEnablingServer() throws Exception {

    assertTrue(! healthcheckFile.exists());

    // first make sure that ping responds back that the service is disabled

    try {
      makeRequest(handler, req());
      fail("Should have thrown a SolrException because not enabled yet");
    } catch (SolrException se){
      assertEquals(SolrException.ErrorCode.SERVICE_UNAVAILABLE.code,se.code());
    }

    // now enable

    makeRequest(handler, req("action", "enable"));

    assertTrue(healthcheckFile.exists());
    assertNotNull(FileUtils.readFileToString(healthcheckFile), "UTF-8");

    // now verify that the handler response with success

    SolrQueryResponse rsp = makeRequest(handler, req());
    assertEquals("OK", rsp.getValues().get("status"));

    // enable when already enabled shouldn't cause any problems
    makeRequest(handler, req("action", "enable"));
    assertTrue(healthcheckFile.exists());

  }
  @Ignore
  public void testDisablingServer() throws Exception {

    assertTrue(! healthcheckFile.exists());
        
    healthcheckFile.createNewFile();

    // first make sure that ping responds back that the service is enabled

    SolrQueryResponse rsp = makeRequest(handler, req());
    assertEquals("OK", rsp.getValues().get("status"));

    // now disable
    
    makeRequest(handler, req("action", "disable"));
    
    assertFalse(healthcheckFile.exists());

    // now make sure that ping responds back that the service is disabled

    try {
      makeRequest(handler, req());
      fail("Should have thrown a SolrException because not enabled yet");
    } catch (SolrException se){
      assertEquals(SolrException.ErrorCode.SERVICE_UNAVAILABLE.code,se.code());
    }
    
    // disable when already disabled shouldn't cause any problems
    makeRequest(handler, req("action", "disable"));
    assertFalse(healthcheckFile.exists());
    
  }

  
  public void testGettingStatus() throws Exception {
    SolrQueryResponse rsp = null;

    handler.handleEnable(true);
    
    rsp = makeRequest(handler, req("action", "status"));
    assertEquals("enabled", rsp.getValues().get("status"));
 
    handler.handleEnable(false);   
    
    rsp = makeRequest(handler, req("action", "status"));
    assertEquals("disabled", rsp.getValues().get("status"));
 
  }
  
  public void testBadActionRaisesException() throws Exception {
    
    try {
      SolrQueryResponse rsp = makeRequest(handler, req("action", "badaction"));
      fail("Should have thrown a SolrException for the bad action");
    } catch (SolrException se){
      assertEquals(SolrException.ErrorCode.BAD_REQUEST.code,se.code());
    }
  }

  /**
   * Helper Method: Executes the request against the handler, returns 
   * the response, and closes the request.
   */
  private SolrQueryResponse makeRequest(PingRequestHandler handler,
                                        SolrQueryRequest req) 
    throws Exception {

    SolrQueryResponse rsp = new SolrQueryResponse();
    try {
      handler.handleRequestBody(req, rsp);
    } finally {
      req.close();
    }
    return rsp;
  }

}

@@ -2878,34 +2878,24 @@
 s%22)); %0A%0A  %7D%0A
-  @Ignore%0A
   public voi
@@ -2928,32 +2928,32 @@
 ows Exception %7B%0A
+
 %0A    assertTrue(
@@ -3058,32 +3058,43 @@
 isabled%0A
-%0A
     
-try %7B%0A     
+SolrQueryResponse sqr =
  makeReq
@@ -3123,91 +3123,134 @@
     
-  fail(%22Should have thrown a SolrException because not enabled yet%22);%0A    %7D catch (
+SolrException se = (SolrException) sqr.getException();%0A    assertEquals(%0A      %22Response should have been replaced with a 503 
 Solr
@@ -3262,33 +3262,29 @@
 tion
- se)%7B
+.%22,
 %0A      
-assertEquals(
+se.code(), 
 Solr
@@ -3327,34 +3327,18 @@
 BLE.code
-,se.code());%0A    %7D
+);
 %0A%0A    //
@@ -3855,18 +3855,8 @@
   %7D%0A
-  @Ignore%0A
   pu
@@ -4389,25 +4389,40 @@
 bled
-%0A%0A
     
-try %7B%0A     
+%0A    SolrQueryResponse sqr =
  mak
@@ -4455,91 +4455,134 @@
     
-  fail(%22Should have thrown a SolrException because not enabled yet%22);%0A    %7D catch (
+SolrException se = (SolrException) sqr.getException();%0A    assertEquals(%0A      %22Response should have been replaced with a 503 
 Solr
@@ -4594,33 +4594,29 @@
 tion
- se)%7B
+.%22,
 %0A      
-assertEquals(
+se.code(), 
 Solr
@@ -4659,34 +4659,18 @@
 BLE.code
-,se.code());%0A    %7D
+);
 %0A    %0A  

Fix tests that are invalid due to logging change