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 6a7699ed7a Refactor DeliveryService Request Comments test (#6746)
6a7699ed7a is described below

commit 6a7699ed7abdbccf22089cab246139ba68ff3a15
Author: Eric Holguin <[email protected]>
AuthorDate: Tue Apr 26 13:07:33 2022 -0600

    Refactor DeliveryService Request Comments test (#6746)
    
    * Added field to ds request comment data
    
    * Refactor tests
    
    * Separate fields by newline
---
 .../v3/deliveryservice_request_comments_test.go    | 289 +++++++++----------
 traffic_ops/testing/api/v3/tc-fixtures.json        |  12 +-
 .../v4/deliveryservice_request_comments_test.go    | 318 +++++++++------------
 traffic_ops/testing/api/v4/tc-fixtures.json        |  12 +-
 4 files changed, 298 insertions(+), 333 deletions(-)

diff --git 
a/traffic_ops/testing/api/v3/deliveryservice_request_comments_test.go 
b/traffic_ops/testing/api/v3/deliveryservice_request_comments_test.go
index f3aab587d9..51dae401dc 100644
--- a/traffic_ops/testing/api/v3/deliveryservice_request_comments_test.go
+++ b/traffic_ops/testing/api/v3/deliveryservice_request_comments_test.go
@@ -16,6 +16,7 @@ package v3
 */
 
 import (
+       "encoding/json"
        "net/http"
        "sort"
        "testing"
@@ -23,182 +24,176 @@ import (
 
        "github.com/apache/trafficcontrol/lib/go-rfc"
        "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/traffic_ops/testing/api/assert"
+       "github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+       "github.com/apache/trafficcontrol/traffic_ops/toclientlib"
 )
 
 func TestDeliveryServiceRequestComments(t *testing.T) {
        WithObjs(t, []TCObj{CDNs, Types, Parameters, Tenants, 
DeliveryServiceRequests, DeliveryServiceRequestComments}, func() {
-               GetTestDeliveryServiceRequestCommentsIMS(t)
-               currentTime := time.Now().UTC().Add(-5 * time.Second)
-               time := currentTime.Format(time.RFC1123)
-               var header http.Header
-               header = make(map[string][]string)
-               header.Set(rfc.IfUnmodifiedSince, time)
-               header.Set(rfc.IfModifiedSince, time)
-               SortTestDeliveryServiceRequestComments(t)
-               UpdateTestDeliveryServiceRequestComments(t)
-               UpdateTestDeliveryServiceRequestCommentsWithHeaders(t, header)
-               header = make(map[string][]string)
-               etag := rfc.ETag(currentTime)
-               header.Set(rfc.IfMatch, etag)
-               UpdateTestDeliveryServiceRequestCommentsWithHeaders(t, header)
-               GetTestDeliveryServiceRequestComments(t)
-               GetTestDeliveryServiceRequestCommentsIMSAfterChange(t, header)
-       })
-}
-
-func UpdateTestDeliveryServiceRequestCommentsWithHeaders(t *testing.T, header 
http.Header) {
-       comments, _, _ := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(header)
 
-       if len(comments) > 0 {
-               firstComment := comments[0]
-               newFirstCommentValue := "new comment value"
-               firstComment.Value = newFirstCommentValue
-
-               _, reqInf, err := 
TOSession.UpdateDeliveryServiceRequestCommentByIDWithHdr(firstComment.ID, 
firstComment, header)
-               if err == nil {
-                       t.Errorf("expected precondition failed error, but got 
none")
-               }
-               if reqInf.StatusCode != http.StatusPreconditionFailed {
-                       t.Errorf("Expected status code 412, got %v", 
reqInf.StatusCode)
+               currentTime := time.Now().UTC().Add(-15 * time.Second)
+               currentTimeRFC := currentTime.Format(time.RFC1123)
+               tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
+
+               methodTests := utils.V3TestCase{
+                       "GET": {
+                               "NOT MODIFIED when NO CHANGES made": {
+                                       ClientSession:  TOSession,
+                                       RequestHeaders: 
http.Header{rfc.IfModifiedSince: {tomorrow}},
+                                       Expectations:   
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusNotModified)),
+                               },
+                               "OK when VALID request": {
+                                       ClientSession: TOSession,
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                               "OK when VALID ID parameter": {
+                                       EndpointId:    GetDSRequestCommentId(t),
+                                       ClientSession: TOSession,
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), 
utils.ResponseHasLength(1)),
+                               },
+                               "VALIDATE SORT when DEFAULT is ASC ORDER": {
+                                       ClientSession: TOSession,
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), 
validateSortedDSRequestComments()),
+                               },
+                       },
+                       "PUT": {
+                               "OK when VALID request": {
+                                       EndpointId:    GetDSRequestCommentId(t),
+                                       ClientSession: TOSession,
+                                       RequestBody: map[string]interface{}{
+                                               "deliveryServiceRequestId": 
GetDSRequestId(t, "test-ds1")(),
+                                               "value":                    
"updated comment",
+                                       },
+                                       Expectations: 
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                               "PRECONDITION FAILED when updating with 
IF-UNMODIFIED-SINCE Header": {
+                                       EndpointId:     
GetDSRequestCommentId(t),
+                                       ClientSession:  TOSession,
+                                       RequestHeaders: 
http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}},
+                                       RequestBody:    
map[string]interface{}{},
+                                       Expectations:   
utils.CkRequest(utils.HasError(), 
utils.HasStatus(http.StatusPreconditionFailed)),
+                               },
+                               "PRECONDITION FAILED when updating with IFMATCH 
ETAG Header": {
+                                       EndpointId:     
GetDSRequestCommentId(t),
+                                       ClientSession:  TOSession,
+                                       RequestBody:    
map[string]interface{}{},
+                                       RequestHeaders: 
http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}},
+                                       Expectations:   
utils.CkRequest(utils.HasError(), 
utils.HasStatus(http.StatusPreconditionFailed)),
+                               },
+                       },
+                       "GET AFTER CHANGES": {
+                               "OK when CHANGES made": {
+                                       ClientSession:  TOSession,
+                                       RequestHeaders: 
http.Header{rfc.IfModifiedSince: {currentTimeRFC}},
+                                       Expectations:   
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                       },
                }
