/*
** Module   : HttpResponsePathExists.java
** Abstract : A test for a path in the HTTP response body.
**
** Copyright (c) 2021-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20210827 Created initial version.
** 002 CA  20220118 The test will return the path's value, and not true/false.
*/

/*
** 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()
   {
      Object value = service.checkPathExists(path);
      if (value == null)
      {
         setStatus(Status.FAILED, String.format("Path '%s' does not exist", path));

         return null;
      }
      
      return value;
   }
}
