!******************************************************************************!
! Test Minio ALIAS command
!
! Arguments: None
!
! @bug      NXM-3774 NeXtMidas should add support for minio storage
! @since    NeXtMidas 4.1.4
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  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)

  call testAliasConfig ! tests CONFIG, LIST, and REMOVE
  call testAliasSet    ! tests SET
  call testAliasImport ! tests IMPORT
 
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
  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

!******************************************************************************!
! test ALIAS CONFIG, LIST, and REMOVE 
!******************************************************************************!
procedure testAliasConfig

  ! make config in WRITE AUX and get path to it
  res configFile "test_minio_alias_config.json"
  call createConfigFile configFile
  invoke pathaux1 nxm.sys.lib.Shell.getAuxPath("WRITE")
  res pathConfig "^{pathaux1}^configFile"

  ! Get expected aliases
  TABLE expTbl1 GETKEYS/S alias1
  TABLE expTbl2 GETKEYS/S alias2

  ! make sure test alias keys are not already present
  invoke initAliases nxm.minio.lib.MinIOResource.getAliases()
  invoke alias1entry initAliases.getKey(alias1)
  assert/text="^reg.main.name: alias ^{alias1} is not already present" alias1entry ISNULL
  invoke alias2entry initAliases.getKey(alias2)
  assert/text="^reg.main.name: alias ^{alias2} is not already present" alias2entry ISNULL


  ! load aliases from custom config file
  alias config pathConfig

  ! check that aliases in config file got loaded
  invoke curAliases nxm.minio.lib.MinIOResource.getAliases()
  invoke alias1entry curAliases.getKey(alias1)
  assert/text="^reg.main.name: alias ^{alias1} is present after ALIAS CONFIG" alias1entry NISNULL
  invoke alias2entry curAliases.getKey(alias2)
  assert/text="^reg.main.name: alias ^{alias2} is present after ALIAS CONFIG" alias2entry NISNULL
  invoke url1act alias1entry.get("url")
  invoke akey1act alias1entry.get("accessKey")
  invoke skey1act alias1entry.get("secretKey")
  invoke alias1exp  expTbl1.^alias1
  
  table alias1exp getKEY "url" url1exp
  table alias1exp getKEY "accessKey" akey1exp
  table alias1exp getKEY "secretKey" skey1exp
  assert/text="^reg.main.name: ALIAS CONFIG url" url1exp EQS url1act
  assert/text="^reg.main.name: ALIAS CONFIG accessKey" akey1exp EQS akey1act
  assert/text="^reg.main.name: ALIAS CONFIG secretKey" skey1exp EQS skey1act

  ! Test ALIAS LIST
  INFO "You should see a listing for ^alias1 below"
  ALIAS LIST alias1

  ! remove new entries
  ALIAS REMOVE alias1
  ALIAS REMOVE alias2
  ! make sure test alias keys were removed
  invoke initAliases nxm.minio.lib.MinIOResource.getAliases()
  invoke alias1entry initAliases.getKey(alias1)
  assert/text="^reg.main.name: alias ^{alias1} was removed" alias1entry ISNULL
  invoke alias2entry initAliases.getKey(alias2)
  assert/text="^reg.main.name: alias ^{alias2} was removed" alias2entry ISNULL

  erase configFile
return

!******************************************************************************!
! Write out the JSON config file for ALIAS CONFIG
!******************************************************************************!
procedure createConfigFile fn:infile
  ! lines for json file
  res line_1 "{"
  res line_2 "   \"aliases\": {"
  res line_3 "       \"miniotestmc\": {"
  res line_4 "           \"url\": \"https://minio:9000\","
  res line_5 "           \"accessKey\": \"user\","
  res line_6 "           \"secretKey\": \"pwd\""
  res line_7 "        },"
  res line_8 "       \"nxmtestmc\": {"
  res line_9 "           \"url\": \"https://nxmminio:9000\","
  res line_10 "           \"accessKey\": \"nxm\","
  res line_11 "           \"secretKey\": \"NeXtMidas\""
  res line_12 "        }"
  res line_13 "   }"
  res line_14 "}"

  ! create expected tables
  res texp1 "{miniotestmc={url=https://minio:9000,accessKey=user,secretKey=pwd}}
  res texp2 "{nxmtestmc={url=https://nxmminio:9000,accessKey=nxm,secretKey=NeXtMidas}}
  invoke csflag nxm.sys.lib.Table.CASESENSITIVE
  new nxm.sys.lib.Table(texp1,csflag) expTbl1
  new nxm.sys.lib.Table(texp2,csflag) expTbl2

  ! Create the JSON config file
  result numLines 14
  file OPEN/t/n tag infile
    do i 1 numLines
      file WRITE tag "^{line_^i}"
    enddo
  file CLOSE tag
