|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package rules |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/linter" |
| 9 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 10 | +) |
| 11 | + |
| 12 | +func TestFindUnhandledCalls_RestCallNoCustom(t *testing.T) { |
| 13 | + objects := []microflows.MicroflowObject{ |
| 14 | + µflows.ActionActivity{ |
| 15 | + BaseActivity: microflows.BaseActivity{ |
| 16 | + ErrorHandlingType: microflows.ErrorHandlingTypeAbort, |
| 17 | + }, |
| 18 | + Action: µflows.RestCallAction{}, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + var violations []linter.Violation |
| 23 | + r := NewErrorHandlingOnCallsRule() |
| 24 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 25 | + |
| 26 | + if len(violations) != 1 { |
| 27 | + t.Fatalf("expected 1 violation, got %d", len(violations)) |
| 28 | + } |
| 29 | + if violations[0].RuleID != "CONV013" { |
| 30 | + t.Errorf("expected CONV013, got %s", violations[0].RuleID) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestFindUnhandledCalls_RestCallCustom(t *testing.T) { |
| 35 | + objects := []microflows.MicroflowObject{ |
| 36 | + µflows.ActionActivity{ |
| 37 | + BaseActivity: microflows.BaseActivity{ |
| 38 | + ErrorHandlingType: microflows.ErrorHandlingTypeCustom, |
| 39 | + }, |
| 40 | + Action: µflows.RestCallAction{}, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + var violations []linter.Violation |
| 45 | + r := NewErrorHandlingOnCallsRule() |
| 46 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 47 | + |
| 48 | + if len(violations) != 0 { |
| 49 | + t.Errorf("expected 0 violations with Custom handling, got %d", len(violations)) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestFindUnhandledCalls_CustomWithoutRollback(t *testing.T) { |
| 54 | + objects := []microflows.MicroflowObject{ |
| 55 | + µflows.ActionActivity{ |
| 56 | + BaseActivity: microflows.BaseActivity{ |
| 57 | + ErrorHandlingType: microflows.ErrorHandlingTypeCustomWithoutRollback, |
| 58 | + }, |
| 59 | + Action: µflows.RestCallAction{}, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + var violations []linter.Violation |
| 64 | + r := NewErrorHandlingOnCallsRule() |
| 65 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 66 | + |
| 67 | + if len(violations) != 0 { |
| 68 | + t.Errorf("expected 0 violations with CustomWithoutRollback, got %d", len(violations)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestFindUnhandledCalls_JavaAction(t *testing.T) { |
| 73 | + objects := []microflows.MicroflowObject{ |
| 74 | + µflows.ActionActivity{ |
| 75 | + BaseActivity: microflows.BaseActivity{ |
| 76 | + ErrorHandlingType: microflows.ErrorHandlingTypeAbort, |
| 77 | + }, |
| 78 | + Action: µflows.JavaActionCallAction{}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + var violations []linter.Violation |
| 83 | + r := NewErrorHandlingOnCallsRule() |
| 84 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 85 | + |
| 86 | + if len(violations) != 1 { |
| 87 | + t.Fatalf("expected 1 violation for Java action, got %d", len(violations)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestFindUnhandledCalls_WebServiceCall(t *testing.T) { |
| 92 | + objects := []microflows.MicroflowObject{ |
| 93 | + µflows.ActionActivity{ |
| 94 | + BaseActivity: microflows.BaseActivity{ |
| 95 | + ErrorHandlingType: microflows.ErrorHandlingTypeContinue, |
| 96 | + }, |
| 97 | + Action: µflows.WebServiceCallAction{}, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + var violations []linter.Violation |
| 102 | + r := NewErrorHandlingOnCallsRule() |
| 103 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 104 | + |
| 105 | + if len(violations) != 1 { |
| 106 | + t.Fatalf("expected 1 violation for WS call, got %d", len(violations)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestFindUnhandledCalls_NonExternalAction(t *testing.T) { |
| 111 | + objects := []microflows.MicroflowObject{ |
| 112 | + µflows.ActionActivity{ |
| 113 | + BaseActivity: microflows.BaseActivity{ |
| 114 | + ErrorHandlingType: microflows.ErrorHandlingTypeAbort, |
| 115 | + }, |
| 116 | + Action: µflows.CommitObjectsAction{}, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + var violations []linter.Violation |
| 121 | + r := NewErrorHandlingOnCallsRule() |
| 122 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 123 | + |
| 124 | + if len(violations) != 0 { |
| 125 | + t.Errorf("expected 0 violations for non-external action, got %d", len(violations)) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestFindUnhandledCalls_InsideLoop(t *testing.T) { |
| 130 | + loopBody := µflows.MicroflowObjectCollection{ |
| 131 | + Objects: []microflows.MicroflowObject{ |
| 132 | + µflows.ActionActivity{ |
| 133 | + BaseActivity: microflows.BaseActivity{ |
| 134 | + ErrorHandlingType: microflows.ErrorHandlingTypeAbort, |
| 135 | + }, |
| 136 | + Action: µflows.RestCallAction{}, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + objects := []microflows.MicroflowObject{ |
| 141 | + µflows.LoopedActivity{ |
| 142 | + ObjectCollection: loopBody, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + var violations []linter.Violation |
| 147 | + r := NewErrorHandlingOnCallsRule() |
| 148 | + findUnhandledCalls(objects, testMicroflow(), r, &violations) |
| 149 | + |
| 150 | + if len(violations) != 1 { |
| 151 | + t.Errorf("expected 1 violation inside loop, got %d", len(violations)) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// --- CONV014 tests --- |
| 156 | + |
| 157 | +func TestFindContinueErrorHandling_Activity(t *testing.T) { |
| 158 | + objects := []microflows.MicroflowObject{ |
| 159 | + µflows.ActionActivity{ |
| 160 | + BaseActivity: microflows.BaseActivity{ |
| 161 | + Caption: "Do something", |
| 162 | + ErrorHandlingType: microflows.ErrorHandlingTypeContinue, |
| 163 | + }, |
| 164 | + Action: µflows.CommitObjectsAction{}, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + var violations []linter.Violation |
| 169 | + r := NewNoContinueErrorHandlingRule() |
| 170 | + findContinueErrorHandling(objects, testMicroflow(), r, &violations) |
| 171 | + |
| 172 | + if len(violations) != 1 { |
| 173 | + t.Fatalf("expected 1 violation, got %d", len(violations)) |
| 174 | + } |
| 175 | + if violations[0].RuleID != "CONV014" { |
| 176 | + t.Errorf("expected CONV014, got %s", violations[0].RuleID) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestFindContinueErrorHandling_Loop(t *testing.T) { |
| 181 | + objects := []microflows.MicroflowObject{ |
| 182 | + µflows.LoopedActivity{ |
| 183 | + Caption: "Process items", |
| 184 | + ErrorHandlingType: microflows.ErrorHandlingTypeContinue, |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + var violations []linter.Violation |
| 189 | + r := NewNoContinueErrorHandlingRule() |
| 190 | + findContinueErrorHandling(objects, testMicroflow(), r, &violations) |
| 191 | + |
| 192 | + if len(violations) != 1 { |
| 193 | + t.Fatalf("expected 1 violation for loop, got %d", len(violations)) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func TestFindContinueErrorHandling_AbortIsOk(t *testing.T) { |
| 198 | + objects := []microflows.MicroflowObject{ |
| 199 | + µflows.ActionActivity{ |
| 200 | + BaseActivity: microflows.BaseActivity{ |
| 201 | + ErrorHandlingType: microflows.ErrorHandlingTypeAbort, |
| 202 | + }, |
| 203 | + Action: µflows.CommitObjectsAction{}, |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + var violations []linter.Violation |
| 208 | + r := NewNoContinueErrorHandlingRule() |
| 209 | + findContinueErrorHandling(objects, testMicroflow(), r, &violations) |
| 210 | + |
| 211 | + if len(violations) != 0 { |
| 212 | + t.Errorf("expected 0 violations for Abort, got %d", len(violations)) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +func TestErrorHandlingOnCallsRule_Metadata(t *testing.T) { |
| 217 | + r := NewErrorHandlingOnCallsRule() |
| 218 | + if r.ID() != "CONV013" { |
| 219 | + t.Errorf("ID = %q, want CONV013", r.ID()) |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +func TestNoContinueErrorHandlingRule_Metadata(t *testing.T) { |
| 224 | + r := NewNoContinueErrorHandlingRule() |
| 225 | + if r.ID() != "CONV014" { |
| 226 | + t.Errorf("ID = %q, want CONV014", r.ID()) |
| 227 | + } |
| 228 | +} |
0 commit comments