github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service_device_asset_test.go (about) 1 package ciossdk 2 3 import ( 4 "context" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "net/url" 10 "testing" 11 12 srvdevice "github.com/optim-corp/cios-golang-sdk/sdk/service/device" 13 14 xmath "github.com/fcfcqloow/go-advance/math" 15 16 cnv "github.com/fcfcqloow/go-advance/convert" 17 18 "github.com/optim-corp/cios-golang-sdk/cios" 19 sdkmodel "github.com/optim-corp/cios-golang-sdk/model" 20 ) 21 22 func TestDeviceAssetManagement_GetModels(t *testing.T) { 23 var ( 24 query url.Values 25 ctx context.Context 26 27 tests = []struct { 28 params cios.ApiGetDeviceModelsRequest 29 test func() 30 }{ 31 { 32 params: srvdevice.MakeGetModelsOpts().Limit(1000), 33 test: func() { 34 if query.Get("limit") != "1000" { 35 t.Fatal("Missing Query", query.Encode()) 36 } else { 37 t.Log(query.Encode()) 38 } 39 }, 40 }, 41 { 42 params: srvdevice.MakeGetModelsOpts().Limit(1000).Offset(50), 43 test: func() { 44 if query.Get("limit") != "1000" || query.Get("offset") != "50" { 45 t.Fatal("Missing Query", query.Encode()) 46 } else { 47 t.Log(query.Encode()) 48 } 49 }, 50 }, 51 { 52 params: srvdevice.MakeGetModelsOpts().ResourceOwnerId("aaaaa"), 53 test: func() { 54 if query.Get("resource_owner_id") != "aaaaa" { 55 t.Fatal("Missing Query", query.Encode()) 56 } else { 57 t.Log(query.Encode()) 58 } 59 }, 60 }, 61 { 62 params: srvdevice.MakeGetModelsOpts().Limit(1000).Offset(50).ResourceOwnerId("aaaaa").Name("name"), 63 test: func() { 64 if query.Get("resource_owner_id") != "aaaaa" || 65 query.Get("name") != "name" { 66 t.Fatal("Missing Query", query.Encode()) 67 } else { 68 t.Log(query.Encode()) 69 } 70 }, 71 }, 72 { 73 params: srvdevice.MakeGetModelsOpts().OrderBy("created_at"), 74 test: func() { 75 if query.Get("order_by") != "created_at" { 76 t.Fatal("Missing Query", query.Encode()) 77 } else { 78 t.Log(query.Encode()) 79 } 80 }, 81 }, 82 { 83 params: srvdevice.MakeGetModelsOpts().OrderBy("").Order("").ResourceOwnerId("").Name(""), 84 test: func() { 85 if query.Encode() != "" { 86 t.Fatal("Missing Query", query.Encode()) 87 } else { 88 t.Log(query.Encode()) 89 } 90 }, 91 }, 92 } 93 ) 94 95 // Query Test 96 responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 97 query = r.URL.Query() 98 w.Header().Set("Content-Type", "application/json") 99 json.NewEncoder(w).Encode(cios.MultipleDevice{Total: 10}) 100 }) 101 ts := httptest.NewServer(responseHandler) 102 client := NewCiosClient( 103 CiosClientConfig{ 104 Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}, 105 }, 106 ) 107 defer ts.Close() 108 for _, test := range tests { 109 client.DeviceAssetManagement().GetModels(ctx, test.params) 110 test.test() 111 } 112 113 ts.Close() 114 115 } 116 117 func TestDeviceAssetManagement_GetModelsAll(t *testing.T) { 118 var offsets []int 119 var limits []int 120 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 121 w.Header().Set("Content-Type", "application/json") 122 response := cios.MultipleDeviceModel{Total: 3500, Models: []cios.DeviceModel{}} 123 offset := cnv.MustInt(r.URL.Query().Get("offset")) 124 limit := cnv.MustInt(r.URL.Query().Get("limit")) 125 offsets = append(offsets, offset) 126 limits = append(limits, limit) 127 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 128 response.Models = append(response.Models, cios.DeviceModel{Id: cnv.MustStr(i)}) 129 } 130 json.NewEncoder(w).Encode(response) 131 })) 132 defer ts.Close() 133 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 134 135 responses, _, _ := client.DeviceAssetManagement().GetModelsAll(nil, srvdevice.MakeGetModelsOpts().Limit(999)) 136 if len(responses) != 999 || offsets[0] != 0 && limits[0] != 1000 { 137 t.Fatal(len(responses)) 138 } 139 140 offsets = []int{} 141 limits = []int{} 142 responses, _, _ = client.DeviceAssetManagement().GetModelsAll(nil, srvdevice.MakeGetModelsOpts().Limit(1500)) 143 if len(responses) != 1500 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 { 144 t.Fatal(len(responses), limits, offsets) 145 } 146 offsets = []int{} 147 limits = []int{} 148 responses, _, _ = client.DeviceAssetManagement().GetModelsAll(nil, srvdevice.MakeGetModelsOpts().Limit(2001)) 149 if len(responses) != 2001 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 || offsets[2] != 2000 || limits[2] != 1 { 150 t.Fatal(len(responses), limits, offsets) 151 152 } 153 offsets = []int{} 154 limits = []int{} 155 responses, _, _ = client.DeviceAssetManagement().GetModelsAll(nil, srvdevice.MakeGetModelsOpts().Limit(3501)) 156 if len(responses) != 3500 || 157 offsets[0] != 0 || limits[0] != 1000 || 158 offsets[1] != 1000 && limits[1] != 1000 || 159 offsets[2] != 2000 || limits[2] != 1000 || 160 offsets[3] != 3000 || limits[3] != 501 { 161 t.Fatal(len(responses), limits, offsets) 162 } 163 offsets = []int{} 164 limits = []int{} 165 responses, _, _ = client.DeviceAssetManagement().GetModelsAll(nil, srvdevice.MakeGetModelsOpts().Limit(2001).Offset(20)) 166 if len(responses) != 2001 || offsets[0] != 20 && limits[0] != 1000 || offsets[1] != 1020 && limits[1] != 1000 || offsets[2] != 2020 || limits[2] != 1 { 167 t.Fatal(len(responses), limits, offsets) 168 169 } 170 } 171 172 func TestDeviceAssetManagement_GetModelsUnlimited(t *testing.T) { 173 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 174 w.Header().Set("Content-Type", "application/json") 175 response := cios.MultipleDeviceModel{Total: 3500, Models: []cios.DeviceModel{}} 176 offset := cnv.MustInt(r.URL.Query().Get("offset")) 177 limit := cnv.MustInt(r.URL.Query().Get("limit")) 178 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 179 response.Models = append(response.Models, cios.DeviceModel{Id: cnv.MustStr(i)}) 180 } 181 json.NewEncoder(w).Encode(response) 182 })) 183 defer ts.Close() 184 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 185 186 responses, _, _ := client.DeviceAssetManagement().GetModelsUnlimited(nil, srvdevice.MakeGetModelsOpts().Limit(1)) 187 if len(responses) != 3500 { 188 t.Fatal(len(responses)) 189 } 190 } 191 192 func TestDeviceAssetManagement_GetModel(t *testing.T) { 193 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 194 w.Header().Set("Content-Type", "application/json") 195 if r.URL.Path == "/v2/device_models/test" { 196 response := cios.SingleDeviceModel{Model: cios.DeviceModel{ 197 Id: "test", 198 ResourceOwnerId: "test_resource_owner", 199 }} 200 json.NewEncoder(w).Encode(response) 201 } 202 })) 203 defer ts.Close() 204 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 205 body, response, err := client.DeviceAssetManagement().GetModel(nil, "test") 206 if body.Id != "test" || err != nil || response.StatusCode != 200 { 207 t.Fatal(body) 208 } 209 } 210 211 func TestDeviceAssetManagement_CreateModel(t *testing.T) { 212 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 213 if r.URL.Path != "/v2/device_models" { 214 t.Fatal(r.URL.Path) 215 } 216 w.Header().Set("Content-Type", "application/json") 217 body := cios.DeviceModelRequest{} 218 if r.Method != "POST" { 219 t.Fatal(r.Method) 220 } 221 byts, _ := ioutil.ReadAll(r.Body) 222 cnv.UnMarshalJson(byts, &body) 223 if body.Name != "name" || body.ResourceOwnerId != "resource_owner_id" { 224 t.Fatal(body) 225 } 226 227 })) 228 defer ts.Close() 229 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 230 _, _, err := client.DeviceAssetManagement().CreateModel(nil, cios.DeviceModelRequest{ 231 Name: "name", 232 ResourceOwnerId: "resource_owner_id", 233 }) 234 if err != nil { 235 t.Fatal(err.Error()) 236 } 237 } 238 239 func TestDeviceAssetManagement_DeleteModel(t *testing.T) { 240 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 241 w.Header().Set("Content-Type", "application/json") 242 if r.URL.Path != "/v2/device_models/device_id" { 243 t.Fatal(r.URL.Path) 244 } 245 if r.Method != "DELETE" { 246 t.Fatal(r.Method) 247 } 248 })) 249 defer ts.Close() 250 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 251 _, err := client.DeviceAssetManagement().DeleteModel(nil, "device_id") 252 if err != nil { 253 t.Fatal(err.Error()) 254 } 255 } 256 257 func TestDeviceAssetManagement_GetEntities(t *testing.T) { 258 var ( 259 query url.Values 260 ctx context.Context 261 262 tests = []struct { 263 params cios.ApiGetDeviceModelsRequest 264 test func() 265 }{ 266 { 267 params: srvdevice.MakeGetModelsOpts().Limit(1000), 268 test: func() { 269 if query.Get("limit") != "1000" { 270 t.Fatal("Missing Query", query.Encode()) 271 } else { 272 t.Log(query.Encode()) 273 } 274 }, 275 }, 276 { 277 params: srvdevice.MakeGetModelsOpts().Limit(1000).Offset(50), 278 test: func() { 279 if query.Get("limit") != "1000" || query.Get("offset") != "50" { 280 t.Fatal("Missing Query", query.Encode()) 281 } else { 282 t.Log(query.Encode()) 283 } 284 }, 285 }, 286 { 287 params: srvdevice.MakeGetModelsOpts().ResourceOwnerId("aaaaa"), 288 test: func() { 289 if query.Get("resource_owner_id") != "aaaaa" { 290 t.Fatal("Missing Query", query.Encode()) 291 } else { 292 t.Log(query.Encode()) 293 } 294 }, 295 }, 296 { 297 params: srvdevice.MakeGetModelsOpts().Limit(1000).Offset(50).ResourceOwnerId("aaaaa").Name("name"), 298 test: func() { 299 if query.Get("resource_owner_id") != "aaaaa" || 300 query.Get("name") != "name" { 301 t.Fatal("Missing Query", query.Encode()) 302 } else { 303 t.Log(query.Encode()) 304 } 305 }, 306 }, 307 { 308 params: srvdevice.MakeGetModelsOpts().OrderBy("created_at"), 309 test: func() { 310 if query.Get("order_by") != "created_at" { 311 t.Fatal("Missing Query", query.Encode()) 312 } else { 313 t.Log(query.Encode()) 314 } 315 }, 316 }, 317 { 318 params: srvdevice.MakeGetModelsOpts().OrderBy("").Order("").ResourceOwnerId("").Name(""), 319 test: func() { 320 if query.Encode() != "" { 321 t.Fatal("Missing Query", query.Encode()) 322 } else { 323 t.Log(query.Encode()) 324 } 325 }, 326 }, 327 } 328 ) 329 330 // Query Test 331 responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 332 query = r.URL.Query() 333 w.Header().Set("Content-Type", "application/json") 334 json.NewEncoder(w).Encode(cios.MultipleDevice{Total: 10}) 335 }) 336 ts := httptest.NewServer(responseHandler) 337 client := NewCiosClient( 338 CiosClientConfig{ 339 Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}, 340 }, 341 ) 342 defer ts.Close() 343 for _, test := range tests { 344 client.DeviceAssetManagement().GetModels(ctx, test.params) 345 test.test() 346 } 347 348 ts.Close() 349 350 } 351 352 func TestDeviceAssetManagement_GetEntitiesAll(t *testing.T) { 353 var offsets []int 354 var limits []int 355 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 356 w.Header().Set("Content-Type", "application/json") 357 response := cios.MultipleDeviceModelEntity{Total: 3500, Entities: []cios.DeviceModelsEntity{}} 358 offset := cnv.MustInt(r.URL.Query().Get("offset")) 359 limit := cnv.MustInt(r.URL.Query().Get("limit")) 360 offsets = append(offsets, offset) 361 limits = append(limits, limit) 362 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 363 response.Entities = append(response.Entities, cios.DeviceModelsEntity{Id: cnv.MustStr(i)}) 364 } 365 json.NewEncoder(w).Encode(response) 366 })) 367 defer ts.Close() 368 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 369 370 responses, _, _ := client.DeviceAssetManagement().GetEntitiesAll(nil, srvdevice.MakeGetEntitiesOpts().Limit(999)) 371 if len(responses) != 999 || offsets[0] != 0 && limits[0] != 1000 { 372 t.Fatal(len(responses)) 373 } 374 375 offsets = []int{} 376 limits = []int{} 377 responses, _, _ = client.DeviceAssetManagement().GetEntitiesAll(nil, srvdevice.MakeGetEntitiesOpts().Limit(1500)) 378 if len(responses) != 1500 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 { 379 t.Fatal(len(responses), limits, offsets) 380 } 381 offsets = []int{} 382 limits = []int{} 383 responses, _, _ = client.DeviceAssetManagement().GetEntitiesAll(nil, srvdevice.MakeGetEntitiesOpts().Limit(2001)) 384 if len(responses) != 2001 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 || offsets[2] != 2000 || limits[2] != 1 { 385 t.Fatal(len(responses), limits, offsets) 386 387 } 388 offsets = []int{} 389 limits = []int{} 390 responses, _, _ = client.DeviceAssetManagement().GetEntitiesAll(nil, srvdevice.MakeGetEntitiesOpts().Limit(3501)) 391 if len(responses) != 3500 || 392 offsets[0] != 0 || limits[0] != 1000 || 393 offsets[1] != 1000 && limits[1] != 1000 || 394 offsets[2] != 2000 || limits[2] != 1000 || 395 offsets[3] != 3000 || limits[3] != 501 { 396 t.Fatal(len(responses), limits, offsets) 397 } 398 offsets = []int{} 399 limits = []int{} 400 responses, _, _ = client.DeviceAssetManagement().GetEntitiesAll(nil, srvdevice.MakeGetEntitiesOpts().Limit(2001).Offset(20)) 401 if len(responses) != 2001 || offsets[0] != 20 && limits[0] != 1000 || offsets[1] != 1020 && limits[1] != 1000 || offsets[2] != 2020 || limits[2] != 1 { 402 t.Fatal(len(responses), limits, offsets) 403 404 } 405 } 406 407 func TestDeviceAssetManagement_GetGetEntitiesUnlimited(t *testing.T) { 408 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 409 w.Header().Set("Content-Type", "application/json") 410 response := cios.MultipleDeviceModelEntity{Total: 3500, Entities: []cios.DeviceModelsEntity{}} 411 offset := cnv.MustInt(r.URL.Query().Get("offset")) 412 limit := cnv.MustInt(r.URL.Query().Get("limit")) 413 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 414 response.Entities = append(response.Entities, cios.DeviceModelsEntity{Id: cnv.MustStr(i)}) 415 } 416 json.NewEncoder(w).Encode(response) 417 })) 418 defer ts.Close() 419 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 420 421 responses, _, err := client.DeviceAssetManagement().GetEntitiesUnlimited(nil, srvdevice.MakeGetEntitiesOpts().Limit(1)) 422 if err != nil { 423 t.Log(err.Error()) 424 } 425 if len(responses) != 3500 { 426 t.Fatal(len(responses)) 427 } 428 } 429 430 func TestDeviceAssetManagement_GetEntity(t *testing.T) { 431 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 432 w.Header().Set("Content-Type", "application/json") 433 if r.URL.Path == "/v2/device_entities/test" { 434 response := cios.SingleDeviceModelsEntity{Entity: cios.DeviceModelsEntity{ 435 Id: "test", 436 ResourceOwnerId: "test_resource_owner", 437 }} 438 json.NewEncoder(w).Encode(response) 439 } 440 })) 441 defer ts.Close() 442 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 443 body, response, err := client.DeviceAssetManagement().GetEntity(nil, "test") 444 if body.Id != "test" || err != nil || response.StatusCode != 200 { 445 t.Fatal(body) 446 } 447 } 448 449 func TestDeviceAssetManagement_CreateEntity(t *testing.T) { 450 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 451 if r.URL.Path != "/v2/device_models/name/entities" { 452 t.Fatal(r.URL.Path) 453 } 454 w.Header().Set("Content-Type", "application/json") 455 body := cios.Inventory{} 456 if r.Method != "POST" { 457 t.Fatal(r.Method) 458 } 459 cnv.UnMarshalJson(r.Body, &body) 460 if *body.SerialNumber != "111" { 461 t.Fatal(body) 462 } 463 464 })) 465 defer ts.Close() 466 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 467 _, _, err := client.DeviceAssetManagement().CreateEntity(nil, "name", cios.Inventory{ 468 SerialNumber: cnv.StrPtr("111"), 469 }) 470 if err != nil { 471 t.Fatal(err.Error()) 472 } 473 } 474 475 func TestDeviceAssetManagement_DeleteEntity(t *testing.T) { 476 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 477 w.Header().Set("Content-Type", "application/json") 478 if r.URL.Path != "/v2/device_entities/device_id" { 479 t.Fatal(r.URL.Path) 480 } 481 if r.Method != "DELETE" { 482 t.Fatal(r.Method) 483 } 484 })) 485 defer ts.Close() 486 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 487 _, err := client.DeviceAssetManagement().DeleteEntity(nil, "device_id") 488 if err != nil { 489 t.Fatal(err.Error()) 490 } 491 } 492 493 func TestDeviceAssetManagement_GetLifecycles(t *testing.T) { 494 var ( 495 query url.Values 496 ctx context.Context 497 498 tests = []struct { 499 params cios.ApiGetDeviceEntitiesLifecyclesRequest 500 test func() 501 }{ 502 { 503 params: srvdevice.MakeGetLifecyclesOpts().Limit(1000), 504 test: func() { 505 if query.Get("limit") != "1000" { 506 t.Fatal("Missing Query", query.Encode()) 507 } else { 508 t.Log(query.Encode()) 509 } 510 }, 511 }, 512 { 513 params: srvdevice.MakeGetLifecyclesOpts().Limit(1000).Offset(50), 514 test: func() { 515 if query.Get("limit") != "1000" || query.Get("offset") != "50" { 516 t.Fatal("Missing Query", query.Encode()) 517 } else { 518 t.Log(query.Encode()) 519 } 520 }, 521 }, 522 { 523 params: srvdevice.MakeGetLifecyclesOpts().ComponentId("aaaaa"), 524 test: func() { 525 if query.Get("component_id") != "aaaaa" { 526 t.Fatal("Missing Query", query.Encode()) 527 } else { 528 t.Log(query.Encode()) 529 } 530 }, 531 }, 532 { 533 params: srvdevice.MakeGetLifecyclesOpts().Limit(1000).Offset(50).EndEventAt("aaaaa").StartEventAt(":190219021"), 534 test: func() { 535 if query.Get("end_event_at") != "aaaaa" || 536 query.Get("start_event_at") != ":190219021" { 537 t.Fatal("Missing Query", query.Encode()) 538 } else { 539 t.Log(query.Encode()) 540 } 541 }, 542 }, 543 { 544 params: srvdevice.MakeGetLifecyclesOpts().OrderBy("created_at"), 545 test: func() { 546 if query.Get("order_by") != "created_at" { 547 t.Fatal("Missing Query", query.Encode()) 548 } else { 549 t.Log(query.Encode()) 550 } 551 }, 552 }, 553 { 554 params: srvdevice.MakeGetLifecyclesOpts().OrderBy("").Order("").AfterId(""). 555 EndEventAt("").StartEventAt("").BeforeId(""). 556 EventType("").EventMode("").ComponentId(""), 557 test: func() { 558 if query.Encode() != "" { 559 t.Fatal("Missing Query", query.Encode()) 560 } else { 561 t.Log(query.Encode()) 562 } 563 }, 564 }, 565 } 566 ) 567 568 // Query Test 569 responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 570 query = r.URL.Query() 571 w.Header().Set("Content-Type", "application/json") 572 json.NewEncoder(w).Encode(cios.MultipleDevice{Total: 10}) 573 }) 574 ts := httptest.NewServer(responseHandler) 575 client := NewCiosClient( 576 CiosClientConfig{ 577 Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}, 578 }, 579 ) 580 defer ts.Close() 581 for _, test := range tests { 582 client.DeviceAssetManagement().GetLifecycles(ctx, "key", test.params) 583 test.test() 584 } 585 586 ts.Close() 587 588 } 589 590 func TestDeviceAssetManagement_GetLifecyclesAll(t *testing.T) { 591 var offsets []int 592 var limits []int 593 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 594 w.Header().Set("Content-Type", "application/json") 595 if r.URL.Path != "/v2/device_entities/key/lifecycles" { 596 t.Fatal(r.URL.Path) 597 } 598 response := cios.MultipleLifeCycle{Total: 3500, Lifecycles: []cios.LifeCycle{}} 599 offset := cnv.MustInt(r.URL.Query().Get("offset")) 600 limit := cnv.MustInt(r.URL.Query().Get("limit")) 601 offsets = append(offsets, offset) 602 limits = append(limits, limit) 603 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 604 response.Lifecycles = append(response.Lifecycles, cios.LifeCycle{Id: cnv.MustStr(i)}) 605 } 606 json.NewEncoder(w).Encode(response) 607 })) 608 defer ts.Close() 609 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 610 611 responses, _, _ := client.DeviceAssetManagement().GetLifecyclesAll(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(999)) 612 if len(responses) != 999 || offsets[0] != 0 && limits[0] != 1000 { 613 t.Fatal(len(responses)) 614 } 615 616 offsets = []int{} 617 limits = []int{} 618 responses, _, _ = client.DeviceAssetManagement().GetLifecyclesAll(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(1500)) 619 if len(responses) != 1500 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 { 620 t.Fatal(len(responses), limits, offsets) 621 } 622 offsets = []int{} 623 limits = []int{} 624 responses, _, _ = client.DeviceAssetManagement().GetLifecyclesAll(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(2001)) 625 if len(responses) != 2001 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 || offsets[2] != 2000 || limits[2] != 1 { 626 t.Fatal(len(responses), limits, offsets) 627 628 } 629 offsets = []int{} 630 limits = []int{} 631 responses, _, _ = client.DeviceAssetManagement().GetLifecyclesAll(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(3501)) 632 if len(responses) != 3500 || 633 offsets[0] != 0 || limits[0] != 1000 || 634 offsets[1] != 1000 && limits[1] != 1000 || 635 offsets[2] != 2000 || limits[2] != 1000 || 636 offsets[3] != 3000 || limits[3] != 501 { 637 t.Fatal(len(responses), limits, offsets) 638 } 639 offsets = []int{} 640 limits = []int{} 641 responses, _, _ = client.DeviceAssetManagement().GetLifecyclesAll(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(2001).Offset(20)) 642 if len(responses) != 2001 || offsets[0] != 20 && limits[0] != 1000 || offsets[1] != 1020 && limits[1] != 1000 || offsets[2] != 2020 || limits[2] != 1 { 643 t.Fatal(len(responses), limits, offsets) 644 645 } 646 } 647 648 func TestDeviceAssetManagement_GetLifecyclesUnlimited(t *testing.T) { 649 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 650 w.Header().Set("Content-Type", "application/json") 651 if r.URL.Path != "/v2/device_entities/key/lifecycles" { 652 t.Fatal(r.URL.Path) 653 } 654 response := cios.MultipleLifeCycle{Total: 3500, Lifecycles: []cios.LifeCycle{}} 655 offset := cnv.MustInt(r.URL.Query().Get("offset")) 656 limit := cnv.MustInt(r.URL.Query().Get("limit")) 657 for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ { 658 response.Lifecycles = append(response.Lifecycles, cios.LifeCycle{Id: cnv.MustStr(i)}) 659 } 660 json.NewEncoder(w).Encode(response) 661 })) 662 defer ts.Close() 663 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 664 665 responses, _, _ := client.DeviceAssetManagement().GetLifecyclesUnlimited(nil, "key", srvdevice.MakeGetLifecyclesOpts().Limit(1)) 666 if len(responses) != 3500 { 667 t.Fatal(len(responses)) 668 } 669 } 670 671 func TestDeviceAssetManagement_CreateLifecycle(t *testing.T) { 672 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 673 w.Header().Set("Content-Type", "application/json") 674 body := cios.LifeCycleRequest{} 675 byts, _ := ioutil.ReadAll(r.Body) 676 cnv.UnMarshalJson(byts, &body) 677 if body.EventKind != "a" || body.EventMode != "b" || body.EventType != "c" || body.EventAt != "d" { 678 t.Fatal(body) 679 } 680 if r.URL.Path != "/v2/device_entities/device_id/lifecycles" { 681 t.Fatal(r.URL.Path) 682 } 683 if r.Method != "POST" { 684 t.Fatal(r.Method) 685 } 686 })) 687 defer ts.Close() 688 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 689 _, _, err := client.DeviceAssetManagement().CreateLifecycle(nil, "device_id", cios.LifeCycleRequest{ 690 EventKind: "a", 691 EventMode: "b", 692 EventType: "c", 693 EventAt: "d", 694 }) 695 if err != nil { 696 t.Fatal(err.Error()) 697 } 698 } 699 700 func TestDeviceAssetManagement_DeleteLifecycle(t *testing.T) { 701 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 702 w.Header().Set("Content-Type", "application/json") 703 if r.URL.Path != "/v2/device_entities/device_id/lifecycles/test_id" { 704 t.Fatal(r.URL.Path) 705 } 706 if r.Method != "DELETE" { 707 t.Fatal(r.Method) 708 } 709 })) 710 defer ts.Close() 711 client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceAssetManagementUrl: ts.URL}}) 712 _, err := client.DeviceAssetManagement().DeleteLifecycle(nil, "device_id", "test_id") 713 if err != nil { 714 t.Fatal(err.Error()) 715 } 716 }