!******************************************************************************!
! Test issue with NeXtMidas Web Start caching partial downloads
!
! Arguments: None
!
! NOTE: This test is designed to be run from the DEMO in test_webstart
!       Instructions are included there, but not specifically
!       mentioned is the option of running with the switch DEBUG to see
!       the HTTP responses.
!       When the second file access fails, it triggers 2 different assert failures.
!
! @bug      NXM-3044 NeXtMidas interfering with Java Web Start caching
! @since    NeXtMidas 3.7.2
! @author:  J. Czechowski IV, Jeanette McCall 
!******************************************************************************!
startmacro fn:url["http://nextmidas.com"]
  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 DEBUG       debug      GET DEF=FALSE SDEF=TRUE  ! to get full HTTP messages to facilitate in debug
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE
  ! to recreate test file that needs to be placed at ^{url}/nextmidas/test_files/
  switch CREATEFILE  needCreateFile GET DEF=FALSE SDEF=TRUE

  if alttestenv then
    res urlFileAlt "nextmidas_url_alt.txt"
    if urlFileAlt nfexists then
      warn "^reg.main.name: SKIPPING since running in alternate test environment but ^{urlFileAlt} not found"
      stop
    else
      file open tag urlFileAlt
      file read tag url
      file close tag
    endif
  endif

  res test_file "test_webstart_cache_3044.tmp"

  if (needCreateFile) then
    call createTestFile
  else
    ! run test

    res cachingErrorMsg "DataFile ^{url}/nextmidas/test_files/test_webstart_cache_3044.tmp does not have a valid BLUE Header"

    invoke debugSetting nxm.sys.lib.HttpResource.getDebug()
    if DEBUG then
      invoke ,, nxm.sys.lib.HttpResource.setDebug(true)
    endif

    ! Initial File access should return HTTP OK (200) and HTTP Partial Content (206), unless already in failure mode
    status ^{url}/nextmidas/test_files/^{test_file} framesize=fsize1
    ! Second file access will only look at the cached file when failing (i.e. HTTP Partial Content (206))
    status ^{url}/nextmidas/test_files/^{test_file} framesize=fsize2
    assert/text="^reg.main.name: WebStart NOT Caching Partial Content" fsize2 REXISTS

    invoke ,, nxm.sys.lib.HttpResource.setDebug(debugSetting)
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    res msg.name
    res msg.data
    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 exception is caught, mark test as failed, but differentiate between
! Web Start caching exception and others
!******************************************************************************!
procedure processException m:emsg
  res errorMsg emsg.data.toString()
  invoke cachingError errorMsg.contains("^{cachingErrorMsg}")
  if (cachingError ISTRUE) then
    assert/text="^reg.main.name: STATUS able to read File Header after Partial (206): ^{emsg.data}" cachingError ISFALSE
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
  endif

  if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
return

!******************************************************************************!
! Recreate test file that needs to be exist at 
! http://nextmidas.com/nextmidas/test_files/
! This file is specifically designed to trigger the partial caching failure.
!******************************************************************************!
procedure createTestFile
  res FS 1012
  waveform  ^test_file FORM=SB ELEM=FS*287 SHAPE=WHITE
  headermod ^test_file FS=^{FS}
  keyword   ^test_file PUT L:NNNNNN 0 S:PPPP XX S:SSSSS XXX-000XX D:TTTTTTTT &
            (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,&
            21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,&
            41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,&
            61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,&
            81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,&
            101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117)
  status/key ^test_file
return
