blib/lib/HPPPM/ErrorHandler.pm

filter: Statementstmt 63.4% 104 / 164 Branchbran 22.0% 11 / 50 Conditioncond 15.9% 7 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 88.8% 16 / 18 Podpod 100% 5 / 5 totaltotal 46.5% 143 / 307 SCAR 57.9
CC
44
Cov
47%
validate_read_cmdargs
210.0
_get_file_content
42.0
extract
30.6
CRAP 327.6
↑ Summary blib/lib/FieldParser.pm »
1 package HPPPM::ErrorHandler;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Error::TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = '0.01';
16  
17 4 has 'input_parsed_xml' => (
18 is => 'rw',
19 isa => 'XML::Simple',
20 );
21  
22 #Checks if the input fields that will be used to construct the SOAP
23 #message have all the reqd. (per operation) types present.Both inputs
24 #fields and the reqd types are mandatory inputs.Returns True if the reqd.
25 #types are present
26  
27 sub _check_reqd_types {
28 1 ▶ my $self = shift;
Subroutine _check_reqd_types: called CC=4 SCAR=17.2
29 1 ▶ my $fields = shift || confess "No fields to check properties";
Condition33%1/3
shift() || confess('No fields to check properties')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('No fields to check properties')
shift() confess('No fields to check properties')
Truth table33%1/3
A || B
A= shift()B= confess('No fields to check properties')
ABresult
1X1
011
000
30 1 ▶ my $reqd_types = shift || confess "No types to check";
Condition33%1/3
shift() || confess('No types to check')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('No types to check')
shift() confess('No types to check')
Truth table33%1/3
A || B
A= shift()B= confess('No types to check')
ABresult
1X1
011
000
31 1 my $operation = $self->current_operation();
32 1 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
33 1 my (@present_types, $msg, $present, $reqd);
34  
35 1 @present_types = grep { exists $fields->{$_} } @{ $reqd_types };
36  
37 1 ▶ return 1 if @present_types == @{ $reqd_types };
Branch: 1 branch
BranchTrueFalseTotal
if @present_types == @{$reqd_types;} 1 0 1
38  
39 0 $reqd = join " ",@{ $reqd_types };
40 0 $present = join " ",@present_types;
41 0 $msg = "Properties present donot match the min. no of properties";
42 0 $msg .= "needed for $operation operation.Properties present:$present";
43 0 $msg .= " Properties required:$reqd Exiting!";
44  
45 0 $logger->logconfess($msg);
46 }
47  
48  
49 #Read and return file contents as a single string
50  
51 sub _get_file_content {
52 0 ▶ my $self = shift;
Subroutine _get_file_content: not called CC=6 SCAR=37.4
53 0 ▶ my $fname = shift || confess "No filename to read content from";
Condition0%0/3
shift() || confess('No filename to read content from')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || confess('No filename to read content from')
shift() confess('No filename to read content from')
Truth table0%0/3
A || B
A= shift()B= confess('No filename to read content from')
ABresult
1X1
011
000
54 0 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
55 0 my $fields;
56  
57 0 try {
58 0 $logger->debug("About to read fields containing req fields");
59  
60 0 ▶ open my $fhandle, "<", $fname
Condition0%0/3
$fname || throw('Error::Unhandled'->new('-text', $HPPPM::ErrorHandler::OS_ERROR))
l!l&&r!l&&!r
000
MC/DC0%0/2
$fname || throw('Error::Unhandled'->new('-text', $HPPPM::ErrorHandler::OS_ERROR))
$fname throw('Error::Unhandled'->new('-text', $HPPPM::ErrorHandler::OS_ERROR))
Truth table0%0/3
A || B
A= $fnameB= throw('Error::Unhandled'->new('-text', $HPPPM::ErrorHandler::OS_ERROR))
ABresult
1X1
011
000
61 || throw new Error::Unhandled -text => $OS_ERROR;
62 0 local $INPUT_RECORD_SEPARATOR = undef;
63 0 ($fields = <$fhandle>) =~ s/\\n//g;
64 }
65 0 ▶ catch Error::Unhandled with {
Branch: 3 branches
BranchTrueFalseTotal
if ($@) 0 0 0
unless ref $@ 0 0 0
if ($@->isa('Error::Unhandled')) { } 0 0 0
66 0 print "Unable to read $fname..Exiting! $OS_ERROR";
67 0 $logger->logcroak("Unable to read $fname $OS_ERROR");
68 }
69  
70 0 $logger->debug("$fname read! content: $fields");
71
72 0 return $fields;
73 }
74  
75  
76 sub validate_read_cmdargs {
77 0 ▶ my $self = shift;
Subroutine validate_read_cmdargs: not called CC=14 SCAR=53.5
Pod documented
78 0 my $p = new Getopt::Long::Parser;
79 0 my ($oper, $fields, $log_cfg, $ret, $logger,
80 $user, $pawd, $help, $oper_exists);
81
82 0 ▶ $p->getoptions(
Branch: 1 branch
BranchTrueFalseTotal
$p->getoptions("operation=s", \$oper, "user=s", \$user, "password=s", \$pawd, "fields=s", \$fields, "config=s", \$log_cfg, "help|?", \$help) or confess(pod2usage("-verbose", 2, "-noperldoc", 1, "-msg", "Command line options parsing failed!")) 0 0 0
83 'operation=s'=> \$oper,
84 'user=s' => \$user,
85 'password=s' => \$pawd,
86 'fields=s' => \$fields,
87 'config=s' => \$log_cfg,
88 'help|?' => \$help,
89 ) || confess pod2usage(-verbose => 2, -noperldoc => 1,
90 -msg => 'Command line options parsing failed!');
91  
92 #validate command line args
93 0 ▶ pod2usage(-verbose => 2, -noperldoc => 1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
94 0 ▶ confess pod2usage(-verbose => 2, -noperldoc => 1, -msg => 'Insufficient Args!')
Branch: 1 branch
BranchTrueFalseTotal
unless $oper or $user or $pawd or $fields or $log_cfg 0 0 0
Condition0%0/12
$oper or $user or $pawd or $fields or $log_cfg
l!l&&r!l&&!r
000
$oper or $user or $pawd or $fields
l!l&&r!l&&!r
000
$oper or $user or $pawd
l!l&&r!l&&!r
000
$oper or $user
l!l&&r!l&&!r
000
MC/DC0%0/5
$oper or $user or $pawd or $fields or $log_cfg
$oper $user $pawd $fields $log_cfg
Truth table0%0/6
A or B or C or D or E
A= $operB= $userC= $pawdD= $fieldsE= $log_cfg
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
95 if ! ($oper || $user || $pawd || $fields || $log_cfg);
96 0 ▶ confess pod2usage(-verbose => 2, -noperldoc => 1, -msg => "$log_cfg missing!")
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log_cfg or -s $log_cfg 0 0 0
Condition0%0/3
-f $log_cfg or -s $log_cfg
l!l&&r!l&&!r
000
MC/DC0%0/2
-f $log_cfg or -s $log_cfg
-f $log_cfg -s $log_cfg
Truth table0%0/3
A or B
A= -f $log_cfgB= -s $log_cfg
ABresult
1X1
011
000
97 if ! (-f $log_cfg || -s $log_cfg);
98 #Most important, initialize the logger first
99 0 Log::Log4perl->init($log_cfg);
100 0 $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
101
102 0 $oper_exists = grep { /$oper/ } $self->get_supported_ops();
103 0 ▶ $logger->info("Current operation: $oper") if $oper_exists;
Branch: 1 branch
BranchTrueFalseTotal
if $oper_exists 0 0 0
104 0 ▶ $logger->logconfess("Unsupported operation: $oper") if ! $oper_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $oper_exists 0 0 0
105
106 #set current oper, user and password
107 0 $self->current_operation($oper);
108 0 $self->user($user);
109 0 $self->password($pawd);
110
111 #If $fields points to a file, slurp it
112 0 ▶ $fields = $self->_get_file_content($fields) if( -f $fields and -s $fields );
Branch: 1 branch
BranchTrueFalseTotal
if -f $fields and -s $fields 0 0 0
Condition0%0/3
-f $fields and -s $fields
!ll&&!rl&&r
000
MC/DC0%0/2
-f $fields and -s $fields
-f $fields -s $fields
Truth table0%0/3
A and B
A= -f $fieldsB= -s $fields
ABresult
0X0
100
111
113  
114 0 return $fields;
115 }
116  
117  
118 sub validate_inputs {
119 1 ▶ my $self = shift;
Subroutine validate_inputs: called CC=3 SCAR=11.1
Pod documented
120 1 ▶ my $fields = shift || confess "No args to validate";
Condition33%1/3
shift() || confess('No args to validate')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('No args to validate')
shift() confess('No args to validate')
Truth table33%1/3
A || B
A= shift()B= confess('No args to validate')
ABresult
1X1
011
000
121 1 my $ignore_types = shift;
122 1 my $operation = $self->current_operation();
123 1 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
124 1 my %ops_inputs_reqd = %{$self->ops_inputs_reqd};
125 1 my (@reqd_types, $ret, $url);
126  
127 #Lookup & localize reqd types needed perform the op
128 1 @reqd_types = @{ $ops_inputs_reqd{ $operation } };
129 1 $ret
130 = $self->_check_reqd_types($fields, \@reqd_types);
131 1 ▶ $logger->debug("Reqd. Types for Current Oper Present!") if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 1 0 1
132
133 1 return 1;
134 }
135  
136  
137 sub validate_tokens {
138 1 ▶ my $self = shift;
Subroutine validate_tokens: called CC=3 SCAR=11.5
Pod documented
139 1 my $fields = shift;
140 1 my $operation = $self->current_operation();
141 1 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
142 1 my %ops_inputs_reqd = %{ $self->ops_inputs_reqd };
143 1 my (@tokens, $has_tokens, @illegal_tokens, $illegal);
144  
145 1 ▶ $logger->info("No token tag in input fields!")
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 0 1 1
146 if ! $fields;
147  
148 1 @tokens = ($fields =~ /\b((?:REQD|REQ|UD|T)\.[A-Z\._0-9]+)\b/gc);
149 @illegal_tokens
150 1 = grep {! /(^(?:REQD|REQ|UD|T)\.?(?:VP|P)?\.[A-Z_0-9]+?)$/} @tokens;
151 1 ▶ if(@illegal_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@illegal_tokens) 0 1 1
152 0 $illegal = join " ",@illegal_tokens;
153 0 $logger->logconfess("Illegal Token names: $illegal Exiting!");
154 }
155  
156 1 return 1;
157 }
158  
159  
160 sub check_url_availability {
161 1 ▶ my $self = shift;
Subroutine check_url_availability: called CC=7 SCAR=24.8
Pod documented
162 1 ▶ my $service_url = shift || confess "No url to check availability";
Condition33%1/3
shift() || confess('No url to check availability')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('No url to check availability')
shift() confess('No url to check availability')
Truth table33%1/3
A || B
A= shift()B= confess('No url to check availability')
ABresult
1X1
011
000
163 1 ▶ my $timeout = shift || 60;
Condition50%1/2
shift() || 60
l!l
01
MC/DC0%0/1
shift() || 60
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
164 1 my $ua = LWP::UserAgent->new('timeout' => $timeout);
165 1 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
166 1 my ($resp, $msg);
167  
168 1 try {
169 1 $resp = $ua->get($service_url);
170 1 ▶ throw new Error::Unhandled -text => $resp->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 1 1
171 if ! $resp->is_success;
172 }
173 1 ▶ catch Error::Unhandled with {
Branch: 3 branches
BranchTrueFalseTotal
if ($@) 0 1 1
unless ref $@ 0 0 0
if ($@->isa('Error::Unhandled')) { } 0 0 0
174 0 $logger->logcroak($resp->status_line);
175 }
176  
177 0 return 1;
178 }
179  
180  
181 sub extract {
182 1 ▶ my $self = shift;
Subroutine extract: called CC=13 SCAR=34.2
Pod documented
183 1 ▶ my $resp = shift || confess "No response to extract from";
Condition33%1/3
shift() || confess('No response to extract from')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('No response to extract from')
shift() confess('No response to extract from')
Truth table33%1/3
A || B
A= shift()B= confess('No response to extract from')
ABresult
1X1
011
000
184 1 ▶ my $to_extract = shift || confess "Nothing to extract";
Condition33%1/3
shift() || confess('Nothing to extract')
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || confess('Nothing to extract')
shift() confess('Nothing to extract')
Truth table33%1/3
A || B
A= shift()B= confess('Nothing to extract')
ABresult
1X1
011
000
185 1 my $logger = Log::Log4perl->get_logger( $PROGRAM_NAME );
186 1 my ($xml_parser, $xml_ref, %details, $key, $code, $string, %tag_vals);
187  
188 1 $logger->debug("Extracting values in tags ". join ' ', @{ $to_extract });
189 #$resp =~ s/^.+(\<?xml.*)$/$1/i if $resp =~ /^.+(\<?xml.*)$/;
190  
191 1 try {
192 require XML::Simple
193 1 ▶ || throw new Error::Unhandled -text => 'XML::Simple not found';
Branch: 1 branch
BranchTrueFalseTotal
require XML::Simple or throw("Error::Unhandled"->new("-text", "XML::Simple not found")) 0 0 0
194  
195 0 $xml_parser = XML::Simple->new();
196 0 $xml_ref = $xml_parser->XMLin($resp);
197  
198 0 $logger->debug("Extracting tag values using the neat XML Parsing");
199  
200 0 for my $key (keys %{$xml_ref}) {
201 0 ▶ next if $key !~ /\:body$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\:body$/i 0 0 0
202  
203 0 %details = %{$xml_ref->{$key}};
204 #if ( $key =~ /\:fault$/i ) {
205 # $tag_vals{$_} ||= $details{$_} for @{ $to_extract };
206 #}
207 #($key) = keys %details;
208  
209 0 $tag_vals{$_} = $details{$key}->{$_} for @{ $to_extract };
210 }
211 }
212 1 ▶ catch Error::Unhandled with {
Branch: 3 branches
BranchTrueFalseTotal
if ($@) 1 0 1
unless ref $@ 1 0 1
if ($@->isa('Error::Unhandled')) { } 1 0 1
213 1 $logger->debug("Extraction Failed...");
214 }
215  
216 0 ▶ if (! %tag_vals ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%tag_vals) 1 0 1
217 1 $logger->debug("Trying to extract fault with regexp...");
218  
219 1 for my $tag ( @{ $to_extract } ) {
220 2 ▶ $tag_vals{$tag} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $resp =~ m[<$tag>(.+)</$tag>]six 2 0 2
221 if $resp =~ /<$tag>(.+)<\/$tag>/isx;
222 }
223 }
224
225 1 $logger->debug("TAGS -> VALUES: ", %tag_vals);
226  
227 1 return \%tag_vals;
228 }
229  
230 4 __PACKAGE__->meta->make_immutable;
231  
232 4 1; # End of HPPPM::ErrorHandler
233  
234 __END__
235  
236 =head1 NAME
237  
238 HPPPM::ErrorHandler - Error Handling Base class for all HPPPM Classes
239  
240 =head1 VERSION
241  
242 Version 0.01
243  
244 =head1 SYNOPSIS
245  
246 Error Handling Base class for all HPPPM Classes.Performs command line parsing,
247 validation of arguments and error extraction.Desginwise, this class is meant to
248 be subclassed and used transparently by HPPPM classes, however it can be
249 instantiated directly.
250  
251 $hpppm = HPPPM::Demand::Management->new();
252  
253 $fields = $hpppm->validate_read_cmdargs(@ARGV);
254 $tags = $hpppm->get_inputs($hpppm->get_current_oper());
255  
256 $inputs = FieldParser::parser($fields, $tags);
257  
258 $ret = $hpppm->validate_inputs($inputs);
259 $ret = $hpppm->validate_tokens($inputs->{'fields'})
260 if grep /^fields$/, @{ $tags };
261  
262  
263 $ret = $hpppm->extract($res, ['faultcode', 'faultstring',
264 'exception:detail', 'id', 'return']);
265  
266  
267 =head1 DESCRIPTION
268  
269 Error Handling Base class for all HPPPM Classes.Performs command line parsing,
270 validation of arguments and error extraction.Desginwise, this class is meant to
271 be subclassed and used transparently by HPPPM classes, however it can be
272 instantiated directly.
273  
274 The class performs validation at various levels:
275  
276 1. Validating the presence of filenames(with data) passed as cmd args.
277  
278 2. Web service operation being performed is legal and supported.
279  
280 3. Before posting Check if the Web Service is up and accessible or not.
281  
282 4. Validate data that will be used to create Web service request(optional).
283  
284 The class also provides in-detail execption extraction.
285  
286  
287 =head1 ATTRIBUTES
288  
289 =head1 METHODS
290  
291 =head2 validate_read_cmdargs
292  
293 perl bin/hpppm_demand.pl -o createRequest -u user -p password -f data/createRequest.data -c cfg/logging.conf
294  
295 -o or --operation is the webservice operation being performed
296 -u or --user user authorized to perform web service operation
297 -p or --password user's password
298 -f or --fields location of file containing input fields that will be used to create
299 the web service request.Instead of a path this can also be a string
300 containing the input fields.A sample data file for each web service
301 operation has been bundled along with distribution under data dir.
302 -c or --logconfig location to the configuration file that drives logging behavior.
303 -h or --help or -? display help.
304  
305 =head2 validate_inputs
306  
307 Checks if the required types need in order to perform
308 the operation successfully are present in the input data or not.
309  
310 =head2 validate_tokens
311  
312 Checks if the operation being performed supports tokens or not. If no
313 tokens are needed the method returns 0.Performs the following checks on
314 tokens as well -All field tokens must be all caps. Token prefixes
315 (REQD, REQ, UD, T, VP, P) must be one of the specified types.All tokens
316 can contain only alphanumeric characters and _ (underscore).Input is
317 input fields and output is Success or Failure
318  
319 =head2 check_url_availability
320  
321 Tests service URL for accessibility.Input is url to test and return
322 Success or Failure
323  
324 =head2 extract
325  
326 Extracts the value(s) which are valid tags in the response received
327 in response to the request posted to the webservice.The value(s)/tag(s)
328 must be passed in as a array ref.The return value is a hash ref with
329 key as the tag and value as its extracted value.
330  
331 =head1 AUTHOR
332  
333 Varun Juyal, <varunjuyal123@yahoo.com>
334  
335 =head1 BUGS
336  
337 Please report any bugs or feature requests to C<bug-hpppm-demand-management at rt.cpan.org>, or through
338 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HPPPM-Demand-Management>. I will be notified, and then you'll
339 automatically be notified of progress on your bug as I make changes.
340  
341 =head1 SUPPORT
342  
343 You can find documentation for this module with the perldoc command.
344  
345 perldoc HPPPM::ErrorHandler
346  
347  
348 You can also look for information at:
349  
350 =over 4
351  
352 =item * RT: CPAN's request tracker (report bugs here)
353  
354 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HPPPM-Demand-Management>
355  
356 =item * AnnoCPAN: Annotated CPAN documentation
357  
358 L<http://annocpan.org/dist/HPPPM-Demand-Management>
359  
360 =item * CPAN Ratings
361  
362 L<http://cpanratings.perl.org/d/HPPPM-Demand-Management>
363  
364 =item * Search CPAN
365  
366 L<http://search.cpan.org/dist/HPPPM-Demand-Management/>
367  
368 =back
369  
370  
371 =head1 ACKNOWLEDGEMENTS
372  
373  
374 =head1 LICENSE AND COPYRIGHT
375  
376 Copyright 2012 Varun Juyal.
377  
378 This program is free software; you can redistribute it and/or modify it
379 under the terms of either: the GNU General Public License as published
380 by the Free Software Foundation; or the Artistic License.
381  
382 See http://dev.perl.org/licenses/ for more information.
383  
384  
385 =cut
386  
↑ Summary blib/lib/FieldParser.pm »