package nxm.sys.test.junit.lib;

import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;
import nxm.sys.test.junit.NMTestCase;

/** Class to test Shell and run CONFIDENCE macros */
public class ShellTest extends NMTestCase{

  /** basic confidence testing */
  private static String BASIC_CONFIDENCE = "conf/alttestenv/unittest=false";
  /** run full baseline testing when midas==null, as this may be a gradle build */
  private static String BASELINE_ADDON = "/baseline/interactive=false";
  private String confidenceCommand;


  /** public constructor to allow running this test case directly with JUnit. */
  public ShellTest (String methodName) { this(methodName, null); }

  /** This constructor is required by NeXtMidas Unit Testing framework.
      @param methodName The name of the method being called.
      @param midas      The midas context to use when running the tests.
   */
  public ShellTest(String methodName, Midas midas) {
    super(methodName, midas);
    confidenceCommand = (midas!=null) ? BASIC_CONFIDENCE : BASIC_CONFIDENCE + BASELINE_ADDON;
  }

  /** Test not only runCommand but also all of alternate test environment confidence macro tests*/
  public void testRunCommand () {
    if (Shell.isGradleBuild()) {
      info("Shell.runCommand("+confidenceCommand+")");
      Shell.runCommand(getMidas(), confidenceCommand);
      // G_STATUS holds the results of confidence testing
      if (getMidas().results.get("G_STATUS") != null) {
        String failCount = ((Table)((Table)(getMidas().results.get("G_STATUS"))).get("ALL")).get("FAIL_COUNT").toString();
        int numFailed = Integer.parseInt(failCount);
        assertEquals("ShellTest " + confidenceCommand + " failed count:",0, numFailed);
      }
    }
    else {
      info("nxm.sys.test.junit.lib.ShellTest only activated with Gradle Build");
    }
  }

}
