Skip to content

Commit

Permalink
version_service: can't close version if active assemblyWF
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Jun 11, 2020
1 parent 090a2bd commit a51f2fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
25 changes: 17 additions & 8 deletions app/services/version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def open(opts = {})
vmd_ds.sync_then_increment_version sdr_version
vmd_ds.save unless work.new_record?

WorkflowClientFactory.build.create_workflow_by_name(work.pid, 'versioningWF', version: work.current_version)
workflow_client.create_workflow_by_name(work.pid, 'versioningWF', version: work.current_version)

return if (opts.keys & open_options_requiring_work_save).empty?

Expand Down Expand Up @@ -81,14 +81,15 @@ def close(opts = {})

raise Dor::Exception, "latest version in versionMetadata for #{work.pid} requires tag and description before it can be closed" unless work.versionMetadata.current_version_closeable?
raise Dor::Exception, "Trying to close version on #{work.pid} which is not opened for versioning" unless open_for_versioning?
raise Dor::Exception, "Trying to close version on #{work.pid} which has active assemblyWF" if active_assembly_wf?
raise Dor::Exception, "accessionWF already created for versioned object #{work.pid}" if accessioning?

# Default to creating accessionWF when calling close_version
create_accession_wf = opts.fetch(:start_accession, true)
WorkflowClientFactory.build.close_version(repo: 'dor',
druid: work.pid,
version: work.current_version,
create_accession_wf: create_accession_wf)
workflow_client.close_version(repo: 'dor',
druid: work.pid,
version: work.current_version,
create_accession_wf: create_accession_wf)
work.events.add_event('close', opts[:user_name], "Version #{work.current_version} closed") if opts[:user_name]
work.save!
event_factory.create(druid: work.pid, event_type: 'version_close', data: { who: opts[:user_name], version: work.current_version })
Expand All @@ -105,7 +106,7 @@ def try_to_get_current_version(assume_accessioned = false)
# The accessioned milestone is the last step of the accessionWF.
# During local development, we need a way to open a new version even if the object has not been accessioned.
raise(Dor::Exception, 'Object net yet accessioned') unless
assume_accessioned || WorkflowClientFactory.build.lifecycle('dor', work.pid, 'accessioned')
assume_accessioned || workflow_client.lifecycle('dor', work.pid, 'accessioned')
# Raised when the current version has any incomplete wf steps and there is a versionWF.
# The open milestone is part of the versioningWF.
raise Dor::VersionAlreadyOpenError, 'Object already opened for versioning' if open_for_versioning?
Expand All @@ -122,15 +123,15 @@ def try_to_get_current_version(assume_accessioned = false)
# Checks if current version has any incomplete wf steps and there is a versionWF
# @return [Boolean] true if object is open for versioning
def open_for_versioning?
return true if WorkflowClientFactory.build.active_lifecycle('dor', work.pid, 'opened', version: work.current_version)
return true if workflow_client.active_lifecycle('dor', work.pid, 'opened', version: work.current_version)

false
end

# Checks if the current version has any incomplete wf steps and there is an accessionWF.
# @return [Boolean] true if object is currently being accessioned
def accessioning?
return true if WorkflowClientFactory.build.active_lifecycle('dor', work.pid, 'submitted', version: work.current_version)
return true if workflow_client.active_lifecycle('dor', work.pid, 'submitted', version: work.current_version)

false
end
Expand All @@ -142,4 +143,12 @@ def accessioning?
def open_options_requiring_work_save
[:opening_user_name, :significance, :description]
end

def active_assembly_wf?
return true if workflow_client.workflow_status(druid: work.pid, version: work.current_version, workflow: 'assemblyWF', process: 'accessioning-initiate') == 'waiting'
end

def workflow_client
@workflow_client ||= WorkflowClientFactory.build
end
end
36 changes: 35 additions & 1 deletion spec/services/version_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
allow(vmd_ds).to receive(:pid).and_return('druid:ab123cd4567')
# stub out calls for open_for_versioning?
allow(workflow_client).to receive(:active_lifecycle).and_return(true, false)
allow(workflow_client).to receive(:workflow_status).with(hash_including(workflow: 'assemblyWF')).and_return('completed')

allow(vmd_ds).to receive(:save)
allow(ev_ds).to receive(:add_event)
Expand Down Expand Up @@ -292,6 +293,7 @@
allow(vmd_ds).to receive(:pid).and_return('druid:ab123cd4567')
# stub out calls for open_for_versioning?
allow(workflow_client).to receive(:active_lifecycle).and_return(true, false)
allow(workflow_client).to receive(:workflow_status).with(hash_including(workflow: 'assemblyWF')).and_return('completed')
allow(vmd_ds).to receive(:save)
allow(ev_ds).to receive(:add_event)
allow(obj).to receive(:save!)
Expand All @@ -312,14 +314,46 @@
end
end

context 'when the object already has an active instance of accesssionWF' do
context 'when the object has an active accesssionWF' do
before do
allow(workflow_client).to receive(:workflow_status).with(hash_including(workflow: 'assemblyWF')).and_return('completed')
end

it 'raises an exception' do
expect(workflow_client).to receive(:active_lifecycle).with('dor', druid, 'opened', version: '1').and_return(Time.new)
expect(workflow_client).to receive(:active_lifecycle).with('dor', druid, 'submitted', version: '1').and_return(true)
expect { close }.to raise_error(Dor::Exception, 'accessionWF already created for versioned object druid:ab12cd3456')
end
end

context 'when the object has an active assemblyWF' do
before do
allow(workflow_client).to receive(:workflow_status).with(hash_including(workflow: 'assemblyWF')).and_return('waiting')
end

it 'raises an exception' do
expect(workflow_client).to receive(:active_lifecycle).with('dor', druid, 'opened', version: '1').and_return(Time.new)
expect { close }.to raise_error(Dor::Exception, 'Trying to close version on druid:ab12cd3456 which has active assemblyWF')
expect(workflow_client).to have_received(:workflow_status).with(hash_including(workflow: 'assemblyWF'))
end
end

context 'when the object has no assemblyWF' do
before do
allow(workflow_client).to receive(:workflow_status).with(hash_including(workflow: 'assemblyWF')).and_return(nil)
allow(workflow_client).to receive(:close_version)
allow(obj).to receive(:save!)
end

it 'creates the accessioningWF' do
expect(workflow_client).to receive(:active_lifecycle).with('dor', druid, 'opened', version: '1').and_return(Time.new)
expect(workflow_client).to receive(:active_lifecycle).with('dor', druid, 'submitted', version: '1').and_return(false)
close
expect(workflow_client).to have_received(:workflow_status).with(hash_including(workflow: 'assemblyWF'))
expect(workflow_client).to have_received(:close_version).with(repo: 'dor', druid: druid, version: '1', create_accession_wf: true)
end
end

context 'when the latest version does not have a tag and a description' do
it 'raises an exception' do
vmd_ds.increment_version
Expand Down

0 comments on commit a51f2fc

Please sign in to comment.