You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by zr...@apache.org on 2021/04/14 02:30:43 UTC

[trafficcontrol] branch master updated: Adds TO client api tests for Division automation (#5735)

This is an automated email from the ASF dual-hosted git repository.

zrhoffman 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 d5cb153  Adds TO client api tests for Division automation (#5735)
d5cb153 is described below

commit d5cb1537d2ee7b4e4ee411cb65520c2cce986fe8
Author: dmohan001c <de...@comcast.com>
AuthorDate: Wed Apr 14 08:00:30 2021 +0530

    Adds TO client api tests for Division automation (#5735)
    
    * added test case for CacheGroup Pagination functionalites
    
    * added testcase for division pagination support
    
    * formatted code using go fmt:
    
    * merged two functions and updated the signature
    
    * added validation for getdivisionbyparams
    
    * added tests for sort desc and invalid scenarios validation
    
    * formatted the code using gofmt
---
 traffic_ops/testing/api/v4/divisions_test.go | 130 ++++++++++++++++++++++++++-
 traffic_ops/v4-client/division.go            |   8 +-
 2 files changed, 135 insertions(+), 3 deletions(-)

diff --git a/traffic_ops/testing/api/v4/divisions_test.go b/traffic_ops/testing/api/v4/divisions_test.go
index 5c935c5..5d3e7e4 100644
--- a/traffic_ops/testing/api/v4/divisions_test.go
+++ b/traffic_ops/testing/api/v4/divisions_test.go
@@ -17,6 +17,8 @@ package v4
 
 import (
 	"net/http"
+	"net/url"
+	"reflect"
 	"sort"
 	"strings"
 	"testing"
@@ -37,6 +39,7 @@ func TestDivisions(t *testing.T) {
 		header.Set(rfc.IfModifiedSince, time)
 		header.Set(rfc.IfUnmodifiedSince, time)
 		SortTestDivisions(t)
+		SortTestDivisionDesc(t)
 		UpdateTestDivisions(t)
 		UpdateTestDivisionsWithHeaders(t, header)
 		GetTestDivisionsIMSAfterChange(t, header)
@@ -45,6 +48,10 @@ func TestDivisions(t *testing.T) {
 		etag := rfc.ETag(currentTime)
 		header.Set(rfc.IfMatch, etag)
 		UpdateTestDivisionsWithHeaders(t, header)
+		VerifyPaginationSupportDivision(t)
+		GetDivisionByInvalidId(t)
+		GetDivisionByInvalidName(t)
+		DeleteTestDivisionsByInvalidId(t)
 	})
 }
 
@@ -152,7 +159,7 @@ func CreateTestDivisions(t *testing.T) {
 func SortTestDivisions(t *testing.T) {
 	var header http.Header
 	var sortedList []string
-	resp, _, err := TOSession.GetDivisions(header)
+	resp, _, err := TOSession.GetDivisions(nil, header)
 	if err != nil {
 		t.Fatalf("Expected no error, but got %v", err.Error())
 	}
@@ -168,6 +175,36 @@ func SortTestDivisions(t *testing.T) {
 	}
 }
 
+func SortTestDivisionDesc(t *testing.T) {
+
+	var header http.Header
+	respAsc, _, err1 := TOSession.GetDivisions(nil, header)
+	params := url.Values{}
+	params.Set("sortOrder", "desc")
+	respDesc, _, err2 := TOSession.GetDivisions(params, header)
+
+	if err1 != nil {
+		t.Errorf("Expected no error, but got error in Division Ascending %v", err1)
+	}
+	if err2 != nil {
+		t.Errorf("Expected no error, but got error in Division Descending %v", err2)
+	}
+
+	if len(respAsc) > 0 && len(respDesc) > 0 {
+		// reverse the descending-sorted response and compare it to the ascending-sorted one
+		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 != "" {
+			if !reflect.DeepEqual(respDesc[0].Name, respAsc[0].Name) {
+				t.Errorf("Division responses are not equal after reversal: %s - %s", *&respDesc[0].Name, *&respAsc[0].Name)
+			}
+		}
+	} else {
+		t.Errorf("No Response returned from GET Delivery Service using SortOrder")
+	}
+}
+
 func UpdateTestDivisions(t *testing.T) {
 
 	firstDivision := testData.Divisions[0]
@@ -239,3 +276,94 @@ func DeleteTestDivisions(t *testing.T) {
 		}
 	}
 }
+
+func VerifyPaginationSupportDivision(t *testing.T) {
+	qparams := url.Values{}
+	qparams.Set("orderby", "id")
+	divisions, _, err := TOSession.GetDivisions(qparams, nil)
+	if err != nil {
+		t.Fatalf("cannot GET Divisions: %v", err)
+	}
+
+	qparams = url.Values{}
+	qparams.Set("orderby", "id")
+	qparams.Set("limit", "1")
+	divisionsWithLimit, _, err := TOSession.GetDivisions(qparams, nil)
+	if !reflect.DeepEqual(divisions[:1], divisionsWithLimit) {
+		t.Error("expected GET Divisions with limit = 1 to return first result")
+	}
+
+	qparams = url.Values{}
+	qparams.Set("orderby", "id")
+	qparams.Set("limit", "1")
+	qparams.Set("offset", "1")
+	divisionsWithOffset, _, err := TOSession.GetDivisions(qparams, nil)
+	if !reflect.DeepEqual(divisions[1:2], divisionsWithOffset) {
+		t.Error("expected GET Divisions with limit = 1, offset = 1 to return second result")
+	}
+
+	qparams = url.Values{}
+	qparams.Set("orderby", "id")
+	qparams.Set("limit", "1")
+	qparams.Set("page", "2")
+	divisionsWithPage, _, err := TOSession.GetDivisions(qparams, nil)
+	if !reflect.DeepEqual(divisions[1:2], divisionsWithPage) {
+		t.Error("expected GET Divisions with limit = 1, page = 2 to return second result")
+	}
+
+	qparams = url.Values{}
+	qparams.Set("limit", "-2")
+	_, _, err = TOSession.GetDivisions(qparams, nil)
+	if err == nil {
+		t.Error("expected GET Divisions to return an error when limit is not bigger than -1")
+	} else if !strings.Contains(err.Error(), "must be bigger than -1") {
+		t.Errorf("expected GET Divisions to return an error for limit is not bigger than -1, actual error: " + err.Error())
+	}
+
+	qparams = url.Values{}
+	qparams.Set("limit", "1")
+	qparams.Set("offset", "0")
+	_, _, err = TOSession.GetDivisions(qparams, nil)
+	if err == nil {
+		t.Error("expected GET Divisions to return an error when offset is not a positive integer")
+	} else if !strings.Contains(err.Error(), "must be a positive integer") {
+		t.Errorf("expected GET Divisions to return an error for offset is not a positive integer, actual error: " + err.Error())
+	}
+
+	qparams = url.Values{}
+	qparams.Set("limit", "1")
+	qparams.Set("page", "0")
+	_, _, err = TOSession.GetDivisions(qparams, nil)
+	if err == nil {
+		t.Error("expected GET Divisions to return an error when page is not a positive integer")
+	} else if !strings.Contains(err.Error(), "must be a positive integer") {
+		t.Errorf("expected GET Divisions to return an error for page is not a positive integer, actual error: " + err.Error())
+	}
+}
+
+func GetDivisionByInvalidId(t *testing.T) {
+	resp, _, err := TOSession.GetDivisionByID(10000, nil)
+	if err != nil {
+		t.Errorf("Error!! Getting Division by Invalid ID %v", err)
+	}
+	if len(resp) >= 1 {
+		t.Errorf("Error!! Invalid ID shouldn't have any response %v Error %v", resp, err)
+	}
+}
+
+func GetDivisionByInvalidName(t *testing.T) {
+	resp, _, err := TOSession.GetDivisionByName("abcd", nil)
+	if err != nil {
+		t.Errorf("Error!! Getting Division by Invalid Name %v", err)
+	}
+	if len(resp) >= 1 {
+		t.Errorf("Error!! Invalid Name shouldn't have any response %v Error %v", resp, err)
+	}
+}
+
+func DeleteTestDivisionsByInvalidId(t *testing.T) {
+	delResp, _, err := TOSession.DeleteDivision(10000)
+	if err == nil {
+		t.Errorf("cannot DELETE Division by Invalid ID: %v - %v", err, delResp)
+	}
+}
diff --git a/traffic_ops/v4-client/division.go b/traffic_ops/v4-client/division.go
index ebee40f..5b1fdf2 100644
--- a/traffic_ops/v4-client/division.go
+++ b/traffic_ops/v4-client/division.go
@@ -46,9 +46,13 @@ func (to *Session) UpdateDivision(id int, division tc.Division, header http.Head
 }
 
 // GetDivisions returns all Divisions in Traffic Ops.
-func (to *Session) GetDivisions(header http.Header) ([]tc.Division, toclientlib.ReqInf, error) {
+func (to *Session) GetDivisions(params url.Values, header http.Header) ([]tc.Division, toclientlib.ReqInf, error) {
+	uri := APIDivisions
+	if params != nil {
+		uri += "?" + params.Encode()
+	}
 	var data tc.DivisionsResponse
-	reqInf, err := to.get(APIDivisions, header, &data)
+	reqInf, err := to.get(uri, header, &data)
 	return data.Response, reqInf, err
 }