1
1
package condition_test
2
2
3
3
import (
4
+ "errors"
4
5
"soarca/internal/executors/condition"
5
6
"soarca/models/cacao"
6
7
"soarca/models/execution"
8
+ "soarca/test/unittest/mocks/mock_reporter"
7
9
mock_stix "soarca/test/unittest/mocks/mock_utils/stix"
8
10
"testing"
9
11
@@ -13,8 +15,9 @@ import (
13
15
14
16
func TestExecuteConditionTrue (t * testing.T ) {
15
17
mock_stix := new (mock_stix.MockStix )
18
+ mock_reporter := new (mock_reporter.Mock_Reporter )
16
19
17
- conditionExecutior := condition .New (mock_stix )
20
+ conditionExecutior := condition .New (mock_stix , mock_reporter )
18
21
19
22
executionId := uuid .New ()
20
23
@@ -28,19 +31,25 @@ func TestExecuteConditionTrue(t *testing.T) {
28
31
OnFalse : "4" }
29
32
vars := cacao .NewVariables ()
30
33
34
+ mock_reporter .On ("ReportStepStart" , executionId , step , vars )
31
35
mock_stix .On ("Evaluate" , "a = a" , vars ).Return (true , nil )
36
+ mock_reporter .On ("ReportStepEnd" , executionId , step , vars , nil )
32
37
33
38
nextStepId , goToBranch , err := conditionExecutior .Execute (meta , step , vars )
34
39
assert .Equal (t , nil , err )
35
40
assert .Equal (t , true , goToBranch )
36
41
assert .Equal (t , "3" , nextStepId )
37
42
43
+ mock_reporter .AssertExpectations (t )
44
+ mock_stix .AssertExpectations (t )
45
+
38
46
}
39
47
40
48
func TestExecuteConditionFalse (t * testing.T ) {
41
49
mock_stix := new (mock_stix.MockStix )
50
+ mock_reporter := new (mock_reporter.Mock_Reporter )
42
51
43
- conditionExecutior := condition .New (mock_stix )
52
+ conditionExecutior := condition .New (mock_stix , mock_reporter )
44
53
45
54
executionId := uuid .New ()
46
55
@@ -54,11 +63,48 @@ func TestExecuteConditionFalse(t *testing.T) {
54
63
OnFalse : "4" }
55
64
vars := cacao .NewVariables ()
56
65
57
- mock_stix .On ("Evaluate" , "a = a" , vars ).Return (true , nil )
66
+ mock_reporter .On ("ReportStepStart" , executionId , step , vars )
67
+ mock_stix .On ("Evaluate" , "a = a" , vars ).Return (false , nil )
68
+ mock_reporter .On ("ReportStepEnd" , executionId , step , vars , nil )
58
69
59
70
nextStepId , goToBranch , err := conditionExecutior .Execute (meta , step , vars )
60
71
assert .Equal (t , nil , err )
61
72
assert .Equal (t , true , goToBranch )
62
- assert .Equal (t , "3" , nextStepId )
73
+ assert .Equal (t , "4" , nextStepId )
74
+
75
+ mock_reporter .AssertExpectations (t )
76
+ mock_stix .AssertExpectations (t )
77
+ }
78
+
79
+ func TestExecuteConditionError (t * testing.T ) {
80
+ mock_stix := new (mock_stix.MockStix )
81
+ mock_reporter := new (mock_reporter.Mock_Reporter )
82
+
83
+ conditionExecutior := condition .New (mock_stix , mock_reporter )
84
+
85
+ executionId := uuid .New ()
86
+
87
+ meta := execution.Metadata {ExecutionId : executionId ,
88
+ PlaybookId : "1" ,
89
+ StepId : "2" }
90
+
91
+ step := cacao.Step {Type : cacao .StepTypeIfCondition ,
92
+ Condition : "a = a" ,
93
+ OnTrue : "3" ,
94
+ OnFalse : "4" }
95
+ vars := cacao .NewVariables ()
96
+
97
+ evaluationError := errors .New ("some ds error" )
98
+
99
+ mock_reporter .On ("ReportStepStart" , executionId , step , vars )
100
+ mock_stix .On ("Evaluate" , "a = a" , vars ).Return (false , evaluationError )
101
+ mock_reporter .On ("ReportStepEnd" , executionId , step , vars , evaluationError )
102
+
103
+ nextStepId , goToBranch , err := conditionExecutior .Execute (meta , step , vars )
104
+ assert .Equal (t , evaluationError , err )
105
+ assert .Equal (t , false , goToBranch )
106
+ assert .Equal (t , "" , nextStepId )
63
107
108
+ mock_reporter .AssertExpectations (t )
109
+ mock_stix .AssertExpectations (t )
64
110
}
0 commit comments