/*
** Module   : SubstitutionReport.java
** Abstract : base class for steps that allow runtime substitutions
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090227 Base class for steps that allow runtime substitutions.
** 002 CA  20210827 Refactored to allow other test types, beside terminal (which can be executed from the same 
**                  test suite).  Added SOAP and REST web service tests.
*/

/*
** 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.*;
import com.goldencode.harness.terminal.*;

/**
 * Base class for steps that allow runtime substitutions.
 */
public abstract class SubstitutionReport
extends Report
implements TerminalTestable
{
   /** Substitution list. */
   private Substitution[] subs = null;
   
   /**
    * Construct an instance with no runtime substitutions.
    *
    * @param    name
    *           Short name of the testable activity.
    * @param    descr
    *           Verbose description of the testable activity.
    */
   public SubstitutionReport(String name, String descr)
   {
      super(name, descr);
   }
   
   /**
    * Get runtime substitutions.
    *
    * @return   Substitutions to process at runtime or <code>null</code> if
    *           none are configured.
    */
   public Substitution[] getSubstitutions()
   {
      return subs;
   }
   
   /**
    * Set runtime substitutions.
    *
    * @param    subs
    *           Substitutions to process at runtime.
    */
   public void setSubstitutions(Substitution[] subs)
   {
      this.subs = subs;
   }
   
   /**
    * Finds the setter associated with the given field name and this current
    * instance (such a setter can only be used to set the field for this
    * specific instance).
    *
    * @param    parm
    *           The field name.
    *
    * @return   The delegated setter.
    */
   public abstract Setter obtainSetter(String parm);
   
   /**
    * Execute the directive of the test using the given terminal as the data
    * sink and/or source for the operation.
    *
    * @param    term
    *           The terminal that represents the system under test.
    */
   @Override
   public void execute(Terminal term)
   {
      if (subs != null)
      {
         // handle resolutions
         for (Substitution sub : subs)
         {
            sub.resolve();
         }
      }
      
      updateDescription();
      
      // call the worker
      executeWorker(term);
   }
   
   /**
    * Worker for the step-specific test processing which is called after all
    * substitutions have been processed.
    *
    * @param    term
    *           The terminal that represents the system under test.
    */
   protected abstract void executeWorker(Terminal term);
   
   /**
    * Called after substitutions are processed to allow the description
    * to be updated if necessary. This implementation does nothing.
    */
   protected void updateDescription()
   {
      // nothing to do
   }
}
