17
17
18
18
import static io .serverlessworkflow .api .WorkflowReader .readWorkflowFromClasspath ;
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .catchThrowableOfType ;
20
21
21
22
import java .io .IOException ;
22
23
import java .time .Instant ;
@@ -42,10 +43,9 @@ static void init() {
42
43
43
44
@ ParameterizedTest
44
45
@ MethodSource ("provideParameters" )
45
- void testWorkflowExecution (String fileName , Object input , Consumer <Object > assertions )
46
+ void testWorkflowExecution (String fileName , Consumer <WorkflowDefinition > assertions )
46
47
throws IOException {
47
- assertions .accept (
48
- appl .workflowDefinition (readWorkflowFromClasspath (fileName )).execute (input ).output ());
48
+ assertions .accept (appl .workflowDefinition (readWorkflowFromClasspath (fileName )));
49
49
}
50
50
51
51
private static Stream <Arguments > provideParameters () {
@@ -54,15 +54,15 @@ private static Stream<Arguments> provideParameters() {
54
54
"switch-then-string.yaml" ,
55
55
Map .of ("orderType" , "electronic" ),
56
56
o ->
57
- assertThat (o )
57
+ assertThat (o . output () )
58
58
.isEqualTo (
59
59
Map .of (
60
60
"orderType" , "electronic" , "validate" , true , "status" , "fulfilled" ))),
61
61
args (
62
62
"switch-then-string.yaml" ,
63
63
Map .of ("orderType" , "physical" ),
64
64
o ->
65
- assertThat (o )
65
+ assertThat (o . output () )
66
66
.isEqualTo (
67
67
Map .of (
68
68
"orderType" ,
@@ -77,7 +77,7 @@ private static Stream<Arguments> provideParameters() {
77
77
"switch-then-string.yaml" ,
78
78
Map .of ("orderType" , "unknown" ),
79
79
o ->
80
- assertThat (o )
80
+ assertThat (o . output () )
81
81
.isEqualTo (
82
82
Map .of (
83
83
"orderType" ,
@@ -89,27 +89,53 @@ private static Stream<Arguments> provideParameters() {
89
89
args (
90
90
"for-sum.yaml" ,
91
91
Map .of ("input" , Arrays .asList (1 , 2 , 3 )),
92
- o -> assertThat (o ).isEqualTo (6 )),
92
+ o -> assertThat (o . output () ).isEqualTo (6 )),
93
93
args (
94
94
"for-collect.yaml" ,
95
95
Map .of ("input" , Arrays .asList (1 , 2 , 3 )),
96
96
o ->
97
- assertThat (o )
97
+ assertThat (o . output () )
98
98
.isEqualTo (
99
99
Map .of ("input" , Arrays .asList (1 , 2 , 3 ), "output" , Arrays .asList (2 , 4 , 6 )))),
100
100
args (
101
101
"simple-expression.yaml" ,
102
102
Map .of ("input" , Arrays .asList (1 , 2 , 3 )),
103
- WorkflowDefinitionTest ::checkSpecialKeywords ));
103
+ WorkflowDefinitionTest ::checkSpecialKeywords ),
104
+ args (
105
+ "raise-inline copy.yaml" ,
106
+ WorkflowDefinitionTest ::checkWorkflowException ,
107
+ WorkflowException .class ),
108
+ args (
109
+ "raise-reusable.yaml" ,
110
+ WorkflowDefinitionTest ::checkWorkflowException ,
111
+ WorkflowException .class ));
104
112
}
105
113
106
114
private static Arguments args (
107
- String fileName , Map <String , Object > input , Consumer <Object > object ) {
108
- return Arguments .of (fileName , input , object );
115
+ String fileName , Map <String , Object > input , Consumer <WorkflowInstance > instance ) {
116
+ return Arguments .of (
117
+ fileName , (Consumer <WorkflowDefinition >) d -> instance .accept (d .execute (input )));
118
+ }
119
+
120
+ private static <T extends Throwable > Arguments args (
121
+ String fileName , Consumer <T > consumer , Class <T > clazz ) {
122
+ return Arguments .of (
123
+ fileName ,
124
+ (Consumer <WorkflowDefinition >)
125
+ d -> consumer .accept (catchThrowableOfType (clazz , () -> d .execute (Map .of ()))));
126
+ }
127
+
128
+ private static void checkWorkflowException (WorkflowException ex ) {
129
+ assertThat (ex .getWorflowError ().type ())
130
+ .isEqualTo ("https://serverlessworkflow.io/errors/not-implemented" );
131
+ assertThat (ex .getWorflowError ().status ()).isEqualTo (500 );
132
+ assertThat (ex .getWorflowError ().title ()).isEqualTo ("Not Implemented" );
133
+ assertThat (ex .getWorflowError ().details ()).contains ("raise-not-implemented" );
134
+ assertThat (ex .getWorflowError ().instance ()).isEqualTo ("do/0/notImplemented" );
109
135
}
110
136
111
- private static void checkSpecialKeywords (Object obj ) {
112
- Map <String , Object > result = (Map <String , Object >) obj ;
137
+ private static void checkSpecialKeywords (WorkflowInstance obj ) {
138
+ Map <String , Object > result = (Map <String , Object >) obj . output () ;
113
139
assertThat (Instant .ofEpochMilli ((long ) result .get ("startedAt" )))
114
140
.isAfterOrEqualTo (before )
115
141
.isBeforeOrEqualTo (Instant .now ());
0 commit comments