-
Notifications
You must be signed in to change notification settings - Fork 2
/
preserved_object_handler_check_exist_spec.rb
752 lines (695 loc) · 34.3 KB
/
preserved_object_handler_check_exist_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
require 'rails_helper'
require 'services/shared_examples_preserved_object_handler'
RSpec.describe PreservedObjectHandler do
let(:druid) { 'ab123cd4567' }
let(:incoming_version) { 6 }
let(:incoming_size) { 9876 }
let!(:default_prez_policy) { PreservationPolicy.default_policy }
let(:po) { PreservedObject.find_by(druid: druid) }
let(:ep) { Endpoint.find_by(storage_location: 'spec/fixtures/storage_root01/moab_storage_trunk') }
let(:pc) { PreservedCopy.find_by(preserved_object: po, endpoint: ep) }
let(:exp_msg_prefix) { "PreservedObjectHandler(#{druid}, #{incoming_version}, #{incoming_size}, #{ep.endpoint_name})" }
let(:db_update_failed_prefix_regex_escaped) { Regexp.escape("#{exp_msg_prefix} db update failed") }
let(:po_handler) { described_class.new(druid, incoming_version, incoming_size, ep) }
describe '#check_existence' do
it_behaves_like 'attributes validated', :check_existence
context 'druid in db' do
before do
po = PreservedObject.create!(druid: druid, current_version: 2, preservation_policy: default_prez_policy)
PreservedCopy.create!(
preserved_object: po,
version: po.current_version,
size: 1,
endpoint: ep,
status: PreservedCopy::OK_STATUS # NOTE: we are pretending we checked for moab validation errs
)
end
context "incoming and db versions match" do
let(:po_handler) { described_class.new(druid, 2, 1, ep) }
let(:exp_msg_prefix) { "PreservedObjectHandler(#{druid}, 2, 1, #{ep.endpoint_name})" }
let(:version_matches_po_msg) { "#{exp_msg_prefix} incoming version (2) matches PreservedObject db version" }
let(:version_matches_pc_msg) { "#{exp_msg_prefix} incoming version (2) matches PreservedCopy db version" }
let(:updated_pc_db_msg) { "#{exp_msg_prefix} PreservedCopy db object updated" }
context 'PreservedCopy' do
context 'changed' do
it 'last_version_audit' do
orig = Time.current
pc.last_version_audit = orig
pc.save!
po_handler.check_existence
expect(pc.reload.last_version_audit).to be > orig
end
it 'updated_at' do
orig = pc.updated_at
po_handler.check_existence
expect(pc.reload.updated_at).to be > orig
end
end
context 'unchanged' do
it 'status' do
orig = pc.status
po_handler.check_existence
expect(pc.reload.status).to eq orig
end
it 'version' do
orig = pc.version
po_handler.check_existence
expect(pc.reload.version).to eq orig
end
it 'size' do
orig = pc.size
po_handler.check_existence
expect(pc.reload.size).to eq orig
end
it 'last_moab_validation' do
orig = pc.last_moab_validation
po_handler.check_existence
expect(pc.reload.last_moab_validation).to eq orig
end
end
end
it 'PreservedObject is not updated' do
orig = po.updated_at
po_handler.check_existence
expect(po.reload.updated_at).to eq orig
end
it "logs at info level" do
expect(Rails.logger).to receive(:log).with(Logger::INFO, version_matches_po_msg)
expect(Rails.logger).to receive(:log).with(Logger::INFO, version_matches_pc_msg)
expect(Rails.logger).to receive(:log).with(Logger::INFO, updated_pc_db_msg)
po_handler.check_existence
end
it 'does not validate moab' do
expect(po_handler).not_to receive(:moab_validation_errors)
po_handler.check_existence
end
context 'returns' do
let!(:results) { po_handler.check_existence }
# results = [result1, result2]
# result1 = {response_code: msg}
# result2 = {response_code: msg}
it '3 results' do
expect(results).to be_an_instance_of Array
expect(results.size).to eq 3
end
it 'VERSION_MATCHES results' do
code = PreservedObjectHandlerResults::VERSION_MATCHES
expect(results).to include(a_hash_including(code => version_matches_pc_msg))
expect(results).to include(a_hash_including(code => version_matches_po_msg))
end
it 'UPDATED_DB_OBJECT PreservedCopy result' do
code = PreservedObjectHandlerResults::UPDATED_DB_OBJECT
expect(results).to include(a_hash_including(code => updated_pc_db_msg))
end
end
end
context "incoming version > db version" do
let(:incoming_version) { 6 }
let(:incoming_size) { 9876 }
let(:version_gt_pc_msg) { "#{exp_msg_prefix} incoming version (#{incoming_version}) greater than PreservedCopy db version" }
let(:version_gt_po_msg) { "#{exp_msg_prefix} incoming version (#{incoming_version}) greater than PreservedObject db version" }
let(:updated_pc_db_msg) { "#{exp_msg_prefix} PreservedCopy db object updated" }
let(:updated_po_db_msg) { "#{exp_msg_prefix} PreservedObject db object updated" }
let(:updated_status_msg_regex) { Regexp.new(Regexp.escape("#{exp_msg_prefix} PreservedCopy status changed from")) }
it 'calls Stanford::StorageObjectValidator.validation_errors for moab' do
mock_sov = instance_double(Stanford::StorageObjectValidator)
expect(mock_sov).to receive(:validation_errors).and_return([])
allow(Stanford::StorageObjectValidator).to receive(:new).and_return(mock_sov)
po_handler.check_existence
end
context 'when moab is valid' do
context 'PreservedCopy' do
context 'changed' do
before do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
allow(po_handler).to receive(:ran_moab_validation?).and_return(true)
end
it 'version to incoming_version' do
orig = pc.version
po_handler.check_existence
expect(pc.reload.version).to be > orig
expect(pc.reload.version).to eq incoming_version
end
it 'size if supplied' do
orig = pc.size
po_handler.check_existence
expect(pc.reload.size).not_to eq orig
expect(pc.reload.size).to eq incoming_size
end
it 'last_moab_validation' do
orig = Time.current
pc.last_moab_validation = orig
pc.save!
po_handler.check_existence
expect(pc.reload.last_moab_validation).to be > orig
end
it 'last_version_audit' do
orig = Time.current
pc.last_version_audit = orig
pc.save!
po_handler.check_existence
expect(pc.reload.last_version_audit).to be > orig
end
it 'updated_at' do
orig = pc.updated_at
po_handler.check_existence
expect(pc.reload.updated_at).to be > orig
end
it 'status becomes "ok" if it was invalid_moab (b/c after validation)' do
pc.status = PreservedCopy::INVALID_MOAB_STATUS
pc.save!
po_handler.check_existence
expect(pc.reload.status).to eq PreservedCopy::OK_STATUS
end
end
context 'unchanged' do
before do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
end
it 'status if former status was ok' do
pc.status = PreservedCopy::OK_STATUS
pc.save!
po_handler.check_existence
expect(pc.reload.status).to eq PreservedCopy::OK_STATUS
end
it 'size if incoming size is nil' do
orig = pc.size
po_handler = described_class.new(druid, incoming_version, nil, ep)
po_handler.check_existence
expect(pc.reload.size).to eq orig
end
end
end
context 'PreservedObject' do
context 'changed' do
before do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
end
it 'current_version' do
orig = po.current_version
po_handler.check_existence
expect(po.reload.current_version).to be > orig
expect(po.reload.current_version).to eq incoming_version
end
it 'updated_at' do
orig = pc.updated_at
po_handler.check_existence
expect(pc.reload.updated_at).to be > orig
end
end
end
it "logs at info level" do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
expect(Rails.logger).to receive(:log).with(Logger::INFO, version_gt_po_msg)
expect(Rails.logger).to receive(:log).with(Logger::INFO, version_gt_pc_msg)
expect(Rails.logger).to receive(:log).with(Logger::INFO, updated_po_db_msg)
expect(Rails.logger).to receive(:log).with(Logger::INFO, updated_pc_db_msg)
expect(Rails.logger).not_to receive(:log).with(Logger::INFO, updated_status_msg_regex)
po_handler.check_existence
end
context 'returns' do
let(:results) { po_handler.check_existence }
before do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
end
# results = [result1, result2]
# result1 = {response_code: msg}
# result2 = {response_code: msg}
it '4 results' do
expect(results).to be_an_instance_of Array
expect(results.size).to eq 4
end
it 'ARG_VERSION_GREATER_THAN_DB_OBJECT results' do
code = PreservedObjectHandlerResults::ARG_VERSION_GREATER_THAN_DB_OBJECT
expect(results).to include(a_hash_including(code => version_gt_pc_msg))
expect(results).to include(a_hash_including(code => version_gt_po_msg))
end
it "UPDATED_DB_OBJECT results" do
code = PreservedObjectHandlerResults::UPDATED_DB_OBJECT
expect(results).to include(a_hash_including(code => updated_pc_db_msg))
expect(results).to include(a_hash_including(code => updated_po_db_msg))
end
it 'what results/logging desired for incoming version > catalog' do
skip 'we need clarification of requirements on results/logging in this case'
end
end
end
context 'when moab is invalid' do
let(:invalid_druid) { 'xx000xx0000' }
let(:invalid_storage_dir) { 'spec/fixtures/bad_root01/bad_moab_storage_trunk' }
let(:invalid_ep) { Endpoint.find_by(storage_location: invalid_storage_dir) }
let(:invalid_po_handler) { described_class.new(invalid_druid, incoming_version, incoming_size, invalid_ep) }
let(:invalid_po) { PreservedObject.find_by(druid: invalid_druid) }
let(:invalid_pc) { PreservedCopy.find_by(preserved_object: invalid_po) }
let(:exp_msg_prefix) do
"PreservedObjectHandler(#{invalid_druid}, #{incoming_version}, #{incoming_size}, #{invalid_ep.endpoint_name})"
end
before do
# add storage root with the invalid moab to the Endpoints table
Endpoint.find_or_create_by!(endpoint_name: 'bad_fixture_dir') do |endpoint|
endpoint.endpoint_type = Endpoint.default_storage_root_endpoint_type
endpoint.endpoint_node = Settings.endpoints.storage_root_defaults.endpoint_node
endpoint.storage_location = invalid_storage_dir
endpoint.recovery_cost = Settings.endpoints.storage_root_defaults.recovery_cost
end
# these need to be in before loop so it happens before each context below
invalid_po = PreservedObject.create!(
druid: invalid_druid,
current_version: 2,
preservation_policy: default_prez_policy
)
t = Time.current
PreservedCopy.create!(
preserved_object: invalid_po,
version: invalid_po.current_version,
size: 1,
endpoint: invalid_ep,
status: PreservedCopy::OK_STATUS, # NOTE: we are pretending we checked for moab validation errs
last_version_audit: t,
last_moab_validation: t
)
end
context 'PreservedCopy' do
context 'changed' do
it 'last_version_audit' do
orig = invalid_pc.last_version_audit
invalid_po_handler.check_existence
expect(invalid_pc.reload.last_version_audit).to be > orig
end
it 'last_moab_validation' do
orig = invalid_pc.last_moab_validation
invalid_po_handler.check_existence
expect(invalid_pc.reload.last_moab_validation).to be > orig
end
it 'updated_at' do
orig = invalid_pc.updated_at
invalid_po_handler.check_existence
expect(invalid_pc.reload.updated_at).to be > orig
end
it 'ensures status becomes invalid_moab from ok' do
invalid_pc.status = PreservedCopy::OK_STATUS
invalid_pc.save!
invalid_po_handler.check_existence
expect(invalid_pc.reload.status).to eq PreservedCopy::INVALID_MOAB_STATUS
end
it 'ensures status becomes invalid_moab from expected_vers_not_found_on_storage' do
invalid_pc.status = PreservedCopy::EXPECTED_VERS_NOT_FOUND_ON_STORAGE_STATUS
invalid_pc.save!
invalid_po_handler.check_existence
expect(invalid_pc.reload.status).to eq PreservedCopy::INVALID_MOAB_STATUS
end
end
context 'unchanged' do
it 'version' do
orig = invalid_pc.version
invalid_po_handler.check_existence
expect(invalid_pc.reload.version).to eq orig
end
it 'size' do
orig = invalid_pc.size
invalid_po_handler.check_existence
expect(invalid_pc.reload.size).to eq orig
end
end
end
it 'PreservedObject is not updated' do
orig_timestamp = invalid_po.updated_at
invalid_po_handler.check_existence
expect(invalid_po.reload.updated_at).to eq orig_timestamp
end
it 'logs at error level' do
allow(Rails.logger).to receive(:log)
errors = Regexp.escape("#{exp_msg_prefix} Invalid moab, validation errors:")
expect(Rails.logger).to receive(:log).with(Logger::ERROR, a_string_matching(errors))
invalid_po_handler.check_existence
end
context 'returns' do
let!(:results) { invalid_po_handler.check_existence }
# results = [result1, result2]
# result1 = {response_code: msg}
# result2 = {response_code: msg}
it '5 results' do
expect(results).to be_an_instance_of Array
expect(results.size).to eq 5
end
it 'ARG_VERSION_GREATER_THAN_DB_OBJECT results' do
code = PreservedObjectHandlerResults::ARG_VERSION_GREATER_THAN_DB_OBJECT
expect(results).to include(a_hash_including(code => version_gt_pc_msg))
expect(results).to include(a_hash_including(code => version_gt_po_msg))
end
it "UPDATED_DB_OBJECT results" do
code = PreservedObjectHandlerResults::UPDATED_DB_OBJECT
expect(results).to include(a_hash_including(code => updated_pc_db_msg))
expect(results).to include(hash_not_including(code => updated_po_db_msg))
end
it 'INVALID_MOAB result' do
expect(results).to include(a_hash_including(PreservedObjectHandlerResults::INVALID_MOAB))
end
it 'what results/logging desired for incoming version > catalog and invalid moab' do
skip 'we need clarification of requirements on results/logging in this case'
end
end
end
end
context 'incoming version < db version' do
let(:druid) { 'bp628nk4868' }
let(:ep) { Endpoint.find_by(storage_location: 'spec/fixtures/storage_root02/moab_storage_trunk') }
it_behaves_like 'unexpected version with validation', :check_existence, 1, PreservedCopy::EXPECTED_VERS_NOT_FOUND_ON_STORAGE_STATUS
end
context 'PreservedCopy already has a status other than OK_STATUS' do
it_behaves_like 'PreservedCopy already has a status other than OK_STATUS, and incoming_version == pc.version', :check_existence
it_behaves_like 'PreservedCopy already has a status other than OK_STATUS, and incoming_version < pc.version', :check_existence
context 'incoming_version > db version' do
let(:incoming_version) { pc.version + 1 }
it 'had OK_STATUS, version increased, should still have OK_STATUS' do
pc.status = PreservedCopy::OK_STATUS
pc.save!
allow(po_handler).to receive(:moab_validation_errors).and_return([])
po_handler.check_existence
expect(pc.reload.status).to eq PreservedCopy::OK_STATUS
end
it 'had INVALID_MOAB_STATUS, was remediated, should now have OK_STATUS' do
pc.status = PreservedCopy::INVALID_MOAB_STATUS
pc.save!
allow(po_handler).to receive(:moab_validation_errors).and_return([])
po_handler.check_existence
expect(pc.reload.status).to eq PreservedCopy::OK_STATUS
end
it 'had EXPECTED_VERS_NOT_FOUND_ON_STORAGE_STATUS, seems to have an acceptable version now' do
pc.status = PreservedCopy::EXPECTED_VERS_NOT_FOUND_ON_STORAGE_STATUS
pc.save!
allow(po_handler).to receive(:moab_validation_errors).and_return([])
po_handler.check_existence
expect(pc.reload.status).to eq PreservedCopy::OK_STATUS
end
end
end
context 'PreservedCopy version does NOT match PreservedObject current_version (online Moab)' do
before do
po.current_version = 8
po.save!
end
it_behaves_like 'PreservedObject current_version does not match online PC version', :check_existence, 3, 2, 8
end
context 'db update error' do
context 'ActiveRecordError' do
let(:result_code) { PreservedObjectHandlerResults::DB_UPDATE_FAILED }
let(:incoming_version) { 2 }
let(:results) do
allow(PreservedObject).to receive(:find_by!).with(druid: druid).and_return(po)
allow(PreservedCopy).to receive(:find_by!).with(preserved_object: po, endpoint: ep).and_return(pc)
allow(pc).to receive(:save!).and_raise(ActiveRecord::ActiveRecordError, 'foo')
po_handler.check_existence
end
context 'transaction is rolled back' do
it 'PreservedCopy is not updated' do
orig = pc.updated_at
results
expect(pc.reload.updated_at).to eq orig
end
it 'PreservedObject is not updated' do
orig = po.updated_at
results
expect(po.reload.updated_at).to eq orig
end
end
context 'DB_UPDATE_FAILED error' do
it 'prefix' do
expect(results).to include(a_hash_including(result_code => a_string_matching(db_update_failed_prefix_regex_escaped)))
end
it 'specific exception raised' do
expect(results).to include(a_hash_including(result_code => a_string_matching('ActiveRecord::ActiveRecordError')))
end
it "exception's message" do
expect(results).to include(a_hash_including(result_code => a_string_matching('foo')))
end
end
end
end
it 'calls PreservedCopy.touch (but not PreservedObject.touch) if the existing record is NOT altered' do
po_handler = described_class.new(druid, 1, 1, ep)
po = instance_double(PreservedObject)
pc = instance_double(PreservedCopy)
allow(PreservedObject).to receive(:find_by!).with(druid: druid).and_return(po)
allow(po).to receive(:current_version).and_return(1)
allow(po).to receive(:touch)
allow(PreservedCopy).to receive(:find_by!).with(preserved_object: po, endpoint: ep).and_return(pc)
allow(pc).to receive(:matches_po_current_version?).and_return(true)
allow(pc).to receive(:status).and_return(PreservedCopy::OK_STATUS)
allow(pc).to receive(:version).and_return(1)
allow(pc).to receive(:update_audit_timestamps)
allow(pc).to receive(:changed?).and_return(false)
allow(pc).to receive(:touch)
po_handler.check_existence
expect(po).not_to have_received(:touch)
expect(pc).to have_received(:touch)
end
it 'logs a debug message' do
msg = "check_existence #{druid} called"
allow(Rails.logger).to receive(:debug)
allow(po_handler).to receive(:moab_validation_errors).and_return([])
po_handler.check_existence
expect(Rails.logger).to have_received(:debug).with(msg)
end
end
context 'object not in db' do
let(:exp_po_not_exist_msg) { "#{exp_msg_prefix} PreservedObject db object does not exist" }
let(:exp_obj_created_msg) { "#{exp_msg_prefix} added object to db as it did not exist" }
context 'presume validity and test other common behavior' do
before do
allow(po_handler).to receive(:moab_validation_errors).and_return([])
end
# FIXME: if requirements change to a single message for "object does not exist" and "created object"
# then this will no longer be correct?
# NOTE: this pertains to PreservedObject
it_behaves_like 'druid not in catalog', :check_existence
# FIXME: if requirements change to a single message for "object does not exist" and "created object"
# then this will no longer be correct?
it_behaves_like 'PreservedCopy does not exist', :check_existence
end
context 'adds to catalog after validation' do
let(:valid_druid) { 'bp628nk4868' }
let(:storage_dir) { 'spec/fixtures/storage_root02/moab_storage_trunk' }
let(:ep) { Endpoint.find_by(storage_location: storage_dir) }
let(:incoming_version) { 2 }
let(:po_handler) { described_class.new(valid_druid, incoming_version, incoming_size, ep) }
it 'calls Stanford::StorageObjectValidator.validation_errors for moab' do
mock_sov = instance_double(Stanford::StorageObjectValidator)
expect(mock_sov).to receive(:validation_errors).and_return([])
allow(Stanford::StorageObjectValidator).to receive(:new).and_return(mock_sov)
po_handler.check_existence
end
context 'moab is valid' do
let(:exp_msg_prefix) { "PreservedObjectHandler(#{valid_druid}, #{incoming_version}, #{incoming_size}, #{ep.endpoint_name})" }
it 'PreservedObject created' do
po_args = {
druid: valid_druid,
current_version: incoming_version,
preservation_policy_id: PreservationPolicy.default_policy_id
}
expect(PreservedObject).to receive(:create!).with(po_args).and_call_original
po_handler.check_existence
end
it 'PreservedCopy created' do
pc_args = {
preserved_object: an_instance_of(PreservedObject), # TODO: ensure we got the preserved object we expected
version: incoming_version,
size: incoming_size,
endpoint: ep,
status: PreservedCopy::OK_STATUS, # NOTE: ensuring this particular status
last_moab_validation: an_instance_of(ActiveSupport::TimeWithZone),
last_version_audit: an_instance_of(ActiveSupport::TimeWithZone)
}
expect(PreservedCopy).to receive(:create!).with(pc_args).and_call_original
po_handler.check_existence
end
context 'logging' do
it 'not sure what logging we REALLY want - maybe a single WARN?' do
skip 'need to get requirements on what exactly we want in logs'
end
it 'object does not exist error' do
allow(Rails.logger).to receive(:log)
expect(Rails.logger).to receive(:log).with(Logger::ERROR, exp_po_not_exist_msg)
po_handler.check_existence
end
it 'created db object message' do
allow(Rails.logger).to receive(:log)
expect(Rails.logger).to receive(:log).with(Logger::INFO, exp_obj_created_msg)
po_handler.check_existence
end
end
context 'returns' do
let!(:results) { po_handler.check_existence }
# results = [result1, result2]
# result1 = {response_code: msg}
# result2 = {response_code: msg}
it '2 results' do
expect(results).to be_an_instance_of Array
expect(results.size).to eq 2
end
it 'not sure what results we REALLY want' do
skip 'need to get requirements on what exactly we want in results'
end
it 'OBJECT_DOES_NOT_EXIST results' do
code = PreservedObjectHandlerResults::OBJECT_DOES_NOT_EXIST
expect(results).to include(a_hash_including(code => exp_po_not_exist_msg))
end
it 'CREATED_NEW_OBJECT result' do
code = PreservedObjectHandlerResults::CREATED_NEW_OBJECT
expect(results).to include(a_hash_including(code => exp_obj_created_msg))
end
end
context 'db update error' do
context 'ActiveRecordError' do
let(:result_code) { PreservedObjectHandlerResults::DB_UPDATE_FAILED }
let(:results) do
allow(Rails.logger).to receive(:log)
# FIXME: couldn't figure out how to put next line into its own test
expect(Rails.logger).to receive(:log).with(Logger::ERROR, /#{db_update_failed_prefix_regex_escaped}/)
po = instance_double("PreservedObject")
allow(PreservedObject).to receive(:create!).with(hash_including(druid: valid_druid)).and_return(po)
allow(PreservedCopy).to receive(:create!).and_raise(ActiveRecord::ActiveRecordError, 'foo')
po_handler.check_existence
end
context 'transaction is rolled back' do
it 'PreservedCopy does not exist' do
expect(PreservedCopy.find_by(endpoint: ep)).to be_nil
end
it 'PreservedObject does not exist' do
expect(PreservedObject.find_by(druid: valid_druid)).to be_nil
end
end
context 'DB_UPDATE_FAILED error' do
it 'prefix' do
expect(results).to include(a_hash_including(result_code => a_string_matching(db_update_failed_prefix_regex_escaped)))
end
it 'specific exception raised' do
expect(results).to include(a_hash_including(result_code => a_string_matching('ActiveRecord::ActiveRecordError')))
end
it "exception's message" do
expect(results).to include(a_hash_including(result_code => a_string_matching('foo')))
end
end
end
end
end
context 'moab is invalid' do
let(:storage_dir) { 'spec/fixtures/bad_root01/bad_moab_storage_trunk' }
let(:ep) { Endpoint.find_by(storage_location: storage_dir) }
let(:invalid_druid) { 'xx000xx0000' }
let(:exp_msg_prefix) { "PreservedObjectHandler(#{invalid_druid}, #{incoming_version}, #{incoming_size}, #{ep.endpoint_name})" }
let(:exp_moab_errs_msg) { "#{exp_msg_prefix} Invalid moab, validation errors: [\"Missing directory: [\\\"data\\\", \\\"manifests\\\"] Version: v0001\"]" }
let(:po_handler) { described_class.new(invalid_druid, incoming_version, incoming_size, ep) }
before do
# add storage root with the invalid moab to the Endpoints table
Endpoint.find_or_create_by!(endpoint_name: 'bad_fixture_dir') do |endpoint|
endpoint.endpoint_type = Endpoint.default_storage_root_endpoint_type
endpoint.endpoint_node = Settings.endpoints.storage_root_defaults.endpoint_node
endpoint.storage_location = storage_dir
endpoint.recovery_cost = Settings.endpoints.storage_root_defaults.recovery_cost
end
end
it 'creates PreservedObject; PreservedCopy with "invalid_moab" status' do
po_args = {
druid: invalid_druid,
current_version: incoming_version,
preservation_policy_id: PreservationPolicy.default_policy_id
}
pc_args = {
preserved_object: an_instance_of(PreservedObject), # TODO: ensure we got the preserved object we expected
version: incoming_version,
size: incoming_size,
endpoint: ep,
status: PreservedCopy::INVALID_MOAB_STATUS, # NOTE ensuring this particular status
last_moab_validation: an_instance_of(ActiveSupport::TimeWithZone),
last_version_audit: an_instance_of(ActiveSupport::TimeWithZone)
}
expect(PreservedObject).to receive(:create!).with(po_args).and_call_original
expect(PreservedCopy).to receive(:create!).with(pc_args).and_call_original
po_handler = described_class.new(invalid_druid, incoming_version, incoming_size, ep)
po_handler.check_existence
end
context 'logging' do
it "moab validation errors" do
allow(Rails.logger).to receive(:log)
expect(Rails.logger).to receive(:log).with(Logger::ERROR, exp_moab_errs_msg)
po_handler.check_existence
end
it 'not sure what logging we REALLY want - maybe a single WARN?' do
skip 'need to get requirements on what exactly we want in logs'
end
it 'object does not exist error' do
allow(Rails.logger).to receive(:log)
expect(Rails.logger).to receive(:log).with(Logger::ERROR, exp_po_not_exist_msg)
po_handler.check_existence
end
it 'created db object message' do
allow(Rails.logger).to receive(:log)
expect(Rails.logger).to receive(:log).with(Logger::INFO, exp_obj_created_msg)
po_handler.check_existence
end
end
context 'returns' do
let!(:results) { po_handler.check_existence }
# results = [result1, result2]
# result1 = {response_code: msg}
# result2 = {response_code: msg}
it '3 results' do
expect(results).to be_an_instance_of Array
expect(results.size).to eq 3
end
it 'not sure what results we REALLY want' do
skip 'need to get requirements on what exactly we want in results'
end
it 'INVALID_MOAB result' do
code = PreservedObjectHandlerResults::INVALID_MOAB
expect(results).to include(a_hash_including(code => exp_moab_errs_msg))
end
it 'OBJECT_DOES_NOT_EXIST results' do
code = PreservedObjectHandlerResults::OBJECT_DOES_NOT_EXIST
expect(results).to include(a_hash_including(code => exp_po_not_exist_msg))
end
it 'CREATED_NEW_OBJECT result' do
code = PreservedObjectHandlerResults::CREATED_NEW_OBJECT
expect(results).to include(a_hash_including(code => exp_obj_created_msg))
end
end
context 'db update error' do
context 'ActiveRecordError' do
let(:result_code) { PreservedObjectHandlerResults::DB_UPDATE_FAILED }
let(:results) do
allow(Rails.logger).to receive(:log)
# FIXME: couldn't figure out how to put next line into its own test
expect(Rails.logger).to receive(:log).with(Logger::ERROR, /#{db_update_failed_prefix_regex_escaped}/)
po = instance_double("PreservedObject")
allow(PreservedObject).to receive(:create!).with(hash_including(druid: invalid_druid)).and_return(po)
allow(PreservedCopy).to receive(:create!).and_raise(ActiveRecord::ActiveRecordError, 'foo')
po_handler = described_class.new(invalid_druid, incoming_version, incoming_size, ep)
po_handler.check_existence
end
context 'transaction is rolled back' do
it 'PreservedCopy does not exist' do
expect(PreservedCopy.find_by(endpoint: ep)).to be_nil
end
it 'PreservedObject does not exist' do
expect(PreservedObject.find_by(druid: invalid_druid)).to be_nil
end
end
context 'DB_UPDATE_FAILED error' do
it 'prefix' do
expect(results).to include(a_hash_including(result_code => a_string_matching(db_update_failed_prefix_regex_escaped)))
end
it 'specific exception raised' do
expect(results).to include(a_hash_including(result_code => a_string_matching('ActiveRecord::ActiveRecordError')))
end
it "exception's message" do
expect(results).to include(a_hash_including(result_code => a_string_matching('foo')))
end
end
end
end
end
end
end
end
end