libis-tools 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +16 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +37 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +289 -0
  9. data/Rakefile +6 -0
  10. data/lib/libis-tools.rb +1 -0
  11. data/lib/libis/tools.rb +16 -0
  12. data/lib/libis/tools/assert.rb +41 -0
  13. data/lib/libis/tools/checksum.rb +84 -0
  14. data/lib/libis/tools/command.rb +40 -0
  15. data/lib/libis/tools/config.rb +160 -0
  16. data/lib/libis/tools/dc_record.rb +47 -0
  17. data/lib/libis/tools/extend/empty.rb +7 -0
  18. data/lib/libis/tools/extend/hash.rb +107 -0
  19. data/lib/libis/tools/extend/ostruct.rb +3 -0
  20. data/lib/libis/tools/extend/string.rb +85 -0
  21. data/lib/libis/tools/extend/struct.rb +29 -0
  22. data/lib/libis/tools/logger.rb +71 -0
  23. data/lib/libis/tools/mets_file.rb +575 -0
  24. data/lib/libis/tools/parameter.rb +172 -0
  25. data/lib/libis/tools/sharepoint_mapping.rb +118 -0
  26. data/lib/libis/tools/sharepoint_record.rb +260 -0
  27. data/lib/libis/tools/version.rb +5 -0
  28. data/lib/libis/tools/xml_document.rb +574 -0
  29. data/libis-tools.gemspec +39 -0
  30. data/spec/assert_spec.rb +65 -0
  31. data/spec/checksum_spec.rb +132 -0
  32. data/spec/command_spec.rb +68 -0
  33. data/spec/config_spec.rb +86 -0
  34. data/spec/data/test.data +9 -0
  35. data/spec/data/test.xml +8 -0
  36. data/spec/data/test.yml +1 -0
  37. data/spec/logger_spec.rb +107 -0
  38. data/spec/parameter_container_spec.rb +83 -0
  39. data/spec/parameter_spec.rb +139 -0
  40. data/spec/spec_helper.rb +12 -0
  41. data/spec/test.xsd +20 -0
  42. data/spec/xmldocument_spec.rb +413 -0
  43. data/test/test_helper.rb +7 -0
  44. data/test/webservices/test_ca_item_info.rb +59 -0
  45. data/test/webservices/test_ca_search.rb +35 -0
  46. metadata +244 -0
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/parameter'
4
+
5
+ describe 'ParameterContainer' do
6
+
7
+ before :context do
8
+ class TestContainer
9
+ include ::Libis::Tools::ParameterContainer
10
+
11
+ parameter check: true
12
+ parameter count: 0
13
+ parameter price: 1.0
14
+ parameter name: 'nobody'
15
+ parameter calendar: Date.new(2014, 01, 01)
16
+ parameter clock: Time.parse('10:10')
17
+ parameter timestamp: DateTime.new(2014, 01, 01, 10, 10)
18
+
19
+ end
20
+ end
21
+
22
+ before :example do
23
+ @test_container = TestContainer.new
24
+ end
25
+
26
+ it 'class should return parameter if only name is given' do
27
+ [:check, :count, :price, :name, :calendar, :clock, :timestamp].each do |v|
28
+ expect(TestContainer.parameter(v)).to be_a ::Libis::Tools::Parameter
29
+ end
30
+ end
31
+
32
+ it 'should have default parameters' do
33
+ expect(@test_container.parameter(:check)).to be_truthy
34
+ expect(@test_container.parameter(:count)).to eq 0
35
+ expect(@test_container.parameter(:price)).to eq 1.0
36
+ expect(@test_container.parameter(:name)).to eq 'nobody'
37
+ expect(@test_container.parameter(:calendar).year).to eq 2014
38
+ expect(@test_container.parameter(:calendar).month).to eq 1
39
+ expect(@test_container.parameter(:calendar).day).to eq 1
40
+ expect(@test_container.parameter(:clock).hour).to eq 10
41
+ expect(@test_container.parameter(:clock).min).to eq 10
42
+ expect(@test_container.parameter(:clock).sec).to eq 0
43
+ expect(@test_container.parameter(:timestamp).year).to eq 2014
44
+ expect(@test_container.parameter(:timestamp).month).to eq 1
45
+ expect(@test_container.parameter(:timestamp).day).to eq 1
46
+ expect(@test_container.parameter(:timestamp).hour).to eq 10
47
+ expect(@test_container.parameter(:timestamp).min).to eq 10
48
+ expect(@test_container.parameter(:timestamp).sec).to eq 0
49
+ end
50
+
51
+ it 'should allow to set values' do
52
+ @test_container.parameter(:check, false)
53
+ expect(@test_container.parameter(:check)).to be_falsey
54
+
55
+ @test_container.parameter(:count, 99)
56
+ expect(@test_container.parameter(:count)).to eq 99
57
+
58
+ @test_container.parameter(:price, 99.99)
59
+ expect(@test_container.parameter(:price)).to eq 99.99
60
+
61
+ @test_container.parameter(:name, 'everybody')
62
+ expect(@test_container.parameter(:name)).to eq 'everybody'
63
+
64
+ @test_container.parameter(:calendar, Date.new(2015, 02, 03))
65
+ expect(@test_container.parameter(:calendar).year).to eq 2015
66
+ expect(@test_container.parameter(:calendar).month).to eq 2
67
+ expect(@test_container.parameter(:calendar).day).to eq 3
68
+
69
+ @test_container.parameter(:clock, Time.parse('14:40:23'))
70
+ expect(@test_container.parameter(:clock).hour).to eq 14
71
+ expect(@test_container.parameter(:clock).min).to eq 40
72
+ expect(@test_container.parameter(:clock).sec).to eq 23
73
+
74
+ @test_container.parameter(:timestamp, DateTime.new(2015, 02, 03, 14, 40, 23))
75
+ expect(@test_container.parameter(:timestamp).year).to eq 2015
76
+ expect(@test_container.parameter(:timestamp).month).to eq 2
77
+ expect(@test_container.parameter(:timestamp).day).to eq 3
78
+ expect(@test_container.parameter(:timestamp).hour).to eq 14
79
+ expect(@test_container.parameter(:timestamp).min).to eq 40
80
+ expect(@test_container.parameter(:timestamp).sec).to eq 23
81
+ end
82
+
83
+ end
@@ -0,0 +1,139 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/parameter'
4
+
5
+ describe 'Parameter' do
6
+
7
+ before :context do
8
+ @parameter_types = %w'bool string int float datetime'
9
+
10
+ @bool_parameter = ::Libis::Tools::Parameter.new('BoolParam', true)
11
+ @string_parameter = ::Libis::Tools::Parameter.new('StringParam', 'default string')
12
+ @int_parameter = ::Libis::Tools::Parameter.new('IntParam', 5)
13
+ @float_parameter = ::Libis::Tools::Parameter.new('FloatParam', 1.0)
14
+ @datetime_parameter = ::Libis::Tools::Parameter.new('DateTimeParam', DateTime.now)
15
+
16
+ @constrained_bool_parameter = ::Libis::Tools::Parameter.new('BoolParam', true, nil, nil, true)
17
+ @constrained_string_parameter = ::Libis::Tools::Parameter.new('StringParam', 'default string', nil, nil, /^ABC.*XYZ$/i)
18
+ @constrained_int_parameter = ::Libis::Tools::Parameter.new('IntParam', 5, nil, nil, [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37])
19
+ @constrained_float_parameter = ::Libis::Tools::Parameter.new('FloatParam', 0.0, nil, nil, 1.0...3.1415927)
20
+ end
21
+
22
+ it 'should detect datatype from default value' do
23
+ @parameter_types.each do |dtype|
24
+ expect(eval("@#{dtype}_parameter").guess_datatype).to eq dtype
25
+ end
26
+ end
27
+
28
+ it 'should return default value' do
29
+ @parameter_types.each do |dtype|
30
+ expect(eval("@#{dtype}_parameter").parse).to eq eval("@#{dtype}_parameter").default
31
+ end
32
+ end
33
+
34
+ it 'should test if boolean value is valid' do
35
+ [false, true, nil, 'true', 'false', 'T', 'F', 'y', 'n', 1, 0].each do |v|
36
+ # noinspection RubyResolve
37
+ expect(@bool_parameter).to be_valid_value(v)
38
+ end
39
+
40
+ [5, 0.1, 'abc'].each do |v|
41
+ # noinspection RubyResolve
42
+ expect(@bool_parameter).not_to be_valid_value(v)
43
+ end
44
+ end
45
+
46
+ it 'should test if string value is valid' do
47
+ ['abc', true, false, nil, 1, 1.0, Object.new].each do |v|
48
+ # noinspection RubyResolve
49
+ expect(@string_parameter).to be_valid_value(v)
50
+ end
51
+ end
52
+
53
+ it 'should test if integer value is valid' do
54
+ [0, 5, -6, 3.1415926, '3', nil, Rational('1/3')].each do |v|
55
+ # noinspection RubyResolve
56
+ expect(@int_parameter).to be_valid_value(v)
57
+ end
58
+
59
+ ['abc', '3.1415926', '1 meter', false, true, Object.new].each do |v|
60
+ # noinspection RubyResolve
61
+ expect(@int_parameter).not_to be_valid_value(v)
62
+ end
63
+ end
64
+
65
+ it 'should test if float value is valid' do
66
+ [1.1, Rational('1/3'), 3.1415926, '3.1415926', nil].each do |v|
67
+ # noinspection RubyResolve
68
+ expect(@float_parameter).to be_valid_value(v)
69
+ end
70
+
71
+ ['abc', '1.0.0', false, true, Object.new].each do |v|
72
+ # noinspection RubyResolve
73
+ expect(@float_parameter).not_to be_valid_value(v)
74
+ end
75
+ end
76
+
77
+ it 'should test if datetime value is valid' do
78
+ [Time.now, Date.new, '10:10', '2014/01/01', '2014/01/01 10:10:10.000001', nil].each do |v|
79
+ # noinspection RubyResolve
80
+ expect(@datetime_parameter).to be_valid_value(v)
81
+ end
82
+
83
+ ['abc', 5, false].each do |v|
84
+ # noinspection RubyResolve
85
+ expect(@datetime_parameter).not_to be_valid_value(v)
86
+ end
87
+ end
88
+
89
+ it 'should accept and convert value for boolean parameter' do
90
+ ['true', 'True', 'TRUE','tRuE', 't', 'T', 'y', 'Y', '1', 1].each do |v|
91
+ expect(@bool_parameter.parse(v)).to be_truthy
92
+ end
93
+
94
+ ['false', 'False', 'FALSE','fAlSe', 'f', 'F', 'n', 'N', '0', 0].each do |v|
95
+ expect(@bool_parameter.parse(v)).to be_falsey
96
+ end
97
+ end
98
+
99
+ # noinspection RubyResolve
100
+ it 'should check values against constraint' do
101
+ expect(@constrained_bool_parameter).to be_valid_value(true)
102
+ expect(@constrained_bool_parameter).to be_valid_value('y')
103
+
104
+ expect(@constrained_bool_parameter).not_to be_valid_value(false)
105
+ expect(@constrained_bool_parameter).not_to be_valid_value('n')
106
+
107
+ %w'ABCXYZ ABC123XYZ abcxyz AbC__++__xYz'.each do |v|
108
+ # noinspection RubyResolve
109
+ expect(@constrained_string_parameter).to be_valid_value(v)
110
+ end
111
+
112
+ %w'ABC XYZ ABC123'.each do |v|
113
+ # noinspection RubyResolve
114
+ expect(@constrained_string_parameter).not_to be_valid_value(v)
115
+ end
116
+
117
+ [1, 7, 11, nil].each do |v|
118
+ # noinspection RubyResolve
119
+ expect(@constrained_int_parameter).to be_valid_value(v)
120
+ end
121
+
122
+ [0, 4, 9, 43].each do |v|
123
+ # noinspection RubyResolve
124
+ expect(@constrained_int_parameter).not_to be_valid_value(v)
125
+ end
126
+
127
+ [1.0, 3.1415, 2.718281828459].each do |v|
128
+ # noinspection RubyResolve
129
+ expect(@constrained_float_parameter).to be_valid_value(v)
130
+ end
131
+
132
+ [nil, 0.5, -2.5].each do |v|
133
+ # noinspection RubyResolve
134
+ expect(@constrained_float_parameter).not_to be_valid_value(v)
135
+ end
136
+
137
+ end
138
+
139
+ end
@@ -0,0 +1,12 @@
1
+ # require 'codeclimate-test-reporter'
2
+ # ::CodeClimate::TestReporter.start
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+
7
+ require 'bundler/setup'
8
+ Bundler.setup
9
+
10
+ require 'rspec'
11
+ require 'libis-tools'
12
+
data/spec/test.xsd ADDED
@@ -0,0 +1,20 @@
1
+ <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
2
+ <xs:element name="patron">
3
+ <xs:complexType>
4
+ <xs:sequence>
5
+ <xs:element type="xs:string" name="name"/>
6
+ <xs:element name="barcode">
7
+ <xs:complexType>
8
+ <xs:simpleContent>
9
+ <xs:extension base="xs:int">
10
+ <xs:attribute type="xs:string" name="library"/>
11
+ </xs:extension>
12
+ </xs:simpleContent>
13
+ </xs:complexType>
14
+ </xs:element>
15
+ <xs:element type="xs:string" name="access_level"/>
16
+ <xs:element type="xs:string" name="email" maxOccurs="unbounded" minOccurs="0"/>
17
+ </xs:sequence>
18
+ </xs:complexType>
19
+ </xs:element>
20
+ </xs:schema>
@@ -0,0 +1,413 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/xml_document'
4
+ require 'libis/tools/extend/string'
5
+
6
+ require 'rspec/matchers'
7
+ require 'equivalent-xml'
8
+
9
+ describe 'XML Document' do
10
+
11
+ test_file = File.join(File.dirname(__FILE__), 'data', 'test.xml')
12
+ test_xsd = File.join(File.dirname(__FILE__), 'test.xsd')
13
+
14
+ def match_xml(doc1, doc2)
15
+ doc1 = ::Nokogiri::XML(doc1) if doc1.is_a?(String)
16
+ doc2 = ::Nokogiri::XML(doc2) if doc2.is_a?(String)
17
+ # noinspection RubyResolve
18
+ # expect(doc1).to be_equivalent_to(doc2).respecting_element_order
19
+ expect(doc1).to be_equivalent_to(doc2)
20
+ end
21
+
22
+ before :context do
23
+ @xml_template = <<-END.align_left
24
+ <?xml version="1.0" encoding="utf-8"?>
25
+ <patron>
26
+ <name>Harry Potter</name>
27
+ <barcode library="Hogwarts Library">1234567890</barcode>
28
+ <access_level>student</access_level>
29
+ <email>harry.potter@hogwarts.edu</email>
30
+ <email>hpotter@JKRowling.com</email>
31
+ </patron>
32
+ END
33
+ end
34
+
35
+ it 'should create new empty XML document' do
36
+
37
+ xml_doc = ::Libis::Tools::XmlDocument.new
38
+
39
+ expect(xml_doc.document).not_to be_nil
40
+ # noinspection RubyResolve
41
+ expect(xml_doc).not_to be_valid
42
+ # noinspection RubyResolve
43
+ expect(xml_doc).to be_invalid
44
+
45
+ match_xml xml_doc.document, <<-END.align_left
46
+ <?xml version="1.0" encoding="utf-8"?>
47
+ END
48
+
49
+ end
50
+
51
+ it 'should load test file' do
52
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
53
+ # noinspection RubyResolve
54
+ expect(xml_doc).to be_valid
55
+ match_xml xml_doc.document, @xml_template
56
+
57
+ end
58
+
59
+ it 'should parse XML from string' do
60
+ xml_doc = ::Libis::Tools::XmlDocument.parse(<<-END.align_left)
61
+ <patron>
62
+ <name>Harry Potter</name>
63
+ <barcode library="Hogwarts Library">1234567890</barcode>
64
+ <access_level>student</access_level>
65
+ <email>harry.potter@hogwarts.edu</email>
66
+ <email>hpotter@JKRowling.com</email>
67
+ </patron>
68
+ END
69
+
70
+ match_xml xml_doc.document, @xml_template
71
+ end
72
+
73
+ it 'should parse XML from Hash' do
74
+ xml_doc = ::Libis::Tools::XmlDocument.from_hash({patron: {
75
+ name: 'Harry Potter',
76
+ barcode: {
77
+ '@library' => 'Hogwarts Library',
78
+ content!: '1234567890',
79
+ },
80
+ access_level: 'student',
81
+ email: %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
82
+ }},
83
+ {:key_converter => :none}
84
+ )
85
+
86
+ match_xml xml_doc.document, @xml_template
87
+
88
+ end
89
+
90
+ it 'should validate document against schema' do
91
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
92
+
93
+ expect(xml_doc.validates_against? test_xsd).to be_truthy
94
+ # noinspection RubyResolve
95
+ expect(xml_doc.validate test_xsd).to be_empty
96
+
97
+ end
98
+
99
+ it 'should allow to add a processing instruction' do
100
+ xml_doc = ::Libis::Tools::XmlDocument.parse '<patron/>'
101
+ xml_doc.add_processing_instruction 'xml-stylesheet', 'type="text/xsl" href="style.xsl"'
102
+
103
+ match_xml xml_doc.document, <<-END.align_left
104
+ <?xml version="1.0" encoding="utf-8"?>
105
+ <?xml-stylesheet type="text/xsl" href="style.xsl"?>
106
+ <patron/>
107
+ END
108
+ end
109
+
110
+ it 'should get the root node of the document' do
111
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
112
+ root = xml_doc.root
113
+
114
+ expect(root.name).to eq 'patron'
115
+ match_xml root.document, <<-END.align_left
116
+ <patron>
117
+ <name>Harry Potter</name>
118
+ <barcode library="Hogwarts Library">1234567890</barcode>
119
+ <access_level>student</access_level>
120
+ <email>harry.potter@hogwarts.edu</email>
121
+ <email>hpotter@JKRowling.com</email>
122
+ </patron>
123
+ END
124
+
125
+ end
126
+
127
+ it 'should set the root node of the document' do
128
+ xml_doc = ::Libis::Tools::XmlDocument.new
129
+ patron = ::Nokogiri::XML::Node.new 'patron', xml_doc.document
130
+ xml_doc.root = patron
131
+
132
+ match_xml xml_doc.document, <<-END.align_left
133
+ <?xml version="1.0" encoding="utf-8"?>
134
+ <patron/>
135
+ END
136
+
137
+ end
138
+
139
+ it 'should enable Nokogiri Build syntax' do
140
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
141
+
142
+ xml_doc.build(xml_doc.root) do
143
+ # noinspection RubyResolve
144
+ books do
145
+ book title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23'
146
+ end
147
+ end
148
+
149
+ match_xml xml_doc.document, <<-END.align_left
150
+ <?xml version="1.0" encoding="utf-8"?>
151
+ <patron>
152
+ <name>Harry Potter</name>
153
+ <barcode library="Hogwarts Library">1234567890</barcode>
154
+ <access_level>student</access_level>
155
+ <email>harry.potter@hogwarts.edu</email>
156
+ <email>hpotter@JKRowling.com</email>
157
+ <books>
158
+ <book title="Quidditch Through the Ages" author="Kennilworthy Whisp" due_date="1992-4-23"/>
159
+ </books>
160
+ </patron>
161
+ END
162
+
163
+ end
164
+
165
+ it 'should enable Nokogiri Build syntax for new document' do
166
+ xml_doc = ::Libis::Tools::XmlDocument.build do
167
+ # noinspection RubyResolve
168
+ patron {
169
+ name 'Harry Potter'
170
+ barcode( '1234567890', library: 'Hogwarts Library')
171
+ access_level 'student'
172
+ email 'harry.potter@hogwarts.edu'
173
+ email 'hpotter@JKRowling.com'
174
+ # noinspection RubyResolve
175
+ books {
176
+ book title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23'
177
+ }
178
+ }
179
+ end
180
+
181
+ match_xml xml_doc.document, <<-END.align_left
182
+ <?xml version="1.0" encoding="utf-8"?>
183
+ <patron>
184
+ <name>Harry Potter</name>
185
+ <barcode library="Hogwarts Library">1234567890</barcode>
186
+ <access_level>student</access_level>
187
+ <email>harry.potter@hogwarts.edu</email>
188
+ <email>hpotter@JKRowling.com</email>
189
+ <books>
190
+ <book title="Quidditch Through the Ages" author="Kennilworthy Whisp" due_date="1992-4-23"/>
191
+ </books>
192
+ </patron>
193
+ END
194
+
195
+ end
196
+
197
+ it 'should add a new node to the document' do
198
+ xml_doc = ::Libis::Tools::XmlDocument.new
199
+
200
+ xml_doc.add_node :patron
201
+ xml_doc.add_node :name, 'Harry Potter'
202
+ books = xml_doc.add_node :books, nil, nil, namespaces: { jkr: 'http://JKRowling.com' , node_ns: 'jkr' }
203
+ xml_doc.add_node :book, nil, books,
204
+ title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23',
205
+ namespaces: {node_ns: 'jkr'}
206
+
207
+ match_xml xml_doc.document, <<-END.align_left
208
+ <?xml version="1.0" encoding="utf-8"?>
209
+ <patron>
210
+ <name>Harry Potter</name>
211
+ <jkr:books xmlns:jkr="http://JKRowling.com">
212
+ <jkr:book author="Kennilworthy Whisp" due_date="1992-4-23" title="Quidditch Through the Ages"/>
213
+ </jkr:books>
214
+ </patron>
215
+ END
216
+
217
+ expect(xml_doc.root.name).to eq 'patron'
218
+ expect(xml_doc.root.children.size).to be 2
219
+ expect(xml_doc.root.children[0].name).to eq 'name'
220
+ expect(xml_doc.root.children[0].content).to eq 'Harry Potter'
221
+ expect(xml_doc.root.children[1].name).to eq 'books'
222
+ expect(xml_doc.root.children[1].namespace.prefix).to eq 'jkr'
223
+ expect(xml_doc.root.children[1].namespace.href).to eq 'http://JKRowling.com'
224
+ expect(xml_doc.root.children[1].namespaces.size).to be 1
225
+ expect(xml_doc.root.children[1].namespaces['xmlns:jkr']).to eq 'http://JKRowling.com'
226
+ expect(xml_doc.root.children[1].children.size).to be 1
227
+ expect(xml_doc.root.children[1].children[0].name).to eq 'book'
228
+ expect(xml_doc.root.children[1].children[0].namespace.prefix).to eq 'jkr'
229
+ expect(xml_doc.root.children[1].children[0].namespace.href).to eq 'http://JKRowling.com'
230
+ expect(xml_doc.root.children[1].children[0]['title']).to eq 'Quidditch Through the Ages'
231
+ expect(xml_doc.root.children[1].children[0]['author']).to eq 'Kennilworthy Whisp'
232
+ expect(xml_doc.root.children[1].children[0]['due_date']).to eq '1992-4-23'
233
+
234
+ end
235
+
236
+ it 'should add attributes to a node' do
237
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
238
+
239
+ xml_doc.add_attributes xml_doc.root, id: '123456', status: 'active'
240
+
241
+ match_xml xml_doc.document, <<-END.align_left
242
+ <?xml version="1.0" encoding="utf-8"?>
243
+ <patron status="active" id="123456">
244
+ <name>Harry Potter</name>
245
+ <barcode library="Hogwarts Library">1234567890</barcode>
246
+ <access_level>student</access_level>
247
+ <email>harry.potter@hogwarts.edu</email>
248
+ <email>hpotter@JKRowling.com</email>
249
+ </patron>
250
+ END
251
+
252
+ end
253
+
254
+ it 'should add namespaces to a node' do
255
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
256
+
257
+ xml_doc.add_namespaces xml_doc.root, jkr: 'http://JKRowling.com', node_ns: 'jkr'
258
+ # noinspection RubyResolve
259
+ xml_doc.add_namespaces xml_doc.barcode, nil => 'http://hogwarts.edu'
260
+
261
+ match_xml xml_doc.document, <<-END.align_left
262
+ <?xml version="1.0" encoding="utf-8"?>
263
+ <jkr:patron xmlns:jkr="http://JKRowling.com">
264
+ <name>Harry Potter</name>
265
+ <barcode library="Hogwarts Library" xmlns="http://hogwarts.edu">1234567890</barcode>
266
+ <access_level>student</access_level>
267
+ <email>harry.potter@hogwarts.edu</email>
268
+ <email>hpotter@JKRowling.com</email>
269
+ </jkr:patron>
270
+ END
271
+
272
+ expect(xml_doc.document.root.namespace.prefix).to eq 'jkr'
273
+ expect(xml_doc.document.root.namespace.href).to eq 'http://JKRowling.com'
274
+ expect(xml_doc.document.root.elements[1].namespace.prefix).to be_nil
275
+ expect(xml_doc.document.root.elements[1].namespace.href).to eq 'http://hogwarts.edu'
276
+
277
+ end
278
+
279
+ it 'should search for nodes in the current document root' do
280
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
281
+
282
+ nodes = xml_doc.xpath('//email')
283
+ expect(nodes.size).to be 2
284
+ expect(nodes.map(&:content)).to eq %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
285
+
286
+ end
287
+
288
+ it 'should check if the XML document contains certain element(s)' do
289
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
290
+
291
+ expect(xml_doc.has_element? 'barcode[@library="Hogwarts Library"]').to be_truthy
292
+
293
+ end
294
+
295
+ it 'should return the content of the first element found' do
296
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
297
+
298
+ expect(xml_doc.value('//email')).to eq 'harry.potter@hogwarts.edu'
299
+
300
+ end
301
+
302
+ it 'should return the content of all elements found' do
303
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
304
+
305
+ expect(xml_doc.values('//email')).to eq %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
306
+
307
+ end
308
+
309
+ it 'should return the content of the first element in the set of nodes' do
310
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
311
+
312
+ expect(::Libis::Tools::XmlDocument.get_content(xml_doc.xpath('//email'))).to eq 'harry.potter@hogwarts.edu'
313
+ end
314
+
315
+ it 'should Find a node and set its content' do
316
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
317
+
318
+ xml_doc['//access_level'] = 'postgraduate'
319
+
320
+ match_xml xml_doc.document, <<-END.align_left
321
+ <?xml version="1.0" encoding="utf-8"?>
322
+ <patron>
323
+ <name>Harry Potter</name>
324
+ <barcode library="Hogwarts Library">1234567890</barcode>
325
+ <access_level>postgraduate</access_level>
326
+ <email>harry.potter@hogwarts.edu</email>
327
+ <email>hpotter@JKRowling.com</email>
328
+ </patron>
329
+ END
330
+
331
+ end
332
+
333
+ # noinspection RubyResolve
334
+ it 'should allow node access by method name' do
335
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
336
+
337
+ expect(xml_doc.email.content).to eq 'harry.potter@hogwarts.edu'
338
+ expect(xml_doc.barcode 'library').to eq 'Hogwarts Library'
339
+
340
+ xml_doc.access_level = 'postgraduate'
341
+ xml_doc.barcode 'library', 'Hogwarts Dumbledore Library'
342
+ # noinspection RubyResolve
343
+ xml_doc.dates do |dates|
344
+ dates.birth_date '1980-07-31'
345
+ dates.member_since '1991-09-01'
346
+ end
347
+
348
+ match_xml xml_doc.document, <<-END.align_left
349
+ <?xml version="1.0" encoding="utf-8"?>
350
+ <patron>
351
+ <name>Harry Potter</name>
352
+ <barcode library="Hogwarts Dumbledore Library">1234567890</barcode>
353
+ <access_level>postgraduate</access_level>
354
+ <email>harry.potter@hogwarts.edu</email>
355
+ <email>hpotter@JKRowling.com</email>
356
+ <dates>
357
+ <birth_date>1980-07-31</birth_date>
358
+ <member_since>1991-09-01</member_since>
359
+ </dates>
360
+ </patron>
361
+ END
362
+
363
+ end
364
+
365
+ it 'should work' do
366
+ xml_doc = ::Libis::Tools::XmlDocument.parse(<<-END.align_left)
367
+ <patron>
368
+ <name>Harry Potter</name>
369
+ <barcode library="Hogwarts Library">1234567890</barcode>
370
+ <access_level>student</access_level>
371
+ <email>harry.potter@hogwarts.edu</email>
372
+ <email>hpotter@JKRowling.com</email>
373
+ </patron>
374
+ END
375
+
376
+ match_xml xml_doc.document, @xml_template
377
+
378
+ xml_doc.save('/tmp/test.xml')
379
+
380
+ xml_doc = ::Libis::Tools::XmlDocument.open('/tmp/test.xml')
381
+
382
+ match_xml xml_doc.document, @xml_template
383
+
384
+ xml_doc = ::Libis::Tools::XmlDocument.build do
385
+ # noinspection RubyResolve
386
+ patron {
387
+ name 'Harry Potter'
388
+ barcode( '1234567890', library: 'Hogwarts Library')
389
+ access_level 'student'
390
+ email 'harry.potter@hogwarts.edu'
391
+ email 'hpotter@JKRowling.com'
392
+ }
393
+ end
394
+
395
+ match_xml xml_doc.document, @xml_template
396
+
397
+ xml_doc = ::Libis::Tools::XmlDocument.new
398
+ xml_doc.add_node :patron
399
+ xml_doc.name = 'Harry Potter'
400
+ # noinspection RubyResolve
401
+ xml_doc.barcode = '1234567890'
402
+ # noinspection RubyResolve
403
+ xml_doc.barcode :library, 'Hogwarts Library'
404
+ # noinspection RubyResolve
405
+ xml_doc.access_level = 'student'
406
+ xml_doc.email = 'harry.potter@hogwarts.edu'
407
+ xml_doc.add_node :email, 'hpotter@JKRowling.com'
408
+
409
+ match_xml xml_doc.document, @xml_template
410
+
411
+ end
412
+
413
+ end