From fdf4cec4b94134a2b5858b0476ae8400e3dcc389 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Tue, 21 May 2019 09:03:17 -0500 Subject: [PATCH] Start workflow after registration The dor-services-app no longer starts an initial workflow automatically --- app/models/hydrus/collection.rb | 4 +- app/models/hydrus/generic_object.rb | 3 +- app/services/item_service.rb | 10 ++- spec/models/hydrus/generic_object_spec.rb | 49 ++++++++------ spec/services/item_service_spec.rb | 79 ++++++++++++----------- 5 files changed, 84 insertions(+), 61 deletions(-) diff --git a/app/models/hydrus/collection.rb b/app/models/hydrus/collection.rb index 6ba871fe1..dd2d4957e 100644 --- a/app/models/hydrus/collection.rb +++ b/app/models/hydrus/collection.rb @@ -94,7 +94,9 @@ def self.create(user) # Create the object, with the correct model. apo = Hydrus::AdminPolicyObject.create(user) response = Hydrus::GenericObject.register_dor_object(user, 'collection', apo.pid) - coll = Hydrus::Collection.find(response[:pid]) + workflow_client.create_workflow_by_name(response[:pid], Dor::Config.hydrus.app_workflow) + + coll = Hydrus::Collection.find(response[:pid]) coll.remove_relationship :has_model, 'info:fedora/afmodel:Dor_Collection' coll.assert_content_model # Set the item_type, and add some Hydrus-specific info to identityMetadata. diff --git a/app/models/hydrus/generic_object.rb b/app/models/hydrus/generic_object.rb index 214525528..b42028ce2 100644 --- a/app/models/hydrus/generic_object.rb +++ b/app/models/hydrus/generic_object.rb @@ -306,8 +306,7 @@ def self.dor_registration_params(user_string, obj_typ, apo_pid) admin_policy: apo_pid, source_id: "Hydrus:#{obj_typ}-#{user_string}-#{HyTime.now_datetime_full}", label: 'Hydrus', - tag: [Settings.hydrus.project_tag], - initiate_workflow: [Dor::Config.hydrus.app_workflow], + tag: [Settings.hydrus.project_tag] } end diff --git a/app/services/item_service.rb b/app/services/item_service.rb index c9fd3c723..658c8436f 100644 --- a/app/services/item_service.rb +++ b/app/services/item_service.rb @@ -39,8 +39,10 @@ def validate! # Create the object, with the correct model. # @return [Hydrus::Item] def build_item(item_type) - dor_item = Hydrus::GenericObject.register_dor_object(user, 'item', collection.apo_pid) - Hydrus::Item.find(dor_item[:pid]).tap do |item| + registration_response = Hydrus::GenericObject.register_dor_object(user, 'item', collection.apo_pid) + workflow_client.create_workflow_by_name(registration_response[:pid], Dor::Config.hydrus.app_workflow) + + Hydrus::Item.find(registration_response[:pid]).tap do |item| item.remove_relationship :has_model, 'info:fedora/afmodel:Dor_Item' item.assert_content_model # Set the item_type, and add some Hydrus-specific info to identityMetadata. @@ -52,6 +54,10 @@ def build_item(item_type) end end + def workflow_client + @workflow_client ||= Dor::Workflow::Client.new(url: Settings.workflow.url) + end + # Add the Item to the Collection. def assign_to_collection item.collections << collection diff --git a/spec/models/hydrus/generic_object_spec.rb b/spec/models/hydrus/generic_object_spec.rb index f919861df..1c405fca6 100644 --- a/spec/models/hydrus/generic_object_spec.rb +++ b/spec/models/hydrus/generic_object_spec.rb @@ -59,27 +59,38 @@ expect(ri.url).to eq('') end - describe 'registration' do - it 'dor_registration_params() should return the expected hash' do - # Non-APO: hash should include initiate_workflow. - args = %w(whobar item somePID) - drp = Hydrus::GenericObject.dor_registration_params(*args) - expect(drp).to be_instance_of Hash - expect(drp[:admin_policy]).to eq(args.last) - expect(drp).to include(:initiate_workflow) - # APO: hash should not includes initiate_workflow. - args = %w(whobar adminPolicy somePID) - drp = Hydrus::GenericObject.dor_registration_params(*args) - expect(drp).to be_instance_of Hash - expect(drp).to include(:initiate_workflow) - end - - it 'should be able to exercise register_dor_object(), using stubbed call to Dor' do - args = %w(whobar item somePID) - drp = Hydrus::GenericObject.dor_registration_params(*args) - expect(Dor::Services::Client.objects).to receive(:register).with(params: hash_including(*drp.keys)) + describe '.dor_registration_params' do + subject(:dor_registration_params) do + Hydrus::GenericObject.dor_registration_params(*args) + end + + let(:args) { %w(whobar item somePID) } + + it 'returns the expected hash' do + expect(dor_registration_params).to be_instance_of Hash + expect(dor_registration_params[:admin_policy]).to eq(args.last) + end + end + + describe '.register_dor_object' do + subject(:register_dor_object) do Hydrus::GenericObject.register_dor_object(nil, nil, nil) end + + let(:dor_registration_params) do + Hydrus::GenericObject.dor_registration_params(*args) + end + let(:args) { %w(whobar item somePID) } + let(:objects_client) { instance_double(Dor::Services::Client::Objects, register: nil) } + + before do + allow(Dor::Services::Client).to receive(:objects).and_return(objects_client) + end + + it 'calls the dor-serivices-client to register' do + register_dor_object + expect(objects_client).to have_received(:register).with(params: hash_including(*dor_registration_params.keys)) + end end describe 'license and terms of use' do diff --git a/spec/services/item_service_spec.rb b/spec/services/item_service_spec.rb index 0d4f7ee54..131ff8f7f 100644 --- a/spec/services/item_service_spec.rb +++ b/spec/services/item_service_spec.rb @@ -11,47 +11,52 @@ end describe '.create' do - describe('terms of acceptance for a new item', integration: true) do - let(:item) { described_class.create(collection.pid, user) } - let(:collection) { Hydrus::Collection.find('druid:oo000oo0003') } - - context 'if the user has already accepted another item in this collection but it was more than 1 year ago' do - let(:user) { create :archivist1 } # this user accepted more than 1 year ago - it 'indicates that a new item in a collection requires terms acceptance' do - expect(collection.users_accepted_terms_of_deposit.keys.include?(user.sunetid)).to eq(true) - expect(item.requires_terms_acceptance(user, collection)).to eq(true) - expect(item.accepted_terms_of_deposit).to eq('false') - expect(item.terms_of_deposit_accepted?).to eq(false) - end + subject(:item) { described_class.create(collection.pid, user) } + + let(:workflow_client) { instance_double(Dor::Workflow::Client, create_workflow_by_name: nil) } + let(:collection) { Hydrus::Collection.find('druid:oo000oo0003') } + + before do + allow(Dor::Workflow::Client).to receive(:new).and_return(workflow_client) + end + + context 'if the user has already accepted another item in this collection but it was more than 1 year ago' do + let(:user) { create :archivist1 } # this user accepted more than 1 year ago + it 'indicates that a new item in a collection requires terms acceptance' do + expect(collection.users_accepted_terms_of_deposit.keys.include?(user.sunetid)).to eq(true) + expect(item.requires_terms_acceptance(user, collection)).to eq(true) + expect(item.accepted_terms_of_deposit).to eq('false') + expect(item.terms_of_deposit_accepted?).to eq(false) + expect(workflow_client).to have_received(:create_workflow_by_name).with(String, 'hydrusAssemblyWF') + end + end + + context 'if the user has already accepted another item in this collection less than 1 year ago' do + let(:user) { create :archivist3 } + before do + dt = HyTime.now - 1.month # force this user to have accepted 1 month ago. + collection.hydrusProperties.accept_terms_of_deposit(user, HyTime.datetime(dt)) + collection.save! end - context 'if the user has already accepted another item in this collection less than 1 year ago' do - let(:user) { create :archivist3 } - before do - dt = HyTime.now - 1.month # force this user to have accepted 1 month ago. - collection.hydrusProperties.accept_terms_of_deposit(user, HyTime.datetime(dt)) - collection.save! - end - - it 'indicates that a new item in a collection does not require terms acceptance' do - expect(collection.users_accepted_terms_of_deposit.keys.include?(user.sunetid)).to eq(true) - expect(item.requires_terms_acceptance(user, collection)).to eq(false) - expect(item.accepted_terms_of_deposit).to eq('true') - expect(item.terms_of_deposit_accepted?).to eq(true) - end + it 'indicates that a new item in a collection does not require terms acceptance' do + expect(collection.users_accepted_terms_of_deposit.keys.include?(user.sunetid)).to eq(true) + expect(item.requires_terms_acceptance(user, collection)).to eq(false) + expect(item.accepted_terms_of_deposit).to eq('true') + expect(item.terms_of_deposit_accepted?).to eq(true) + end + end + + context 'when the user has not already accepted another item in this collection' do + let(:user) { create :archivist5 } + before do + allow(Hydrus::Authorizable).to receive(:can_create_items_in).and_return(true) end - context 'when the user has not already accepted another item in this collection' do - let(:user) { create :archivist5 } - before do - allow(Hydrus::Authorizable).to receive(:can_create_items_in).and_return(true) - end - - it 'indicates that a new item in a collection requires terms acceptance' do - expect(item.requires_terms_acceptance(user, collection)).to eq(true) - expect(item.accepted_terms_of_deposit).to eq('false') - expect(item.terms_of_deposit_accepted?).to eq(false) - end + it 'indicates that a new item in a collection requires terms acceptance' do + expect(item.requires_terms_acceptance(user, collection)).to eq(true) + expect(item.accepted_terms_of_deposit).to eq('false') + expect(item.terms_of_deposit_accepted?).to eq(false) end end end