ruby-fedora 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ Rakefile
6
6
  config/hoe.rb
7
7
  config/requirements.rb
8
8
  lib/ruby-fedora.rb
9
- lib/fedora/base_object.rb
9
+ lib/fedora/base.rb
10
10
  lib/fedora/connection.rb
11
11
  lib/fedora/datastream.rb
12
12
  lib/fedora/fedora_object.rb
data/README.txt CHANGED
@@ -1,19 +1,34 @@
1
- This should describe how this stuff can be built, tested and used...
2
-
3
1
  == Installation
4
2
 
5
- Required gems:
6
- - facets
7
- - activeresource
8
-
3
+ gem install ruby-fedora
4
+
5
+ irb
6
+
7
+ require 'ruby-fedora'
8
+ repository = Fedora::Repository.new('http://localhost:8080/fedora')
9
+ test_object = Fedora::FedoraObject.new(:label => 'test', :contentModel => 'Image', :state => 'A', :ownerID => 'fedoraAdmin')
10
+ repository.save(test_object)
11
+
12
+ objects = repository.find_objects('label~Image*')
13
+ object = repository.fetch_conent('demo:1')
14
+
15
+
16
+ Check out spec/repository_spec.rb for more examples.
17
+
18
+ == Build Test/Specs
19
+
20
+ To run ruby-fedora specs, install the following gems:
21
+
22
+ gem install facets mime-types rspec
23
+
24
+ To run the active-fedora specs, install the following gems:
25
+
26
+ - ambition
9
27
  - syntax
10
28
  - ParseTree
11
- - ambition
29
+
30
+ Then run "rake spec"
12
31
 
13
32
  == TODO
14
33
 
