package nxm.sys.test.junit.lib;

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

/** This will test the nxm.sys.lib.NeXtMidas class.

    WARNING: This unit test has not been added to the regular confidence baseline for a reason.
    As stated in its documentation, the NeXtMidas class is intended to be treated as a singleton.
    After this test is run inside a NeXtMidas Shell, global variables are affected.  It is best to
    exit and reenter the shell after this test.

    @author  J. McCall
    @since NeXtMidas 3.5.2 for new methods introduced because of bug 2828
 */
public class NeXtMidasTest extends NMTestCase {

  private NeXtMidas nxm;

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

  /** Creates a new instance of the class.
      @param methodName The name of the method being called.
      @param midas      The midas context to use when running the tests.
   */
  public NeXtMidasTest(String methodName, Midas midas) {
    super(methodName, midas);
    nxm = NeXtMidas.getGlobalInstance();
  }

  //***************** Methods tested elsewhere *******************************
  public void testGetAuxEntry () {}       // tested in testAddAuxEntry
  public void testRemoveAuxEntry () {}    // tested in testAddAuxEntry
  public void testGetDebug () {}          // tested in testSetDebug
  public void isHeadless () {}            // tested in setHeadless
  public void getPath () {}               // tested in setPath

  /**
     Test addAuxEntry, getAuxEntry, and removeAuxEntry
     1) Get Path of non-existent entry (should be null)
     2) Add AUX entry and check path
     3) Remove AUX entry and path should again be null
   */
  public void testAddAuxEntry () {
    final String AUXNAME = "MYTESTAUX_BUG_2828";
    final String expectedPath = nxm.getNmRoot();

    String testPath = nxm.getAuxPath(AUXNAME);
    if (testPath != null) {
      warn(AUXNAME + " already defined, this test will overwrite that AUX entry pointing to: " + testPath);
    }

//    System.out.println("getAux before add:" + nxm.getAux());
    nxm.addAuxEntry(AUXNAME, expectedPath);
    testPath = nxm.getAuxPath(AUXNAME);
    assertEquals("TEST AUX added succcessfully:", expectedPath, testPath);
//    System.out.println("getAux after add:" + nxm.getAux());

    nxm.removeAuxEntry(AUXNAME);
    testPath = nxm.getAuxPath(AUXNAME);
    assertNull("TEST AUX removed succcessfully:", testPath);
//    System.out.println("getAux after remove:" + nxm.getAux());

    // the below should print a warning since that path should not exist on most systems
    String badPath = "/nonexisting/path/test/bug/2828/expected_this_warning/";
    info("A warning is expected below for defining AUX " + AUXNAME);
    nxm.addAuxEntry(AUXNAME, badPath);
  }

  /**
     Test setDebug and getDebug
     1) Set Debug to Trace and check that it was successfully set
     2) Let Debug default by passing in null, and check that it defaulted to Deprecate
   */
  public void testSetDebug () {
    nxm.setDebug("trace");
    assertEquals("Debug set to Trace:", "|Trace|", nxm.getDebug());
    nxm.setDebug(null);
    assertEquals("Debug allowed to default to Deprecate:", "|Deprecate|", nxm.getDebug());
  }

  /**
     Test setHeadless and isHeadless methods
   */
  public void testSetHeadless () {
    nxm.setHeadless(false);
    assertEquals("Headless set to false:", false, nxm.isHeadless());
    nxm.setHeadless(true);
    assertEquals("Headless set to true:", true, nxm.isHeadless());
  }

  /**
     Test setPath and getPath
   */
  public void testSetPath () {
    if(Shell.isGradleBuild()) return;
    String threePath = "UCL,DSP,SYS";
    String sysPath = "SYS";
    nxm.setPath(threePath);
    assertEquals("Path set to: UCL,DSP,SYS",threePath, nxm.getPath());
    nxm.setPath(sysPath);
    assertEquals("Path set to: SYS",sysPath, nxm.getPath());
  }

}