Hello!
I am implementing an approval approcess using Temporal. There are two Workflows, one for creating a request (CreateResourceAccessWFImpl) which waits unit a signal is sent and other for sending Signal(UpdateResourceAccessWFImpl) to the CreateRequest workflow (the first workflow) to approve or reject.
public class UpdateResourceAccessWFImpl implements UpdateResourceAccessWF {
public void updateResource(String workflowId) {
...
ExternalWorkflowStub callRespondWorkflow = Workflow.newUntypedExternalWorkflowStub(workflowId);
callRespondWorkflow.signal("updateStatus", approvalState, updatedDate);
...
}
}
How do I write a JUnit for this?
The Junit for the createRequest looks something like this -
@RegisterExtension
public TestWorkflowExtension testWorkflowRule =
TestWorkflowExtension.newBuilder()
.registerWorkflowImplementationTypes(CreateResourceAccessWFImpl.class)
.setDoNotStart(true)
.build();
@Test
void createResourceAccessRequestAndSendApproveSignal(
TestWorkflowEnvironment testEnv,
Worker worker,
CreateResourceAccessWF createResourceAccessWF) {
...
ResourceAccessActivity resourceAccessRepository = mock(ResourceAccessActivity.class);
worker.registerActivitiesImplementations(resourceAccessRepository);
testEnv.start();
String workflowId =
WorkflowClient.start(createResourceAccessWF::createResourceRequest, tenantResourceV1)
.getWorkflowId();
testEnv.sleep(Duration.ofDays(1));
verify(resourceAccessRepository).save(any());
createResourceAccessWF.updateStatus(
ResourceStatusEnum.APPROVED.toString(), ZonedDateTime.now());
testEnv.sleep(Duration.ofSeconds(5));
verify(resourceAccessRepository).update(any());
List<PendingActivityInfo> pendingActivitiesList =
testEnv
.getWorkflowClient()
.getWorkflowServiceStubs()
.blockingStub()
.describeWorkflowExecution(
DescribeWorkflowExecutionRequest.newBuilder()
.setExecution(WorkflowExecution.newBuilder().setWorkflowId(workflowId).build())
.setNamespace(testEnv.getNamespace())
.build())
.getPendingActivitiesList();
assertEquals(0, pendingActivitiesList.size());
}