LS/Service/DataService.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 158 CC 22 SCAR 30.5
CC
22
Cov
0%
getDataByRange
42.0
getData
30.0
getMetadata
30.0
CRAP 21.1
1 # =====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::Service::DataService;
10  
11 use strict;
12 use warnings;
13  
14 use vars qw( $METHODS );
15  
16 use LS;
17 use LS::ID;
18  
19 use base 'LS::Base';
20  
21 sub BEGIN {
22  
23 $METHODS =[
24 'location',
25 'namespaces',
26
27 'authenticationHandler',
28 ];
29
30 LS::makeAccessorMethods($METHODS, __PACKAGE__);
31 }
32  
33 #
34 # new( %options )
35 #
36 sub new {
37  
38 my ($self, %options) = @_;
39
40 unless (ref $self) {
41
42 $self = bless {}, $self;
43
44 $self->authenticationHandler( ($options{'auth_handler'} || $options{'authenticate'} ) );
45
46 $self->namespaces( {} );
47 }
48
49 return $self;
50 }
51  
52  
53 #
54 # addNamespace( $namespace )
55 #
56 sub addNamespace {
57
58 my $self = shift;
59 my ($ns) = @_;
60
61 unless(UNIVERSAL::isa($ns, 'LS::Service::Namespace') ) {
62  
63 $self->recordError('Namespace parameter is not a LS::Service::Namespace reference');
64 $self->addStackTrace();
65  
66 return undef;
67 }
68
69 unless($ns->name()) {
70
71 $self->recordError('LS::Service::Namespace does not have a name');
72 $self->addStackTrace();
73  
74 return undef;
75 }
76  
77 $self->namespaces()->{ $ns->name() } = $ns;
78 }
79  
80  
81 #
82 # add_namespace( $namespace )
83 # Synonym for addNamespace.
84 #
85 sub add_namespace {
86  
87 my $self = shift;
88 return $self->addNamespace(@_);
89 }
90  
91  
92  
93 #
94 # authenticate( )
95 #
96 sub authenticate {
97
98 my $self = shift;
99
100 if($self->authenticationHandler()) {
101
102 my @credentials = shift;
103 # FIXME: CREATE A CREDENTIALS OBJECT
104 return $self->authenticationHandler()->(@credentials);
105 }
106
107 return undef;
108 }
109  
110  
111 #
112 # findNamespace( $namespace )
113 #
114 sub findNamespace {
115  
116 my $self = shift;
117 my $namespace = shift;
118 return $self->namespaces()->{ $namespace };
119 }
120  
121  
122 #
123 # Call points
124 #
125  
126  
127 #
128 # getData( $lsid )
129 #
130 sub getData {
131
132 my ($self, $lsid) = @_;
133  
134 #
135 # Make sure we have an LSID or a string representing an LSID
136 #
137 return LS::Service::Fault->fault('Malformed LSID')
138 unless($lsid);
139  
140 $lsid = new LS::ID($lsid)
141 unless(UNIVERSAL::isa($lsid, 'LS::ID') );
142
143 return LS::Service::Fault->fault('Malformed LSID')
144 unless($lsid);
145
146 my $ns = $self->findNamespace( $lsid->namespace() );
147 return LS::Service::Fault->fault('Unknown LSID')
148 unless( $ns );
149
150 return $ns->getData($lsid);
151 }
152  
153  
154 #
155 # getDataByRange( $lsid, $start, $length )
156 #
157 sub getDataByRange {
158  
159 my ($self, $lsid, $start, $length) = @_;
160  
161 # We need a range
162 return LS::Service::Fault->fault('Internal server error')
163 unless($start && $length);
164  
165 return LS::Service::Fault->fault('Malformed LSID')
166 unless($lsid);
167  
168 $lsid = new LS::ID($lsid)
169 unless(UNIVERSAL::isa($lsid, 'LS::ID'));
170
171 return LS::Service::Fault->fault('Malformed LSID')
172 unless($lsid);
173  
174
175 my $ns = $self->findNamespace( $lsid->namespace() );
176 return LS::Service::Fault->fault('Unknown LSID')
177 unless($ns);
178
179 return $ns->getDataByRange($lsid, $start, $length);
180 }
181  
182  
183 #
184 # getMetadata( $lsid, $format, @params )
185 #
186 sub getMetadata {
187
188 my ($self, $lsid, $format, @params) = @_;
189  
190 return LS::Service::Fault->fault('Malformed LSID')
191 unless($lsid);
192  
193 $lsid = new LS::ID($lsid)
194 unless(UNIVERSAL::isa($lsid, 'LS::ID'));
195
196 return LS::Service::Fault->fault('Malformed LSID')
197 unless($lsid);
198
199  
200 my $ns = $self->findNamespace( $lsid->namespace() );
201 return LS::Service::Fault->fault('Unknown LSID')
202 unless( $ns );
203  
204 return $ns->getMetadata($lsid, $format, @params);
205 }
206  
207  
208 #
209 # getMetadataSubset( $lsid, $format, @params )
210 #
211 sub getMetadataSubset {
212
213 my ($self, $lsid, $format, @params) = @_;
214  
215 return LS::Service::Fault->fault('Malformed LSID')
216 unless($lsid);
217  
218 $lsid = new LS::ID($lsid)
219 unless(UNIVERSAL::isa($lsid, 'LS::ID'));
220
221 return LS::Service::Fault->fault('Malformed LSID')
222 unless($lsid);
223
224  
225 my $ns = $self->findNamespace( $lsid->namespace() );
226 return LS::Service::Fault->fault('Unknown LSID')
227 unless($ns);
228
229 return $ns->getMetadataSubset($lsid, @params);
230 }
231  
232 1;
233  
234 __END__