|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package catalog |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 9 | + "github.com/mendixlabs/mxcli/model" |
| 10 | +) |
| 11 | + |
| 12 | +// unknownMicroflowObject satisfies MicroflowObject but is not in the type switch. |
| 13 | +type unknownMicroflowObject struct{} |
| 14 | + |
| 15 | +func (u *unknownMicroflowObject) GetID() model.ID { return "" } |
| 16 | +func (u *unknownMicroflowObject) GetPosition() model.Point { return model.Point{} } |
| 17 | +func (u *unknownMicroflowObject) SetPosition(model.Point) {} |
| 18 | + |
| 19 | +func TestGetMicroflowObjectType(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + obj microflows.MicroflowObject |
| 23 | + want string |
| 24 | + }{ |
| 25 | + {"ActionActivity", µflows.ActionActivity{}, "ActionActivity"}, |
| 26 | + {"StartEvent", µflows.StartEvent{}, "StartEvent"}, |
| 27 | + {"EndEvent", µflows.EndEvent{}, "EndEvent"}, |
| 28 | + {"ExclusiveSplit", µflows.ExclusiveSplit{}, "ExclusiveSplit"}, |
| 29 | + {"InheritanceSplit", µflows.InheritanceSplit{}, "InheritanceSplit"}, |
| 30 | + {"ExclusiveMerge", µflows.ExclusiveMerge{}, "ExclusiveMerge"}, |
| 31 | + {"LoopedActivity", µflows.LoopedActivity{}, "LoopedActivity"}, |
| 32 | + {"Annotation", µflows.Annotation{}, "Annotation"}, |
| 33 | + {"BreakEvent", µflows.BreakEvent{}, "BreakEvent"}, |
| 34 | + {"ContinueEvent", µflows.ContinueEvent{}, "ContinueEvent"}, |
| 35 | + {"ErrorEvent", µflows.ErrorEvent{}, "ErrorEvent"}, |
| 36 | + {"unknown object falls to default", &unknownMicroflowObject{}, "MicroflowObject"}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + if got := getMicroflowObjectType(tt.obj); got != tt.want { |
| 42 | + t.Errorf("getMicroflowObjectType() = %q, want %q", got, tt.want) |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetMicroflowActionType(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + action microflows.MicroflowAction |
| 52 | + want string |
| 53 | + }{ |
| 54 | + {"CreateObjectAction", µflows.CreateObjectAction{}, "CreateObjectAction"}, |
| 55 | + {"ChangeObjectAction", µflows.ChangeObjectAction{}, "ChangeObjectAction"}, |
| 56 | + {"RetrieveAction", µflows.RetrieveAction{}, "RetrieveAction"}, |
| 57 | + {"MicroflowCallAction", µflows.MicroflowCallAction{}, "MicroflowCallAction"}, |
| 58 | + {"JavaActionCallAction", µflows.JavaActionCallAction{}, "JavaActionCallAction"}, |
| 59 | + {"ShowMessageAction", µflows.ShowMessageAction{}, "ShowMessageAction"}, |
| 60 | + {"LogMessageAction", µflows.LogMessageAction{}, "LogMessageAction"}, |
| 61 | + {"ValidationFeedbackAction", µflows.ValidationFeedbackAction{}, "ValidationFeedbackAction"}, |
| 62 | + {"ChangeVariableAction", µflows.ChangeVariableAction{}, "ChangeVariableAction"}, |
| 63 | + {"CreateVariableAction", µflows.CreateVariableAction{}, "CreateVariableAction"}, |
| 64 | + {"AggregateListAction", µflows.AggregateListAction{}, "AggregateListAction"}, |
| 65 | + {"ListOperationAction", µflows.ListOperationAction{}, "ListOperationAction"}, |
| 66 | + {"CastAction", µflows.CastAction{}, "CastAction"}, |
| 67 | + {"DownloadFileAction", µflows.DownloadFileAction{}, "DownloadFileAction"}, |
| 68 | + {"ClosePageAction", µflows.ClosePageAction{}, "ClosePageAction"}, |
| 69 | + {"ShowPageAction", µflows.ShowPageAction{}, "ShowPageAction"}, |
| 70 | + {"CallExternalAction", µflows.CallExternalAction{}, "CallExternalAction"}, |
| 71 | + {"unknown action falls to default", µflows.UnknownAction{TypeName: "CustomThing"}, "MicroflowAction"}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + if got := getMicroflowActionType(tt.action); got != tt.want { |
| 77 | + t.Errorf("getMicroflowActionType() = %q, want %q", got, tt.want) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestGetDataTypeName(t *testing.T) { |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + dt microflows.DataType |
| 87 | + want string |
| 88 | + }{ |
| 89 | + {"nil", nil, ""}, |
| 90 | + {"Boolean", µflows.BooleanType{}, "Boolean"}, |
| 91 | + {"Integer", µflows.IntegerType{}, "Integer"}, |
| 92 | + {"Long", µflows.LongType{}, "Long"}, |
| 93 | + {"Decimal", µflows.DecimalType{}, "Decimal"}, |
| 94 | + {"String", µflows.StringType{}, "String"}, |
| 95 | + {"DateTime", µflows.DateTimeType{}, "DateTime"}, |
| 96 | + {"Date", µflows.DateType{}, "Date"}, |
| 97 | + {"Void", µflows.VoidType{}, "Void"}, |
| 98 | + {"Object with entity", µflows.ObjectType{EntityQualifiedName: "Module.Entity"}, "Object:Module.Entity"}, |
| 99 | + {"List with entity", µflows.ListType{EntityQualifiedName: "Module.Entity"}, "List:Module.Entity"}, |
| 100 | + {"Enumeration", µflows.EnumerationType{EnumerationQualifiedName: "Module.Color"}, "Enumeration:Module.Color"}, |
| 101 | + {"Binary falls to Unknown", µflows.BinaryType{}, "Unknown"}, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + if got := getDataTypeName(tt.dt); got != tt.want { |
| 107 | + t.Errorf("getDataTypeName() = %q, want %q", got, tt.want) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestCountMicroflowActivities(t *testing.T) { |
| 114 | + tests := []struct { |
| 115 | + name string |
| 116 | + mf *microflows.Microflow |
| 117 | + want int |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "nil object collection", |
| 121 | + mf: µflows.Microflow{}, |
| 122 | + want: 0, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "empty objects", |
| 126 | + mf: µflows.Microflow{ |
| 127 | + ObjectCollection: µflows.MicroflowObjectCollection{}, |
| 128 | + }, |
| 129 | + want: 0, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "excludes start/end/merge", |
| 133 | + mf: µflows.Microflow{ |
| 134 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 135 | + Objects: []microflows.MicroflowObject{ |
| 136 | + µflows.StartEvent{}, |
| 137 | + µflows.ActionActivity{}, |
| 138 | + µflows.ExclusiveSplit{}, |
| 139 | + µflows.EndEvent{}, |
| 140 | + µflows.ExclusiveMerge{}, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + want: 2, // ActionActivity + ExclusiveSplit |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "counts loops and annotations", |
| 148 | + mf: µflows.Microflow{ |
| 149 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 150 | + Objects: []microflows.MicroflowObject{ |
| 151 | + µflows.LoopedActivity{}, |
| 152 | + µflows.Annotation{}, |
| 153 | + µflows.ErrorEvent{}, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + want: 3, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + if got := countMicroflowActivities(tt.mf); got != tt.want { |
| 164 | + t.Errorf("countMicroflowActivities() = %d, want %d", got, tt.want) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestCalculateMcCabeComplexity(t *testing.T) { |
| 171 | + tests := []struct { |
| 172 | + name string |
| 173 | + mf *microflows.Microflow |
| 174 | + want int |
| 175 | + }{ |
| 176 | + { |
| 177 | + name: "nil object collection — base complexity", |
| 178 | + mf: µflows.Microflow{}, |
| 179 | + want: 1, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "no decision points", |
| 183 | + mf: µflows.Microflow{ |
| 184 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 185 | + Objects: []microflows.MicroflowObject{ |
| 186 | + µflows.StartEvent{}, |
| 187 | + µflows.ActionActivity{}, |
| 188 | + µflows.EndEvent{}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + want: 1, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "exclusive split adds 1", |
| 196 | + mf: µflows.Microflow{ |
| 197 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 198 | + Objects: []microflows.MicroflowObject{ |
| 199 | + µflows.ExclusiveSplit{}, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + want: 2, |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "inheritance split adds 1", |
| 207 | + mf: µflows.Microflow{ |
| 208 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 209 | + Objects: []microflows.MicroflowObject{ |
| 210 | + µflows.InheritanceSplit{}, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + want: 2, |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "loop adds 1 plus nested decisions", |
| 218 | + mf: µflows.Microflow{ |
| 219 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 220 | + Objects: []microflows.MicroflowObject{ |
| 221 | + µflows.LoopedActivity{ |
| 222 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 223 | + Objects: []microflows.MicroflowObject{ |
| 224 | + µflows.ExclusiveSplit{}, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + want: 3, // 1 base + 1 loop + 1 nested split |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "error event adds 1", |
| 235 | + mf: µflows.Microflow{ |
| 236 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 237 | + Objects: []microflows.MicroflowObject{ |
| 238 | + µflows.ErrorEvent{}, |
| 239 | + }, |
| 240 | + }, |
| 241 | + }, |
| 242 | + want: 2, |
| 243 | + }, |
| 244 | + { |
| 245 | + name: "complex flow", |
| 246 | + mf: µflows.Microflow{ |
| 247 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 248 | + Objects: []microflows.MicroflowObject{ |
| 249 | + µflows.ExclusiveSplit{}, |
| 250 | + µflows.ExclusiveSplit{}, |
| 251 | + µflows.InheritanceSplit{}, |
| 252 | + µflows.LoopedActivity{}, |
| 253 | + µflows.ErrorEvent{}, |
| 254 | + }, |
| 255 | + }, |
| 256 | + }, |
| 257 | + want: 6, // 1 + 2 splits + 1 inheritance + 1 loop + 1 error |
| 258 | + }, |
| 259 | + } |
| 260 | + |
| 261 | + for _, tt := range tests { |
| 262 | + t.Run(tt.name, func(t *testing.T) { |
| 263 | + if got := calculateMcCabeComplexity(tt.mf); got != tt.want { |
| 264 | + t.Errorf("calculateMcCabeComplexity() = %d, want %d", got, tt.want) |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +func TestCountNanoflowActivities(t *testing.T) { |
| 271 | + tests := []struct { |
| 272 | + name string |
| 273 | + nf *microflows.Nanoflow |
| 274 | + want int |
| 275 | + }{ |
| 276 | + { |
| 277 | + name: "nil object collection", |
| 278 | + nf: µflows.Nanoflow{}, |
| 279 | + want: 0, |
| 280 | + }, |
| 281 | + { |
| 282 | + name: "excludes structural elements", |
| 283 | + nf: µflows.Nanoflow{ |
| 284 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 285 | + Objects: []microflows.MicroflowObject{ |
| 286 | + µflows.StartEvent{}, |
| 287 | + µflows.ActionActivity{}, |
| 288 | + µflows.EndEvent{}, |
| 289 | + µflows.ExclusiveMerge{}, |
| 290 | + }, |
| 291 | + }, |
| 292 | + }, |
| 293 | + want: 1, // only ActionActivity |
| 294 | + }, |
| 295 | + } |
| 296 | + |
| 297 | + for _, tt := range tests { |
| 298 | + t.Run(tt.name, func(t *testing.T) { |
| 299 | + if got := countNanoflowActivities(tt.nf); got != tt.want { |
| 300 | + t.Errorf("countNanoflowActivities() = %d, want %d", got, tt.want) |
| 301 | + } |
| 302 | + }) |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +func TestCalculateNanoflowComplexity(t *testing.T) { |
| 307 | + nf := µflows.Nanoflow{ |
| 308 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 309 | + Objects: []microflows.MicroflowObject{ |
| 310 | + µflows.ExclusiveSplit{}, |
| 311 | + µflows.LoopedActivity{ |
| 312 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 313 | + Objects: []microflows.MicroflowObject{ |
| 314 | + µflows.InheritanceSplit{}, |
| 315 | + }, |
| 316 | + }, |
| 317 | + }, |
| 318 | + }, |
| 319 | + }, |
| 320 | + } |
| 321 | + |
| 322 | + got := calculateNanoflowComplexity(nf) |
| 323 | + want := 4 // 1 base + 1 split + 1 loop + 1 nested inheritance |
| 324 | + if got != want { |
| 325 | + t.Errorf("calculateNanoflowComplexity() = %d, want %d", got, want) |
| 326 | + } |
| 327 | +} |
0 commit comments