!******************************************************************************!
! Test httprequest works with POST/GET/PUT/DELETE requests
!
! Note: Need to have a valid certificate that can be loaded
!       automatically using the sslconfigure primitive
!       that is signed by canextmidas
!
! @bug      NXM-3459 httprequest should do all types of requests
! @since    3.9.2
! @author:  Kevin Esterheld
!******************************************************************************!
startmacro S:rootURL["https://canextmidas.com/ca_rest_service/"]
  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 bump priority level of SSLUtiltities log messages
  switch INSECURE    insecure    GET DEF=FALSE SDEF=TRUE  ! run in insecure mode (trust everything, but provide cert)
  switch ANYHOSTNAME anyhostname GET DEF=FALSE SDEF=TRUE  ! run regular ssl, except do not do host name verification
  switch ALTTESTENV  alttestenv  GET DEF=FALSE SDEF=TRUE

  res propsFileDefault "SSLUtilities.properties"
  res propsFileAlt "SSLUtilitiesAlt.properties"
  res urlFileAltCA "nextmidas_url_alt_ca.txt"

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

    if propsFileAlt fexists then
      res propsFile propsFileAlt
    else
      warn "^reg.main.name: Running in alternate test environment but SSLUtilitiesAlt.properties file not found, defaulting to SSLUtilities.properties"
      res propsFile propsFileDefault
    endif
  else
    res propsFile propsFileDefault
  endif

  invoke levelCurrent nxm.sys.lib.SSLUtilities.getCertInfoLogLevel()
  invoke levelConfig  java.util.logging.Level.CONFIG
  if DEBUG then
    invoke ,, nxm.sys.lib.SSLUtilities.setCertInfoLogLevel(levelConfig)
  endif

  if interAct then
    pipe on
      invoke done nxm.sys.lib.SSLUtilities.isInitDone()
      if done isFalse then
        sslconfigure /INSECURE=^{insecure} /ANYHOSTNAME=^{anyhostname} PROPSFILE=^propsFile
      endif
      httprequest url=^{rootURL}rest/v3/testValues body={"key":"test", "value":"goodvalue"} anyhost=true request=put CONTENTTYPE=application/json
      httprequest label=testValue url=^{rootURL}rest/v3/testValues/test
      assert/text="^reg.main.name: testValue should equal 'goodValue'" ^testValue EQSS {"user":"test","value":"goodvalue"}
      httprequest url=^{rootURL}rest/v3/testValues body={"key":"test2", "value":"goodvalue2"} anyhost=false request=post CONTENTTYPE=application/json
      httprequest label=testValue2 url=^{rootURL}rest/v3/testValues/test2
      assert/text="^reg.main.name: testValue2 should equal 'goodValue2'" ^testValue2 EQSS {"user":"test2","value":"goodvalue2"}
      httprequest url=^{rootURL}rest/v3/testValues/test2 request=DELETE
      httprequest label=testValue2 url=^{rootURL}rest/v3/testValues/test2
      assert/text="^reg.main.name: testValue2 should equal 'null'" ^testValue2 EQSS {"user":"test2","value":"null"}
      httprequest url=^{rootURL}rest/v3/testValues/clearAll request=DELETE
    pipe off
  endif

  ! restore logging level
  invoke ,, nxm.sys.lib.SSLUtilities.setCertInfoLogLevel(levelCurrent)
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
