1package OpenXPKI::Test::QA::Role::Workflows; 2use Moose::Role; 3 4=head1 NAME 5 6OpenXPKI::Test::QA::Role::Workflows - Moose role that extends L<OpenXPKI::Test> 7for workflow execution. 8 9=cut 10 11# CPAN modules 12use Test::More; 13use Test::Exception; 14 15# Project modules 16use OpenXPKI::Server::Context; 17use OpenXPKI::Serialization::Simple; 18use OpenXPKI::Test::QA::Role::Workflows::CertParams; 19use OpenXPKI::Test::QA::Role::Workflows::Instance; 20 21 22requires 'also_init'; 23 24 25# 26# Attributes 27# 28has 'managed_workflows' => ( 29 is => "rw", 30 isa => "HashRef[Int]", 31 init_arg => undef, 32 default => sub { {} }, 33); 34 35# 36# Modified methods 37# 38before 'init_server' => sub { 39 my $self = shift; 40 # prepend to existing array in case a user supplied "also_init" needs our modules 41 unshift @{ $self->also_init }, 'workflow_factory'; 42}; 43 44after 'set_user' => sub { 45 my $self = shift; 46 # reset condition cache so e.g. user role checks are re-evaluated 47 for my $id (keys %{ $self->managed_workflows }) { 48 OpenXPKI::Server::Context::CTX('workflow_factory') 49 ->get_factory 50 ->fetch_workflow($self->managed_workflows->{$id}, $id) 51 ->_get_workflow_state 52 ->clear_condition_cache; 53 } 54}; 55 56=head1 METHODS 57 58This role adds the following methods to L<OpenXPKI::Test>: 59 60=cut 61 62=head2 create_workflow 63 64Creates and returns a workflow instance wrapped in 65L<OpenXPKI::Test::QA::Role::Workflows::Instance> which can be used for 66further tests. 67 68B<Positional Parameters> 69 70=over 71 72=item * C<$type> I<Str> - workflow name / type 73 74=item * C<$params> I<HashRef> - workflow parameters. Optional. 75 76=back 77 78=cut 79sub create_workflow { 80 my ($self, $type, $params) = @_; 81 82 my $wf = OpenXPKI::Test::QA::Role::Workflows::Instance->new( 83 oxitest => $self, 84 type => $type, 85 $params ? (params => $params) : (), 86 ); 87 $self->managed_workflows->{$wf->id} = $wf->type; 88 return $wf; 89} 90 91=head2 fetch_workflow 92 93Fetches an existing workflow instance wrapped in 94L<OpenXPKI::Test::QA::Role::Workflows::Instance> which can be used for 95further tests. 96 97B<Positional Parameters> 98 99=over 100 101=item * C<$id> I<Str> - workflow ID 102 103=back 104 105=cut 106sub fetch_workflow { 107 my ($self, $id) = @_; 108 109 my $wf = OpenXPKI::Test::QA::Role::Workflows::Instance->new( 110 oxitest => $self, 111 id => $id, 112 ); 113 $self->managed_workflows->{$wf->id} = $wf->type; 114 return $wf; 115} 116 117sub wait_for_proc_state { 118 my ($self, $wfid, $state_regex) = @_; 119 120 my $result; 121 my $count = 0; 122 while ($count++ < 20) { 123 $result = OpenXPKI::Server::Context::CTX('api2')->search_workflow_instances(id => [ $wfid ]); 124 # no workflow found? 125 if (not scalar @$result or $result->[0]->{'workflow_id'} != $wfid) { 126 die("Workflow with ID $wfid not found"); 127 } 128 # wait if paused (i.e. resuming in progress) or still running (the remaining steps) 129 if (not $result->[0]->{'workflow_proc_state'} =~ $state_regex) { 130 sleep 1; 131 next; 132 } 133 # expected proc state reached 134 return 1; 135 } 136 die("Timeout reached while waiting for workflow to reach state $state_regex"); 137} 138 1391; 140