!******************************************************************************!
! This tests the FtpResource Class by writing to and reading from an ftp server.
!
! @bug      NXM-3985 FTP testing needed - uses commons.net
! @since    NeXtMidas 4.1.3
! @author:  Zachary Matthews
!******************************************************************************!
startmacro S:ftpserver["ftp://nxmservices"] S:user["nxmftpuser"] S:pass["NeXtMidas"] S:ftpdir["upload"] D:timeout[0.5]
  switch INTERACTIVE interAct   GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline   GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE

  ! Since NeXtMidas 4.1.4
  invoke isEmptyVersion nxm.sys.lib.FtpResource.getIsEmptyVersion()

  if isEmptyVersion isFalse then
    res runningtest "YES"
    if alttestenv then
      res urlFileAltFtp "nextmidas_url_alt_ftp.txt"
      if urlFileAltFtp nfexists then
        warn "^reg.main.name: SKIPPING since running in alternate test environment because ^{urlFileAltFtp} not found"
        res runningtest "NO"
      else
        file open tag urlFileAltFtp
        file read tag ftpserver
        file close tag
      endif
    elseif interact then
      popup/blocking=macro RESULT=runningtest FUNC=YESNO MESSAGE="Do you have an FTP server set up on your box?"
      if runningtest eqs "NO" then
        warn "^reg.main.name is being skipped, please run again later ALTTESTENV setup or FTP server"
      endif
    else
      res runningtest "NO"
      warn "^reg.main.name: SKIPPING since user may not have FTP server available"
    endif
  else
    warn "^reg.main.name: SKIPPING since running with the empty version of FtpResource."
    res runningtest "NO"
  endif

  if runningtest EQS "YES" then
    call setDatDir

     set outfile "mytest1000file_^{env.hostname}_^{env.pid}.tmp"
     set infile  "mybrandnew1000file_^{env.hostname}_^{env.pid}.tmp"
     res Z:ftpTestFailed false

     ! Write out to ftp server
     noop ^{datDir}.test1000.prm ^{ftpserver}/^{outfile}{USER="^{user}",PASS="^{pass}",ftpdir="^{ftpdir}",timeout=^{timeout},sotimeout=^{timeout}}
     assert/text="^reg.main.name: File written out to ftp server" ftpTestFailed ISFALSE
     set ftpTestFailed false

     ! Read in from ftp server
     noop ^{ftpserver}/^{outfile}{USER="^{user}",PASS="^{pass}",ftpdir="^{ftpdir}",timeout=^{timeout},sotimeout=^{timeout}} infile
     assert/text="^reg.main.name: File read in from ftp server" ftpTestFailed ISFALSE

    if infile fexists then erase infile
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  elseif "Could not open ^{ftpserver}/^{outfile}" SUBS "^{emsg.data}" then
    set ftpTestFailed true
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