-       }
-}
 
-func GetTestDeliveryServiceRequestCommentsIMSAfterChange(t *testing.T, header 
http.Header) {
-       _, reqInf, err := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(header)
-       if err != nil {
-               t.Fatalf("could not GET delivery service request comments: %v", 
err)
-       }
-       if reqInf.StatusCode != http.StatusOK {
-               t.Fatalf("Expected 200 status code, got %v", reqInf.StatusCode)
-       }
-       header = make(map[string][]string)
-       futureTime := time.Now().AddDate(0, 0, 1)
-       time := futureTime.Format(time.RFC1123)
-       header.Set(rfc.IfModifiedSince, time)
-       _, reqInf, err = 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(header)
-       if err != nil {
-               t.Fatalf("could not GET delivery service request comments: %v", 
err)
-       }
-       if reqInf.StatusCode != http.StatusNotModified {
-               t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
-       }
-}
-
-func CreateTestDeliveryServiceRequestComments(t *testing.T) {
-
-       // Retrieve a delivery service request by xmlId so we can get the ID 
needed to create a dsr comment
-       dsr := testData.DeliveryServiceRequests[0].DeliveryService
-
-       resp, _, err := TOSession.GetDeliveryServiceRequestByXMLID(dsr.XMLID)
-       if err != nil {
-               t.Errorf("cannot GET delivery service request by xml id: %v - 
%v", dsr.XMLID, err)
-       }
-       if len(resp) != 1 {
-               t.Errorf("found %d delivery service request by xml id, expected 
%d: %s", len(resp), 1, dsr.XMLID)
-       } else {
-               respDSR := resp[0]
-
-               for _, comment := range testData.DeliveryServiceRequestComments 
{
-                       comment.DeliveryServiceRequestID = respDSR.ID
-                       resp, _, err := 
TOSession.CreateDeliveryServiceRequestComment(comment)
-                       if err != nil {
-                               t.Errorf("could not CREATE delivery service 
request comment: %v - %v", err, resp)
-                       }
+               for method, testCases := range methodTests {
+                       t.Run(method, func(t *testing.T) {
+                               for name, testCase := range testCases {
+                                       comment := 
tc.DeliveryServiceRequestComment{}
+
+                                       if testCase.RequestBody != nil {
+                                               dat, err := 
json.Marshal(testCase.RequestBody)
+                                               assert.NoError(t, err, "Error 
occurred when marshalling request body: %v", err)
+                                               err = json.Unmarshal(dat, 
&comment)
+                                               assert.NoError(t, err, "Error 
occurred when unmarshalling request body: %v", err)
+                                       }
+
+                                       switch method {
+                                       case "GET", "GET AFTER CHANGES":
+                                               t.Run(name, func(t *testing.T) {
+                                                       if name == "OK when 
VALID ID parameter" {
+                                                               resp, reqInf, 
err := 
testCase.ClientSession.GetDeliveryServiceRequestCommentByIDWithHdr(testCase.EndpointId(),
 testCase.RequestHeaders)
+                                                               for _, check := 
range testCase.Expectations {
+                                                                       
check(t, reqInf, resp, tc.Alerts{}, err)
+                                                               }
+                                                       } else {
+                                                               resp, reqInf, 
err := 
testCase.ClientSession.GetDeliveryServiceRequestCommentsWithHdr(testCase.RequestHeaders)
+                                                               for _, check := 
range testCase.Expectations {
+                                                                       
check(t, reqInf, resp, tc.Alerts{}, err)
+                                                               }
+                                                       }
+                                               })
+                                       case "PUT":
+                                               t.Run(name, func(t *testing.T) {
+                                                       alerts, reqInf, err := 
testCase.ClientSession.UpdateDeliveryServiceRequestCommentByIDWithHdr(testCase.EndpointId(),
 comment, testCase.RequestHeaders)
+                                                       for _, check := range 
testCase.Expectations {
+                                                               check(t, 
reqInf, nil, alerts, err)
+                                                       }
+                                               })
+                                       }
+                               }
+                       })
                }
-       }
-
+       })
 }
 
-func SortTestDeliveryServiceRequestComments(t *testing.T) {
-       var header http.Header
-       var sortedList []string
-       resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(header)
-       if err != nil {
-               t.Fatalf("Expected no error, but got %v", err.Error())
-       }
-       for i, _ := range resp {
-               sortedList = append(sortedList, resp[i].XMLID)
-       }
+func GetDSRequestCommentId(t *testing.T) func() int {
+       return func() int {
+               resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(http.Header{})
+               assert.RequireNoError(t, err, "Get Delivery Service Request 
Comments failed with error: %v", err)
+               assert.RequireGreaterOrEqual(t, len(resp), 1, "Expected 
delivery service request comments response object length of atleast 1, but got 
%d", len(resp))
+               assert.RequireNotNil(t, resp[0].ID, "Expected id to not be nil")
 
-       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 names: %v", sortedList)
+               return resp[0].ID
        }
 }
 
-func UpdateTestDeliveryServiceRequestComments(t *testing.T) {
-
-       comments, _, err := TOSession.GetDeliveryServiceRequestComments()
+func GetDSRequestId(t *testing.T, xmlId string) func() int {
+       return func() int {
+               resp, _, err := 
TOSession.GetDeliveryServiceRequestByXMLIDWithHdr(xmlId, http.Header{})
+               assert.RequireNoError(t, err, "Get Delivery Service Requests 
failed with error: %v", err)
+               assert.RequireGreaterOrEqual(t, len(resp), 1, "Expected 
delivery service requests response object length of atleast 1, but got %d", 
len(resp))
+               assert.RequireNotNil(t, resp[0].ID, "Expected id to not be nil")
 
-       firstComment := comments[0]
-       newFirstCommentValue := "new comment value"
-       firstComment.Value = newFirstCommentValue
-
-       var alert tc.Alerts
-       alert, _, err = 
TOSession.UpdateDeliveryServiceRequestCommentByID(firstComment.ID, firstComment)
-       if err != nil {
-               t.Errorf("cannot UPDATE delivery service request comment by id: 
%v - %v", err, alert)
+               return resp[0].ID
        }
+}
 
-       // Retrieve the delivery service request comment to check that the 
value got updated
-       resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentByID(firstComment.ID)
-       if err != nil {
-               t.Errorf("cannot GET delivery service request comment by id: 
'$%d', %v", firstComment.ID, err)
-       }
-       respDSRC := resp[0]
-       if respDSRC.Value != newFirstCommentValue {
-               t.Errorf("results do not match actual: %s, expected: %s", 
respDSRC.Value, newFirstCommentValue)
-       }
+func validateSortedDSRequestComments() utils.CkReqFunc {
+       return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ 
tc.Alerts, err error) {
+               var sortedList []string
+               dsReqComments := resp.([]tc.DeliveryServiceRequestComment)
 
-}
+               for _, comment := range dsReqComments {
+                       sortedList = append(sortedList, comment.XMLID)
+               }
 
-func GetTestDeliveryServiceRequestCommentsIMS(t *testing.T) {
-       var header http.Header
-       header = make(map[string][]string)
-       futureTime := time.Now().AddDate(0, 0, 1)
-       time := futureTime.Format(time.RFC1123)
-       header.Set(rfc.IfModifiedSince, time)
-       _, reqInf, err := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(header)
-       if err != nil {
-               t.Fatalf("could not GET delivery service request comments: %v", 
err)
-       }
-       if reqInf.StatusCode != http.StatusNotModified {
-               t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
+               res := sort.SliceIsSorted(sortedList, func(p, q int) bool {
+                       return sortedList[p] < sortedList[q]
+               })
+               assert.Equal(t, res, true, "List is not sorted by their names: 
%v", sortedList)
        }
 }
 
-func GetTestDeliveryServiceRequestComments(t *testing.T) {
-
-       comments, _, _ := TOSession.GetDeliveryServiceRequestComments()
+func CreateTestDeliveryServiceRequestComments(t *testing.T) {
+       for _, comment := range testData.DeliveryServiceRequestComments {
+               resp, _, err := 
TOSession.GetDeliveryServiceRequestByXMLIDWithHdr(comment.XMLID, http.Header{})
+               assert.NoError(t, err, "Cannot get Delivery Service Request by 
XMLID '%s': %v", comment.XMLID, err)
+               assert.Equal(t, len(resp), 1, "Found %d Delivery Service 
request by XMLID '%s, expected exactly one", len(resp), comment.XMLID)
+               assert.NotNil(t, resp[0].ID, "Got Delivery Service Request with 
xml_id '%s' that had a null ID", comment.XMLID)
 
-       for _, comment := range comments {
-               resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentByID(comment.ID)
-               if err != nil {
-                       t.Errorf("cannot GET delivery service request comment 
by id: %v - %v", err, resp)
-               }
+               comment.DeliveryServiceRequestID = resp[0].ID
+               alerts, _, err := 
TOSession.CreateDeliveryServiceRequestComment(comment)
+               assert.NoError(t, err, "Could not create Delivery Service 
Request Comment: %v - alerts: %+v", err, alerts.Alerts)
        }
 }
 
 func DeleteTestDeliveryServiceRequestComments(t *testing.T) {
+       resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentsWithHdr(http.Header{})
+       assert.NoError(t, err, "Unexpected error getting Delivery Service 
Request Comments: %v", err)
 
-       comments, _, _ := TOSession.GetDeliveryServiceRequestComments()
-
-       for _, comment := range comments {
-               _, _, err := 
TOSession.DeleteDeliveryServiceRequestCommentByID(comment.ID)
-               if err != nil {
-                       t.Errorf("cannot DELETE delivery service request 
comment by id: '%d' %v", comment.ID, err)
-               }
+       for _, comment := range resp {
+               alerts, _, err := 
TOSession.DeleteDeliveryServiceRequestCommentByID(comment.ID)
+               assert.NoError(t, err, "Cannot delete Delivery Service Request 
Comment #%d: %v - alerts: %+v", comment.ID, err, alerts.Alerts)
 
                // Retrieve the delivery service request comment to see if it 
got deleted
-               comments, _, err := 
TOSession.GetDeliveryServiceRequestCommentByID(comment.ID)
-               if err != nil {
-                       t.Errorf("error deleting delivery service request 
comment: %s", err.Error())
-               }
-               if len(comments) > 0 {
-                       t.Errorf("expected delivery service request comment: %d 
to be deleted", comment.ID)
-               }
+               resp, _, err := 
TOSession.GetDeliveryServiceRequestCommentByIDWithHdr(comment.ID, http.Header{})
+               assert.NoError(t, err, "Unexpected error fetching Delivery 
Service Request Comment %d after deletion: %v", comment.ID, err)
+               assert.Equal(t, len(resp), 0, "Expected Delivery Service 
Request Comment #%d to be deleted, but it was found in Traffic Ops", comment.ID)
        }
 }
diff --git a/traffic_ops/testing/api/v3/tc-fixtures.json 
b/traffic_ops/testing/api/v3/tc-fixtures.json
index ab8616ae4d..7c2f2994ff 100644
--- a/traffic_ops/testing/api/v3/tc-fixtures.json
+++ b/traffic_ops/testing/api/v3/tc-fixtures.json
@@ -281,16 +281,20 @@
     ],
     "deliveryServiceRequestComments": [
         {
-            "value": "this is comment one"
+            "value": "this is comment one",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment two"
+            "value": "this is comment two",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment three"
+            "value": "this is comment three",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment four"
+            "value": "this is comment four",
+            "xmlId": "test-ds1"
         }
     ],
     "deliveryServiceRequests": [
diff --git 
a/traffic_ops/testing/api/v4/deliveryservice_request_comments_test.go 
b/traffic_ops/testing/api/v4/deliveryservice_request_comments_test.go
index dd5c410809..512289661f 100644
--- a/traffic_ops/testing/api/v4/deliveryservice_request_comments_test.go
+++ b/traffic_ops/testing/api/v4/deliveryservice_request_comments_test.go
@@ -16,7 +16,9 @@ package v4
 */
 
 import (
+       "encoding/json"
        "net/http"
+       "net/url"
        "sort"
        "strconv"
        "testing"
@@ -24,220 +26,180 @@ import (
 
        "github.com/apache/trafficcontrol/lib/go-rfc"
        "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/traffic_ops/testing/api/assert"
+       "github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+       "github.com/apache/trafficcontrol/traffic_ops/toclientlib"
        client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
 )
 
 func TestDeliveryServiceRequestComments(t *testing.T) {
        WithObjs(t, []TCObj{CDNs, Types, Parameters, Tenants, 
DeliveryServiceRequests, DeliveryServiceRequestComments}, func() {
-               GetTestDeliveryServiceRequestCommentsIMS(t)
-               currentTime := time.Now().UTC().Add(-5 * time.Second)
-               time := currentTime.Format(time.RFC1123)
-               var header http.Header
-               header = make(map[string][]string)
-               header.Set(rfc.IfUnmodifiedSince, time)
-               header.Set(rfc.IfModifiedSince, time)
-               SortTestDeliveryServiceRequestComments(t)
-               UpdateTestDeliveryServiceRequestComments(t)
-               UpdateTestDeliveryServiceRequestCommentsWithHeaders(t, header)
-               header = make(map[string][]string)
-               etag := rfc.ETag(currentTime)
-               header.Set(rfc.IfMatch, etag)
-               UpdateTestDeliveryServiceRequestCommentsWithHeaders(t, header)
-               GetTestDeliveryServiceRequestComments(t)
-               GetTestDeliveryServiceRequestCommentsIMSAfterChange(t, header)
-       })
-}
-
-func UpdateTestDeliveryServiceRequestCommentsWithHeaders(t *testing.T, header 
http.Header) {
-       opts := client.NewRequestOptions()
-       opts.Header = header
-       comments, _, _ := TOSession.GetDeliveryServiceRequestComments(opts)
-
-       if len(comments.Response) > 0 {
-               firstComment := comments.Response[0]
-               newFirstCommentValue := "new comment value"
-               firstComment.Value = newFirstCommentValue
 
-               _, reqInf, err := 
TOSession.UpdateDeliveryServiceRequestComment(firstComment.ID, firstComment, 
opts)
-               if err == nil {
-                       t.Errorf("expected precondition failed error, but got 
none")
+               currentTime := time.Now().UTC().Add(-15 * time.Second)
+               currentTimeRFC := currentTime.Format(time.RFC1123)
+               tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
+
+               methodTests := utils.V4TestCase{
+                       "GET": {
+                               "NOT MODIFIED when NO CHANGES made": {
+                                       ClientSession: TOSession,
+                                       RequestOpts:   
client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {tomorrow}}},
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusNotModified)),
+                               },
+                               "OK when VALID request": {
+                                       ClientSession: TOSession,
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                               "OK when VALID ID parameter": {
+                                       ClientSession: TOSession,
+                                       RequestOpts:   
client.RequestOptions{QueryParameters: url.Values{"id": 
{strconv.Itoa(GetDSRequestCommentId(t, "admin")())}}},
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), 
utils.ResponseHasLength(1)),
+                               },
+                               "VALIDATE SORT when DEFAULT is ASC ORDER": {
+                                       ClientSession: TOSession,
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), 
validateSortedDSRequestComments()),
+                               },
+                       },
+                       "PUT": {
+                               "OK when VALID request": {
+                                       EndpointId:    GetDSRequestCommentId(t, 
"admin"),
+                                       ClientSession: TOSession,
+                                       RequestBody: map[string]interface{}{
+                                               "deliveryServiceRequestId": 
GetDSRequestId(t, "test-ds1")(),
+                                               "value":                    
"updated comment",
+                                       },
+                                       Expectations: 
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                               "PRECONDITION FAILED when updating with 
IF-UNMODIFIED-SINCE Header": {
+                                       EndpointId:    GetDSRequestCommentId(t, 
"admin"),
+                                       ClientSession: TOSession,
+                                       RequestOpts:   
client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: 
{currentTimeRFC}}},
+                                       RequestBody:   map[string]interface{}{},
+                                       Expectations:  
utils.CkRequest(utils.HasError(), 
utils.HasStatus(http.StatusPreconditionFailed)),
+                               },
+                               "PRECONDITION FAILED when updating with IFMATCH 
ETAG Header": {
+                                       EndpointId:    GetDSRequestCommentId(t, 
"admin"),
+                                       ClientSession: TOSession,
+                                       RequestBody:   map[string]interface{}{},
+                                       RequestOpts:   
client.RequestOptions{Header: http.Header{rfc.IfMatch: 
{rfc.ETag(currentTime)}}},
+                                       Expectations:  
utils.CkRequest(utils.HasError(), 
utils.HasStatus(http.StatusPreconditionFailed)),
+                               },
+                       },
+                       "GET AFTER CHANGES": {
+                               "OK when CHANGES made": {
+                                       ClientSession: TOSession,
+                                       RequestOpts:   
client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: 
{currentTimeRFC}}},
+                                       Expectations:  
utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+                               },
+                       },
                }
-               if reqInf.StatusCode != http.StatusPreconditionFailed {
-                       t.Errorf("Expected status code 412, got %v", 
reqInf.StatusCode)
-               }
-       }
-}
 
-func GetTestDeliveryServiceRequestCommentsIMSAfterChange(t *testing.T, header 
http.Header) {
-       opts := client.NewRequestOptions()
-       opts.Header = header
-       resp, reqInf, err := TOSession.GetDeliveryServiceRequestComments(opts)
-       if err != nil {
-               t.Fatalf("could not get Delivery Service Request Comments: %v - 
alerts: %+v", err, resp.Alerts)
-       }
-       if reqInf.StatusCode != http.StatusOK {
-               t.Fatalf("Expected 200 status code, got %v", reqInf.StatusCode)
-       }
-       opts.Header = make(map[string][]string)
-       futureTime := time.Now().AddDate(0, 0, 1)
-       time := futureTime.Format(time.RFC1123)
-       opts.Header.Set(rfc.IfModifiedSince, time)
-       resp, reqInf, err = TOSession.GetDeliveryServiceRequestComments(opts)
-       if err != nil {
-               t.Fatalf("could not get Delivery Service Request Comments: %v - 
alerts: %+v", err, resp.Alerts)
-       }
-       if reqInf.StatusCode != http.StatusNotModified {
-               t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
-       }
-}
-
-func CreateTestDeliveryServiceRequestComments(t *testing.T) {
-       if len(testData.DeliveryServiceRequests) < 1 {
-               t.Fatal("Need at least one Delivery Service Request to test 
creating Delivery Service Request Comments")
-       }
-
-       // Retrieve a delivery service request by xmlId so we can get the ID 
needed to create a dsr comment
-       dsr := testData.DeliveryServiceRequests[0]
-       var ds *tc.DeliveryServiceV4
-       if dsr.ChangeType == tc.DSRChangeTypeDelete {
-               ds = dsr.Original
-       } else {
-               ds = dsr.Requested
-       }
-       resetDS(ds)
-       if ds == nil || ds.XMLID == nil {
-               t.Fatal("first DSR in the test data had a nil Delivery Service, 
or one with no XMLID")
-       }
-
-       opts := client.NewRequestOptions()
-       opts.QueryParameters.Set("xmlId", *ds.XMLID)
-       resp, _, err := TOSession.GetDeliveryServiceRequests(opts)
-       if err != nil {
-               t.Fatalf("cannot get Delivery Service Request by XMLID '%s': %v 
- alerts: %+v", *ds.XMLID, err, resp.Alerts)
-       }
-       if len(resp.Response) != 1 {
-               t.Fatalf("found %d Delivery Service request by XMLID '%s, 
expected exactly one", len(resp.Response), *ds.XMLID)
-       }
-       respDSR := resp.Response[0]
-       if respDSR.ID == nil {
-               t.Fatalf("got Delivery Service Request with xml_id '%s' that 
had a null ID", *ds.XMLID)
-       }
-
-       for _, comment := range testData.DeliveryServiceRequestComments {
-               comment.DeliveryServiceRequestID = *respDSR.ID
-               resp, _, err := 
TOSession.CreateDeliveryServiceRequestComment(comment, client.RequestOptions{})
-               if err != nil {
-                       t.Errorf("could not create Delivery Service Request 
Comment: %v - alerts: %+v", err, resp.Alerts)
+               for method, testCases := range methodTests {
+                       t.Run(method, func(t *testing.T) {
+                               for name, testCase := range testCases {
+                                       comment := 
tc.DeliveryServiceRequestComment{}
+
+                                       if testCase.RequestBody != nil {
+                                               dat, err := 
json.Marshal(testCase.RequestBody)
+                                               assert.NoError(t, err, "Error 
occurred when marshalling request body: %v", err)
+                                               err = json.Unmarshal(dat, 
&comment)
+                                               assert.NoError(t, err, "Error 
occurred when unmarshalling request body: %v", err)
+                                       }
+
+                                       switch method {
+                                       case "GET", "GET AFTER CHANGES":
+                                               t.Run(name, func(t *testing.T) {
+                                                       resp, reqInf, err := 
testCase.ClientSession.GetDeliveryServiceRequestComments(testCase.RequestOpts)
+                                                       for _, check := range 
testCase.Expectations {
+                                                               check(t, 
reqInf, resp.Response, resp.Alerts, err)
+                                                       }
+                                               })
+                                       case "PUT":
+                                               t.Run(name, func(t *testing.T) {
+                                                       alerts, reqInf, err := 
testCase.ClientSession.UpdateDeliveryServiceRequestComment(testCase.EndpointId(),
 comment, testCase.RequestOpts)
+                                                       for _, check := range 
testCase.Expectations {
+                                                               check(t, 
reqInf, nil, alerts, err)
+                                                       }
+                                               })
+                                       }
+                               }
+                       })
                }
-       }
+       })
 }
 
-func SortTestDeliveryServiceRequestComments(t *testing.T) {
-       var sortedList []string
-       resp, _, err := 
TOSession.GetDeliveryServiceRequestComments(client.RequestOptions{})
-       if err != nil {
-               t.Fatalf("Expected no error, but got: %v - alerts: %+v", err, 
resp.Alerts)
-       }
-       for _, dsrc := range resp.Response {
-               sortedList = append(sortedList, dsrc.XMLID)
-       }
+func GetDSRequestCommentId(t *testing.T, author string) func() int {
+       return func() int {
+               opts := client.NewRequestOptions()
+               opts.QueryParameters.Set("author", author)
 
-       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 names: %v", sortedList)
+               resp, _, err := 
TOSession.GetDeliveryServiceRequestComments(opts)
+               assert.RequireNoError(t, err, "Get Delivery Service Request 
Comments failed with error: %v", err)
+               assert.RequireGreaterOrEqual(t, len(resp.Response), 1, 
"Expected delivery service request comments response object length of atleast 
1, but got %d", len(resp.Response))
+               assert.RequireNotNil(t, resp.Response[0].ID, "Expected id to 
not be nil")
+
+               return resp.Response[0].ID
        }
 }
 
-func UpdateTestDeliveryServiceRequestComments(t *testing.T) {
+func GetDSRequestId(t *testing.T, xmlId string) func() int {
+       return func() int {
+               opts := client.NewRequestOptions()
+               opts.QueryParameters.Set("xmlId", xmlId)
 
-       comments, _, err := 
TOSession.GetDeliveryServiceRequestComments(client.RequestOptions{})
-       if err != nil {
-               t.Errorf("Unexpected error getting Delivery Service Request 
Comments: %v - alerts: %+v", err, comments.Alerts)
-       }
-       if len(comments.Response) < 1 {
-               t.Fatal("Expected at least one Delivery Service Request Comment 
to exist in Traffic Ops - none did")
-       }
-       firstComment := comments.Response[0]
-       newFirstCommentValue := "new comment value"
-       firstComment.Value = newFirstCommentValue
-
-       var alert tc.Alerts
-       alert, _, err = 
TOSession.UpdateDeliveryServiceRequestComment(firstComment.ID, firstComment, 
client.RequestOptions{})
-       if err != nil {
-               t.Errorf("cannot update Delivery Service Request Comment #%d: 
%v - alerts: %+v", firstComment.ID, err, alert.Alerts)
-       }
+               resp, _, err := TOSession.GetDeliveryServiceRequests(opts)
+               assert.RequireNoError(t, err, "Get Delivery Service Requests 
failed with error: %v", err)
+               assert.RequireGreaterOrEqual(t, len(resp.Response), 1, 
"Expected delivery service requests response object length of atleast 1, but 
got %d", len(resp.Response))
+               assert.RequireNotNil(t, resp.Response[0].ID, "Expected id to 
not be nil")
 
-       // Retrieve the delivery service request comment to check that the 
value got updated
-       opts := client.NewRequestOptions()
-       opts.QueryParameters.Set("id", strconv.Itoa(firstComment.ID))
-       resp, _, err := TOSession.GetDeliveryServiceRequestComments(opts)
-       if err != nil {
-               t.Errorf("cannot get Delivery Service Request Comment #%d: %v - 
alerts: %+v", firstComment.ID, err, resp.Alerts)
-       }
-       if len(resp.Response) != 1 {
-               t.Fatalf("Expected exactly one Delivery Service Request Comment 
to exist with ID %d, found: %d", firstComment.ID, len(resp.Response))
-       }
-       respDSRC := resp.Response[0]
-       if respDSRC.Value != newFirstCommentValue {
-               t.Errorf("results do not match actual: %s, expected: %s", 
respDSRC.Value, newFirstCommentValue)
+               return *resp.Response[0].ID
        }
-
 }
 
-func GetTestDeliveryServiceRequestCommentsIMS(t *testing.T) {
-       futureTime := time.Now().AddDate(0, 0, 1)
-       time := futureTime.Format(time.RFC1123)
+func validateSortedDSRequestComments() utils.CkReqFunc {
+       return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ 
tc.Alerts, err error) {
+               var sortedList []string
+               dsReqComments := resp.([]tc.DeliveryServiceRequestComment)
 
-       opts := client.NewRequestOptions()
-       opts.Header.Set(rfc.IfModifiedSince, time)
-       resp, reqInf, err := TOSession.GetDeliveryServiceRequestComments(opts)
-       if err != nil {
-               t.Fatalf("could not get Delivery Service Request Comments: %v - 
alerts: %+v", err, resp.Alerts)
-       }
-       if reqInf.StatusCode != http.StatusNotModified {
-               t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
+               for _, comment := range dsReqComments {
+                       sortedList = append(sortedList, comment.XMLID)
+               }
+
+               res := sort.SliceIsSorted(sortedList, func(p, q int) bool {
+                       return sortedList[p] < sortedList[q]
+               })
+               assert.Equal(t, res, true, "List is not sorted by their names: 
%v", sortedList)
        }
 }
 
-func GetTestDeliveryServiceRequestComments(t *testing.T) {
-       comments, _, err := 
TOSession.GetDeliveryServiceRequestComments(client.RequestOptions{})
-       if err != nil {
-               t.Errorf("Unexpected error getting Delivery Service Request 
Comments: %v - alerts: %+v", err, comments.Alerts)
-       }
+func CreateTestDeliveryServiceRequestComments(t *testing.T) {
+       for _, comment := range testData.DeliveryServiceRequestComments {
+               opts := client.NewRequestOptions()
+               opts.QueryParameters.Set("xmlId", comment.XMLID)
+               resp, _, err := TOSession.GetDeliveryServiceRequests(opts)
+               assert.NoError(t, err, "Cannot get Delivery Service Request by 
XMLID '%s': %v - alerts: %+v", comment.XMLID, err, resp.Alerts)
+               assert.Equal(t, len(resp.Response), 1, "Found %d Delivery 
Service request by XMLID '%s, expected exactly one", len(resp.Response), 
comment.XMLID)
+               assert.NotNil(t, resp.Response[0].ID, "Got Delivery Service 
Request with xml_id '%s' that had a null ID", comment.XMLID)
 
-       opts := client.NewRequestOptions()
-       for _, comment := range comments.Response {
-               opts.QueryParameters.Set("id", strconv.Itoa(comment.ID))
-               resp, _, err := 
TOSession.GetDeliveryServiceRequestComments(opts)
-               if err != nil {
-                       t.Errorf("cannot get Delivery Service Request Comment 
by id %d: %v - alerts: %+v", comment.ID, err, resp.Alerts)
-               }
+               comment.DeliveryServiceRequestID = *resp.Response[0].ID
+               alerts, _, err := 
TOSession.CreateDeliveryServiceRequestComment(comment, client.RequestOptions{})
+               assert.NoError(t, err, "Could not create Delivery Service 
Request Comment: %v - alerts: %+v", err, alerts.Alerts)
        }
 }
 
 func DeleteTestDeliveryServiceRequestComments(t *testing.T) {
        comments, _, err := 
TOSession.GetDeliveryServiceRequestComments(client.RequestOptions{})
-       if err != nil {
-               t.Errorf("Unexpected error getting Delivery Service Request 
Comments: %v - alerts: %+v", err, comments.Alerts)
-       }
+       assert.NoError(t, err, "Unexpected error getting Delivery Service 
Request Comments: %v - alerts: %+v", err, comments.Alerts)
 
-       opts := client.NewRequestOptions()
        for _, comment := range comments.Response {
                resp, _, err := 
TOSession.DeleteDeliveryServiceRequestComment(comment.ID, 
client.RequestOptions{})
-               if err != nil {
-                       t.Errorf("cannot delete Delivery Service Request 
Comment #%d: %v - alerts: %+v", comment.ID, err, resp.Alerts)
-               }
+               assert.NoError(t, err, "Cannot delete Delivery Service Request 
Comment #%d: %v - alerts: %+v", comment.ID, err, resp.Alerts)
 
                // Retrieve the delivery service request comment to see if it 
got deleted
+               opts := client.NewRequestOptions()
                opts.QueryParameters.Set("id", strconv.Itoa(comment.ID))
                comments, _, err := 
TOSession.GetDeliveryServiceRequestComments(opts)
-               if err != nil {
-                       t.Errorf("Unexpected error fetching Delivery Service 
Request Comment %d after deletion: %v - alerts: %+v", comment.ID, err, 
comments.Alerts)
-               }
-               if len(comments.Response) > 0 {
-                       t.Errorf("expected Delivery Service Request Comment #%d 
to be deleted, but it was found in Traffic Ops", comment.ID)
-               }
+               assert.NoError(t, err, "Unexpected error fetching Delivery 
Service Request Comment %d after deletion: %v - alerts: %+v", comment.ID, err, 
comments.Alerts)
+               assert.Equal(t, len(comments.Response), 0, "Expected Delivery 
Service Request Comment #%d to be deleted, but it was found in Traffic Ops", 
comment.ID)
        }
 }
diff --git a/traffic_ops/testing/api/v4/tc-fixtures.json 
b/traffic_ops/testing/api/v4/tc-fixtures.json
index 7af7a485c8..011a1a8042 100644
--- a/traffic_ops/testing/api/v4/tc-fixtures.json
+++ b/traffic_ops/testing/api/v4/tc-fixtures.json
@@ -310,16 +310,20 @@
     ],
     "deliveryServiceRequestComments": [
         {
-            "value": "this is comment one"
+            "value": "this is comment one",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment two"
+            "value": "this is comment two",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment three"
+            "value": "this is comment three",
+            "xmlId": "test-ds1"
         },
         {
-            "value": "this is comment four"
+            "value": "this is comment four",
+            "xmlId": "test-ds1"
         }
     ],
     "deliveryServiceRequests": [

Reply via email to