From 8020a2a57f42ad5937e8364c196d8700f7ee340b Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Wed, 18 Sep 2013 15:07:09 +1200 Subject: [PATCH] Continuing work on NCIP.pm We can now recognised (in theory) the type of request we are getting. Have a basic test of a LookupItem message Signed-off-by: Chris Cormack --- lib/NCIP.pm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- t/NCIP.t | 14 +++++++++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/lib/NCIP.pm b/lib/NCIP.pm index b8bc2ce..c5101f7 100644 --- a/lib/NCIP.pm +++ b/lib/NCIP.pm @@ -48,10 +48,11 @@ sub process_request { # We have invalid xml, or we can't figure out what kind of request this is # Handle error here } - my $response = -" Hello There

Hello You Big JERK!

Who would take this book seriously if the first eaxample didn't say \"hello world\"? "; - return $response; +#my $response = " Hello There

Hello You Big JERK!

Who would take this book seriously if the first eaxample didn't say \"hello world\"? "; + + #return $response; + return $request_type; } =head2 handle_initiation @@ -69,11 +70,54 @@ sub handle_initiation { warn "Invalid xml, caught error: $_"; }; if ($dom) { - return ('lookup_item'); + + # should check validity with validate at this point + my $request_type = $self->parse_request($dom); + return $request_type; } else { return; } } +sub validate { + + # this should perhaps be in it's own module + my $self = shift; + my $dom = shift; + my $validity = $dom->is_valid(); + + # we could validate against the dtd here, might be good? + # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str); + # my $validity = $dom->is_valid($dtd); + # perhaps we could check the ncip version and validate that too + return $validity; +} + +sub parse_request { + my $self = shift; + my $dom = shift; + my $nodes = $dom->findnodes('/*'); + if ( $nodes->[0]->nodeName() ne 'ns1:NCIPMessage' ) { + + # we don't have a valid ncip message + # bail out + warn "bad xml"; + } + else { + my @childnodes = $nodes->[0]->childNodes(); + + # the second child should be the type of request + if ( $childnodes[1] && $childnodes[1]->nodeName =~ /ns1\:(.*)/ ) { + return $1; + } + else { + # just while developing return not found + return ('Not_found'); + } + } + + return 0; +} + 1; diff --git a/t/NCIP.t b/t/NCIP.t index 74422cb..d2dbe81 100644 --- a/t/NCIP.t +++ b/t/NCIP.t @@ -17,8 +17,9 @@ use strict; use warnings; +use File::Slurp; -use Test::More tests => 5; # last test to print +use Test::More tests => 7; # last test to print use lib 'lib'; @@ -26,8 +27,10 @@ use_ok('NCIP'); ok( my $ncip = NCIP->new('t/config_sample'), 'Create new object' ); my $xml = <<'EOT'; - - + + + EOT ok( my $response = $ncip->process_request($xml), 'Process a request' ); @@ -43,3 +46,8 @@ EOT # anyway ok( !$ncip->handle_initiation($xmlbad), 'Bad xml' ); ok( $ncip->handle_initiation($xml), 'Good XML' ); + +my $lookupitem = read_file('t/sample_data/LookupItem.xml'); + +ok( my $response = $ncip->process_request($lookupitem), 'Try looking up an item'); +is ($response, 'LookupItem', 'We got lookupitem'); -- 2.43.2