!******************************************************************************!
! Test Keyword putAll that contains MAIN Header Keywords(s).
! This tests with default scope "" (checks both "MAIN" and "EXT" scopes),
! scope set to just "MAIN", and scope set to just "EXT".
!
! Arguments: None
!
! @bug      NXM-4003 ArrayIndexOutOfBounds with kw.putALL that contains MAIN header keywords
! @since    NeXtMidas 4.1.3
! @author:  Jennifer Czechowski, Jeanette McCall
!******************************************************************************!
startmacro
  ! Starter Files
  res eeeifile keywords_putall_eeei_default.tmp
  res ieeefile keywords_putall_ieee_main.tmp
  res extfile  keywords_putall_eeei_ext.tmp

  invoke convertExists reg.shell.M.dictionary.isDictionaryEntry(CONVERT)

  ! Call the tests
  ! NOTE: The default-scope test must be called before the scope-MAIN test
  !       because the scope-MAIN test references the tmp file created in
  !       the default-scope test.
  call testWithEEEIAndDefaultScope
  if convertExists then
    call testWithIEEEAndScopeMain
  else
    info "^reg.main.name: Skipping IEEE Scope MAIN tests due to running in LIBNXM version of NeXtMidas"
  endif
  call testWithEEEIAndScopeExt

  ! Cleanup
  erase eeeifile extfile
  if ieeefile fexists then erase ieeefile
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

!******************************************************************************!
! Test putAll with a default representation file (EEEI) and default scope
!******************************************************************************!
procedure testWithEEEIAndDefaultScope
  headermod/create eeeifile

  key/SCOPE=MAIN eeeifile get CREATOR origcreator VER origver

  IF origver NEQS "1.2" THEN
    res mapver "1.2"
  ELSE
    res mapver "1.1"
  ENDIF

  new java.util.HashMap mapeeei
  invoke ,, mapeeei.put("VER","^mapver")
  invoke ,, mapeeei.put("IO","NeXtMidas")
  invoke ,, mapeeei.put("CREATOR","NXM3.2.1")
  invoke ,, mapeeei.put("TC_PREC",1.4E-7)
  invoke ,, mapeeei.put("PKT_BYTE_COUNT",20)
  invoke ,, mapeeei.put("USRKEY","4321")
  invoke ,, mapeeei.put("CLASSIFICATION","UN//CL/AS//REL TO USA, FVEY")

  key/SCOPE=EXT eeeifile put USRKEY "0815"

  !!!!! now do putAll with map !!!!

  ! Open file
  file open/n/a tag eeeifile

  ! get keywords object
  invoke kw tag.getKeyWordsObject()
  invoke defscope kw.getScope()
  invoke ,, kw.putAll(mapeeei)

  ! update
  invoke ,, tag.update()

  ! close file
  file close tag

  key/SCOPE=MAIN eeeifile get CREATOR newcreator VER newver TC_PREC tcprec PKT_BYTE_COUNT pktcnt
  key/SCOPE=EXT eeeifile get USRKEY newusrkey
  assert/text="^reg.main.name: EEEI file default scope is as expected" defscope EQS ""
  assert/text="^reg.main.name: EEEI file with default scope - MAIN header keyword VER replaced" newver eqs mapver
  assert/text="^reg.main.name: EEEI file with default scope - MAIN header keyword CREATOR replaced" newcreator eqs "NXM3.2.1"
  assert/text="^reg.main.name: EEEI file with default scope - MAIN header keyword TC_PREC added" tcprec eqs "1.4E-7"
  assert/text="^reg.main.name: EEEI file with default scope - MAIN header keyword PKT_BYTE_COUNT added" pktcnt eqs "20"
  assert/text="^reg.main.name: EEEI file with default scope - EXT header keyword USRKEY replaced" newusrkey eqs "4321"
return

!******************************************************************************!
! Test putAll with EEEI file switched to IEEE representation and scope set to "MAIN"
!******************************************************************************!
procedure testWithIEEEAndScopeMain
  convert eeeifile IEEE ieeefile

  new java.util.HashMap mapieee
  invoke ,, mapieee.put("VER","1.0")
  invoke ,, mapieee.put("CREATOR","NXM1.2.3")
  invoke ,, mapieee.put("USRKEY","1234")
  invoke ,, mapieee.put("TC_PREC",1.4E-7)
  invoke ,, mapieee.put("PKT_BYTE_COUNT",20)

  !!!!! now do putAll with map !!!!

  ! Open file
  file open/n/a tag ieeefile
  invoke ,, tag.setKeyScope("MAIN")

  ! get keywords object
  invoke kw tag.getKeyWordsObject()
  invoke ,, kw.putAll(mapieee)

  ! update
  invoke ,, tag.update()

  ! close file
  file close tag

  key/SCOPE=MAIN ieeefile get CREATOR newcreator VER newver TC_PREC tcprec PKT_BYTE_COUNT pktcnt
  key/SCOPE=EXT ieeefile get USRKEY newusrkey
  assert/text="^reg.main.name: IEEE file with scope MAIN - MAIN header keyword VER replaced" newver eqs "1.0"
  assert/text="^reg.main.name: IEEE file with scope MAIN - MAIN header keyword CREATOR replaced" newcreator eqs "NXM1.2.3"
  assert/text="^reg.main.name: IEEE file with scope MAIN - MAIN header keyword TC_PREC added" tcprec eqs "1.4E-7"
  assert/text="^reg.main.name: IEEE file with scope MAIN - MAIN header keyword PKT_BYTE_COUNT added" pktcnt eqs "20"
  assert/text="^reg.main.name: IEEE file with scope MAIN - EXT header keyword USRKEY replaced" newusrkey eqs "1234"
return

!******************************************************************************!
! Test putAll with a default representation file (EEEI) and scope set to "EXT"
!******************************************************************************!
procedure testWithEEEIAndScopeExt
  headermod/create extfile

  ! NOTE: Map created during first test scenario

  key/SCOPE=EXT extfile put USRKEY "0815"

  !!!!! now do putAll with map !!!!
  invoke ,, mapeeei.put("TC_PREC","1.45E-7")

  ! Open file
  file open/n/a tag extfile
  invoke ,, tag.setKeyScope("EXT")

  ! get keywords object
  invoke kw tag.getKeyWordsObject()
  invoke ,, kw.putAll(mapeeei)

  ! update
  invoke ,, tag.update()

  ! close file
  file close tag

  key/SCOPE=EXT extfile get CREATOR newcreator VER newver USRKEY newusrkey TC_PREC tcprec
  assert/text="^reg.main.name: EEEI file with scope EXT - EXT header keyword VER added" newver eqs newver
  assert/text="^reg.main.name: EEEI file with scope EXT - EXT header keyword CREATOR added" newcreator eqs newcreator
  assert/text="^reg.main.name: IEEE file with scope EXT - EXT header keyword TC_PREC added" tcprec eqs 1.45E-7
  assert/text="^reg.main.name: EEEI file with scope EXT - EXT header keyword USRKEY replaced" newusrkey eqs "4321"
return