-
Notifications
You must be signed in to change notification settings - Fork 2
/
goobi_spec.rb
161 lines (143 loc) · 6.26 KB
/
goobi_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Dor::Goobi do
subject(:goobi) { Dor::Goobi.new(item) }
let(:pid) { 'druid:aa123bb4567' }
let(:item) { Dor::Item.new(pid: pid) }
# rubocop:disable RSpec/SubjectStub
before do
# all of the methods we are stubbing out below are tested elsewhere,
# this just lets us test the methods in goobi.rb without doing a lot of setup
allow(Dor::Item).to receive(:find).and_return(item)
allow(item).to receive(:source_id).and_return('some_source_id')
allow(item).to receive(:label).and_return('Object Title & A Special character')
allow(item).to receive(:content_type_tag).and_return('book')
allow(goobi).to receive(:project_name).and_return('Project Name')
allow(goobi).to receive(:object_type).and_return('item')
allow(goobi).to receive(:ckey).and_return('ckey_12345')
allow(goobi).to receive(:goobi_workflow_name).and_return('goobi_workflow')
allow(goobi).to receive(:barcode).and_return('barcode_12345')
allow(goobi).to receive(:collection_id).and_return('druid:oo000oo0001')
allow(goobi).to receive(:collection_name).and_return('collection name')
end
# rubocop:enable RSpec/SubjectStub
describe '#title_or_label' do
subject(:xml) { Nokogiri::XML(goobi.xml_request).xpath('//title').first.content }
context 'when MODS title is present' do
before do
item.datastreams['descMetadata'].ng_xml = build_desc_metadata_1
end
it 'returns title text' do
expect(xml).to eq 'Constituent label & A Special character'
end
end
context 'when MODS title is absent or empty' do
it 'returns object label' do
expect(xml).to eq 'Object Title & A Special character'
end
end
end
it 'creates the correct xml request without ocr tag present' do
allow(item).to receive(:tags).and_return(['DPG : Workflow : book_workflow & stuff', 'Process : Content Type : Book', 'LAB : MAPS'])
expect(goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow & stuff"/><tag name="Process" value="Content Type : Book"/><tag name="LAB" value="MAPS"/>')
expect(goobi.xml_request).to be_equivalent_to <<-END
<stanfordCreationRequest>
<objectId>#{pid}</objectId>
<objectType>item</objectType>
<sourceID>some_source_id</sourceID>
<title>Object Title & A Special character</title>
<contentType>book</contentType>
<project>Project Name</project>
<catkey>ckey_12345</catkey>
<barcode>barcode_12345</barcode>
<collectionId>druid:oo000oo0001</collectionId>
<collectionName>collection name</collectionName>
<sdrWorkflow>#{Settings.goobi.dpg_workflow_name}</sdrWorkflow>
<goobiWorkflow>goobi_workflow</goobiWorkflow>
<ocr>false</ocr>
<tags>
<tag name="DPG" value="Workflow : book_workflow & stuff"/>
<tag name="Process" value="Content Type : Book"/>
<tag name="LAB" value="MAPS"/>
</tags>
</stanfordCreationRequest>
END
end
it 'creates the correct xml request with ocr tag present' do
allow(item).to receive(:tags).and_return(['DPG : Workflow : book_workflow', 'DPG : OCR : TRUE'])
expect(goobi.goobi_xml_tags).to eq('<tag name="DPG" value="Workflow : book_workflow"/><tag name="DPG" value="OCR : TRUE"/>')
expect(goobi.xml_request).to be_equivalent_to <<-END
<stanfordCreationRequest>
<objectId>#{pid}</objectId>
<objectType>item</objectType>
<sourceID>some_source_id</sourceID>
<title>Object Title & A Special character</title>
<contentType>book</contentType>
<project>Project Name</project>
<catkey>ckey_12345</catkey>
<barcode>barcode_12345</barcode>
<collectionId>druid:oo000oo0001</collectionId>
<collectionName>collection name</collectionName>
<sdrWorkflow>#{Settings.goobi.dpg_workflow_name}</sdrWorkflow>
<goobiWorkflow>goobi_workflow</goobiWorkflow>
<ocr>true</ocr>
<tags>
<tag name="DPG" value="Workflow : book_workflow"/>
<tag name="DPG" value="OCR : TRUE"/>
</tags>
</stanfordCreationRequest>
END
end
it 'creates the correct xml request when MODs title exists with a special character' do
item.datastreams['descMetadata'].ng_xml = build_desc_metadata_1
expect(goobi.xml_request).to be_equivalent_to <<-END
<stanfordCreationRequest>
<objectId>#{pid}</objectId>
<objectType>item</objectType>
<sourceID>some_source_id</sourceID>
<title>Constituent label & A Special character</title>
<contentType>book</contentType>
<project>Project Name</project>
<catkey>ckey_12345</catkey>
<barcode>barcode_12345</barcode>
<collectionId>druid:oo000oo0001</collectionId>
<collectionName>collection name</collectionName>
<sdrWorkflow>#{Settings.goobi.dpg_workflow_name}</sdrWorkflow>
<goobiWorkflow>goobi_workflow</goobiWorkflow>
<ocr>false</ocr>
<tags></tags>
</stanfordCreationRequest>
END
end
it 'creates the correct xml request with no tags present' do
allow(item).to receive(:tags).and_return([])
expect(goobi.goobi_xml_tags).to eq('')
expect(goobi.xml_request).to include('<tags></tags>')
end
describe '#register' do
subject(:response) { goobi.register }
before do
allow(goobi).to receive(:xml_request)
end
context 'with a successful response' do
before do
stub_request(:post, Settings.goobi.url)
.to_return(body: '<somexml/>', headers: { 'Content-Type' => 'text/xml' }, status: 201)
end
it 'makes a call to the goobi server with the appropriate xml params' do
expect(response.status).to eq 201
end
end
context 'with a 409 response' do
before do
allow(Faraday).to receive(:post).and_call_original
stub_request(:post, Settings.goobi.url)
.to_return(body: '<somexml/>', headers: { 'Content-Type' => 'text/xml' }, status: 409)
end
it 'makes a call to the goobi server with the appropriate xml params' do
expect(response.status).to eq 409
expect(Faraday).to have_received(:post).once # Don't retry request errors
end
end
end
end