15
- - specify gem dependencies in hoe.rb
16
- - implement finders with Ambition
17
- - implement ActiveFedora::Base.establish_connection to read from repository.yml (or fedora.yml)
18
- - remove dependencies on activeresource gem
19
34
  - remove anything in test/* that are no longer relevant
@@ -58,8 +58,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
58
58
 
59
59
  # == Optional
60
60
  p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
61
- # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
62
- p.extra_deps = [['activeresource'], ['facets']]
61
+ p.extra_deps = [['facets', '>=2.4.0'], ['xml-simple', '>=1.0.11'], ['mime-types', '>=1.15']]
63
62
  #p.spec_extras = {} # A hash of extra values to set in the gemspec.
64
63
  end
65
64
 
@@ -0,0 +1,31 @@
1
+ require 'xmlsimple'
2
+
3
+ class Hash
4
+ def to_query
5
+ self.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.sort * '&'
6
+ end
7
+
8
+ def self.from_xml(xml)
9
+ XmlSimple.xml_in(xml, 'ForceArray' => false)
10
+ end
11
+ end
12
+
13
+ class Fedora::BaseObject
14
+ attr_accessor :attributes, :blob, :uri, :url
15
+ attr_reader :errors, :uri
16
+ attr_writer :new_object
17
+
18
+ # == Parameters
19
+ # attrs<Hash>:: object attributes
20
+ #-
21
+ def initialize(attrs = nil)
22
+ @new_object = true
23
+ @attributes = attrs || {}
24
+ @errors = []
25
+ @blob = attributes.delete(:blob)
26
+ end
27
+
28
+ def new_object?
29
+ @new_object
30
+ end
31
+ end
@@ -1,6 +1,8 @@
1
1
  require "base64"
2
2
  require 'cgi'
3
3
  require "mime/types"
4
+ require 'net/http'
5
+ require 'net/https'
4
6
 
5
7
  # Add multipart/form-data support to net/http
6
8
  #
@@ -55,43 +57,183 @@ class Net::HTTP::Put
55
57
  include Fedora::Multipart
56
58
  end
57
59
 
58
- # Add multipart/form-data support to active_resource
59
- module ActiveResource
60
+ module RubyFedora
61
+ # This class is based on ActiveResource::Connection so MIT license applies.
62
+ class ConnectionError < StandardError # :nodoc:
63
+ attr_reader :response
64
+
65
+ def initialize(response, message = nil)
66
+ @response = response
67
+ @message = message
68
+ end
69
+
70
+ def to_s
71
+ "Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
72
+ end
73
+ end
74
+
75
+ # 3xx Redirection
76
+ class Redirection < ConnectionError # :nodoc:
77
+ def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
78
+ end
79
+
80
+ # 4xx Client Error
81
+ class ClientError < ConnectionError; end # :nodoc:
82
+
83
+ # 400 Bad Request
84
+ class BadRequest < ClientError; end # :nodoc
85
+
86
+ # 401 Unauthorized
87
+ class UnauthorizedAccess < ClientError; end # :nodoc
88
+
89
+ # 403 Forbidden
90
+ class ForbiddenAccess < ClientError; end # :nodoc
91
+
92
+ # 404 Not Found
93
+ class ResourceNotFound < ClientError; end # :nodoc:
94
+
95
+ # 409 Conflict
96
+ class ResourceConflict < ClientError; end # :nodoc:
97
+
98
+ # 5xx Server Error
99
+ class ServerError < ConnectionError; end # :nodoc:
100
+
101
+ # 405 Method Not Allowed
102
+ class MethodNotAllowed < ClientError # :nodoc:
103
+ def allowed_methods
104
+ @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
105
+ end
106
+ end
107
+
108
+ # Class to handle connections to remote web services.
109
+ # This class is used by ActiveResource::Base to interface with REST
110
+ # services.
60
111
  class Connection
61
- alias_method :post_without_multipart, :post
62
- alias_method :put_without_multipart, :put
63
-
112
+ attr_reader :site
113
+ attr_accessor :format
114
+
115
+ class << self
116
+ def requests
117
+ @@requests ||= []
118
+ end
119
+ end
120
+
121
+ # The +site+ parameter is required and will set the +site+
122
+ # attribute to the URI for the remote resource service.
123
+ def initialize(site, format = ActiveResource::Formats[:xml])
124
+ raise ArgumentError, 'Missing site URI' unless site
125
+ self.site = site
126
+ self.format = format
127
+ end
128
+
129
+ # Set URI for remote service.
130
+ def site=(site)
131
+ @site = site.is_a?(URI) ? site : URI.parse(site)
132
+ end
133
+
134
+ # Execute a GET request.
135
+ # Used to get (find) resources.
136
+ def get(path, headers = {})
137
+ format.decode(request(:get, path, build_request_headers(headers)).body)
138
+ end
139
+
140
+ # Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
141
+ # Used to delete resources.
142
+ def delete(path, headers = {})
143
+ request(:delete, path, build_request_headers(headers))
144
+ end
145
+
146
+ # Execute a PUT request (see HTTP protocol documentation if unfamiliar).
147
+ # Used to update resources.
64
148
  def put(path, body = '', headers = {})
65
149
  if body.is_a?(File)
66
150
  multipart_request(Net::HTTP::Put.new(path, build_request_headers(headers)), body)
67
151
  else
68
- put_without_multipart(path, body, headers)
69
- end
152
+ request(:put, path, body.to_s, build_request_headers(headers))
153
+ end
70
154
  end
71
-
155
+
156
+ # Execute a POST request.
157
+ # Used to create new resources.
72
158
  def post(path, body = '', headers = {})
73
159
  if body.is_a?(File)
74
- multipart_request(Net::HTTP::Post.new(path, build_request_headers(headers)), body)
160
+ multipart_request(Net::HTTP::Post.new(path, build_request_headers(headers)), body)
75
161
  else
76
- post_without_multipart(path, body, headers)
162
+ request(:post, path, body.to_s, build_request_headers(headers))
77
163
  end
78
164
  end
79
-
80
- def multipart_request(req, file)
81
- logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
82
- result = nil
83
- time = Benchmark.realtime do
165
+
166
+ def raw_get(path, headers = {})
167
+ request(:get, path, build_request_headers(headers))
168
+ end
169
+
170
+ private
171
+ def multipart_request(req, file)
172
+ result = nil
84
173
  http.start do |conn|
85
174
  req.set_multipart_data(:file => file)
86
175
  result = conn.request(req)
87
176
  end
177
+ handle_response(result)
88
178
  end
89
- logger.info "--> #{result.code} #{result.message} (#{result.body ? result.body : 0}b %.2fs)" % time if logger
90
- handle_response(result)
91
- end
92
179
 
93
- def raw_get(path, headers = {})
94
- request(:get, path, build_request_headers(headers))
95
- end
180
+ # Makes request to remote service.
181
+ def request(method, path, *arguments)
182
+ result = http.send(method, path, *arguments)
183
+ handle_response(result)
184
+ end
185
+
186
+ # Handles response and error codes from remote service.
187
+ def handle_response(response)
188
+ case response.code.to_i
189
+ when 301,302
190
+ raise(Redirection.new(response))
191
+ when 200...400
192
+ response
193
+ when 400
194
+ raise(BadRequest.new(response))
195
+ when 401
196
+ raise(UnauthorizedAccess.new(response))
197
+ when 403
198
+ raise(ForbiddenAccess.new(response))
199
+ when 404
200
+ raise(ResourceNotFound.new(response))
201
+ when 405
202
+ raise(MethodNotAllowed.new(response))
203
+ when 409
204
+ raise(ResourceConflict.new(response))
205
+ when 422
206
+ raise(ResourceInvalid.new(response))
207
+ when 401...500
208
+ raise(ClientError.new(response))
209
+ when 500...600
210
+ raise(ServerError.new(response))
211
+ else
212
+ raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
213
+ end
214
+ end
215
+
216
+ # Creates new Net::HTTP instance for communication with
217
+ # remote service and resources.
218
+ def http
219
+ http = Net::HTTP.new(@site.host, @site.port)
220
+ http.use_ssl = @site.is_a?(URI::HTTPS)
221
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
222
+ http
223
+ end
224
+
225
+ def default_header
226
+ @default_header ||= { 'Content-Type' => format.mime_type }
227
+ end
228
+
229
+ # Builds headers for request to remote service.
230
+ def build_request_headers(headers)
231
+ authorization_header.update(default_header).update(headers)
232
+ end
233
+
234
+ # Sets authorization header; authentication information is pulled from credentials provided with site URI.
235
+ def authorization_header
236
+ (@site.user || @site.password ? { 'Authorization' => 'Basic ' + ["#{@site.user}:#{ @site.password}"].pack('m').delete("\r\n") } : {})
237
+ end
96
238
  end
97
- end
239
+ end
@@ -1,4 +1,4 @@
1
- require 'fedora/base_object'
1
+ require 'fedora/base'
2
2
 
3
3
  class Fedora::Datastream < Fedora::BaseObject
4
4
  def initialize(attrs = nil)
@@ -1,6 +1,6 @@
1
1
  require 'xmlsimple'
2
2
  require 'rexml/document'
3
- require 'fedora/base_object'
3
+ require 'fedora/base'
4
4
 
5
5
  class Fedora::FedoraObject < Fedora::BaseObject
6
6
  attr_accessor :target_repository
@@ -1,6 +1,6 @@
1
1
  require 'facets/hash/symbolize_keys'
2
2
 
3
- require 'active_resource'
3
+ require 'fedora/base'
4
4
  require 'fedora/connection'
5
5
  require 'fedora/formats'
6
6
  require 'fedora/fedora_object'
@@ -73,7 +73,7 @@ module Fedora
73
73
  case object
74
74
  when Fedora::FedoraObject
75
75
  pid = (object.pid ? object : 'new')
76
- response = connection.post("#{url_for(pid)}?" + object.attributes.to_query, object.xml_content)
76
+ response = connection.post("#{url_for(pid)}?" + object.attributes.to_query, object.blob)
77
77
  if response.code == '201'
78
78
  object.pid = extract_pid(response)
79
79
  object.new_object = false
@@ -84,7 +84,7 @@ module Fedora
84
84
  when Fedora::Datastream
85
85
  raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
86
86
  response = connection.post("#{url_for(object)}?" + object.attributes.to_query,
87
- object.xml_content)
87
+ object.blob)
88
88
  if response.code == '201'
89
89
  object.new_object = false
90
90
  true
@@ -109,7 +109,7 @@ module Fedora
109
109
  response.code == '307'
110
110
  when Fedora::Datastream
111
111
  raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
112
- response = connection.put("#{url_for(object)}?" + object.attributes.to_query, object.xml_content)
112
+ response = connection.put("#{url_for(object)}?" + object.attributes.to_query, object.blob)
113
113
  response.code == '201'
114
114
  else
115
115
  raise ArgumentError, "Unknown object type"
@@ -154,7 +154,7 @@ module Fedora
154
154
  def convert_xml(response)
155
155
  results = FedoraObjects.new
156
156
  return results unless response && response['resultList']
157
-
157
+
158
158
  results.session_token = response['listSession']['token'] if response['listSession']
159
159
  objectFields = response['resultList']['objectFields']
160
160
  case objectFields
@@ -177,7 +177,7 @@ module Fedora
177
177
  # or not (defaults to +false+).
178
178
  def connection(refresh = false)
179
179
  if refresh || @connection.nil?
180
- @connection = ActiveResource::Connection.new(@fedora_url, Fedora::XmlFormat)
180
+ @connection = RubyFedora::Connection.new(@fedora_url, Fedora::XmlFormat)
181
181
  end
182
182
  @connection
183
183
  end
@@ -6,7 +6,7 @@ module RubyFedora #:nodoc:
6
6
  module VERSION #:nodoc:
7
7
  MAJOR = 0
8
8
  MINOR = 1
9
- TINY = 0
9
+ TINY = 1
10
10
 
11
11
  STRING = [MAJOR, MINOR, TINY].join('.')
12
12
  end
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
 
4
- require File.dirname(__FILE__) + '/../lib/RubyFedora'
4
+ require File.dirname(__FILE__) + '/../lib/ruby-fedora'
5
5
 
6
6
  FEDORA_URL = 'http://localhost:8080/fedora' unless defined? FEDORA_URL
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fedora
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zumwalt
@@ -9,26 +9,35 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-01 00:00:00 -05:00
12
+ date: 2008-04-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: activeresource
16
+ name: facets
17
17
  version_requirement:
18
18
  version_requirements: !ruby/object:Gem::Requirement
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: "0"
22
+ version: 2.4.0
23
23
  version:
24
24
  - !ruby/object:Gem::Dependency
25
- name: facets
25
+ name: xml-simple
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.0.11
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: mime-types
26
35
  version_requirement:
27
36
  version_requirements: !ruby/object:Gem::Requirement
28
37
  requirements:
29
38
  - - ">="
30
39
  - !ruby/object:Gem::Version
31
- version: "0"
40
+ version: "1.15"
32
41
  version:
33
42
  description: Ruby API for Fedora
34
43
  email:
@@ -52,7 +61,7 @@ files:
52
61
  - config/hoe.rb
53
62
  - config/requirements.rb
54
63
  - lib/ruby-fedora.rb
55
- - lib/fedora/base_object.rb
64
+ - lib/fedora/base.rb
56
65
  - lib/fedora/connection.rb
57
66
  - lib/fedora/datastream.rb
58
67
  - lib/fedora/fedora_object.rb
@@ -106,8 +115,4 @@ signing_key:
106
115
  specification_version: 2
107
116
  summary: Ruby API for Fedora
108
117
  test_files:
109
- - test/test_fedora.rb
110
118
  - test/test_helper.rb
111
- - test/test_httpclient.rb
112
- - test/test_restful_bits.rb
113
- - test/test_restful_fedora.rb
@@ -1,19 +0,0 @@
1
- class Fedora::BaseObject
2
- attr_accessor :attributes, :xml_content, :uri, :url
3
- attr_reader :errors, :uri
4
- attr_writer :new_object
5
-
6
- # == Parameters
7
- # attrs<Hash>:: object attributes
8
- #-
9
- def initialize(attrs = nil)
10
- @new_object = true
11
- @attributes = attrs || {}
12
- @errors = []
13
- @xml_content = attributes.delete(:xml_content)
14
- end
15
-
16
- def new_object?
17
- @new_object
18
- end
19
- end
@@ -1,55 +0,0 @@
1
- #
2
- # test_fedora.rb
3
- #
4
- # Created: Nov 3, 2007, 6:28:37 PM
5
- #
6
- # Author: Matt Zumwalt <matt.zumwalt@yourmediashelf.com>
7
- # Copyright: Copyright (c) 2007 Matt Zumwalt <matt.zumwalt@yourmediashelf.com>
8
- # License:
9
- #
10
- require File.dirname(__FILE__) + '/test_helper.rb'
11
-
12
- class TestFedoraRepository < Test::Unit::TestCase
13
-
14
- def setup
15
- @fedora = Fedora::Repository.new(FEDORA_URL, :user => 'fedoraAdmin', :password => 'fedoraAdmin')
16
- end
17
-
18
- def teardown
19
- # TODO cleanup
20
- end
21
-
22
- def test_namespace_juggling
23
- namespace_info = @fedora.namespace_info
24
- new_pid = @fedora.next_pid
25
-
26
- assert_equal "#{namespace_info[:default_namespace]}#{namespace_info[:pid_delimiter]}100", namespace_info[:sample_pid]
27
- assert_true namespace_info[:retained_pids].include?("fedora-bdef")
28
- assert_true namespace_info[:retained_pids].include?("fedora-bmech")
29
- end
30
-
31
- def test_objects
32
- puts 'DEBUG 3'
33
- test_object_1 = @fedora.objects.create(:namespace => "test") # Create a new empty object with an auto-assigned pid
34
- # Creating multiple objects simultaneously by submitting an array
35
- objects_array = @fedora.objects.create([{:owner_id => "MISC_USER_ID", :namespace => "test"},{:pid => "test:OBJECT_FROM_ARRAY_CREATE_TEST"}])
36
-
37
- test_object_1.label = "DELETE_ME"
38
- test_object_1.state = "I"
39
-
40
- # Not sure if "save should return PID or "true"
41
- assert_equal(true, test_object_1.save)
42
- assert_equal(test_object_1.state, "I")
43
-
44
- found_by_pid = @fedora.objects.find_by_pid(test_object_1.pid)
45
-
46
- assert_equal(found_by_pid.label, test_object_1.label)
47
- assert_equal(objects_array[0].owner_id, @fedora.objects.find_by_pid(objects_array[0].pid).owner_id)
48
-
49
- test_object1.destroy
50
- objects_array.each do |obj|
51
- obj.destroy
52
- end
53
- # test_object2.destroy # leave this until the final cleanup...
54
- end
55
- end
@@ -1,73 +0,0 @@
1
- #
2
- # @Creator Matt Zumwalt, MediaShelf LLC
3
- # @Copyright Matt Zumwalt, 2007. All Rights Reserved.
4
- #
5
- require File.dirname(__FILE__) + '/test_helper.rb'
6
-
7
- require 'uri'
8
- require 'httpclient'
9
-
10
- class TestFedoraHttpClient < Test::Unit::TestCase
11
-
12
- def setup
13
- @test_pid = "mshlf:test_httpclient"
14
- @user = "fedoraAdmin"
15
- @password = "fedoraAdmin"
16
- end
17
-
18
- def url_for(pid, dsid = nil, params = {})
19
- url = "#{FEDORA_URL}/objects/#{@test_pid}"
20
- url += "/datastreams/#{dsid}" if dsid
21
- url += ("?" + params.collect { |key, value| "#{key}=#{value}" } * '&') unless params.empty?
22
- url
23
- end
24
-
25
- def test_post_empty
26
- client = HTTPClient.new
27
-
28
- extheader = {'User-Agent' => 'RubyFedora'}
29
- extheader['content-type'] = "text/xml"
30
- uri = url_for(@test_pid)
31
-
32
- client.set_basic_auth(uri, @user, @password)
33
- create_response = client.post(uri, "", extheader)
34
-
35
- assert_equal 201, create_response.header.response_status_code
36
-
37
- end
38
-
39
- def test_post_xml
40
- client = HTTPClient.new
41
- sample_xml_1 = File.new(File.dirname(__FILE__) + '/fixtures/mods-mskcc-filledsample.xml')
42
-
43
- extheader = {'User-Agent' => 'RubyFedora'}
44
- extheader['content-type'] = "text/xml"
45
- request_body = sample_xml_1
46
- uri = url_for(@test_pid, "XML_DS")
47
- client.set_basic_auth(uri, @user, @password)
48
- create_response = client.post(uri, request_body, extheader)
49
- delete_response = client.delete(uri)
50
-
51
- assert_equal 201, create_response.header.response_status_code
52
- assert_equal 200, delete_response.header.response_status_code
53
- end
54
-
55
- def test_multipart_post
56
- client = HTTPClient.new
57
- sample_binary_file_1 = File.open(File.dirname(__FILE__) + "/fixtures/dino.jpg" )
58
-
59
- boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
60
- extheader = {'User-Agent' => 'RubyFedora'}
61
- extheader['content-type'] = "multipart/form-data; boundary=___#{ boundary }___"
62
- extheader['Content-Type'] = "image/jpeg"
63
-
64
- request_body = {'inputfile' => sample_binary_file_1}
65
- uri = url_for(@test_pid, "BINARY_DS", { :dsLabel => "Foo+Bar" })
66
- client.set_basic_auth(uri, @user, @password)
67
- create_response = client.post(uri, request_body, extheader)
68
- delete_response = client.delete(uri)
69
-
70
- assert_equal 201, create_response.header.response_status_code
71
- assert_equal 200, delete_response.header.response_status_code
72
- end
73
- end
@@ -1,63 +0,0 @@
1
- #
2
- # @Creator Matt Zumwalt, MediaShelf LLC
3
- # @Copyright Matt Zumwalt, 2007. All Rights Reserved.
4
- #
5
- #++
6
-
7
- require File.dirname(__FILE__) + '/test_helper.rb'
8
-
9
- class TestRestfulFedora < Test::Unit::TestCase
10
- def setup
11
- @fedora = RubyFedora::RestfulFedora.new(FEDORA_URL, :user=>'fedoraAdmin', :password=>'fedoraAdmin')
12
- @search_term = "test" # this should be a search term that will return at least one hit
13
- @pid_delimiter = ":"
14
- end
15
-
16
- def test_simple_search
17
- test_object_pid = "test:do_not_delete"
18
- should_not_exist = "test:never_use_this_pid"
19
-
20
- find_by_pid_response = @fedora.simple_search(:conditions => {:pid => test_object_pid})
21
- find_by_pid_convenience_response = @fedora.find_by_pid(test_object_pid)
22
-
23
- #Should support conditions provided as an array and conditions provided as a hash.
24
- namespace = "test"
25
- find_by_namespace_response1 = @fedora.simple_search(:conditions => {:namespace => namespace})
26
- #puts find_by_namespace_response1.body.content
27
- # Decided not to support directly feeding in url query strings verbatim...
28
- # found_by_namespace2 = @fedora.simple_search(:conditions => ["pid~#{namespace}:*"])
29
- find_with_conditions_response = @fedora.simple_search(:conditions => {:fType => "O"})
30
- #puts find_with_conditions_response.body.content
31
-
32
- # found_all = @fedora.simple_search(:all)
33
- # found_all_limited = @fedora.simple_search(:all, :limit => 10)
34
- #
35
- # found_by_created_range = @fedora.simple_search(:conditions => {:created_before => "2008-01-22", :created_after => "2008-01-20"})
36
- # found_by_created_on = @fedora.simple_search(:conditions => {:date_created => "2008-01-21"})
37
- #
38
- # found_by_modified_range = @fedora.simple_search(:conditions => {:modified_before => "2008-01-22", :modified_after => "2008-01-20"})
39
- # found_by_modified_on = @fedora.simple_search(:conditions => {:date_modified => "2008-01-21"})
40
-
41
-
42
- ## TODO: Decide if this is the right way to go. Possibly return XMLSimple objects instead?
43
- assert_equal Array, find_by_pid_response.class
44
- assert_equal Array, find_by_pid_convenience_response.class
45
- assert_equal Array, find_by_namespace_response1.class
46
- assert_equal Array, find_with_conditions_response.class
47
- assert_equal test_object_pid, find_by_pid_response.first[:pid]
48
- assert_equal test_object_pid, find_by_pid_convenience_response.first[:pid]
49
-
50
- assert_throws RecordNotFound, @fedora.simple_search(:conditions => {:pid => should_not_exist})
51
- end
52
-
53
- def test_datetime_finders
54
- created_after_response = @fedora.find_by_create_date(:after => "2008-01-01")
55
- created_before_response = @fedora.find_by_create_date(:before => "2008-01-22")
56
-
57
- ## TODO: Decide if this is the right way to go. Possibly return XMLSimple objects instead?
58
- assert_equal Array, created_after_response
59
- assert_equal Array, created_before_response
60
- assert_true created_after_response.first.has_key?(:pid)
61
- assert_true created_before_response.last.has_key?(:pid)
62
- end
63
- end
@@ -1,160 +0,0 @@
1
- #
2
- # @Creator Matt Zumwalt, MediaShelf LLC
3
- # @Copyright Matt Zumwalt, 2007. All Rights Reserved.
4
- #
5
- #++
6
-
7
- require File.dirname(__FILE__) + '/test_helper.rb'
8
-
9
- class TestRestfulFedora < Test::Unit::TestCase
10
-
11
- def setup
12
- @fedora = Fedora::RestfulFedora.new(FEDORA_URL, :user=>'fedoraAdmin', :password=>'fedoraAdmin')
13
- @search_term = "test" # this should be a search term that will return at least one hit
14
- @pid_delimiter = ":"
15
- @test_object_pid = "test:do_not_delete"
16
- end
17
-
18
- def test_pid_generator
19
- pid1 = @fedora.next_pid
20
- pid2 = @fedora.next_pid
21
- pid_from_old_api = @fedora.next_pid_old_api
22
-
23
- # Everything up to the pid_delimiter should be identical in pid1 & pid_from_old_api
24
- assert_equal pid1[0..pid1.index(@pid_delimiter)], pid_from_old_api[0..pid1.index(@pid_delimiter)]
25
- # After the pid_delimiter should be two integers, where the pid2 = pid1 + 1
26
- assert_equal pid1[pid1.index(@pid_delimiter)+1..pid1.length].to_i, pid2[pid1.index(@pid_delimiter)+1..pid2.length].to_i-1
27
-
28
- end
29
-
30
- def test_datastream_resource
31
- pid_of_test_object = @fedora.create_object(:pid=>"test:test_datastream_resource", :params=>{:label=>"DELETE ME"})
32
-
33
- sample_xml_1 = File.new(File.dirname(__FILE__) + '/fixtures/mods-mskcc-filledsample.xml')
34
- sample_xml_2 = File.new(File.dirname(__FILE__) + '/fixtures/sample_mods_file_A.xml')
35
- sample_binary_file_1 = File.open(File.dirname(__FILE__) + "/fixtures/dino.jpg" )
36
- sample_binary_file_2 = File.open(File.dirname(__FILE__) + "/fixtures/minivan-in-minneapolis.jpg" )
37
-
38
- create_xml_result = @fedora.add_xml_datastream(:pid=>pid_of_test_object, :dsid=>"XML_DS", :xml_content=>sample_xml_1.read, :params=>{:ds_label=>"Sample XML"})
39
- create_binary_result = @fedora.add_binary_datastream(:pid=>pid_of_test_object, :dsid=>"BINARY_DS", :file=>sample_binary_file_1, :content_type => "image/jpeg", :params=>{:ds_label=>"Sample Binary File"})
40
- object_xml = REXML::Document.new(@fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid_of_test_object}/objectXML").content)
41
- create_redirect_result = @fedora.add_redirect_datastream(:pid=>pid_of_test_object, :dsid=>"REDIRECT", :content_type=>"image/jpeg", :params=>{:ds_label=>"Sample Redirect", :ds_location=>create_binary_result.header['Location'].to_s})
42
-
43
- assert_equal 201, create_xml_result.header.response_status_code
44
- assert_equal 201, create_binary_result.header.response_status_code
45
- #puts object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='BINARY_DS']"].attributes
46
- assert_equal "image/jpeg", object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='BINARY_DS']/foxml:datastreamVersion"].attributes["MIMETYPE"]
47
- assert_equal 201, create_redirect_result.header.response_status_code
48
- # GETting the REDIRECT datastream should return a Location header that points to the BINARY datastream
49
- assert_equal @fedora.get_datastream(pid_of_test_object, "REDIRECT").header['Location'], create_binary_result.header['Location']
50
-
51
- update_xml_result = @fedora.update_datastream(:pid=>pid_of_test_object, :dsid=>"XML_DS", :xml_content=>sample_xml_2.read, :params=>{:ds_label=>"Updated XML Datastream"})
52
- update_binary_result = @fedora.update_datastream(:pid=>pid_of_test_object, :dsid=>"BINARY_DS", :file=>sample_binary_file_1, :content_type => "image/jpeg")
53
-
54
- assert_equal 201, update_xml_result.header.response_status_code
55
- assert_equal 201, update_binary_result.header.response_status_code
56
-
57
- update_attributes_only_result = @fedora.update_datastream(:pid=>pid_of_test_object, :dsid=>"XML_DS", :params=>{:ds_label=>"Updated Datastream"})
58
-
59
- # TODO: Fix update_datastream to support updating attributes only. (Requires fix in Fedora REST code)
60
- #assert_equal 200, update_attributes_only_result.header.response_status_code
61
- # Make sure that updating attributes didn't change the datastream content
62
- #assert_equal sample_xml_2, @fedora.get_datastream(:pid=>pid_of_test_object, :dsid=>"XML_DS")
63
-
64
- delete_xml_result = @fedora.delete_datastream(:pid=>pid_of_test_object, :dsid=>"XML_DS")
65
- delete_binary_result = @fedora.delete_datastream(:pid=>pid_of_test_object, :dsid=>"BINARY_DS")
66
-
67
- assert_equal 200, delete_xml_result.header.response_status_code
68
- assert_equal 200, delete_binary_result.header.response_status_code
69
-
70
- @fedora.delete_object(:pid=>pid_of_test_object)
71
-
72
- end
73
-
74
- def test_simple_search
75
- should_not_exist = "test:never_use_this_pid"
76
-
77
- find_by_pid_response = @fedora.simple_search(:conditions => {:pid => @test_object_pid})
78
- find_by_pid_convenience_response = @fedora.find_by_pid(@test_object_pid)
79
-
80
- #Should support conditions provided as an array and conditions provided as a hash.
81
- namespace = "test"
82
- find_by_namespace_response1 = @fedora.simple_search(:conditions => {:namespace => namespace})
83
- #puts find_by_namespace_response1.body.content
84
- # Decided not to support directly feeding in url query strings verbatim...
85
- # found_by_namespace2 = @fedora.simple_search(:conditions => ["pid~#{namespace}:*"])
86
- find_with_conditions_response = @fedora.simple_search(:conditions => {:fType => "O"})
87
- #puts find_with_conditions_response.body.content
88
- # found_all = @fedora.simple_search(:all)
89
- # found_all_limited = @fedora.simple_search(:all, :limit => 10)
90
- #
91
- # found_by_created_range = @fedora.simple_search(:conditions => {:created_before => "2008-01-22", :created_after => "2008-01-20"})
92
- # found_by_created_on = @fedora.simple_search(:conditions => {:date_created => "2008-01-21"})
93
- #
94
- # found_by_modified_range = @fedora.simple_search(:conditions => {:modified_before => "2008-01-22", :modified_after => "2008-01-20"})
95
- # found_by_modified_on = @fedora.simple_search(:conditions => {:date_modified => "2008-01-21"})
96
-
97
- assert true
98
-
99
- end
100
-
101
-
102
-
103
- def test_datetime_finders
104
-
105
- created_after = @fedora.find_by_create_date(:after => "2008-01-01")
106
- created_before = @fedora.find_by_create_date(:before => "2008-01-22")
107
-
108
- assert true
109
- end
110
-
111
- def test_call_resource
112
-
113
- sample_xml_1 = File.new(File.dirname(__FILE__) + '/fixtures/mods-mskcc-filledsample.xml')
114
- sample_binary_file_1 = File.open(File.dirname(__FILE__) + "/fixtures/dino.jpg" )
115
- search_result = @fedora.call_resource(:method => "GET", :resource_path => "/objects", :request_body => "", :content_type => "text/xml", :auth => {}, :params => {:query =>"", :pid=>"true", :format=>"xml", :terms=>@search_term})
116
- result_rexml = REXML::Document.new(search_result.content)
117
- pid = result_rexml.elements["result/resultList/objectFields/pid"].text
118
- #pid = "changeme:1"
119
-
120
- assert_equal(result_rexml.root.name, "result")
121
- assert_equal result_rexml.elements["result/resultList/objectFields"][1].name, "pid"
122
- # test each of the endpoints that we want to be able to send requests to...
123
-
124
- # Other RETRIEVE Operaitons ..
125
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}", :params=>{:format=>"xml"}).header.response_status_code
126
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/objectXML", :params=>{:format=>"xml"}).header.response_status_code
127
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/versions", :params=>{:format=>"xml"}).header.response_status_code
128
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/export", :params=>{}).header.response_status_code
129
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/methods", :params=>{:format=>"xml"}).header.response_status_code
130
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/datastreams", :params=>{:format=>"xml"}).header.response_status_code
131
- assert_equal 200, @fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/datastreams/DC", :params=>{:format=>"xml"}).header.response_status_code
132
-
133
- pid = "test:test_object_1"
134
- # Create Object
135
- object_create_response = @fedora.call_resource(:method => "POST", :resource_path =>"/objects/#{pid}", :request_body => "", :params => {"label"=>"TEST ME"})
136
-
137
- assert_equal "#{@@fedora_url}/#{@fedora.resource_base}/objects/test%3Atest_object_1", object_create_response.header['location'][0]
138
- assert_equal 201, object_create_response.header.response_status_code
139
-
140
- # Create XML Datastream
141
- xml_datastream_create_response = @fedora.call_resource(:method => "POST", :resource_path =>"/objects/#{pid}/datastreams/XML", :request_body => sample_xml_1.read, :params => {"dsLabel"=>"Sample XML File"})
142
- # Create Binary Datastream
143
- # !! set mimetype by setting :content_type !!
144
- binary_datastream_create_response = @fedora.call_resource(:method => "UPLOAD", :resource_path =>"/objects/#{pid}/datastreams/BINARY", :request_body => sample_binary_file_1, :content_type => "image/jpeg", :params => {"dsLabel"=>"Sample XML File"})
145
- assert_equal 201, xml_datastream_create_response.header.response_status_code
146
- assert_equal 201, binary_datastream_create_response.header.response_status_code
147
-
148
-
149
- object_xml = REXML::Document.new(@fedora.call_resource(:method => "GET", :resource_path =>"/objects/#{pid}/objectXML").content)
150
- assert_equal "image/jpeg", object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='BINARY']/foxml:datastreamVersion"].attributes["MIMETYPE"]
151
- assert_equal "X", object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='XML']"].attributes["CONTROL_GROUP"]
152
- assert_equal "M", object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='BINARY']"].attributes["CONTROL_GROUP"]
153
- #puts object_xml.elements["foxml:digitalObject/foxml:datastream[@ID='BINARY']/foxml:datastreamVersion"].attributes["MIMETYPE"]
154
-
155
- # Delete Object
156
- delete_response = @fedora.call_resource(:method => "DELETE", :resource_path =>"/objects/#{pid}")
157
-
158
- assert_equal 200, delete_response.header.response_status_code
159
- end
160
- end