#!/usr/bin/perl
########################################################################
# Title:	getws.pl
# Version: 	1.0
# Date: 2014-07
# Description:	Test script voor test alle webservice mogelijkheden
#
#######################################################################
# parameters:
#
# voor het inloggen:
# xaction=login
# xdomain= naam van de database/klant
# xuser= de te gebruiken username, moet een bestaande username zijn
# xpassword= het wachtwoord behorende bij xuser
#
# CGISESSID= het unieke session id verkregen na het inloggen
# xaction: bij elke xaction hoort xdomain en CGISESSID
#	=login: voor het starten van een sessie, inloggen met een username en password
#	=logout: voor het beeindigen van een sessie
#	=add: voor toevoegen van een record
#	=select: voor ophalen van een of meer records
#	=delete: voor het verwijderen van 1 record
#	=update: voor het updaten van de gegevens van 1 record
#	=attach: voor uploaden van een bestand en koppelen aan een ... 
#	=describe: geeft terug de veldnamen en het type veldnaam van de meegegeven tabel


use strict;
use Data::Dumper;		# for dumping hash values
use constant {
	ok=>1,
	nok=>0,
};
use XML::TinyXML;
use LWP::UserAgent;
use HTTP::Request::Common;
########################
# declare used vars
########################
my $hosturl=qq{http://test.digirent.nl/cgi-bin/psm/jq7/ws.cgi?};
my $user='webservice';
my $passw='DigiRent';
my $domein='psmsys';
my $debug=0;

my $SID;			# session id
my $sid;			# save session id
my $url;
my $result;			# hash met resultaten na aanroep
# $result->{error}		# omschrijving van de fout of leeg
# $result->{status}		# ok or nok
# $result=>{sessid}		# geeft sessid na inlog en daarna leeg
# $result->{record}		# array van records of leeg

my $status;
my $session;
####################################
# start main programma
####################################
my $ua = LWP::UserAgent->new;
   $ua->agent("MyApp/0.1 ");

###################
test1:
$url=qq{$hosturl&xaction=login&xdomain=$domein&xuser=wrong&xpassword=fakeww};
&lprint(qq{Test 1: Login with fake username and password});
($status,$session) = &geturl($url);
&print_result;

###################
test2:
$url=qq{$hosturl&xaction=login&xdomain=$domein&xuser=$user&xpassword=$passw};
&lprint(qq{Test 2: Login with real username and password});
($status,$session) = &geturl($url);
$sid=$result->{sessid};		# save sessid for parameter
$SID=qq{CGISESSID=$result->{sessid}};		# save sessid for parameter
&print_result;

###################
test3:
$url=qq{$hosturl&xaction=eaction&xdomain=$domein&$SID&xtabel=user};
&lprint(qq{Test 3: Check unknown command});
($status,$session) = &geturl($url);
&print_result;

###################
test4:
$url=qq{$hosturl&xaction=select&xdomain=$domein&$SID&xtabel=user&xfields=user_nr,naam&xwhere=user_nr<50};
&lprint(qq{Test 4: Check select command from user table});
($status,$session) = &geturl($url);
&print_result;

###################
test5:
$url=qq{$hosturl&xaction=add&xdomain=$domein&$SID&xtabel=melding&korte_omschrijving='Test omschrijving'};
&lprint(qq{Test 5: Check add command on melding table});
($status,$session) = &geturl($url);
my $recid=$result->{recordnr};
&print_result;

###################
test6:
$url= qq{$hosturl&xaction=select&xdomain=$domein&$SID&xtabel=melding&xfields=melding_nr,korte_omschrijving&xwhere=melding_nr=$recid};
&lprint(qq{Test 6: Check select command on melding table});
($status,$session) = &geturl($url);
&print_result;
###################
test7:
$url= qq{$hosturl&xaction=update&xdomain=$domein&$SID&xtabel=melding&xrecordnr=$recid&korte_omschrijving='Gewijzigde omschrijving};
&lprint(qq{Test 7: Check update command on melding table});
($status,$session) = &geturl($url);
&print_result;

test8:
$url= qq{$hosturl&xaction=select&xdomain=$domein&$SID&xtabel=melding&xfields=melding_nr,korte_omschrijving&xwhere=melding_nr=$recid};
&lprint(qq{Test 8: Check gewijzigd record melding table});
($status,$session) = &geturl($url);
&print_result;
###################
test9:
$url= qq{$hosturl&xaction=attach&xdomain=$domein&$SID};
&lprint(qq{Test 9: Attach file to melding});
($status,$session) = &attachfile($url,'/usr/lib/cgi-bin/psm/proef/data.txt','melding',$recid);
&print_result;
##################

test10:
$url= qq{$hosturl&xaction=delete&xdomain=$domein&$SID&xtabel=melding&xrecordnr=$recid};
&lprint(qq{Test 10: Check delete command on melding table});
($status,$session) = &geturl($url);
&print_result;
#goto test99;

#
###################
test11:
$url= qq{$hosturl&xaction=describe&xtabel=melding&xdomain=$domein&$SID};
&lprint(qq{Test 11: Describe melding tabel file});
($status,$session) = &geturl($url);
&print_result;

###################
test12:
$url= qq{$hosturl&xaction=mail&xtabel=user&xdomain=$domein&$SID&xuser_nr=2};
&lprint(qq{Test 12: Reset password user : 2});
($status,$session) = &geturl($url);
&print_result;




###################
test99:
$url= qq{$hosturl&xaction=logout&xdomain=$domein&$SID};
&lprint(qq{Test 99: logout});
($status,$session) = &geturl($url);
&print_result;


exit 1;


####################################
# from here the internal subroutines
####################################
sub geturl() {
my ($url)=@_;
my $req = HTTP::Request->new(GET => $url);
	$req->content_type('application/x-www-form-urlencoded');
	$req->content('query=libwww-perl&mode=dist');
	my $res = $ua->request($req);		# Pass request to the user agent 
						# and get a response back
	if ($res->is_success) {			# Check the outcome of the response
		my $xml=XML::TinyXML->new();
		print $res->content if($debug eq 1);
		$xml->loadBuffer($res->content);
		$result=$xml->toHash;
		return ok;
	}
	else {
		print $res->status_line."\n";
		return nok;
	}
}
sub attachfile() {
my ($url,$file,$tabel,$recid)=@_;
my $ua1 = LWP::UserAgent->new;
my $res = $ua1->request(POST $url,
	Content_Type => 'form-data',
	Content => [
		xdomain => $domein,		# welke database
		xaction=>'attach',		# attach file aan bestaand record
		xtabel=>$tabel,			# record in tabel
		xrecordnr=>$recid,		# het unieke recordnummer
		CGISESSID=>$sid,		# save sessid for parameter
		datafile => ["$file"]		# filename te koppelen bestand
	]
	);
 
	if ($res->is_success) {
		my $xml=XML::TinyXML->new();
		print $res->content if($debug eq 1);
		$xml->loadBuffer($res->content);
		$result=$xml->toHash;
		return ok;
	}
	else {
		print $res->status_line."\n";
		return nok;
	}
}
sub print_result() {
	my $s=Data::Dumper->new([\$result],[qq{*$result}]);
	my $t=$s->Dump;
	&lprint($t);
}
sub lprint() {
  my ($p)=@_;
  print $p."\n";
  print $url."\n" if($debug eq 1);
  return
}
