blib/lib/MOBY/Client/Exception/MobyException.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 311 CC 35 SCAR 40.4
CC
35
Cov
0%
retrieveExceptionResponse
182.0
new
56.0
getExceptionMessage
20.0
CRAP 57.0
1 #$Id: MOBY::MobyException.pm ,v 1.2
2 # Created: 26-01-2006
3 # Updated: 29-03-2006
4  
5 # Name of the package
6 package MOBY::Client::Exception::MobyException;
7  
8 # Perl pragma to restrict unsafe constructs
9 use strict;
10  
11 # Issue warnings about suspicious programming.
12 use warnings;
13  
14 use Carp qw(croak);
15  
16 use vars qw /$VERSION/;
17 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
18  
19 use MOBY::Client::Exception::MobyExceptionCodes;
20  
21  
22 ##########################################
23 # Create new instance of exception class #
24 ##########################################
25 sub new {
26 # Get parameters
27 my ($caller, %args) = @_;
28
29 my ($caller_is_obj) = ref($caller);
30 my ($class) = $caller_is_obj || $caller;
31 my ($self) = {};
32  
33 # Add attributes related to mobyData where has been generating exception.
34 $self->{queryID} = (exists($args{queryID}) && defined($args{queryID})) ? $args{queryID} : '';
35 $self->{refElement} = (exists($args{refElement}) && defined($args{refElement})) ? $args{refElement} : '';
36  
37  
38 # Add attribute of exception code
39 if (exists($args{code}) && defined($args{code})) {
40 $self->{code} = $args{code};
41 } else {
42 $self->{code} = 0;
43 }
44  
45 # Add attribute of exception "dynamic" message
46 $self->{message} = (exists($args{message}) && defined($args{message})) ? $args{message} : undef;
47  
48 # Add attribute of type of exception
49 if (exists($args{type}) && defined($args{type})) {
50 my ($type) = lc($args{type}); # Converts all characters in the string to lower case
51 if ($type eq 'information' || $type eq 'warning' || $type eq 'error') {
52 $self->{type} = $type; # (undef | error | warning | information)
53 } else {
54 $self->{type} = undef;
55 }
56 } else {
57 $self->{type} = undef;
58 }
59  
60 # The magic object creation command
61 bless ($self, $class);
62  
63 # Returns a new 'MobyException' object
64 return $self;
65 }
66  
67 #################
68 # Method bodies #
69 #################
70  
71 #########################
72 # Return exception code #
73 #########################
74 sub getExceptionCode {
75 # Get parameters
76 my($self) = shift;
77
78 croak("This is an instance method!") unless(ref($self));
79
80 return $self->{code};
81 } # End getExceptionCode
82  
83 ############################
84 # Return exception message #
85 ############################
86 sub getExceptionMessage {
87 # Get parameters
88 my($self) = shift;
89  
90 my ($exceptionMessage) = undef;
91  
92 croak("This is an instance method!") unless(ref($self));
93
94 # Get standard exception message from given code
95 my ($standardMessage) = MOBY::Client::Exception::MobyExceptionCodes::getExceptionCodeDescription($self->{code});
96  
97 # If standard message is not defined that means, the exception code is wrong => return undef
98 return undef unless(defined($standardMessage));
99  
100 # User could add dynamic message into satandard exception message
101 ($exceptionMessage) = (defined($self->{message})) ? $standardMessage.$self->{message} : $standardMessage;
102
103 return $exceptionMessage;
104 } # End getExceptionMessage
105  
106 ############################
107 # Return type of exception #
108 ############################
109 sub getExceptionType {
110 # Get parameters
111 my($self) = shift;
112
113 croak("This is an instance method!") unless(ref($self));
114
115 # type of exception is returned, if defined type of exception; otherwise returns undef
116 return $self->{type};
117 } # End getExceptionType
118  
119 #########################
120 # Assign exception code #
121 #########################
122 sub setExceptionCode {
123 # Get parameters
124 my($self, $code) = @_;
125  
126 croak("This is an instance method!") unless(ref($self));
127  
128 if (defined($code)) {
129 $self->{code} = $code;
130 } else {
131 croak("input argument not defined");
132 }
133 # UPDATE: 19-01-2006-----------------
134 } # End setExceptionCode
135  
136 ############################
137 # Assign exception message #
138 ############################
139 sub setExceptionMessage {
140 # Get parameters
141 my($self, $message) = @_;
142  
143 croak("This is an instance method!") unless(ref($self));
144  
145 if (defined($message)) {
146 $self->{message} = $message;
147 } else {
148 croak("input argument not defined");
149 }
150 } # End setExceptionMessage
151  
152 ##################################################
153 # Assign type of exception to attribute of class #
154 ##################################################
155 sub setExceptionType {
156 # Get parameters
157 my($self, $type) = @_;
158  
159 croak("This is an instance method!") unless(ref($self));
160  
161 # Input type has to be defined and to include within range of values
162 if (defined($type)) {
163 my ($type) = lc($type); # Converts all characters in the string to lower case
164
165 # Input type has to be included within range of values
166 if ($type eq 'information' || $type eq 'warning' || $type eq 'error') {
167 $self->{type} = $type;
168 } else {
169 croak("input argument not defined");
170 }
171 }
172 } # End setExceptionType
173  
174 ########################################################################################
175 # Return xml block that will be the exception response (error, warning or information) #
176 ########################################################################################
177 sub retrieveExceptionResponse {
178 # Get parameters
179 my($self)=shift;
180 my ($exceptionResponse) = undef;
181
182 croak("This is an instance method!") unless(ref($self));
183  
184  
185 # If corresponds to free text message, we don't mind the code or message => User is free to insert what he wants
186 if (defined($self->{type}) && ($self->{type} eq 'information')) {
187  
188 # Chek if is defined information message
189 my ($infoMessage) = (defined($self->{message})) ? $self->{message} : '';
190  
191 # Chek if there is article Name
192 my ($refElement) = '';
193 if (exists($self->{refElement}) && defined($self->{refElement}) && ($self->{refElement} ne '')) { $refElement = "refElement='".$self->{refElement}."'" ; }
194  
195 my ($refQueryID) = '';
196 if (exists($self->{queryID}) && defined($self->{queryID}) && ($self->{queryID} ne '')) { $refQueryID = "refQueryID='".$self->{queryID}."'" ; }
197  
198 $exceptionResponse = "<mobyException $refQueryID $refElement severity='information'>\n\t<exceptionCode>".$self->{code}."</exceptionCode>\n\t<exceptionMessage>$infoMessage</exceptionMessage>\n</mobyException>";
199  
200 } else {
201  
202 # Get standard exception message from given code
203 my ($standardMessage) = MOBY::Client::Exception::MobyExceptionCodes::getExceptionCodeDescription($self->{code});
204  
205 #return undef unless(defined($standardMessage));
206 croak("code of exception is wrong or does not exists") unless(defined($standardMessage));
207  
208 # Chek if there is article Name
209 my ($refElement) = '';
210 if (exists($self->{refElement}) && defined($self->{refElement}) && ($self->{refElement} ne '')) { $refElement = "refElement='".$self->{refElement}."'" ; }
211  
212 my ($refQueryID) = '';
213 if (exists($self->{queryID}) && defined($self->{queryID}) && ($self->{queryID} ne '')) { $refQueryID = "refQueryID='".$self->{queryID}."'" ; }
214  
215 # User could add dynamic message into satandard exception message
216 my ($exceptionMessage) = (defined($self->{message})) ? $standardMessage.$self->{message} : $standardMessage;
217  
218 if (defined($self->{type}) && ($self->{type} eq 'warning' || $self->{type} eq 'error')) {
219 if ($self->{type} eq 'error') {
220  
221 $exceptionResponse = "<mobyException $refQueryID $refElement severity='error'>\n\t<exceptionCode>".$self->{code}."</exceptionCode>\n\t<exceptionMessage>$exceptionMessage</exceptionMessage>\n</mobyException>";
222  
223 } elsif ($self->{type} eq 'warning') {
224  
225 $exceptionResponse = "<mobyException $refQueryID $refElement severity='warning'>\n\t<exceptionCode>".$self->{code}."</exceptionCode>\n\t<exceptionMessage>$exceptionMessage</exceptionMessage>\n</mobyException>";
226  
227 } else {
228 croak("type of exception is wrong or does not exists");
229 }
230
231 } else {
232 croak("type of exception is wrong or does not exists");
233 }
234 }
235  
236 return $exceptionResponse;
237 } # End retrieveExceptionResponse
238  
239 ##########################################
240 # Return xml block of one empty MobyData #
241 ##########################################
242 sub retrieveEmptyMobyData {
243 # Get parameters
244 my($self) = shift;
245
246 croak("This is an instance method!") unless(ref($self));
247
248 return "<moby:mobyData moby:queryID='".$self->{queryID}."' />";
249 } # End retrieveEmptyMobyData
250  
251 ####################################################
252 # Return xml block of one empty simple MobyArticle #
253 ####################################################
254 sub retrieveEmptyMobySimple {
255 # Get parameters
256 my($self, $outputArticle)= @_;
257
258 croak("This is an instance method!") unless(ref($self));
259
260 return "<moby:Simple moby:articleName='$outputArticle' />";
261 } # End retrieveEmptyMobySimple
262  
263 ########################################################
264 # Return xml block of one empty collection MobyArticle #
265 ########################################################
266 sub retrieveEmptyMobyCollection {
267 # Get parameters
268 my($self, $outputArticle) = @_;
269
270 croak("This is an instance method!") unless(ref($self));
271
272 return "<moby:Collection moby:articleName='$outputArticle' />";
273 } # End retrieveEmptyMobyCollection
274  
275 ########################################################################
276 # Return MobyData inserting MobyArticles that has been giving by input #
277 ########################################################################
278 sub embedMOBYArticlesIntoMOBYData {
279 # Get parameters
280 my($self, $outputMOBYArticles) = @_;
281  
282 # Returns MOBYData response
283 return "<moby:mobyData moby:queryID='".$self->{queryID}."'>$outputMOBYArticles</moby:mobyData>";
284  
285 }
286  
287 ##################################################################################
288 # Return ServiceNotes tag inserting MobyExceptions that has been giving by input #
289 ##################################################################################
290 sub embedExceptionsIntoServiceNotes {
291 # Get parameters
292 my($self, $outputMOBYExceptions) = @_;
293  
294 # Returns MOBYData response
295 return "<serviceNotes>$outputMOBYExceptions</serviceNotes>";
296  
297 }
298  
299 # UPDATE: 01-02-2006-----------------
300 # REASON: Method that returns empty mobyStatus during asynchronous callings
301 # Modified by jmrc
302  
303 ############################################
304 # Return xml block of one empty MobyStatus #
305 ############################################
306 sub retrieveEmptyMobyStatus {
307 # Get parameters
308 my($self) = shift;
309
310 croak("This is an instance method!") unless(ref($self));
311
312 return "<moby:mobyStatus moby:queryID='".$self->{queryID}."' />";
313 } # End retrieveEmtyMobyStatus
314 # UPDATE: 01-02-2006-----------------
315  
316  
317 sub DESTROY {}
318  
319 1;
320  
321 ###############################
322 # End General Purpose Package #
323 ###############################
324 __END__