github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudlets/v3/policy_property_test.go (about) 1 package v3 2 3 import ( 4 "context" 5 "encoding/json" 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 ListActivePolicyProperties(t *testing.T) { 15 t.Parallel() 16 17 tests := map[string]struct { 18 params ListActivePolicyPropertiesRequest 19 responseStatus int 20 responseBody string 21 expectedPath string 22 expectedResponse *ListActivePolicyPropertiesResponse 23 withError func(*testing.T, error) 24 }{ 25 "200 OK - no query params": { 26 params: ListActivePolicyPropertiesRequest{ 27 PolicyID: 5, 28 }, 29 responseStatus: http.StatusOK, 30 responseBody: ` 31 { 32 "page": { 33 "number": 0, 34 "size": 1000, 35 "totalElements": 2, 36 "totalPages": 1 37 }, 38 "content": [ 39 { 40 "groupId": 5, 41 "id": 1234, 42 "name": "property", 43 "network": "PRODUCTION", 44 "version": 1 45 }, 46 { 47 "groupId": 5, 48 "id": 1233, 49 "name": "property", 50 "network": "STAGING", 51 "version": 1 52 } 53 ], 54 "links": [ 55 { 56 "href": "/cloudlets/v3/policies/101/properties?page=0&size=1000", 57 "rel": "self" 58 } 59 ] 60 }`, 61 expectedPath: "/cloudlets/v3/policies/5/properties", 62 expectedResponse: &ListActivePolicyPropertiesResponse{ 63 Page: Page{ 64 Number: 0, 65 Size: 1000, 66 TotalElements: 2, 67 TotalPages: 1, 68 }, 69 PolicyProperties: []ListPolicyPropertiesItem{ 70 { 71 GroupID: 5, 72 ID: 1234, 73 Name: "property", 74 Network: "PRODUCTION", 75 PolicyVersion: 1, 76 }, 77 { 78 GroupID: 5, 79 ID: 1233, 80 Name: "property", 81 Network: "STAGING", 82 PolicyVersion: 1, 83 }, 84 }, 85 Links: []Link{ 86 { 87 Href: "/cloudlets/v3/policies/101/properties?page=0&size=1000", 88 Rel: "self", 89 }, 90 }, 91 }, 92 }, 93 "200 OK - with query params": { 94 params: ListActivePolicyPropertiesRequest{ 95 PolicyID: 5, 96 Page: 50, 97 Size: 1000, 98 }, 99 responseStatus: http.StatusOK, 100 responseBody: ` 101 { 102 "page": { 103 "number": 50, 104 "size": 1000, 105 "totalElements": 2, 106 "totalPages": 1 107 }, 108 "content": [ 109 { 110 "groupId": 5, 111 "id": 1234, 112 "name": "property", 113 "network": "PRODUCTION", 114 "version": 1 115 }, 116 { 117 "groupId": 5, 118 "id": 1233, 119 "name": "property", 120 "network": "STAGING", 121 "version": 1 122 } 123 ], 124 "links": [ 125 { 126 "href": "/cloudlets/v3/policies/101/properties?page=50&size=1000", 127 "rel": "self" 128 } 129 ] 130 }`, 131 expectedPath: "/cloudlets/v3/policies/5/properties?page=50&size=1000", 132 expectedResponse: &ListActivePolicyPropertiesResponse{ 133 Page: Page{ 134 Number: 50, 135 Size: 1000, 136 TotalElements: 2, 137 TotalPages: 1, 138 }, 139 PolicyProperties: []ListPolicyPropertiesItem{ 140 { 141 GroupID: 5, 142 ID: 1234, 143 Name: "property", 144 Network: "PRODUCTION", 145 PolicyVersion: 1, 146 }, 147 { 148 GroupID: 5, 149 ID: 1233, 150 Name: "property", 151 Network: "STAGING", 152 PolicyVersion: 1, 153 }, 154 }, 155 Links: []Link{ 156 { 157 Href: "/cloudlets/v3/policies/101/properties?page=50&size=1000", 158 Rel: "self", 159 }, 160 }, 161 }, 162 }, 163 "200 OK - empty": { 164 params: ListActivePolicyPropertiesRequest{ 165 PolicyID: 5, 166 Page: 0, 167 Size: 1000, 168 }, 169 responseStatus: http.StatusOK, 170 responseBody: ` 171 { 172 "page": { 173 "number": 0, 174 "size": 1000, 175 "totalElements": 2, 176 "totalPages": 1 177 }, 178 "content": [], 179 "links": [] 180 }`, 181 expectedPath: "/cloudlets/v3/policies/5/properties?size=1000", 182 expectedResponse: &ListActivePolicyPropertiesResponse{ 183 Page: Page{ 184 Number: 0, 185 Size: 1000, 186 TotalElements: 2, 187 TotalPages: 1, 188 }, 189 PolicyProperties: []ListPolicyPropertiesItem{}, 190 Links: []Link{}, 191 }, 192 }, 193 "validation errors - missing required params": { 194 params: ListActivePolicyPropertiesRequest{}, 195 withError: func(t *testing.T, err error) { 196 assert.Equal(t, "get policy properties: struct validation: PolicyID: cannot be blank", err.Error()) 197 }, 198 }, 199 "validation errors - size lower than 10, negative page number": { 200 params: ListActivePolicyPropertiesRequest{ 201 PolicyID: 1, 202 Page: -2, 203 Size: 5, 204 }, 205 withError: func(t *testing.T, err error) { 206 assert.Equal(t, "get policy properties: struct validation: Page: must be no less than 0\nSize: must be no less than 10", err.Error()) 207 }, 208 }, 209 "500 Internal Server Error": { 210 params: ListActivePolicyPropertiesRequest{ 211 PolicyID: 1, 212 Page: 0, 213 Size: 1000, 214 }, 215 responseStatus: http.StatusInternalServerError, 216 responseBody: ` 217 { 218 "type": "internal_error", 219 "title": "Internal Server Error", 220 "status": 500, 221 "requestId": "1", 222 "requestTime": "12:00", 223 "clientIp": "1.1.1.1", 224 "serverIp": "2.2.2.2", 225 "method": "GET" 226 }`, 227 expectedPath: "/cloudlets/v3/policies/1/properties?size=1000", 228 withError: func(t *testing.T, err error) { 229 want := &Error{ 230 Type: "internal_error", 231 Title: "Internal Server Error", 232 Status: http.StatusInternalServerError, 233 RequestID: "1", 234 RequestTime: "12:00", 235 ClientIP: "1.1.1.1", 236 ServerIP: "2.2.2.2", 237 Method: "GET", 238 } 239 assert.ErrorIs(t, err, want) 240 }, 241 }, 242 "404 Not found": { 243 params: ListActivePolicyPropertiesRequest{ 244 PolicyID: 1, 245 Page: 0, 246 Size: 1000, 247 }, 248 responseStatus: http.StatusNotFound, 249 responseBody: ` 250 { 251 "instance": "TestInstance", 252 "status": 404, 253 "title": "Not found", 254 "type": "/cloudlets/v3/error-types/not-found", 255 "errors": [ 256 { 257 "detail": "Policy with id 1 not found.", 258 "title": "Not found" 259 } 260 ] 261 }`, 262 expectedPath: "/cloudlets/v3/policies/1/properties?size=1000", 263 withError: func(t *testing.T, err error) { 264 want := &Error{ 265 Type: "/cloudlets/v3/error-types/not-found", 266 Title: "Not found", 267 Status: http.StatusNotFound, 268 Instance: "TestInstance", 269 Errors: json.RawMessage(` 270 [ 271 { 272 "detail": "Policy with id 1 not found.", 273 "title": "Not found" 274 } 275 ]`)} 276 assert.ErrorIs(t, err, want) 277 }, 278 }, 279 } 280 281 for name, test := range tests { 282 name, test := name, test 283 t.Run(name, func(t *testing.T) { 284 t.Parallel() 285 286 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 287 assert.Equal(t, test.expectedPath, r.URL.String()) 288 assert.Equal(t, http.MethodGet, r.Method) 289 w.WriteHeader(test.responseStatus) 290 _, err := w.Write([]byte(test.responseBody)) 291 assert.NoError(t, err) 292 })) 293 client := mockAPIClient(t, mockServer) 294 result, err := client.ListActivePolicyProperties(context.Background(), test.params) 295 if test.withError != nil { 296 test.withError(t, err) 297 return 298 } 299 require.NoError(t, err) 300 assert.Equal(t, test.expectedResponse, result) 301 }) 302 } 303 }