github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudwrapper/properties_test.go (about) 1 package cloudwrapper 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestListProperties(t *testing.T) { 15 tests := map[string]struct { 16 params ListPropertiesRequest 17 responseStatus int 18 responseBody string 19 expectedPath string 20 expectedResponse *ListPropertiesResponse 21 withError error 22 }{ 23 "200 OK - multiple properties": { 24 responseStatus: 200, 25 responseBody: ` 26 { 27 "properties":[ 28 { 29 "propertyId":1, 30 "propertyName":"TestPropertyName1", 31 "contractId":"TestContractID1", 32 "groupId":11, 33 "type":"MEDIA" 34 }, 35 { 36 "propertyId":2, 37 "propertyName":"TestPropertyName2", 38 "contractId":"TestContractID2", 39 "groupId":22, 40 "type":"WEB" 41 }, 42 { 43 "propertyId":3, 44 "propertyName":"TestPropertyName3", 45 "contractId":"TestContractID3", 46 "groupId":33, 47 "type":"WEB" 48 } 49 ] 50 }`, 51 expectedPath: "/cloud-wrapper/v1/properties?unused=false", 52 expectedResponse: &ListPropertiesResponse{ 53 Properties: []Property{ 54 { 55 GroupID: 11, 56 ContractID: "TestContractID1", 57 PropertyID: 1, 58 PropertyName: "TestPropertyName1", 59 Type: PropertyTypeMedia, 60 }, 61 { 62 GroupID: 22, 63 ContractID: "TestContractID2", 64 PropertyID: 2, 65 PropertyName: "TestPropertyName2", 66 Type: PropertyTypeWeb, 67 }, 68 { 69 GroupID: 33, 70 ContractID: "TestContractID3", 71 PropertyID: 3, 72 PropertyName: "TestPropertyName3", 73 Type: PropertyTypeWeb, 74 }, 75 }, 76 }, 77 }, 78 "200 OK - single property": { 79 responseStatus: 200, 80 responseBody: ` 81 { 82 "properties":[ 83 { 84 "propertyId":1, 85 "propertyName":"TestPropertyName1", 86 "contractId":"TestContractID1", 87 "groupId":11, 88 "type":"MEDIA" 89 } 90 ] 91 }`, 92 expectedPath: "/cloud-wrapper/v1/properties?unused=false", 93 expectedResponse: &ListPropertiesResponse{ 94 Properties: []Property{ 95 { 96 GroupID: 11, 97 ContractID: "TestContractID1", 98 PropertyID: 1, 99 PropertyName: "TestPropertyName1", 100 Type: PropertyTypeMedia, 101 }, 102 }, 103 }, 104 }, 105 "200 OK - properties with query params": { 106 params: ListPropertiesRequest{ 107 Unused: true, 108 ContractIDs: []string{ 109 "TestContractID1", 110 "TestContractID2", 111 }, 112 }, 113 responseStatus: 200, 114 responseBody: ` 115 { 116 "properties":[ 117 { 118 "propertyId":1, 119 "propertyName":"TestPropertyName1", 120 "contractId":"TestContractID1", 121 "groupId":11, 122 "type":"MEDIA" 123 }, 124 { 125 "propertyId":2, 126 "propertyName":"TestPropertyName2", 127 "contractId":"TestContractID2", 128 "groupId":22, 129 "type":"MEDIA" 130 } 131 ] 132 }`, 133 expectedPath: "/cloud-wrapper/v1/properties?contractIds=TestContractID1&contractIds=TestContractID2&unused=true", 134 expectedResponse: &ListPropertiesResponse{ 135 Properties: []Property{ 136 { 137 GroupID: 11, 138 ContractID: "TestContractID1", 139 PropertyID: 1, 140 PropertyName: "TestPropertyName1", 141 Type: PropertyTypeMedia, 142 }, 143 { 144 GroupID: 22, 145 ContractID: "TestContractID2", 146 PropertyID: 2, 147 PropertyName: "TestPropertyName2", 148 Type: PropertyTypeMedia, 149 }, 150 }, 151 }, 152 }, 153 "500 internal server error": { 154 responseStatus: http.StatusInternalServerError, 155 responseBody: ` 156 { 157 "type": "/cloudwrapper/error-types/cloudwrapper-server-error", 158 "title": "An unexpected error has occurred.", 159 "detail": "Error processing request", 160 "instance": "/cloudwrapper/error-instances/abc", 161 "status": 500 162 }`, 163 expectedPath: "/cloud-wrapper/v1/properties?unused=false", 164 withError: &Error{ 165 Type: "/cloudwrapper/error-types/cloudwrapper-server-error", 166 Title: "An unexpected error has occurred.", 167 Detail: "Error processing request", 168 Instance: "/cloudwrapper/error-instances/abc", 169 Status: 500, 170 }, 171 }, 172 } 173 174 for name, test := range tests { 175 t.Run(name, func(t *testing.T) { 176 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 177 assert.Equal(t, test.expectedPath, r.URL.String()) 178 assert.Equal(t, http.MethodGet, r.Method) 179 w.WriteHeader(test.responseStatus) 180 _, err := w.Write([]byte(test.responseBody)) 181 assert.NoError(t, err) 182 })) 183 client := mockAPIClient(t, mockServer) 184 result, err := client.ListProperties(context.Background(), test.params) 185 if test.withError != nil { 186 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 187 return 188 } 189 require.NoError(t, err) 190 assert.Equal(t, test.expectedResponse, result) 191 }) 192 } 193 } 194 195 func TestListOrigins(t *testing.T) { 196 tests := map[string]struct { 197 params ListOriginsRequest 198 responseStatus int 199 responseBody string 200 expectedPath string 201 expectedResponse *ListOriginsResponse 202 withError func(*testing.T, error) 203 }{ 204 "200 OK - multiple objects": { 205 params: ListOriginsRequest{ 206 PropertyID: 1, 207 ContractID: "TestContractID", 208 GroupID: 11, 209 }, 210 responseStatus: 200, 211 responseBody: ` 212 { 213 "default":[ 214 { 215 "originType":"CUSTOMER", 216 "hostname":"origin-www.example.com" 217 }, 218 { 219 "originType":"NET_STORAGE", 220 "hostname":"origin-www.example2.com" 221 } 222 ], 223 "children":[ 224 { 225 "name":"Default CORS Policy", 226 "behaviors":[ 227 { 228 "originType":"NET_STORAGE", 229 "hostname":"origin-www.example3.com" 230 } 231 ] 232 }, 233 { 234 "name":"Cloud Wrapper", 235 "behaviors":[ 236 { 237 "originType":"CUSTOMER", 238 "hostname":"origin-www.example4.com" 239 }, 240 { 241 "originType":"CUSTOMER", 242 "hostname":"origin-www.example5.com" 243 } 244 ] 245 } 246 ] 247 }`, 248 expectedPath: "/cloud-wrapper/v1/properties/1/origins?contractId=TestContractID&groupId=11", 249 expectedResponse: &ListOriginsResponse{ 250 Children: []Child{ 251 { 252 Name: "Default CORS Policy", 253 Behaviors: []Behavior{ 254 { 255 Hostname: "origin-www.example3.com", 256 OriginType: OriginTypeNetStorage, 257 }, 258 }, 259 }, 260 { 261 Name: "Cloud Wrapper", 262 Behaviors: []Behavior{ 263 { 264 Hostname: "origin-www.example4.com", 265 OriginType: OriginTypeCustomer, 266 }, 267 { 268 Hostname: "origin-www.example5.com", 269 OriginType: OriginTypeCustomer, 270 }, 271 }, 272 }, 273 }, 274 Default: []Behavior{ 275 { 276 Hostname: "origin-www.example.com", 277 OriginType: OriginTypeCustomer, 278 }, 279 { 280 Hostname: "origin-www.example2.com", 281 OriginType: OriginTypeNetStorage, 282 }, 283 }, 284 }, 285 }, 286 "200 OK - empty behaviors": { 287 params: ListOriginsRequest{ 288 PropertyID: 1, 289 ContractID: "TestContractID", 290 GroupID: 11, 291 }, 292 responseStatus: 200, 293 responseBody: ` 294 { 295 "default":[ 296 { 297 "originType":"CUSTOMER", 298 "hostname":"test.com" 299 } 300 ], 301 "children":[ 302 { 303 "name":"Default CORS Policy", 304 "behaviors":[ 305 306 ] 307 } 308 ] 309 }`, 310 expectedPath: "/cloud-wrapper/v1/properties/1/origins?contractId=TestContractID&groupId=11", 311 expectedResponse: &ListOriginsResponse{ 312 Children: []Child{ 313 { 314 Name: "Default CORS Policy", 315 Behaviors: []Behavior{}, 316 }, 317 }, 318 Default: []Behavior{ 319 { 320 Hostname: "test.com", 321 OriginType: OriginTypeCustomer, 322 }, 323 }, 324 }, 325 }, 326 "missing required params - validation errors": { 327 params: ListOriginsRequest{ 328 PropertyID: 0, 329 ContractID: "", 330 GroupID: 0, 331 }, 332 withError: func(t *testing.T, err error) { 333 assert.Equal(t, "list origins: struct validation: ContractID: cannot be blank\nGroupID: cannot be blank\nPropertyID: cannot be blank", err.Error()) 334 }, 335 }, 336 "500 internal server error": { 337 params: ListOriginsRequest{ 338 PropertyID: 1, 339 ContractID: "TestContractID", 340 GroupID: 11, 341 }, 342 responseStatus: http.StatusInternalServerError, 343 responseBody: ` 344 { 345 "type": "/cloudwrapper/error-types/cloudwrapper-server-error", 346 "title": "An unexpected error has occurred.", 347 "detail": "Error processing request", 348 "instance": "/cloudwrapper/error-instances/abc", 349 "status": 500 350 }`, 351 expectedPath: "/cloud-wrapper/v1/properties/1/origins?contractId=TestContractID&groupId=11", 352 withError: func(t *testing.T, err error) { 353 want := &Error{ 354 Type: "/cloudwrapper/error-types/cloudwrapper-server-error", 355 Title: "An unexpected error has occurred.", 356 Detail: "Error processing request", 357 Instance: "/cloudwrapper/error-instances/abc", 358 Status: 500, 359 } 360 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 361 }, 362 }, 363 } 364 365 for name, test := range tests { 366 t.Run(name, func(t *testing.T) { 367 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 368 assert.Equal(t, test.expectedPath, r.URL.String()) 369 assert.Equal(t, http.MethodGet, r.Method) 370 w.WriteHeader(test.responseStatus) 371 _, err := w.Write([]byte(test.responseBody)) 372 assert.NoError(t, err) 373 })) 374 client := mockAPIClient(t, mockServer) 375 result, err := client.ListOrigins(context.Background(), test.params) 376 if test.withError != nil { 377 test.withError(t, err) 378 return 379 } 380 require.NoError(t, err) 381 assert.Equal(t, test.expectedResponse, result) 382 }) 383 } 384 }