Simple/ServiceDescription.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 93 CC 4 SCAR 15.9
CC
4
Cov
0%
_attr_prop
12.0
getServiceInterface
6.0
_accessible
2.0
CRAP 4.9
1 #-----------------------------------------------------------------
2 # SADI::Simple::ServiceDescription
3 # Author: Ben Vandervalk (ben [dot] vvalk [at] gmail [dot] com)
4 # Edward Kawas (edward [dot] kawas [at] gmail [dot] com)
5 #
6 # For copyright and disclaimer see below.
7 #-----------------------------------------------------------------
8  
9 package SADI::Simple::ServiceDescription;
10 {
11 $SADI::Simple::ServiceDescription::VERSION = '0.15';
12 }
13  
14 use strict;
15 use warnings;
16  
17  
18 use SADI::Simple::Utils;
19 use Template;
20  
21 use base ("SADI::Simple::Base");
22  
23 {
24 my %_allowed = (
25 ServiceName => { type => SADI::Simple::Base->STRING },
26 ServiceType => { type => SADI::Simple::Base->STRING },
27 InputClass => { type => SADI::Simple::Base->STRING },
28 OutputClass => { type => SADI::Simple::Base->STRING },
29 ParameterClass => { type => SADI::Simple::Base->STRING },
30 DefaultParameterFile => { type => SADI::Simple::Base->STRING },
31 DefaultParameterRDFXML => { type => SADI::Simple::Base->STRING },
32 DefaultParameterN3 => { type => SADI::Simple::Base->STRING },
33 Description => { type => SADI::Simple::Base->STRING },
34 UniqueIdentifier => { type => SADI::Simple::Base->STRING },
35 Authority => { type => SADI::Simple::Base->STRING },
36 Provider => { type => SADI::Simple::Base->STRING },
37 ServiceURI => { type => SADI::Simple::Base->STRING },
38 NanoPublisher => { type => SADI::Simple::Base->BOOLEAN },
39 URL => {
40 type => SADI::Simple::Base->STRING,
41 post => sub {
42 my $i = shift;
43  
44 # set the signature url to be the URL address unless defined
45 $i->SignatureURL( $i->URL ) unless $i->SignatureURL;
46 # set the service uri to be the URL address unless defined
47 $i->ServiceURI( $i->URL ) unless $i->ServiceURI;
48 # set the unique id to be the URL address unless defined
49 $i->UniqueIdentifier( $i->URL ) unless $i->UniqueIdentifier;
50 }
51 },
52 Authoritative => { type => SADI::Simple::Base->BOOLEAN },
53 Format => { type => SADI::Simple::Base->STRING },
54 SignatureURL => { type => SADI::Simple::Base->STRING },
55 UnitTest => { type => 'SADI::Simple::UnitTest', is_array => 1 },
56 );
57  
58 sub _accessible {
59 my ( $self, $attr ) = @_;
60 exists $_allowed{$attr} or $self->SUPER::_accessible($attr);
61 }
62  
63 sub _attr_prop {
64 my ( $self, $attr_name, $prop_name ) = @_;
65 my $attr = $_allowed{$attr_name};
66 return ref($attr) ? $attr->{$prop_name} : $attr if $attr;
67 return $self->SUPER::_attr_prop( $attr_name, $prop_name );
68 }
69 }
70  
71 use constant SERVICE_DESCRIPTION_TEMPLATE => <<TEMPLATE;
72 [%# A template for a sadi service signature
73 ===========================
74  
75 Expected/recognized parameters:
76 name - the name of the SADI service
77 uri - the service uri
78 type - the service type
79 input - the input class URI
80 output - the output class URI
81 desc - a description for this service
82 id - a unique identifier (LSID, etc)
83 email - the providers email address
84 format - the category of service (sadi)
85 nanopublisher - can the service publish nquads for nanopubs?
86 url - the service url
87 authoritative - whether or not the service is authoritative
88 authority - the service authority URI
89 sigURL - the url to the service signature
90 tests - an array SADI::Service::UnitTest
91 -%]
92 [% SET COUNTER = 0 %]
93 [% SET TEST_COUNTER = 1 %]
94 <?xml version="1.0" encoding="UTF-8"?>
95 <rdf:RDF
96 xmlns:a="http://www.mygrid.org.uk/mygrid-moby-service#"
97 xmlns:b="http://protege.stanford.edu/plugins/owl/dc/protege-dc.owl#"
98 xmlns:np="http://www.nanopub.org/nschema#"
99 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
100 <rdf:Description rdf:about="[% uri %]">
101 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#serviceDescription"/>
102 <b:format>[% format %]</b:format>
103 <b:identifier>[% id %]</b:identifier>
104 <a:locationURI>[% url %]</a:locationURI>
105 <np:nanopublisher>[% nanopublisher %]</np:nanopublisher>
106 <a:hasServiceDescriptionText>[% FILTER xml %][% desc %][% END %]</a:hasServiceDescriptionText>
107 [%- IF sigURL == '' %]
108 <a:hasServiceDescriptionLocation/>[% ELSE %]
109 <a:hasServiceDescriptionLocation>[% sigURL %]</a:hasServiceDescriptionLocation>[%END%]
110 <a:hasServiceNameText>[% name %]</a:hasServiceNameText>
111 <a:providedBy>
112 <rdf:Description rdf:about="[% name %]_[% authority %]_[% GET COUNTER %]">[% SET COUNTER = COUNTER+1 %]
113 <a:authoritative rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">[% authoritative %]</a:authoritative>
114 <b:creator>[% email %]</b:creator>
115 <b:publisher>[% authority %]</b:publisher>
116 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#organisation"/>
117 </rdf:Description>
118 </a:providedBy>
119 <a:hasOperation>
120 <rdf:Description rdf:about="[% name %]_[% authority %]_[% GET COUNTER %]">[% SET COUNTER = COUNTER+1 %]
121 <a:hasOperationNameText>[% name %]</a:hasOperationNameText>
122 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#operation"/>
123 <a:performsTask>
124 <rdf:Description rdf:about="[% name %]_[% authority %]_[% GET COUNTER %]">[% SET COUNTER = COUNTER+1 %]
125 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#operationTask"/>
126 <rdf:type rdf:resource="[% type %]"/>
127 </rdf:Description>
128 </a:performsTask>
129 [% IF parameter && default_parameter_uri %]
130 <mygrid:inputParameter>
131 <mygrid:secondaryParameter>
132 <mygrid:hasDefaultValue rdf:about="[% default_parameter_uri %]"/>
133 <mygrid:objectType rdf:resource="[% parameter %]"/>
134 </mygrid:secondaryParameter>
135 </mygrid:inputParameter>
136 [% END %]
137 <a:inputParameter>
138 <rdf:Description rdf:about="[% name %]_[% authority %]_[% GET COUNTER %]">[% SET COUNTER = COUNTER+1 %]
139 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#parameter"/>
140 <a:objectType>
141 <rdf:Description rdf:about="[% input %]"/>
142 </a:objectType>
143 </rdf:Description>
144 </a:inputParameter>
145 <a:outputParameter>
146 <rdf:Description rdf:about="[% name %]_[% authority %]_[% GET COUNTER %]">[% SET COUNTER = COUNTER+1 %]
147 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#parameter"/>
148 <a:objectType>
149 <rdf:Description rdf:about="[% output %]"/>
150 </a:objectType>
151 </rdf:Description>
152 </a:outputParameter>[% FOREACH test IN tests %]
153 <a:hasUnitTest>
154 <rdf:Description rdf:about="[% name %]_[% authority %]_TEST_[% GET TEST_COUNTER %]">[% SET TEST_COUNTER = TEST_COUNTER + 1 %]
155 <rdf:type rdf:resource="http://www.mygrid.org.uk/mygrid-moby-service#unitTest"/>
156 [%- IF test.regex != '' %]
157 <a:validREGEX>[% FILTER xml %][%- test.regex -%][% END %]</a:validREGEX>[% END %]
158 [%- IF test.xpath != '' %]
159 <a:validXPath>[% FILTER xml %][%- test.xpath -%][% END %]</a:validXPath>[% END %]
160 [%- IF test.input != '' %]
161 [%- SET file = test.input %]
162 <a:exampleInput>[%- FILTER xml -%][%- TRY -%][%- INSERT \$file -%]
163 [%- CATCH -%][%- test.input -%][%- END -%][%- END -%]</a:exampleInput>[% END %]
164 [%- IF test.output != '' %]
165 [%- SET file = test.output %]
166 <a:validOutputXML>[%- FILTER xml -%][%- TRY -%][%- INSERT \$file -%]
167 [%- CATCH -%][%- test.output -%][% END %][%- END -%]</a:validOutputXML>[% END %]
168 </rdf:Description>
169 </a:hasUnitTest>[% END %]
170 </rdf:Description>
171 </a:hasOperation>
172 </rdf:Description>
173 </rdf:RDF>
174 TEMPLATE
175  
176 #-----------------------------------------------------------------
177 # init
178 #-----------------------------------------------------------------
179 sub init {
180 my ($self) = shift;
181 $self->SUPER::init();
182  
183 # set the default format for this signature
184 $self->Format('sadi');
185 $self->Authoritative(0);
186 }
187  
188 sub getServiceInterface {
189  
190 my ($self, $content_type) = @_;
191  
192 my $LOG = Log::Log4perl->get_logger(__PACKAGE__);
193  
194 $content_type ||= 'application/rdf+xml';
195  
196 # generate from template
197  
198 my $tt = Template->new(ABSOLUTE => 1, TRIM => 1);
199  
200 my $input = SERVICE_DESCRIPTION_TEMPLATE;
201 my $sadi_interface_signature;
202  
203 $tt->process(
204  
205 \$input,
206 {
207 name => $self->ServiceName,
208 uri => $self->ServiceURI,
209 type => $self->ServiceType,
210 input => $self->InputClass,
211 output => $self->OutputClass,
212 parameter => $self->ParameterClass,
213 desc => $self->Description,
214 id => $self->UniqueIdentifier || $self->ServiceURI,
215 email => $self->Provider,
216 format => $self->Format,
217 url => $self->URL,
218 nanopublisher => $self->NanoPublisher,
219 authoritative => $self->Authoritative,
220 authority => $self->Authority,
221 sigURL => $self->SignatureURL,
222 tests => $self->UnitTest || undef,
223 },
224 \$sadi_interface_signature
225  
226 ) || $LOG->logdie( $tt->error() );
227  
228 if ($content_type eq 'text/rdf+n3') {
229 return SADI::Simple::Utils->rdfxml_to_n3($sadi_interface_signature);
230 }
231  
232 return $sadi_interface_signature;
233 }
234  
235  
236 1;
237  
238 __END__