#!/bin/bash
# Script that tests an implementation against the OWLlink HTTP/XML binding examples
# (tested on Mac OS X and Linux)
#    request file names: owllink-*-request-*.xml
#   response file names: owllink-*-response-*.xml
#       test file names: owllink-*-response-*.test
# Marko Luther, 2010

OWLLINK_SERVER='http://localhost:8080'
OWLLINK_VERSION='20091116'
OWLLINK_SCHEMA="http://www.owllink.org/owllink-${OWLLINK_VERSION}.xsd"
#PROXY='http://proxy:8080'


# get responses from an OWLlink server via HTTP:
SUBJECT="SERVER"
# get responses from files 
#SUBJECT="FILE"

# if LOG_RESPONSES then repsonses are logged
LOG_RESPONSES=true
#LOG_RESPONSES=

# if VALIDATE then requests and responses are validated against the OWLlink schema
#VALIDATE=true
VALIDATE=

# if EXIT_ON_ERROR then the script stops on the first error it discovers
#EXIT_ON_ERROR=true
EXIT_ON_ERROR=


for owllink_request_file in owllink-*-request-*.xml; do
  unset HTTP_PROXY
  unset http_proxy
  
  echo "--"
  owllink_response_file=${owllink_request_file/"request"/"response"}
  echo -n "processing "
  echo $owllink_request_file

  if [ $SUBJECT == "SERVER" ]; then
    response=$(curl -0 --crlf -d@$owllink_request_file $OWLLINK_SERVER 2> /dev/null)
  else
    if [ -f $owllink_response_file ]; then
      response=$(cat $owllink_response_file)
    else
      response=""
    fi
  fi

  export HTTP_PROXY=$PROXY
  export http_proxy=$HTTP_PROXY

  # validate request
  if [ $VALIDATE ]; then
    echo -n "validate request: "
    xmllint --noout --schema $OWLLINK_SCHEMA $owllink_request_file 2>&1 | tail -1
  fi

  if [ $LOG_RESPONSES ]; then
    echo "response:"
    echo $response | xmllint - --format 
  fi  

  # validate response
  if [ $VALIDATE ]; then
    echo -n "validate response: "
    echo $response | xmllint --noout --schema $OWLLINK_SCHEMA - 2>&1 | tail -1
  fi

  # count #requests in request message
  requests=$(cat $owllink_request_file | /usr/bin/xpath "count(/RequestMessage/*)"  2> /dev/null)
  responses=$(echo $response | /usr/bin/xpath "count(/ResponseMessage/*)"  2> /dev/null)
  echo -n "  #requests = #responses: "
  [ $requests == $responses ] && echo true || echo false
  
  # check if tests exist
  owllink_test_file=${owllink_response_file/".xml"/".test"}
  if [ -f $owllink_test_file ]; then  
    # fill tag and xpath arrays with tests
    OIFS="$IFS"
    IFS=$'\n'
    set -f
    lines=( $(< ${owllink_test_file} ) )
    set +f
    IFS="$OIFS"
  
    number_of_lines=${#lines[@]}
    number_of_pairs=$(( $number_of_lines / 2 ))
  
    unset tag
    unset xpath
  
    for ((i=0; i < "${number_of_pairs}"; i++)); do 
      j=$(( $i * 2))
      k=$(( $j + 1))
      tag[${i}]=${lines[${j}]}
      xpath[${i}]=${lines[${k}]}
    done
    
    # run the tests
    for (( i=0; i<${#tag[@]}; i++ ));
    do
     echo -n "  ${i} ${tag[$i]}: "
     res=$(echo $response | /usr/bin/xpath "${xpath[$i]}"  2> /dev/null)
     if [ "$res" == "1" ]; then
       echo true
     else
       echo false
       if [ $EXIT_ON_ERROR ]; then
         exit
       fi
     fi
    done
  fi

done