github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/nsadapter/nsmodel/model_test.go (about) 1 package nsmodel 2 3 import ( 4 "fmt" 5 "testing" 6 7 validation "github.com/go-ozzo/ozzo-validation/v4" 8 "github.com/stretchr/testify/require" 9 ) 10 11 type cases []struct { 12 data validation.Validatable 13 name string 14 expectErr bool 15 expectedError string 16 } 17 18 func TestReport_Validate(t *testing.T) { 19 validation.ErrRequired = validation.ErrRequired.SetMessage("the value is required") 20 validation.ErrNotNilRequired = validation.ErrNotNilRequired.SetMessage("the value can not be nil") 21 22 validSystem := System{ 23 SystemBase: SystemBase{ 24 Protocol: "protocol", 25 Host: "host", 26 SystemType: "systemType", 27 Description: "description", 28 Status: "status", 29 SystemNumber: "systemNumber", 30 }, 31 TemplateID: "emptyTemplateID", 32 } 33 34 validSCC := SCC{ 35 ExternalSubaccountID: "external-subaccount", 36 InternalSubaccountID: "", 37 LocationID: "loc-id", 38 ExposedSystems: []System{validSystem}, 39 } 40 41 invalidSCC := SCC{ 42 ExternalSubaccountID: "", 43 InternalSubaccountID: "", 44 LocationID: "loc-id", 45 ExposedSystems: []System{}, 46 } 47 48 cases := cases{ 49 { 50 data: Report{ 51 ReportType: "delta", 52 Value: []SCC{validSCC}, 53 }, 54 name: "success", 55 expectErr: false, 56 }, 57 { 58 data: Report{ 59 ReportType: "delta", 60 Value: []SCC{}, 61 }, 62 name: "success with empty slice of SCCs", 63 expectErr: false, 64 }, 65 { 66 data: Report{ 67 Value: []SCC{validSCC}, 68 }, 69 name: "fail with missing report type", 70 expectErr: true, 71 expectedError: "type: the value is required.", 72 }, 73 { 74 data: Report{ 75 Value: []SCC{}, 76 }, 77 name: "fail with empty report type", 78 expectErr: true, 79 expectedError: "type: the value is required.", 80 }, 81 { 82 data: Report{ 83 ReportType: "delta", 84 }, 85 name: "fail with missing value", 86 expectErr: true, 87 expectedError: "value: the value can not be nil.", 88 }, 89 { 90 data: Report{ 91 ReportType: "delta", 92 Value: []SCC{invalidSCC}, 93 }, 94 name: "fail with invalid SCC", 95 expectErr: true, 96 expectedError: "value: (subaccount: the value is required.).", 97 }, 98 } 99 checkCases(cases, t) 100 } 101 102 func TestSCC_Validate(t *testing.T) { 103 validation.ErrRequired = validation.ErrRequired.SetMessage("the value is required") 104 validation.ErrNotNilRequired = validation.ErrNotNilRequired.SetMessage("the value can not be nil") 105 106 validSystem := System{ 107 SystemBase: SystemBase{ 108 Protocol: "protocol", 109 Host: "host", 110 SystemType: "systemType", 111 Description: "description", 112 Status: "status", 113 SystemNumber: "systemNumber", 114 }, 115 TemplateID: "emptyTemplateID", 116 } 117 118 invalidSystem := System{ 119 SystemBase: SystemBase{ 120 Host: "host", 121 SystemType: "systemType", 122 Description: "description", 123 Status: "status", 124 SystemNumber: "systemNumber", 125 }, 126 TemplateID: "emptyTemplateID", 127 } 128 129 cases := cases{ 130 { 131 data: SCC{ 132 ExternalSubaccountID: "external-subaccount", 133 InternalSubaccountID: "", 134 LocationID: "loc-id", 135 ExposedSystems: []System{validSystem}, 136 }, 137 name: "success", 138 expectErr: false, 139 }, 140 { 141 data: SCC{ 142 ExternalSubaccountID: "external-subaccount", 143 InternalSubaccountID: "", 144 LocationID: "", 145 ExposedSystems: []System{validSystem}, 146 }, 147 name: "success with empty location ID", 148 expectErr: false, 149 }, 150 { 151 data: SCC{ 152 ExternalSubaccountID: "external-subaccount", 153 InternalSubaccountID: "", 154 LocationID: "loc-id", 155 ExposedSystems: []System{}, 156 }, 157 name: "success with empty slice of systems", 158 expectErr: false, 159 }, 160 { 161 data: SCC{ 162 ExternalSubaccountID: "external-subaccount", 163 LocationID: "loc-id", 164 ExposedSystems: []System{}, 165 }, 166 name: "success with missing internal subaccount id", 167 expectErr: false, 168 }, 169 { 170 data: SCC{ 171 ExternalSubaccountID: "", 172 InternalSubaccountID: "", 173 LocationID: "loc-id", 174 ExposedSystems: []System{}, 175 }, 176 name: "fail with empty subaccount", 177 expectErr: true, 178 expectedError: "subaccount: the value is required.", 179 }, 180 { 181 data: SCC{ 182 LocationID: "loc-id", 183 ExposedSystems: []System{}, 184 }, 185 name: "fail with missing subaccount", 186 expectErr: true, 187 expectedError: "subaccount: the value is required.", 188 }, 189 { 190 data: SCC{ 191 ExternalSubaccountID: "external-subaccount", 192 InternalSubaccountID: "", 193 LocationID: "loc-id", 194 }, 195 name: "fail with missing ExposedSystem", 196 expectErr: true, 197 expectedError: "exposedSystems: the value can not be nil.", 198 }, 199 { 200 data: SCC{ 201 ExternalSubaccountID: "external-subaccount", 202 InternalSubaccountID: "", 203 LocationID: "loc-id", 204 ExposedSystems: []System{invalidSystem}, 205 }, 206 name: "fail with invalid system", 207 expectErr: true, 208 expectedError: "exposedSystems: (protocol: the value is required.).", 209 }, 210 } 211 212 checkCases(cases, t) 213 } 214 215 func TestSystem_Validate(t *testing.T) { 216 validation.ErrRequired = validation.ErrRequired.SetMessage("the value is required") 217 validation.ErrNotNilRequired = validation.ErrNotNilRequired.SetMessage("the value can not be nil") 218 219 protocol := "HTTP" 220 host := "127.0.0.1:8080" 221 systemType := "nonSAPsys" 222 description := "description" 223 status := "unreachable" 224 systemNumber := "sys-num" 225 emptyTemplateID := "" 226 227 cases := cases{ 228 { 229 name: "success with all fields present", 230 data: System{ 231 SystemBase: SystemBase{ 232 Protocol: protocol, 233 Host: host, 234 SystemType: systemType, 235 Description: description, 236 Status: status, 237 SystemNumber: systemNumber, 238 }, 239 TemplateID: emptyTemplateID, 240 }, 241 expectErr: false, 242 }, 243 { 244 name: "success with empty description", 245 data: System{ 246 SystemBase: SystemBase{ 247 Protocol: protocol, 248 Host: host, 249 SystemType: systemType, 250 Description: "", 251 Status: status, 252 SystemNumber: systemNumber, 253 }, 254 TemplateID: emptyTemplateID, 255 }, 256 expectErr: false, 257 }, 258 { 259 name: "success with empty systemNumber", 260 data: System{ 261 SystemBase: SystemBase{ 262 Protocol: protocol, 263 Host: host, 264 SystemType: systemType, 265 Description: description, 266 Status: status, 267 SystemNumber: "", 268 }, 269 TemplateID: emptyTemplateID, 270 }, 271 expectErr: false, 272 }, 273 { 274 name: "fail when missing protocol", 275 data: System{ 276 SystemBase: SystemBase{ 277 Host: host, 278 SystemType: systemType, 279 Description: description, 280 Status: status, 281 SystemNumber: systemNumber, 282 }, 283 TemplateID: emptyTemplateID, 284 }, 285 expectErr: true, 286 expectedError: "protocol: the value is required.", 287 }, 288 { 289 name: "fail when protocol is empty", 290 data: System{ 291 SystemBase: SystemBase{ 292 Protocol: "", 293 Host: host, 294 SystemType: systemType, 295 Description: description, 296 Status: status, 297 SystemNumber: systemNumber, 298 }, 299 TemplateID: emptyTemplateID, 300 }, 301 expectErr: true, 302 expectedError: "protocol: the value is required.", 303 }, 304 { 305 name: "fail when missing host", 306 data: System{ 307 SystemBase: SystemBase{ 308 Protocol: protocol, 309 SystemType: systemType, 310 Description: description, 311 Status: status, 312 SystemNumber: systemNumber, 313 }, 314 TemplateID: emptyTemplateID, 315 }, 316 expectErr: true, 317 expectedError: "host: the value is required.", 318 }, 319 { 320 name: "fail when host is empty", 321 data: System{ 322 SystemBase: SystemBase{ 323 Protocol: protocol, 324 Host: "", 325 SystemType: systemType, 326 Description: description, 327 Status: status, 328 SystemNumber: systemNumber, 329 }, 330 TemplateID: emptyTemplateID, 331 }, 332 expectErr: true, 333 expectedError: "host: the value is required.", 334 }, 335 { 336 name: "fail when missing systemType", 337 data: System{ 338 SystemBase: SystemBase{ 339 Protocol: protocol, 340 Host: host, 341 Description: description, 342 Status: status, 343 SystemNumber: systemNumber, 344 }, 345 TemplateID: emptyTemplateID, 346 }, 347 expectErr: true, 348 expectedError: "type: the value is required.", 349 }, 350 { 351 name: "fail when systemType is empty", 352 data: System{ 353 SystemBase: SystemBase{ 354 Protocol: protocol, 355 Host: host, 356 SystemType: "", 357 Description: description, 358 Status: status, 359 SystemNumber: systemNumber, 360 }, 361 TemplateID: emptyTemplateID, 362 }, 363 expectErr: true, 364 expectedError: "type: the value is required.", 365 }, 366 { 367 name: "fail when missing status", 368 data: System{ 369 SystemBase: SystemBase{ 370 Protocol: protocol, 371 Host: host, 372 SystemType: systemType, 373 Description: description, 374 SystemNumber: systemNumber, 375 }, 376 TemplateID: emptyTemplateID, 377 }, 378 expectErr: true, 379 expectedError: "status: the value is required.", 380 }, 381 { 382 name: "fail when status is empty", 383 data: System{ 384 SystemBase: SystemBase{ 385 Protocol: protocol, 386 Host: host, 387 SystemType: systemType, 388 Description: description, 389 Status: "", 390 SystemNumber: systemNumber, 391 }, 392 TemplateID: emptyTemplateID, 393 }, 394 expectErr: true, 395 expectedError: "status: the value is required.", 396 }, 397 { 398 name: "fail when multiple fields are missing", 399 data: System{ 400 SystemBase: SystemBase{ 401 Protocol: protocol, 402 SystemType: systemType, 403 Description: description, 404 SystemNumber: systemNumber, 405 }, 406 TemplateID: emptyTemplateID, 407 }, 408 expectErr: true, 409 expectedError: "host: the value is required; status: the value is required.", 410 }, 411 } 412 413 checkCases(cases, t) 414 } 415 416 func TestSystem_UnmarshalJSON(t *testing.T) { 417 t.Run("fail to marshal system", func(t *testing.T) { 418 s := &System{ 419 SystemBase: SystemBase{}, 420 TemplateID: "", 421 } 422 err := s.UnmarshalJSON(nil) 423 424 require.Error(t, err) 425 }) 426 427 t.Run("success when template is matched", func(t *testing.T) { 428 systemString := "{\"protocol\": \"HTTP\",\"host\": \"127.0.0.1:8080\",\"type\": \"otherSAPsys\",\"status\": \"disabled\",\"description\": \"description\"}" 429 430 Mappings = append(Mappings, 431 TemplateMapping{ 432 Name: "", 433 ID: "sss", 434 SourceKey: []string{"type"}, 435 SourceValue: []string{"type"}, 436 }, 437 TemplateMapping{ 438 Name: "", 439 ID: "ss", 440 SourceKey: []string{"description"}, 441 SourceValue: []string{"description"}, 442 }) 443 444 actualSystem := &System{ 445 SystemBase: SystemBase{}, 446 TemplateID: "", 447 } 448 expectedSystem := &System{ 449 SystemBase: SystemBase{ 450 Protocol: "HTTP", 451 Host: "127.0.0.1:8080", 452 SystemType: "otherSAPsys", 453 Description: "description", 454 Status: "disabled", 455 SystemNumber: "", 456 }, 457 TemplateID: "ss", 458 } 459 460 err := actualSystem.UnmarshalJSON([]byte(systemString)) 461 require.NoError(t, err) 462 require.Equal(t, expectedSystem, actualSystem) 463 464 Mappings = nil 465 }) 466 467 t.Run("success when do not match to any template", func(t *testing.T) { 468 systemString := "{\"protocol\": \"HTTP\",\"host\": \"127.0.0.1:8080\",\"type\": \"otherSAPsys\",\"status\": \"disabled\",\"description\": \"description\"}" 469 470 Mappings = append(Mappings, 471 TemplateMapping{ 472 Name: "", 473 ID: "sss", 474 SourceKey: []string{"type"}, 475 SourceValue: []string{"type"}, 476 }, 477 TemplateMapping{ 478 Name: "", 479 ID: "ss", 480 SourceKey: []string{"description"}, 481 SourceValue: []string{"sth"}, 482 }) 483 484 actualSystem := &System{ 485 SystemBase: SystemBase{}, 486 TemplateID: "", 487 } 488 expectedSystem := &System{ 489 SystemBase: SystemBase{ 490 Protocol: "HTTP", 491 Host: "127.0.0.1:8080", 492 SystemType: "otherSAPsys", 493 Description: "description", 494 Status: "disabled", 495 SystemNumber: "", 496 }, 497 TemplateID: "", 498 } 499 500 err := actualSystem.UnmarshalJSON([]byte(systemString)) 501 require.NoError(t, err) 502 require.Equal(t, expectedSystem, actualSystem) 503 504 Mappings = nil 505 }) 506 } 507 508 func checkCases(cases cases, t *testing.T) { 509 for _, c := range cases { 510 t.Run(fmt.Sprintf("Checking case %s", c.name), func(t *testing.T) { 511 err := c.data.Validate() 512 513 if c.expectErr { 514 require.Equal(t, c.expectedError, err.Error()) 515 } else { 516 require.NoError(t, err) 517 } 518 }) 519 } 520 }