return

!******************************************************************************!
! test ALIAS SET
!******************************************************************************!
procedure testAliasSet

  res aliasset "aliasset"
  invoke aliassetentry curAliases.getKey(aliasset)
  assert/text="^reg.main.name: alias ^{alias1entry} is not already present" aliassetentry ISNULL

  ALIAS SET "aliasset" "https://minioset:9000" "akset" "skset"
  invoke aliassetentry curAliases.getKey(aliasset)
  assert/text="^reg.main.name: alias ^{aliasset} is present after ALIAS SET" aliassetentry NISNULL
  invoke urlsetact aliassetentry.get("url")
  invoke akeysetact aliassetentry.get("accessKey")
  invoke skeysetact aliassetentry.get("secretKey")
  assert/text="^reg.main.name: ALIAS SET url" "https://minioset:9000" EQS urlsetact
  assert/text="^reg.main.name: ALIAS SET accessKey" "akset" EQS akeysetact
  assert/text="^reg.main.name: ALIAS SET secretKey" "skset" EQS skeysetact
  
  ALIAS REMOVE aliasset
return

!******************************************************************************!
! test ALIAS IMPORT
!******************************************************************************!
procedure testAliasImport

  res aliasimport "aliasImport"
  invoke aliasimportentry curAliases.getKey(aliasimport)
  assert/text="^reg.main.name: alias ^{aliasimport} is not already present" aliasimportentry ISNULL

  ! make import file in WRITE AUX and get path to it
  res importFile "test_minio_alias_import.json"
  call createImportFile importFile
  invoke pathaux1 nxm.sys.lib.Shell.getAuxPath("WRITE")
  res pathImport "^{pathaux1}^importFile"

  ! load alias from file
  alias IMPORT aliasimport pathImport

  ! check that alias got loaded from import file
  invoke curAliases nxm.minio.lib.MinIOResource.getAliases()
  invoke aliasimentry curAliases.getKey(aliasimport)
  assert/text="^reg.main.name: alias ^{aliasimport} is present after ALIAS IMPORT" aliasimentry NISNULL
  invoke urlimact aliasimentry.get("url")
  invoke akeyimact aliasimentry.get("accessKey")
  invoke skeyimact aliasimentry.get("secretKey")

  ! Test that correct values got imported
  table expTblImport getKEY "url" urlimexp
  table expTblImport getKEY "accessKey" akeyimexp
  table expTblImport getKEY "secretKey" skeyimexp
  assert/text="^reg.main.name: ALIAS CONFIG url" urlimexp EQS urlimact
  assert/text="^reg.main.name: ALIAS CONFIG accessKey" akeyimexp EQS akeyimact
  assert/text="^reg.main.name: ALIAS CONFIG secretKey" skeyimexp EQS skeyimact
  
  ALIAS REMOVE aliasimport
  erase importFile
return

!******************************************************************************!
! Write out the IMPORT file for ALIAS IMPORT
!******************************************************************************!
procedure createImportFile fn:infile
  ! lines for json file
  res line_1 "{"
  res line_2 "    \"url\": \"https://minioimport:9000\","
  res line_3 "    \"accessKey\": \"nmimport\","
  res line_4 "    \"secretKey\": \"importpwd\""
  res line_5 "}"

  ! create expected tables
  res texpim "{url=https://minioimport:9000,accessKey=nmimport,secretKey=importpwd}"
  invoke csflag nxm.sys.lib.Table.CASESENSITIVE
  new nxm.sys.lib.Table(texpim,csflag) expTblImport

  ! Create the JSON config file
  result numLines 5
  file OPEN/t/n tag infile
    do i 1 numLines
      file WRITE tag "^{line_^i}"
    enddo
  file CLOSE tag
return