/*
** Module   : HttpResponsePath.java
** Abstract : A test for a path in the HTTP response body.
**
** Copyright (c) 2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20210827 Created initial version.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import org.apache.http.*;

import com.goldencode.harness.*;

/**
 * A test applied to the response body, when the body is traversable (i.e. a XML or JSON structure).
 */
public abstract class HttpResponsePath
extends Report
implements WebResponseTestable
{
   /** The path to check. */
   protected String path;
   
   /** The variable to save the path's value. */
   protected String variable;

   /** The resource pool to store the variable. */
   protected ResourcePool pool;

   /** The associated web service. */
   protected WebService service;

   /**
    * Configure a new body path test.
    * 
    * @param    path
    *           The path to check.
    * @param    variable
    *           The variable to save the path's value.
    * @param    service
    *           The associated web service.
    */
   public HttpResponsePath(String path, String variable, WebService service)
   {
      this.path = path;
      this.variable = variable;
      this.pool = service.getResourcePool();
      this.service = service;
   }

   /**
    * Test the {@link #path}.
    * 
    * @return   The path's value or <code>null</code> if the test failed (path does not exist or it does not 
    *           match).
    */
   protected abstract Object testPath();
   
   /**
    * Execute the {@link #testPath() test}.  If it passes, {@link #createVariable(String, Object) save the
    * variable}.
    * 
    * @param    response
    *           The HTTP response.
    */
   @Override
   public void execute(HttpResponse response)
   {
      setStatus(Status.PASSED, null);

      Object pathValue = testPath();
      
      if (pathValue != null && variable != null)
      {
         createVariable(variable, pathValue);
      }
   }
   
   /**
    * Create a variable with the specified value.
    * 
    * @param    variable
    *           The variable to save.
    * @param    value
    *           The value to set.
    */
   protected void createVariable(String variable, Object value)
   {
      pool.createVariable(variable, value);
   }
}
