/*
** Module   : RunCode.java
** Abstract : directive to execute user-specified Java code
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090126 Directive to execute user-specified Java code.
** 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.*;

/**
 * Directive to execute user-specified Java code. The test step can read and
 * write variables that are in scope. The step will only fail if the called
 * code abends.
 */
public class RunCode
extends Report
implements TerminalTestable
{
   /** Short name for the activity. */
   private static final String NAME = "RUN-CODE";
   
   /** Description of the code. */
   private final String descr;
   
   /** Source code for the module being run. */
   private final String src;
   
   /** Debug flag (forces source code into output). */
   private final boolean debug;
   
   /** The code to execute. */
   private final DynamicCode code;
   
   /** Variables needed during the test. */
   private final Variable[] vars;
   
   /**
    * Constructor.
    *
    * @param    descr
    *           Description of the code.
    * @param    src
    *           The full source code to the module to be run.
    * @param    debug
    *           <code>true</code> to force the source code into the output
    *           reports. Otherwise, the source code will only be written to
    *           the reports on a failure.
    * @param    code
    *           The code to execute. Must not be <code>null</code>.
    * @param    vars
    *           Variables needed during the test. May be <code>null</code> if
    *           there are no embedded variable references.
    */
   public RunCode(String      descr,
                  String      src,
                  boolean     debug,
                  DynamicCode code,
                  Variable[]  vars)
   {
      super(NAME, "");
      this.descr = descr;
      this.src   = src;
      this.debug = debug;
      this.code  = code;
      this.vars  = vars;
   }
   
   /**
    * 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)
   {
      RuntimeException problem = null;
      
      StringBuilder sb = new StringBuilder();
      
      try
      {
         code.execute(vars, term, sb);
      }
      
      catch (RuntimeException re)
      {
         // we only catch this to detect errors below
         problem = re;
         throw re;
      }
      
      finally
      {
         String txt = (descr == null) ? "" : String.format("%s\n", descr);
         
         // output the source code into the report
         if (debug || problem != null)
         {
            txt = String.format("%s%s\n", txt, src);
         }
         
         if (sb.length() > 0)
         {
            txt = String.format("%s-------------- Output " +
                                "--------------\n\n%s",
                                txt,
                                sb.toString());
         }
         
         if (txt.length() > 0)
            setDescription(txt);
      }
      
      setStatus(Status.PASSED, null);
   }
}
