dmohan001c commented on a change in pull request #5717:
URL: https://github.com/apache/trafficcontrol/pull/5717#discussion_r610509919



##########
File path: traffic_ops/testing/api/v4/deliveryservices_test.go
##########
@@ -1238,6 +1254,272 @@ func VerifyPaginationSupportDS(t *testing.T) {
        }
 }
 
+func GetDeliveryServiceByCdn(t *testing.T) {
+
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               if firstDS.CDNID == nil && firstDS.CDNName != nil {
+                       cdns, _, err := 
TOSession.GetCDNByNameWithHdr(*firstDS.CDNName, nil)
+                       if err != nil {
+                               t.Fatalf("Error in Getting CDN by Name: %v", 
err)
+                       }
+                       if len(cdns) == 0 {
+                               t.Fatalf("no CDN named %v" + *firstDS.CDNName)
+                       }
+                       firstDS.CDNID = &cdns[0].ID
+               }
+               resp, _, err := 
TOSession.GetDeliveryServicesByCDNIDWithHdr(*firstDS.CDNID, nil)
+               if err != nil {
+                       t.Fatalf("Error in Getting DeliveryServices by CDN ID: 
%v - %v", err, resp)
+               }
+               if len(resp) == 0 {
+                       t.Errorf("No delivery service available for the CDN 
%v", *firstDS.CDNName)
+               } else {
+                       if *resp[0].CDNName != *firstDS.CDNName {
+                               t.Fatalf("CDN Name expected: %s, actual: %s", 
*firstDS.CDNName, *resp[0].CDNName)
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByInvalidCdn(t *testing.T) {
+       resp, _, err := TOSession.GetDeliveryServicesByCDNIDWithHdr(10000, nil)
+       if err != nil {
+               t.Fatalf("Error!! Getting CDN by Invalid ID %v", err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid CDN shouldn't have any response %v 
Error %v", resp, err)
+       }
+}
+
+func GetDeliveryServiceByLogsEnabled(t *testing.T) {
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               qparams := url.Values{}
+               qparams.Set("logsEnabled", 
strconv.FormatBool(*firstDS.LogsEnabled))
+               resp, _, err := TOSession.GetDeliveryServicesV4(nil, qparams)
+               if err != nil {
+                       t.Fatalf("Error in Getting deliveryservice by 
logsEnabled: %v - %v", err, resp)
+               }
+               if len(resp) == 0 {
+                       t.Errorf("No delivery service available for the Logs 
Enabled %v", *firstDS.LogsEnabled)
+               } else {
+                       if *resp[0].LogsEnabled != *firstDS.LogsEnabled {
+                               t.Fatalf("name expected: %t, actual: %t", 
*firstDS.LogsEnabled, *resp[0].LogsEnabled)
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByValidProfile(t *testing.T) {
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               if firstDS.ProfileID == nil && firstDS.ProfileName != nil {
+                       profile, _, err := 
TOSession.GetProfileByNameWithHdr(*firstDS.ProfileName, nil)
+                       if err != nil {
+                               t.Fatalf("Error in Getting Profile by Name: 
%v", err)
+                       }
+                       if len(profile) == 0 {
+                               t.Fatalf("no Profile named %v" + 
*firstDS.ProfileName)
+                       }
+                       firstDS.ProfileID = &profile[0].ID
+                       qparams := url.Values{}
+                       qparams.Set("profile", strconv.Itoa(*firstDS.ProfileID))
+                       resp, _, err := TOSession.GetDeliveryServicesV4(nil, 
qparams)
+                       if err != nil {
+                               t.Fatalf("Error in Getting deliveryservice by 
Profile: %v - %v", err, resp)
+                       }
+                       if len(resp) == 0 {
+                               t.Errorf("No delivery service available for the 
Profile %v", *firstDS.ProfileName)
+                       } else {
+                               if *resp[0].ProfileName != *firstDS.ProfileName 
{
+                                       t.Fatalf("Profile name expected: %s, 
actual: %s", *firstDS.ProfileName, *resp[0].ProfileName)
+                               }
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByInvalidProfile(t *testing.T) {
+       qparams := url.Values{}
+       qparams.Set("profile", "10000")
+       resp, _, err := TOSession.GetDeliveryServicesV4(nil, qparams)
+       if err != nil {
+               t.Fatalf("Error!! Getting deliveryservice by Invalid Profile ID 
%v", err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid Profile shouldn't have any response 
%v Error %v", resp, err)
+       }
+}
+
+func GetDeliveryServiceByValidTenant(t *testing.T) {
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               if firstDS.TenantID == nil && firstDS.Tenant != nil {
+                       tenant, _, err := 
TOSession.TenantByNameWithHdr(*firstDS.Tenant, nil)
+                       if err != nil {
+                               t.Fatalf("Error in Getting Tenant by Name: %v", 
err)
+                       }
+                       if tenant == nil {
+                               t.Fatalf("no Tenant named %v" + *firstDS.Tenant)
+                       }
+                       firstDS.TenantID = &tenant.ID
+                       qparams := url.Values{}
+                       qparams.Set("tenant", strconv.Itoa(*firstDS.TenantID))
+                       resp, _, err := TOSession.GetDeliveryServicesV4(nil, 
qparams)
+                       if err != nil {
+                               t.Fatalf("Error in Getting Deliveryservice by 
Tenant:%v - %v", err, resp)
+                       }
+                       if len(resp) == 0 {
+                               t.Errorf("No delivery service available for the 
Tenant %v", *firstDS.CDNName)
+                       } else {
+                               if *resp[0].Tenant != *firstDS.Tenant {
+                                       t.Fatalf("name expected: %s, actual: 
%s", *firstDS.Tenant, *resp[0].Tenant)
+                               }
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByInvalidTenant(t *testing.T) {
+       qparams := url.Values{}
+       qparams.Set("tenant", "10000")
+       resp, _, err := TOSession.GetDeliveryServicesV4(nil, qparams)
+       if err != nil {
+               t.Fatalf("Error!! Getting Deliveryservice by Invalid Tenant ID 
%v", err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid Tenant shouldn't have any response %v 
Error %v", resp, err)
+       }
+}
+
+func GetDeliveryServiceByValidType(t *testing.T) {
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               if firstDS.TypeID == nil && firstDS.Type != nil {
+                       ty, _, err := 
TOSession.GetTypeByNameWithHdr(firstDS.Type.String(), nil)
+                       if err != nil {
+                               t.Fatalf("Error in Getting Type by Name: %v", 
err)
+                       }
+                       if len(ty) == 0 {
+                               t.Fatalf("no Type named %v" + 
firstDS.Type.String())
+                       }
+                       firstDS.TypeID = &ty[0].ID
+                       qparams := url.Values{}
+                       qparams.Set("type", strconv.Itoa(*firstDS.TypeID))
+                       resp, _, err := TOSession.GetDeliveryServicesV4(nil, 
qparams)
+                       if err != nil {
+                               t.Fatalf("Error in Getting Deliveryservice by 
Type:%v - %v", err, resp)
+                       }
+                       if len(resp) == 0 {
+                               t.Errorf("No delivery service available for the 
Type %v", *firstDS.CDNName)
+                       } else {
+                               if *resp[0].Type != *firstDS.Type {
+                                       t.Fatalf("Type expected: %s, actual: 
%s", *firstDS.Type, *resp[0].Type)
+                               }
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByInvalidType(t *testing.T) {
+       qparams := url.Values{}
+       qparams.Set("type", "10000")
+       resp, _, err := TOSession.GetDeliveryServicesV4(nil, qparams)
+       if err != nil {
+               t.Fatalf("Error!! Getting Deliveryservice by Invalid Type ID 
%v", err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid Type shouldn't have any response %v 
Error %v", resp, err)
+       }
+}
+
+func GetDeliveryServiceByInvalidAccessibleTo(t *testing.T) {
+       qparams := url.Values{}
+       qparams.Set("accessibleTo", "10000")
+       resp, _, err := TOSession.GetDeliveryServicesV4(nil, qparams)
+       if err != nil {
+               t.Fatalf("Error!! Getting Deliveryservice by Invalid 
AccessibleTo %v", err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid AccessibleTo shouldn't have any 
response %v Error %v", resp, err)
+       }
+}
+
+func GetDeliveryServiceByValidXmlId(t *testing.T) {
+       if len(testData.DeliveryServices) > 0 {
+               firstDS := testData.DeliveryServices[0]
+
+               resp, _, err := 
TOSession.GetDeliveryServiceByXMLIDNullableWithHdr(*firstDS.XMLID, nil)
+               if err != nil {
+                       t.Fatalf("Error in Getting DeliveryServices by XML ID: 
%v - %v", err, resp)
+               }
+               if len(resp) == 0 {
+                       t.Errorf("No delivery service available for the XML ID 
%v", *firstDS.XMLID)
+               } else {
+                       if *resp[0].XMLID != *firstDS.XMLID {
+                               t.Fatalf("Delivery Service Name expected: %s, 
actual: %s", *firstDS.XMLID, *resp[0].XMLID)
+                       }
+               }
+       }
+}
+
+func GetDeliveryServiceByInvalidXmlId(t *testing.T) {
+       resp, _, err := 
TOSession.GetDeliveryServiceByXMLIDNullableWithHdr("test", nil)
+       if err != nil {
+               t.Fatalf("Error!! Getting Delivery service by Invalid ID %v", 
err)
+       }
+       if len(resp) >= 1 {
+               t.Fatalf("Error!! Invalid Xml Id shouldn't have any response %v 
Error %v", resp, err)
+       }
+}
+
+func SortTestDeliveryServices(t *testing.T) {
+       var header http.Header
+       var sortedList []string
+       resp, _, err := TOSession.GetDeliveryServicesV4(header, nil)
+       if err != nil {
+               t.Fatalf("Expected no error, but got %v", err.Error())
+       }
+       for i, _ := range resp {
+               sortedList = append(sortedList, *resp[i].XMLID)
+       }
+
+       res := sort.SliceIsSorted(sortedList, func(p, q int) bool {
+               return sortedList[p] < sortedList[q]
+       })
+       if res != true {
+               t.Errorf("list is not sorted by their XML Id: %v", sortedList)
+       }
+}
+
+func SortTestDeliveryServicesDesc(t *testing.T) {
+
+       var header http.Header
+       respAsc, _, err1 := TOSession.GetDeliveryServicesV4(header, nil)
+       params := url.Values{}
+       params.Set("sortOrder", "desc")
+       respDesc, _, err2 := TOSession.GetDeliveryServicesV4(header, params)
+
+       if err1 != nil {
+               t.Fatalf("Expected no error, but got error in DS Ascending %v", 
err1.Error())
+       }
+       if err2 != nil {
+               t.Fatalf("Expected no error, but got error in DS Descending 
%v", err2.Error())
+       }
+       // 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 !reflect.DeepEqual(respDesc[0].XMLID, respAsc[0].XMLID) {

Review comment:
       Handled nil values.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to