/*
** Module   : HttpResponsePathExists.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 com.goldencode.harness.*;

/**
 * A test applied to the response body, when the body is traversable (i.e. a XML or JSON structure).
 * <p>
 * This test checks that the path actually exists.
 */
public class HttpResponsePathExists
extends HttpResponsePath
{
   /**
    * 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 HttpResponsePathExists(String path, String variable, WebService service)
   {
      super(path, variable, service);
   }

   /**
    * Test that the {@link #path} actually exists.
    * 
    * @return   The path's value or <code>null</code> if the test failed (path does not exist or it does not 
    *           match).
    */
   @Override
   protected Object testPath()
   {
      if (!service.checkPathExists(path))
      {
         setStatus(Status.FAILED, String.format("Path '%s' does not exist", path));

         return false;
      }
      
      return true;
   }
}
