1
1
package com .jongsoft .finance .bpmn ;
2
2
3
3
import com .jongsoft .finance .ResultPage ;
4
+ import com .jongsoft .finance .bpmn .process .ProcessExtension ;
5
+ import com .jongsoft .finance .bpmn .process .RuntimeContext ;
6
+ import com .jongsoft .finance .domain .account .Account ;
7
+ import com .jongsoft .finance .domain .transaction .Transaction ;
4
8
import com .jongsoft .finance .domain .user .Budget ;
5
- import com .jongsoft .finance .factory .FilterFactory ;
6
- import com .jongsoft .finance .providers .BudgetProvider ;
7
9
import com .jongsoft .finance .providers .SettingProvider ;
8
- import com .jongsoft .finance .providers .TransactionProvider ;
9
10
import com .jongsoft .lang .Collections ;
10
- import com .jongsoft .lang .Control ;
11
11
import jakarta .inject .Inject ;
12
- import org .camunda . bpm . engine . ProcessEngine ;
13
- import org .junit . jupiter . api . BeforeEach ;
12
+ import org .assertj . core . api . Assertions ;
13
+ import org .camunda . bpm . engine . variable . Variables ;
14
14
import org .junit .jupiter .api .DisplayName ;
15
15
import org .junit .jupiter .api .Test ;
16
16
import org .mockito .Mockito ;
17
- import org .mockito .invocation .InvocationOnMock ;
18
17
19
- import static org .assertj .core .api .Assertions .assertThat ;
20
-
21
- public class BudgetAnalysisIT extends ProcessTestSetup {
22
-
23
- @ Inject
24
- private BudgetProvider budgetProvider ;
25
-
26
- @ Inject
27
- private ProcessEngine processEngine ;
28
-
29
- @ Inject
30
- private FilterFactory filterFactory ;
31
-
32
- @ Inject
33
- private TransactionProvider transactionProvider ;
18
+ @ ProcessExtension
19
+ @ DisplayName ("Budget analysis feature" )
20
+ public class BudgetAnalysisIT {
34
21
35
22
@ Inject
36
23
private SettingProvider applicationSettings ;
37
24
38
- private TransactionProvider .FilterCommand filterCommand ;
39
-
40
- @ BeforeEach
41
- void setup () {
42
- Mockito .reset (budgetProvider , filterFactory , transactionProvider , applicationSettings );
43
-
44
- Mockito .when (applicationSettings .getBudgetAnalysisMonths ()).thenReturn (3 );
45
-
46
- Mockito .when (filterFactory .transaction ())
47
- .thenReturn (filterCommand = Mockito .mock (TransactionProvider .FilterCommand .class , InvocationOnMock ::getMock ));
48
-
49
- Mockito .when (budgetProvider .lookup (2019 , 1 )).thenReturn (Control .Option (
50
- Budget .builder ()
25
+ @ Test
26
+ @ DisplayName ("Budget analysis without a recorded deviation" )
27
+ void budgetWithoutDeviation (RuntimeContext context ) {
28
+ context
29
+ .withBudget (2019 , 1 , Budget .builder ()
51
30
.expenses (Collections .List (
52
31
Budget .Expense .builder ()
53
32
.id (1L )
@@ -56,14 +35,8 @@ void setup() {
56
35
.name ("Groceries" )
57
36
.build ()))
58
37
.id (1L )
59
- .build ()
60
- ));
61
- }
62
-
63
- @ Test
64
- @ DisplayName ("Budget analysis without a recorded deviation" )
65
- void budgetWithoutDeviation () {
66
- Mockito .when (transactionProvider .lookup (filterCommand ))
38
+ .build ())
39
+ .withTransactionPages ()
67
40
.thenReturn (ResultPage .of (
68
41
buildTransaction (50.2 , "Groceries" , "My Account" , "To Account" ),
69
42
buildTransaction (20.2 , "Groceries" , "My Account" , "To Account" ),
@@ -79,41 +52,35 @@ void budgetWithoutDeviation() {
79
52
buildTransaction (12 , "Groceries" , "My Account" , "To Account" ),
80
53
buildTransaction (10 , "Groceries" , "My Account" , "To Account" )
81
54
));
82
- Mockito .when (applicationSettings .getMaximumBudgetDeviation ()).thenReturn (0.50 );
83
-
84
- var response = processEngine .getRuntimeService ()
85
- .createProcessInstanceByKey ("budget_analysis" )
86
- .setVariable ("id" , 1L )
87
- .setVariable ("scheduled" , "2019-01-01" )
88
- .execute ();
89
55
90
- waitForSuspended (processEngine , response .getProcessInstanceId ());
91
-
92
- // Validate the process completed successfully
93
- var variables = processEngine .getHistoryService ()
94
- .createHistoricVariableInstanceQuery ()
95
- .processInstanceId (response .getProcessInstanceId ())
96
- .variableName ("deviates" )
97
- .list ();
98
-
99
- assertThat (variables )
100
- .hasSize (2 )
101
- .anySatisfy (variable -> assertThat (variable .getValue ()).isEqualTo (false ));
56
+ Mockito .when (applicationSettings .getMaximumBudgetDeviation ()).thenReturn (0.50 );
57
+ Mockito .when (applicationSettings .getBudgetAnalysisMonths ()).thenReturn (3 );
102
58
103
- // Validate no tasks have been created
104
- var tasks = processEngine .getTaskService ()
105
- .createTaskQuery ()
106
- .processInstanceId (response .getProcessInstanceId ())
107
- .active ()
108
- .list ();
59
+ context .execute ("budget_analysis" , Variables .createVariables ()
60
+ .putValue ("id" , 1L )
61
+ .putValue ("scheduled" , "2019-01-01" ))
62
+ .verifyCompleted ()
63
+ .<Boolean >yankVariables ("deviates" , value ->
64
+ value .hasSize (2 )
65
+ .allMatch (a -> !a ));
109
66
110
- assertThat (tasks ).isEmpty ();
111
67
}
112
68
113
69
@ Test
114
70
@ DisplayName ("Budget analysis with a recorded deviation" )
115
- void budgetWithDeviation () {
116
- Mockito .when (transactionProvider .lookup (filterCommand ))
71
+ void budgetWithDeviation (RuntimeContext context ) {
72
+ context
73
+ .withBudget (2019 , 1 , Budget .builder ()
74
+ .expenses (Collections .List (
75
+ Budget .Expense .builder ()
76
+ .id (1L )
77
+ .lowerBound (75 )
78
+ .upperBound (100 )
79
+ .name ("Groceries" )
80
+ .build ()))
81
+ .id (1L )
82
+ .build ())
83
+ .withTransactionPages ()
117
84
.thenReturn (ResultPage .of (
118
85
buildTransaction (50.2 , "Groceries" , "My Account" , "To Account" ),
119
86
buildTransaction (20.2 , "Groceries" , "My Account" , "To Account" ),
@@ -129,47 +96,40 @@ void budgetWithDeviation() {
129
96
buildTransaction (12 , "Groceries" , "My Account" , "To Account" ),
130
97
buildTransaction (13 , "Groceries" , "My Account" , "To Account" )
131
98
));
132
- Mockito .when (applicationSettings .getMaximumBudgetDeviation ()).thenReturn (0.05 );
133
-
134
- var response = processEngine .getRuntimeService ()
135
- .createProcessInstanceByKey ("budget_analysis" )
136
- .setVariable ("id" , 1L )
137
- .setVariable ("scheduled" , "2019-01-01" )
138
- .execute ();
139
99
140
- waitForSuspended (processEngine , response .getProcessInstanceId ());
141
-
142
- // Validate the process completed successfully
143
- var variables = processEngine .getHistoryService ()
144
- .createHistoricVariableInstanceQuery ()
145
- .processInstanceId (response .getProcessInstanceId ())
146
- .variableName ("deviation" )
147
- .list ();
148
-
149
- assertThat (variables )
150
- .hasSize (2 )
151
- .anySatisfy (variable -> assertThat (variable .getValue ()).isEqualTo (-14.23 ));
100
+ Mockito .when (applicationSettings .getBudgetAnalysisMonths ()).thenReturn (3 );
101
+ Mockito .when (applicationSettings .getMaximumBudgetDeviation ()).thenReturn (0.05 );
152
102
153
- // Validate the tasks have been created according to the number of months
103
+ var execution = context .execute ("budget_analysis" , Variables .createVariables ()
104
+ .putValue ("id" , 1L )
105
+ .putValue ("scheduled" , "2019-01-01" ))
106
+ .<Boolean >yankVariables ("deviates" , value -> value .hasSize (2 ).allMatch (a -> a ))
107
+ .<Number >yankVariables ("deviation" , value -> value .allMatch (v -> v .equals (-14.23 )));
154
108
155
- var tasks = processEngine .getTaskService ()
156
- .createTaskQuery ()
157
- .processInstanceId (response .getProcessInstanceId ())
158
- .active ()
159
- .taskDefinitionKey ("handle_deviation" )
160
- .list ();
109
+ execution .task ("handle_deviation" )
110
+ .verifyVariable ("needed_correction" , a -> Assertions .assertThat (a ).isEqualTo (-14.23 ));
111
+ }
161
112
162
- assertThat (tasks )
163
- .hasSize (1 )
164
- .anySatisfy (task -> {
165
- assertThat (task .getName ()).isEqualTo ("Handle deviation" );
166
- assertThat (processEngine .getRuntimeService ()
167
- .createVariableInstanceQuery ()
168
- .processInstanceIdIn (response .getProcessInstanceId ())
169
- .variableName ("needed_correction" )
170
- .list ())
171
- .hasSize (1 )
172
- .anySatisfy (variable -> assertThat (variable .getValue ()).isEqualTo (-14.23 ));
173
- });
113
+ public static Transaction buildTransaction (double amount , String description , String to , String from ) {
114
+ return Transaction .builder ()
115
+ .description (description )
116
+ .transactions (Collections .List (
117
+ Transaction .Part .builder ()
118
+ .amount (amount )
119
+ .account (Account .builder ()
120
+ .id (1L )
121
+ .name (to )
122
+ .build ())
123
+ .build (),
124
+ Transaction .Part .builder ()
125
+ .amount (-amount )
126
+ .account (Account .builder ()
127
+ .id (2L )
128
+ .name (from )
129
+ .build ())
130
+ .build ()
131
+ ))
132
+ .build ();
174
133
}
134
+
175
135
}
0 commit comments