Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluent assertions #3835

Open
grofcik opened this issue Feb 2, 2024 · 0 comments
Open

Fluent assertions #3835

grofcik opened this issue Feb 2, 2024 · 0 comments

Comments

@grofcik
Copy link

grofcik commented Feb 2, 2024

Is your feature request related to a problem? Please describe.

Assertions are not readable. Instead of:

@Test 
 void followHappyPath(RuntimeService runtimeService, TaskService taskService, HistoryService historyService) { 
     ProcessInstance helloWorldProcess = runtimeService.createProcessInstanceBuilder().processDefinitionKey("P001-helloWorld").start(); 
  
     assertThat(helloWorldProcess).isNotNull(); 
     List<String> path = new ArrayList<>(List.of("theStart", "theStart-theSayHelloUserTask", "theSayHelloUserTask")); 
     assertThat(runtimeService.createActivityInstanceQuery() 
             .processInstanceId(helloWorldProcess.getId()).orderByActivityInstanceStartTime().asc().list()) 
             .as("The hello world process has to go directly through theStart -> theSayHelloUserTask") 
             .extracting(ActivityInstance::getActivityId) 
             .containsExactlyInAnyOrderElementsOf(path); 
     assertThat(runtimeService.createVariableInstanceQuery().processInstanceId(helloWorldProcess.getId()).list()) 
             .extracting("name", "value") 
             .containsExactly(Tuple.tuple("initiator", null)); 
  
     Task userTask = taskService.createTaskQuery().processInstanceId(helloWorldProcess.getId()).singleResult(); 
  
     assertThat(userTask).extracting(Task::getName).isEqualTo("Say hello world"); 
     assertThat(userTask).extracting(Task::getTaskDefinitionKey).isEqualTo("theSayHelloUserTask"); 
  
     taskService.complete(userTask.getId(), Map.of("greeting", "Hello World!")); 
  
     assertThat(runtimeService.createActivityInstanceQuery().processInstanceId(helloWorldProcess.getId()).singleResult()) 
             .as("The hello world process is finished, no instance is present in the runtime.") 
             .isNull(); 
     HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(helloWorldProcess.getId()).singleResult(); 
     assertThat(historicProcessInstance) 
             .as("The hello world process must be present in the history") 
             .isNotNull(); 
     assertThat(historicProcessInstance.getEndTime()) 
             .as("The hello world process must be finished") 
             .isNotNull(); 
     assertThat(historyService.createHistoricVariableInstanceQuery().processInstanceId(helloWorldProcess.getId()).list()) 
             .as("All variables must be present in the history") 
             .extracting("name", "value") 
             .containsExactlyInAnyOrder(Tuple.tuple("initiator", null), Tuple.tuple("greeting", "Hello World!")); 
     path.addAll(List.of("theSayHelloUserTask-theEnd", "theEnd")); 
     assertThat(historyService.createHistoricActivityInstanceQuery().processInstanceId(helloWorldProcess.getId()).orderByHistoricActivityInstanceStartTime().asc().list()) 
             .as("The hello world process must pass through all activities") 
             .extracting(HistoricActivityInstance::getActivityId) 
             .containsExactlyInAnyOrderElementsOf(path); 
 } 

Use:

 @Test 
 void followHappyPathWithFluentAssertions(RuntimeService runtimeService, TaskService taskService) { 
     ProcessInstance helloWorldProcess = runtimeService.createProcessInstanceBuilder().processDefinitionKey("P001-helloWorld").start(); 
  
     List<String> path = new ArrayList<>(List.of("theStart", "theStart-theSayHelloUserTask", "theSayHelloUserTask")); 
     assertThat(helloWorldProcess) 
             .isRunning() 
             .hasVariableWithValue("initiator", null) 
             .activities() 
             .extracting(ActivityInstance::getActivityId) 
             .containsExactlyInAnyOrderElementsOf(path); 
     assertThat(helloWorldProcess) 
             .userTasks() 
             .extracting("name", "taskDefinitionKey") 
             .containsExactly(Tuple.tuple("Say hello world", "theSayHelloUserTask")); 
  
     Task userTask = taskService.createTaskQuery().processInstanceId(helloWorldProcess.getId()).singleResult(); 
     taskService.complete(userTask.getId(), Map.of("greeting", "Hello World!")); 
  
     assertThat(helloWorldProcess) 
             .as("The hello world process is finished, no instance is present in the runtime.") 
             .doesNotExist() 
             .inHistory() 
             .as("The hello world process must be present in the history and finished") 
             .isFinished() 
             .variables() 
             .as("All variables must be present in the history") 
             .extracting("name", "value") 
             .containsExactlyInAnyOrder(Tuple.tuple("initiator", null), Tuple.tuple("greeting", "Hello World!")); 
     path.addAll(List.of("theSayHelloUserTask-theEnd", "theEnd")); 
     assertThat(helloWorldProcess).inHistory() 
             .as("The hello world process must pass through all activities") 
             .activities() 
             .extracting(HistoricActivityInstance::getActivityId) 
             .containsExactlyInAnyOrderElementsOf(path); 
 } 

The code can be even more readable in groovy.

https://github.com/crystal-processes/crp-flowable-springboot-sample/blob/main/docs/01_sample/02-helloWorld.md#partly_sunny-what-to-do-next---tests

martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 2, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 20, 2024
martin-grofcik added a commit to martin-grofcik/flowable-engine that referenced this issue Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant