github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudwrapper/capacity_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/require" 11 "github.com/tj/assert" 12 ) 13 14 func TestListCapacity(t *testing.T) { 15 tests := map[string]struct { 16 request ListCapacitiesRequest 17 responseStatus int 18 responseBody string 19 expectedPath string 20 expectedResponse *ListCapacitiesResponse 21 withError func(*testing.T, error) 22 }{ 23 "200 OK": { 24 request: ListCapacitiesRequest{ 25 ContractIDs: nil, 26 }, 27 responseStatus: 200, 28 expectedPath: "/cloud-wrapper/v1/capacity", 29 responseBody: ` 30 { 31 "capacities": [ 32 { 33 "locationId": 1, 34 "locationName": "US East", 35 "contractId": "A-BCDEFG", 36 "type": "MEDIA", 37 "approvedCapacity": { 38 "value": 2000, 39 "unit": "GB" 40 }, 41 "assignedCapacity": { 42 "value": 2, 43 "unit": "GB" 44 }, 45 "unassignedCapacity": { 46 "value": 1998, 47 "unit": "GB" 48 } 49 } 50 ] 51 }`, 52 expectedResponse: &ListCapacitiesResponse{ 53 Capacities: []LocationCapacity{ 54 { 55 LocationID: 1, 56 LocationName: "US East", 57 ContractID: "A-BCDEFG", 58 Type: CapacityTypeMedia, 59 ApprovedCapacity: Capacity{ 60 Value: 2000, 61 Unit: "GB", 62 }, 63 AssignedCapacity: Capacity{ 64 Value: 2, 65 Unit: "GB", 66 }, 67 UnassignedCapacity: Capacity{ 68 Value: 1998, 69 Unit: "GB", 70 }, 71 }, 72 }, 73 }, 74 }, 75 "200 OK with contracts": { 76 request: ListCapacitiesRequest{ 77 ContractIDs: []string{"A-BCDEF", "B-CDEFG"}, 78 }, 79 responseStatus: 200, 80 expectedPath: "/cloud-wrapper/v1/capacity?contractIds=A-BCDEF&contractIds=B-CDEFG", 81 responseBody: ` 82 { 83 "capacities": [ 84 { 85 "locationId": 1, 86 "locationName": "US East", 87 "contractId": "A-BCDEFG", 88 "type": "WEB_ENHANCED_TLS", 89 "approvedCapacity": { 90 "value": 10, 91 "unit": "TB" 92 }, 93 "assignedCapacity": { 94 "value": 1, 95 "unit": "TB" 96 }, 97 "unassignedCapacity": { 98 "value": 9, 99 "unit": "TB" 100 } 101 } 102 ] 103 }`, 104 expectedResponse: &ListCapacitiesResponse{ 105 Capacities: []LocationCapacity{ 106 { 107 LocationID: 1, 108 LocationName: "US East", 109 ContractID: "A-BCDEFG", 110 Type: CapacityTypeWebEnhancedTLS, 111 ApprovedCapacity: Capacity{ 112 Value: 10, 113 Unit: UnitTB, 114 }, 115 AssignedCapacity: Capacity{ 116 Value: 1, 117 Unit: UnitTB, 118 }, 119 UnassignedCapacity: Capacity{ 120 Value: 9, 121 Unit: UnitTB, 122 }, 123 }, 124 }, 125 }, 126 }, 127 "401 not authorized": { 128 request: ListCapacitiesRequest{}, 129 responseStatus: 401, 130 responseBody: `{ 131 "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny", 132 "title": "Not authorized", 133 "status": 401, 134 "detail": "The signature does not match", 135 "instance": "https://instance.luna-dev.akamaiapis.net/cloud-wrapper/v1/capacity", 136 "method": "GET", 137 "serverIp": "2.2.2.2", 138 "clientIp": "3.3.3.3", 139 "requestId": "a7a7a7a7a7a", 140 "requestTime": "2023-05-22T10:05:22Z" 141 }`, 142 expectedPath: "/cloud-wrapper/v1/capacity", 143 expectedResponse: nil, 144 withError: func(t *testing.T, e error) { 145 err := Error{ 146 Type: "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny", 147 Title: "Not authorized", 148 Instance: "https://instance.luna-dev.akamaiapis.net/cloud-wrapper/v1/capacity", 149 Status: 401, 150 Detail: "The signature does not match", 151 Method: "GET", 152 ServerIP: "2.2.2.2", 153 ClientIP: "3.3.3.3", 154 RequestID: "a7a7a7a7a7a", 155 RequestTime: "2023-05-22T10:05:22Z", 156 } 157 assert.Equal(t, true, err.Is(e)) 158 }, 159 }, 160 "500": { 161 request: ListCapacitiesRequest{}, 162 responseStatus: 500, 163 expectedPath: "/cloud-wrapper/v1/capacity", 164 expectedResponse: nil, 165 responseBody: `{ 166 "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 167 "title": "Server Error", 168 "status": 500, 169 "instance": "https://instance.luna-dev.akamaiapis.net/cloud-wrapper/v1/capacity", 170 "method": "GET", 171 "serverIp": "2.2.2.2", 172 "clientIp": "3.3.3.3", 173 "requestId": "a7a7a7a7a7a", 174 "requestTime": "2021-12-06T10:27:11Z" 175 }`, 176 withError: func(t *testing.T, err error) { 177 want := &Error{ 178 Type: "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 179 Title: "Server Error", 180 Status: 500, 181 Instance: "https://instance.luna-dev.akamaiapis.net/cloud-wrapper/v1/capacity", 182 Method: "GET", 183 ServerIP: "2.2.2.2", 184 ClientIP: "3.3.3.3", 185 RequestID: "a7a7a7a7a7a", 186 RequestTime: "2021-12-06T10:27:11Z", 187 } 188 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 189 }, 190 }, 191 } 192 193 for name, test := range tests { 194 t.Run(name, func(t *testing.T) { 195 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 196 assert.Equal(t, test.expectedPath, r.URL.String()) 197 assert.Equal(t, http.MethodGet, r.Method) 198 w.WriteHeader(test.responseStatus) 199 _, err := w.Write([]byte(test.responseBody)) 200 assert.NoError(t, err) 201 })) 202 client := mockAPIClient(t, mockServer) 203 result, err := client.ListCapacities(context.Background(), test.request) 204 if test.withError != nil { 205 test.withError(t, err) 206 return 207 } 208 require.NoError(t, err) 209 assert.Equal(t, test.expectedResponse, result) 210 }) 211 } 212 }