diff --git a/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go b/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go index b22a846a5c..2358b2e68f 100644 --- a/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go +++ b/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go @@ -19,6 +19,7 @@ import ( "fmt" "net/http" "net/url" + "reflect" "strconv" "strings" "testing" @@ -41,6 +42,7 @@ func TestDeliveryServicesRequiredCapabilities(t *testing.T) { header = make(map[string][]string) header.Set(rfc.IfModifiedSince, time) GetTestDeliveryServicesRequiredCapabilitiesIMSAfterChange(t, header) + GetTestPaginationSupportDsrc(t) }) } @@ -645,3 +647,71 @@ func helperGetDeliveryServiceID(t *testing.T, xmlID *string) *int { } return ds.Response[0].ID } + +func GetTestPaginationSupportDsrc(t *testing.T) { + opts := client.NewRequestOptions() + opts.QueryParameters.Set("orderby", "requiredCapability") + resp, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err != nil { + t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities: %v - alerts: %+v", err, resp.Alerts) + } + dsrc := resp.Response + if len(dsrc) < 3 { + t.Fatalf("Need at least 3 DeliveryServicesRequiredCapabilities in Traffic Ops to test pagination support, found: %d", len(dsrc)) + } + opts.QueryParameters.Set("limit", "1") + dsrcWithLimit, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err != nil { + t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit: %v - alerts: %+v", err, dsrcWithLimit.Alerts) + } + if !reflect.DeepEqual(dsrc[:1], dsrcWithLimit.Response) { + t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1 to return first result") + } + + opts.QueryParameters.Set("offset", "1") + dsrcWithOffset, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err != nil { + t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit and Offset: %v - alerts: %+v", err, dsrcWithOffset.Alerts) + } + if !reflect.DeepEqual(dsrc[1:2], dsrcWithOffset.Response) { + t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1, offset = 1 to return second result") + } + + opts.QueryParameters.Del("offset") + opts.QueryParameters.Set("page", "2") + dsrcWithPage, _, err := TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err != nil { + t.Fatalf("cannot Get DeliveryServicesRequiredCapabilities with Limit and Page: %v - alerts: %+v", err, dsrcWithPage.Alerts) + } + if !reflect.DeepEqual(dsrc[1:2], dsrcWithPage.Response) { + t.Error("expected GET DeliveryServicesRequiredCapabilities with limit = 1, page = 2 to return second result") + } + + opts.QueryParameters = url.Values{} + opts.QueryParameters.Set("limit", "-2") + resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err == nil { + t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when limit is not bigger than -1") + } else if !alertsHaveError(resp.Alerts.Alerts, "must be bigger than -1") { + t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for limit is not bigger than -1, actual error: %v - alerts: %+v", err, resp.Alerts) + } + + opts.QueryParameters.Set("limit", "1") + opts.QueryParameters.Set("offset", "0") + resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err == nil { + t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when offset is not a positive integer") + } else if !alertsHaveError(resp.Alerts.Alerts, "must be a positive integer") { + t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for offset is not a positive integer, actual error: %v - alerts: %+v", err, resp.Alerts) + } + + opts.QueryParameters = url.Values{} + opts.QueryParameters.Set("limit", "1") + opts.QueryParameters.Set("page", "0") + resp, _, err = TOSession.GetDeliveryServicesRequiredCapabilities(opts) + if err == nil { + t.Error("expected GET DeliveryServicesRequiredCapabilities to return an error when page is not a positive integer") + } else if !alertsHaveError(resp.Alerts.Alerts, "must be a positive integer") { + t.Errorf("expected GET DeliveryServicesRequiredCapabilities to return an error for page is not a positive integer, actual error: %v - alerts: %+v", err, resp.Alerts) + } +} diff --git a/traffic_ops/testing/api/v4/tc-fixtures.json b/traffic_ops/testing/api/v4/tc-fixtures.json index 8bca7652b2..967f99ba02 100644 --- a/traffic_ops/testing/api/v4/tc-fixtures.json +++ b/traffic_ops/testing/api/v4/tc-fixtures.json @@ -484,7 +484,11 @@ "cdnName": "cdn1", "ccrDnsTtl": 3600, "checkPath": "", - "consistentHashQueryParams": ["fmt", "limit", "somethingelse"], + "consistentHashQueryParams": [ + "fmt", + "limit", + "somethingelse" + ], "deepCachingType": "NEVER", "displayName": "d s 1", "dnsBypassCname": null, @@ -675,7 +679,11 @@ "cdnName": "cdn1", "ccrDnsTtl": 3600, "checkPath": "", - "consistentHashQueryParams": ["a", "b", "c"], + "consistentHashQueryParams": [ + "a", + "b", + "c" + ], "consistentHashRegex": "foo", "deepCachingType": "ALWAYS", "displayName": "ds-test-minor-versions", @@ -1593,25 +1601,25 @@ "dsName": "ds1", "typeName": "HOST_REGEXP", "setNumber": 1, - "pattern" : ".*" + "pattern": ".*" }, { "dsName": "ds1", "typeName": "HOST_REGEXP", "setNumber": 2, - "pattern" : "\\d+" + "pattern": "\\d+" }, { "dsName": "ds2", "typeName": "HOST_REGEXP", "setNumber": 1, - "pattern" : ".*" + "pattern": ".*" }, { "dsName": "ds1", "typeName": "HOST_REGEXP", "setNumber": 3, - "pattern" : "" + "pattern": "" } ], "deliveryservicesRequiredCapabilities": [ @@ -1711,10 +1719,10 @@ "ipAddress": "1.2.3.4", "ip6Address": "dead:beef:cafe::42", "port": 1234, - "Profile" : "ATS_EDGE_TIER_CACHE", + "Profile": "ATS_EDGE_TIER_CACHE", "protocol": "http", "tenant": "tenant1", - "isPrimary" : true + "isPrimary": true }, { "name": "origin2", @@ -4916,15 +4924,22 @@ }, { "cachegroup": "parentCachegroup", - "parents": [0] + "parents": [ + 0 + ] }, { "cachegroup": "secondaryCachegroup", - "parents": [0] + "parents": [ + 0 + ] }, { "cachegroup": "cachegroup3", - "parents": [1, 2] + "parents": [ + 1, + 2 + ] } ] }, @@ -5106,11 +5121,15 @@ }, { "cachegroup": "dtrc2", - "parents": [0] + "parents": [ + 0 + ] }, { "cachegroup": "dtrc3", - "parents": [0] + "parents": [ + 0 + ] } ] }, @@ -5124,11 +5143,15 @@ }, { "cachegroup": "dtrc1", - "parents": [0] + "parents": [ + 0 + ] }, { "cachegroup": "dtrc2", - "parents": [1] + "parents": [ + 1 + ] } ] }, @@ -5142,11 +5165,15 @@ }, { "cachegroup": "dtrc2", - "parents": [0] + "parents": [ + 0 + ] }, { "cachegroup": "dtrc3", - "parents": [0] + "parents": [ + 0 + ] } ] }, @@ -5534,7 +5561,7 @@ "ttl": 2160 }, { - "deliveryService": "ds2", + "deliveryService": "ds2", "regex": "\\/some-path?.+\\.jpg", "startTime": "2100-06-19T13:57:51-06:00", "ttl": 2.1 diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go index 20d26a8530..043495132e 100644 --- a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go +++ b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go @@ -202,6 +202,7 @@ func (rc *RequiredCapability) getCapabilities(h http.Header, tenantIDs []int, us var runSecond bool var results []tc.DeliveryServicesRequiredCapability where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(rc.APIInfo().Params, rc.ParamColumns()) + if len(errs) > 0 { return nil, util.JoinErrs(errs), nil, http.StatusBadRequest, nil }