/*
** Module   : CheckCursorPos.java
** Abstract : directive to query the cursor position (row/col) and compare it
**            to an expected position
**
** Copyright (c) 2009-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20090126 Directive to query the cursor position (row/col) and compare it to an expected position.
** 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 query the cursor position (row/col) and compare it to an
 * expected position.
 */
public class CheckCursorPos
extends Report
implements TerminalTestable
{
   /** Short name for the activity. */
   private static final String NAME = "CHECK-CURSOR-POS";
   
   /** Format specification for creating a description of this step. */
   private static final String SPEC = "expected row = '%d'; " +
                                      "expected column = '%d';" +
                                      "actual row = '%d'; " +
                                      "actual column = '%d';";
   
   /** The expected row number to compare against. */
   private int expRow = -1;
   
   /** The expected column number to compare against. */
   private int expCol = -1;
   
   /**
    * Constructor with both row and column comparison data. At least one of the
    * comparison values must be specified, but the other can have checking
    * disabled by passing -1 for the value.
    *
    * @param    expRow
    *           The expected row number to compare against. Use -1 to disable
    *           checking of the row. If -1 is passed then the column value
    *           must be specified.
    * @param    expCol
    *           The expected column number to compare against. Use -1 to disable
    *           checking of the column. If -1 is passed then the row value
    *           must be specified.
    */
   public CheckCursorPos(int expRow, int expCol)
   {
      super(NAME, "");
      this.expRow = expRow;
      this.expCol = expCol;
   }
   
   /**
    * 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)
   {
      int actualRow = term.getCursorRow();
      int actualCol = term.getCursorColumn();
      
      setDescription(String.format(SPEC, expRow, expCol, actualRow, actualCol));
      
      boolean rowResult = false;
      boolean colResult = false;
      
      Status status = Status.FAILED;
      String reason = null;
      
      if (expRow == -1 && expCol == -1)
      {
         reason = "No comparison data specified for either row or column.";
      }
      else if (expRow == -1)
      {
         rowResult = true;
      }
      else if (expCol == -1)
      {
         colResult = true;
      }
      
      if (expRow != -1 && expRow == actualRow)
      {
         rowResult = true;
      }
      
      if (expCol != -1 && expCol == actualCol)
      {
         colResult = true;
      }
      
      if (rowResult && colResult)
      {
         status = Status.PASSED;
      }
      
      if (reason == null)
      {
         if (!rowResult && !colResult)
         {
            reason = "Actual row and actual column is not the same as the " +
                     "expected row and expected column.";
         }
         else if (!rowResult && colResult)
         {
            reason = "Actual row is not the same as the expected row.";
         }
         else if (!colResult && rowResult)
         {
            reason = "Actual column is not the same as the expected column.";
         }
         else
         {
            // no failure
            reason = "";
         }
      }
      
      setStatus(status, reason);
   }
}
