/*
** Module   : HttpResponsePathExact.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's value matches exactly the expected value.
 */
public class HttpResponsePathExact
extends HttpResponsePath
{
   /** The expected value. */
   private String value;

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

   /**
    * 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).
    */
   @Override
   protected Object testPath()
   {
      Object theVal = service.checkPathValueExact(path, value);
      if (theVal == null)
      {
         setStatus(Status.FAILED, String.format("Path '%s' does not match the exact value %s", path, value));
         
         return null;
      }
      
      return theVal;
   }
}
