This is an automated email from the ASF dual-hosted git repository.
ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
The following commit(s) were added to refs/heads/master by this push:
new 07344f0 Add TO Client API for Service Categories Automation (#5885)
07344f0 is described below
commit 07344f066cdf665cb51998e54f9a4d61aa6ea62b
Author: dmohan001c <[email protected]>
AuthorDate: Wed Jun 2 05:37:36 2021 +0530
Add TO Client API for Service Categories Automation (#5885)
* added test case for CacheGroup Pagination functionalites
* Update cachegroups_test.go
* add pagination tests for physical location
* added tests for Service categories
* added error variable in the errorf printing
---
.../testing/api/v4/servicecategories_test.go | 172 +++++++++++++++++++++
1 file changed, 172 insertions(+)
diff --git a/traffic_ops/testing/api/v4/servicecategories_test.go
b/traffic_ops/testing/api/v4/servicecategories_test.go
index 6603657..a4f84d8 100644
--- a/traffic_ops/testing/api/v4/servicecategories_test.go
+++ b/traffic_ops/testing/api/v4/servicecategories_test.go
@@ -17,6 +17,8 @@ package v4
import (
"net/http"
+ "net/url"
+ "reflect"
"sort"
"testing"
"time"
@@ -44,6 +46,12 @@ func TestServiceCategories(t *testing.T) {
etag := rfc.ETag(currentTime)
header.Set(rfc.IfMatch, etag)
UpdateTestServiceCategoriesWithHeaders(t, header)
+ GetTestServiceCategoriesByInvalidName(t)
+ VerifyPaginationSupportServiceCategories(t)
+ SortTestServiceCategoriesDesc(t)
+ CreateTestServiceCategoriesAlreadyExist(t)
+ CreateTestServiceCategoriesInvalidName(t)
+ DeleteTestServiceCategoriesInvalidName(t)
})
}
@@ -104,6 +112,34 @@ func CreateTestServiceCategories(t *testing.T) {
}
}
+func CreateTestServiceCategoriesAlreadyExist(t *testing.T) {
+ firstServiceCategory := tc.ServiceCategory{}
+ if len(testData.ServiceCategories) > 0 {
+ firstServiceCategory = testData.ServiceCategories[0]
+ } else {
+ t.Fatalf("cannot CREATE DUPLICATE Service Category, test data
does not have service categories")
+ }
+ resp, reqInf, err :=
TOSession.CreateServiceCategory(firstServiceCategory, client.RequestOptions{})
+ if err == nil {
+ t.Errorf("Expected service_category name already exists. but
got: %v - alerts: %+v", err, resp.Alerts)
+ }
+ if reqInf.StatusCode != http.StatusBadRequest {
+ t.Fatalf("Expected 400 status code, got %v", reqInf.StatusCode)
+ }
+}
+
+func CreateTestServiceCategoriesInvalidName(t *testing.T) {
+ firstServiceCategory := tc.ServiceCategory{}
+ firstServiceCategory.Name = ""
+ resp, reqInf, err :=
TOSession.CreateServiceCategory(firstServiceCategory, client.RequestOptions{})
+ if err == nil {
+ t.Errorf("Expected 'name' cannot be blanks. but got: %v -
alerts: %+v", err, resp.Alerts)
+ }
+ if reqInf.StatusCode != http.StatusBadRequest {
+ t.Fatalf("Expected 400 status code, got %v", reqInf.StatusCode)
+ }
+}
+
func GetTestServiceCategories(t *testing.T) {
opts := client.NewRequestOptions()
for _, sc := range testData.ServiceCategories {
@@ -233,3 +269,139 @@ func DeleteTestServiceCategories(t *testing.T) {
}
}
}
+
+func DeleteTestServiceCategoriesInvalidName(t *testing.T) {
+
+ delResp, reqInf, err := TOSession.DeleteServiceCategory("invalid",
client.RequestOptions{})
+ if err == nil {
+ t.Errorf("Expected no serviceCategory with that key found but
got: %v - alerts: %+v", err, delResp.Alerts)
+ }
+ if reqInf.StatusCode != http.StatusNotFound {
+ t.Fatalf("Expected 404 status code, got %v", reqInf.StatusCode)
+ }
+}
+
+func GetTestServiceCategoriesByInvalidName(t *testing.T) {
+ opts := client.NewRequestOptions()
+ opts.QueryParameters.Set("name", "invalid")
+ resp, _, _ := TOSession.GetServiceCategories(opts)
+ if len(resp.Response) > 0 {
+ t.Errorf("Expected 0 response, but got many %v", resp)
+ }
+}
+
+func VerifyPaginationSupportServiceCategories(t *testing.T) {
+ opts := client.NewRequestOptions()
+ opts.QueryParameters.Set("orderby", "id")
+ resp, _, err := TOSession.GetServiceCategories(opts)
+ if err != nil {
+ t.Fatalf("cannot get Service Categories: %v - alerts: %+v",
err, resp.Alerts)
+ }
+ serviceCategories := resp.Response
+ if len(serviceCategories) < 2 {
+ t.Fatalf("Need at least 2 Service Categories in Traffic Ops to
test pagination support, found: %d", len(serviceCategories))
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("orderby", "id")
+ opts.QueryParameters.Set("limit", "1")
+ serviceCategoriesWithLimit, _, err :=
TOSession.GetServiceCategories(opts)
+ if err == nil {
+ if !reflect.DeepEqual(serviceCategories[:1],
serviceCategoriesWithLimit.Response) {
+ t.Error("expected GET Service Categories with limit = 1
to return first result")
+ }
+ } else {
+ t.Errorf("Error in getting Service Categories by limit %v", err)
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("orderby", "id")
+ opts.QueryParameters.Set("limit", "1")
+ opts.QueryParameters.Set("offset", "1")
+ serviceCategoriesWithOffset, _, err :=
TOSession.GetServiceCategories(opts)
+ if err == nil {
+ if !reflect.DeepEqual(serviceCategories[1:2],
serviceCategoriesWithOffset.Response) {
+ t.Error("expected GET Service Categories with limit =
1, offset = 1 to return second result")
+ }
+ } else {
+ t.Errorf("Error in getting Service Categories by limit and
offset %v", err)
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("orderby", "id")
+ opts.QueryParameters.Set("limit", "1")
+ opts.QueryParameters.Set("page", "2")
+ serviceCategoriesWithPage, _, err :=
TOSession.GetServiceCategories(opts)
+ if err == nil {
+ if !reflect.DeepEqual(serviceCategories[1:2],
serviceCategoriesWithPage.Response) {
+ t.Error("expected GET Service Categories with limit =
1, page = 2 to return second result")
+ }
+ } else {
+ t.Errorf("Error in getting Service Categories by limit and page
%v", err)
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("limit", "-2")
+ resp, _, err = TOSession.GetServiceCategories(opts)
+ if err == nil {
+ t.Error("expected GET Service Categories 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 Service Categories to return an error
for limit is not bigger than -1, actual error: " + err.Error())
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("limit", "1")
+ opts.QueryParameters.Set("offset", "0")
+ resp, _, err = TOSession.GetServiceCategories(opts)
+ if err == nil {
+ t.Error("expected GET Service Categories 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 Service Categories to return an error
for offset is not a positive integer, actual error: " + err.Error())
+ }
+
+ opts.QueryParameters = url.Values{}
+ opts.QueryParameters.Set("limit", "1")
+ opts.QueryParameters.Set("page", "0")
+ resp, _, err = TOSession.GetServiceCategories(opts)
+ if err == nil {
+ t.Error("expected GET Service Categories 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 Service Categories to return an error
for page is not a positive integer, actual error: " + err.Error())
+ }
+}
+
+func SortTestServiceCategoriesDesc(t *testing.T) {
+ resp, _, err := TOSession.GetServiceCategories(client.RequestOptions{})
+ if err != nil {
+ t.Errorf("Expected no error, but got error in Service
Categories with default ordering: %v - alerts: %+v", err, resp.Alerts)
+ }
+ respAsc := resp.Response
+ if len(respAsc) < 1 {
+ t.Fatal("Need at least one Service Categories in Traffic Ops to
test Service Categories sort ordering")
+ }
+
+ opts := client.NewRequestOptions()
+ opts.QueryParameters.Set("sortOrder", "desc")
+ resp, _, err = TOSession.GetServiceCategories(opts)
+ if err != nil {
+ t.Errorf("Expected no error, but got error in Service
Categories with Descending ordering: %v - alerts: %+v", err, resp.Alerts)
+ }
+ respDesc := resp.Response
+ if len(respDesc) < 1 {
+ t.Fatal("Need at least one Service Categories in Traffic Ops to
test Service Categories sort ordering")
+ }
+
+ if len(respAsc) != len(respDesc) {
+ t.Fatalf("Traffic Ops returned %d Service Categories using
default sort order, but %d Service Categories when sort order was explicitly
set to descending", len(respAsc), len(respDesc))
+ }
+
+ // reverse the descending-sorted response and compare it to the
ascending-sorted one
+ // TODO ensure at least two in each slice? A list of length one is
+ // trivially sorted both ascending and descending.
+ for start, end := 0, len(respDesc)-1; start < end; start, end =
start+1, end-1 {
+ respDesc[start], respDesc[end] = respDesc[end], respDesc[start]
+ }
+ if respDesc[0].Name != respAsc[0].Name {
+ t.Errorf("Service Categories responses are not equal after
reversal: Asc: %s - Desc: %s", respDesc[0].Name, respAsc[0].Name)
+ }
+}