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 2022/10/14 16:42:55 UTC
[trafficcontrol] branch master updated: Use Generics in TO Integration Tests (#7132)
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 3e6292d043 Use Generics in TO Integration Tests (#7132)
3e6292d043 is described below
commit 3e6292d0435e854bb0d1e8f935c30bd795be1252
Author: Eric Holguin <14...@users.noreply.github.com>
AuthorDate: Fri Oct 14 10:42:49 2022 -0600
Use Generics in TO Integration Tests (#7132)
* Add generic pointer function
* Use generics and clean up
* undo accidental format
* use generic ptr func
* fix typo
* use generic ptr func
---
lib/go-util/ptr.go | 4 +
lib/go-util/ptr_test.go | 6 +
traffic_ops/testing/api/v4/about_test.go | 10 +-
traffic_ops/testing/api/v4/acme_test.go | 7 +-
traffic_ops/testing/api/v4/cachegroups_test.go | 203 ++++++------
.../api/v4/cachegroupsdeliveryservices_test.go | 25 +-
traffic_ops/testing/api/v4/cdn_domains_test.go | 11 +-
traffic_ops/testing/api/v4/cdn_federations_test.go | 55 ++--
.../testing/api/v4/cdn_queue_updates_test.go | 26 +-
.../testing/api/v4/cdnnotifications_test.go | 10 +-
traffic_ops/testing/api/v4/cdns_test.go | 86 +++--
traffic_ops/testing/api/v4/coordinates_test.go | 84 +++--
.../v4/deliveryservice_request_comments_test.go | 38 +--
.../deliveryservices_required_capabilities_test.go | 140 ++++----
traffic_ops/testing/api/v4/divisions_test.go | 42 +--
.../api/v4/federation_deliveryservices_test.go | 15 +-
.../api/v4/federation_federation_resolvers_test.go | 37 +--
.../testing/api/v4/federation_resolvers_test.go | 24 +-
.../testing/api/v4/federation_users_test.go | 67 ++--
traffic_ops/testing/api/v4/federations_test.go | 112 +++----
traffic_ops/testing/api/v4/logs_test.go | 15 +-
traffic_ops/testing/api/v4/origins_test.go | 359 ++++++++++-----------
traffic_ops/testing/api/v4/phys_locations_test.go | 124 ++++---
traffic_ops/testing/api/v4/ping_test.go | 9 +-
traffic_ops/testing/api/v4/profiles_export_test.go | 3 +-
traffic_ops/testing/api/v4/profiles_import_test.go | 78 ++---
.../testing/api/v4/profiles_name_copy_test.go | 40 +--
traffic_ops/testing/api/v4/profiles_test.go | 108 +++----
traffic_ops/testing/api/v4/regions_test.go | 48 ++-
traffic_ops/testing/api/v4/roles_test.go | 106 +++---
.../testing/api/v4/server_capabilities_test.go | 28 +-
31 files changed, 863 insertions(+), 1057 deletions(-)
diff --git a/lib/go-util/ptr.go b/lib/go-util/ptr.go
index e25ecbc85b..6b33346d41 100644
--- a/lib/go-util/ptr.go
+++ b/lib/go-util/ptr.go
@@ -21,6 +21,10 @@ package util
import "time"
+func Ptr[T any](v T) *T {
+ return &v
+}
+
func StrPtr(str string) *string {
return &str
}
diff --git a/lib/go-util/ptr_test.go b/lib/go-util/ptr_test.go
index 95a27cc0f3..2cc769919d 100644
--- a/lib/go-util/ptr_test.go
+++ b/lib/go-util/ptr_test.go
@@ -21,6 +21,12 @@ package util
import "fmt"
+func ExamplePtr() {
+ ptr := Ptr("testquest")
+ fmt.Println(*ptr)
+ // Output: testquest
+}
+
func ExampleStrPtr() {
ptr := StrPtr("testquest")
fmt.Println(*ptr)
diff --git a/traffic_ops/testing/api/v4/about_test.go b/traffic_ops/testing/api/v4/about_test.go
index e300ba6126..c30f0bf7fc 100644
--- a/traffic_ops/testing/api/v4/about_test.go
+++ b/traffic_ops/testing/api/v4/about_test.go
@@ -21,20 +21,24 @@ import (
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+ client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)
func TestAbout(t *testing.T) {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"UNAUTHORIZED when NOT LOGGED IN": {
- ClientSession: NoAuthTOSession, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
},
},
}
+
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
diff --git a/traffic_ops/testing/api/v4/acme_test.go b/traffic_ops/testing/api/v4/acme_test.go
index b37ee53f49..339b246341 100644
--- a/traffic_ops/testing/api/v4/acme_test.go
+++ b/traffic_ops/testing/api/v4/acme_test.go
@@ -20,17 +20,20 @@ import (
"testing"
"github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+ client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)
func TestAcmeAutoRenew(t *testing.T) {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"POST": {
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusAccepted)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusAccepted)),
},
},
}
+
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
diff --git a/traffic_ops/testing/api/v4/cachegroups_test.go b/traffic_ops/testing/api/v4/cachegroups_test.go
index 90a21d92b2..07172e5694 100644
--- a/traffic_ops/testing/api/v4/cachegroups_test.go
+++ b/traffic_ops/testing/api/v4/cachegroups_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"strconv"
@@ -25,6 +24,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"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"
@@ -34,19 +34,21 @@ import (
func TestCacheGroups(t *testing.T) {
WithObjs(t, []TCObj{Types, Parameters, CacheGroups, CDNs, Profiles, Statuses, Divisions, Regions, PhysLocations, Servers, Topologies}, func() {
- tomorrow := time.Now().AddDate(0, 0, 1).Format(time.RFC1123)
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{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.CacheGroupNullable]{
"GET": {
"OK when VALID NAME parameter AND Lat/Long are 0": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"nullLatLongCG"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1), ValidateResponseFields()),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"nullLatLongCG"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1), ValidateResponseFields()),
},
"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)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {tomorrow}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusNotModified)),
},
"NOT MODIFIED when VALID NAME parameter when NO CHANGES made": {
ClientSession: TOSession,
@@ -65,149 +67,164 @@ func TestCacheGroups(t *testing.T) {
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusNotModified)),
},
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when VALID NAME parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"parentCachegroup"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"parentCachegroup"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1),
ValidateExpectedField("Name", "parentCachegroup")),
},
"OK when VALID SHORTNAME parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"shortName": {"pg2"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"shortName": {"pg2"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1),
ValidateExpectedField("ShortName", "pg2")),
},
"OK when VALID TOPOLOGY parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"topology": {"mso-topology"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"topology": {"mso-topology"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when VALID TYPE parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"type": {"ORG_LOC"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"type": {strconv.Itoa(GetTypeId(t, "ORG_LOC"))}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1),
ValidateExpectedField("TypeName", "ORG_LOC")),
},
"EMPTY RESPONSE when INVALID ID parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"id": {"10000"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(0)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"id": {"10000"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(0)),
},
"EMPTY RESPONSE when INVALID TYPE parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"type": {"10000"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(0)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"type": {"10000"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(0)),
},
"FIRST RESULT when LIMIT=1": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("limit")),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("limit")),
},
"SECOND RESULT when LIMIT=1 OFFSET=1": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}, "offset": {"1"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("offset")),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}, "offset": {"1"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("offset")),
},
"SECOND RESULT when LIMIT=1 PAGE=2": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}, "page": {"2"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("page")),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"id"}, "limit": {"1"}, "page": {"2"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), ValidatePagination("page")),
},
"BAD REQUEST when INVALID LIMIT parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"-2"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"-2"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID OFFSET parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "offset": {"0"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "offset": {"0"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID PAGE parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"UNAUTHORIZED when NOT LOGGED IN": {
- ClientSession: NoAuthTOSession, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
+ },
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
"POST": {
"UNAUTHORIZED when NOT LOGGED IN": {
- ClientSession: NoAuthTOSession, Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
},
},
"PUT": {
"OK when VALID request": {
EndpointId: GetCacheGroupId(t, "cachegroup1"), ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 17.5,
- "longitude": 17.5,
- "name": "cachegroup1",
- "shortName": "newShortName",
- "localizationMethods": []string{"CZ"},
- "fallbacks": []string{"fallback1"},
- "typeName": "EDGE_LOC",
- "typeId": -1,
+ RequestBody: tc.CacheGroupNullable{
+ Latitude: util.Ptr(17.5),
+ Longitude: util.Ptr(17.5),
+ Name: util.Ptr("cachegroup1"),
+ ShortName: util.Ptr("newShortName"),
+ LocalizationMethods: util.Ptr([]tc.LocalizationMethod{tc.LocalizationMethodCZ}),
+ Fallbacks: util.Ptr([]string{"fallback1"}),
+ Type: util.Ptr("EDGE_LOC"),
+ TypeID: util.Ptr(GetTypeId(t, "EDGE_LOC")),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when updating CG with null Lat/Long": {
EndpointId: GetCacheGroupId(t, "nullLatLongCG"), ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "nullLatLongCG",
- "shortName": "null-ll",
- "typeName": "EDGE_LOC",
- "fallbacks": []string{"fallback1"},
- "typeId": -1,
+ RequestBody: tc.CacheGroupNullable{
+ Name: util.Ptr("nullLatLongCG"),
+ ShortName: util.Ptr("null-ll"),
+ Type: util.Ptr("EDGE_LOC"),
+ Fallbacks: util.Ptr([]string{"fallback1"}),
+ TypeID: util.Ptr(GetTypeId(t, "EDGE_LOC")),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"BAD REQUEST when updating TYPE of CG in TOPOLOGY": {
EndpointId: GetCacheGroupId(t, "topology-edge-cg-01"), ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "id": -1,
- "latitude": 0,
- "longitude": 0,
- "name": "topology-edge-cg-01",
- "shortName": "te1",
- "typeName": "MID_LOC",
- "typeId": -1,
+ RequestBody: tc.CacheGroupNullable{
+ Latitude: util.Ptr(0.0),
+ Longitude: util.Ptr(0.0),
+ Name: util.Ptr("topology-edge-cg-01"),
+ ShortName: util.Ptr("te1"),
+ Type: util.Ptr("MID_LOC"),
+ TypeID: util.Ptr(GetTypeId(t, "MID_LOC")),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"PRECONDITION FAILED when updating with IMS & IUS Headers": {
EndpointId: GetCacheGroupId(t, "cachegroup1"), ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "name": "cachegroup1",
- "shortName": "changeName",
- "typeName": "EDGE_LOC",
- "typeId": -1,
+ RequestBody: tc.CacheGroupNullable{
+ Name: util.Ptr("cachegroup1"),
+ ShortName: util.Ptr("changeName"),
+ Type: util.Ptr("EDGE_LOC"),
+ TypeID: util.Ptr(GetTypeId(t, "EDGE_LOC")),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetCacheGroupId(t, "cachegroup1"), ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
- RequestBody: map[string]interface{}{
- "name": "cachegroup1",
- "shortName": "changeName",
- "typeName": "EDGE_LOC",
- "typeId": -1,
+ RequestBody: tc.CacheGroupNullable{
+ Name: util.Ptr("cachegroup1"),
+ ShortName: util.Ptr("changeName"),
+ Type: util.Ptr("EDGE_LOC"),
+ TypeID: util.Ptr(GetTypeId(t, "EDGE_LOC")),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"UNAUTHORIZED when NOT LOGGED IN": {
- EndpointId: GetCacheGroupId(t, "cachegroup1"), ClientSession: NoAuthTOSession,
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
+ EndpointId: GetCacheGroupId(t, "cachegroup1"),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
},
},
"DELETE": {
"NOT FOUND when INVALID ID parameter": {
- EndpointId: func() int { return 111111 }, ClientSession: TOSession,
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
+ EndpointId: func() int { return 111111 },
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"UNAUTHORIZED when NOT LOGGED IN": {
- EndpointId: GetCacheGroupId(t, "cachegroup1"), ClientSession: NoAuthTOSession,
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
- },
- },
- "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)),
+ EndpointId: GetCacheGroupId(t, "cachegroup1"),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusUnauthorized)),
},
},
}
@@ -215,34 +232,8 @@ func TestCacheGroups(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- cg := tc.CacheGroupNullable{}
-
- if testCase.RequestOpts.QueryParameters.Has("type") {
- val := testCase.RequestOpts.QueryParameters.Get("type")
- if _, err := strconv.Atoi(val); err != nil {
- testCase.RequestOpts.QueryParameters.Set("type", strconv.Itoa(GetTypeId(t, val)))
- }
- }
-
- if testCase.RequestBody != nil {
- if _, ok := testCase.RequestBody["id"]; ok {
- testCase.RequestBody["id"] = testCase.EndpointId()
- }
- if typeId, ok := testCase.RequestBody["typeId"]; ok {
- if typeId == -1 {
- if typeName, ok := testCase.RequestBody["typeName"]; ok {
- testCase.RequestBody["typeId"] = GetTypeId(t, typeName.(string))
- }
- }
- }
- dat, err := json.Marshal(testCase.RequestBody)
- assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
- err = json.Unmarshal(dat, &cg)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetCacheGroups(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -251,14 +242,14 @@ func TestCacheGroups(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CreateCacheGroup(cg, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CreateCacheGroup(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.UpdateCacheGroup(testCase.EndpointId(), cg, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.UpdateCacheGroup(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/cachegroupsdeliveryservices_test.go b/traffic_ops/testing/api/v4/cachegroupsdeliveryservices_test.go
index 4646cfdb3f..29f7d188a2 100644
--- a/traffic_ops/testing/api/v4/cachegroupsdeliveryservices_test.go
+++ b/traffic_ops/testing/api/v4/cachegroupsdeliveryservices_test.go
@@ -29,27 +29,24 @@ import (
func TestCacheGroupsDeliveryServices(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, Topologies, ServiceCategories, DeliveryServices, CacheGroupsDeliveryServices}, func() {
- methodTests := utils.V4TestCase{
+
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, []int]{
"POST": {
"BAD REQUEST assigning TOPOLOGY-BASED DS to CACHEGROUP": {
EndpointId: GetCacheGroupId(t, "cachegroup3"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "deliveryServices": []int{GetDeliveryServiceId(t, "top-ds-in-cdn1")()},
- },
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ RequestBody: []int{GetDeliveryServiceId(t, "top-ds-in-cdn1")()},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"OK when valid request": {
EndpointId: GetCacheGroupId(t, "cachegroup3"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "deliveryServices": []int{
- GetDeliveryServiceId(t, "ds1")(),
- GetDeliveryServiceId(t, "ds2")(),
- GetDeliveryServiceId(t, "ds3")(),
- GetDeliveryServiceId(t, "ds3")(),
- GetDeliveryServiceId(t, "DS5")(),
- },
+ RequestBody: []int{
+ GetDeliveryServiceId(t, "ds1")(),
+ GetDeliveryServiceId(t, "ds2")(),
+ GetDeliveryServiceId(t, "ds3")(),
+ GetDeliveryServiceId(t, "ds3")(),
+ GetDeliveryServiceId(t, "DS5")(),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), validateCGDSServerAssignments()),
},
@@ -62,7 +59,7 @@ func TestCacheGroupsDeliveryServices(t *testing.T) {
switch method {
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.SetCacheGroupDeliveryServices(testCase.EndpointId(), testCase.RequestBody["deliveryServices"].([]int), testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.SetCacheGroupDeliveryServices(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/cdn_domains_test.go b/traffic_ops/testing/api/v4/cdn_domains_test.go
index 0c0a9fccf2..24e3cf2ff2 100644
--- a/traffic_ops/testing/api/v4/cdn_domains_test.go
+++ b/traffic_ops/testing/api/v4/cdn_domains_test.go
@@ -30,15 +30,16 @@ func TestCDNDomains(t *testing.T) {
tomorrow := time.Now().AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"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)),
+ 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),
- utils.ResponseLengthGreaterOrEqual(1)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1)),
},
},
}
diff --git a/traffic_ops/testing/api/v4/cdn_federations_test.go b/traffic_ops/testing/api/v4/cdn_federations_test.go
index 145cc4701d..64bc7027e7 100644
--- a/traffic_ops/testing/api/v4/cdn_federations_test.go
+++ b/traffic_ops/testing/api/v4/cdn_federations_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -26,6 +25,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"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"
@@ -45,7 +45,7 @@ func TestCDNFederations(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.CDNFederation]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -97,15 +97,20 @@ func TestCDNFederations(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"PUT": {
"OK when VALID request": {
EndpointId: GetFederationID(t, "google.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cname": "new.cname.",
- "ttl": 34,
- "description": "updated",
+ RequestBody: tc.CDNFederation{
+ CName: util.Ptr("new.cname."),
+ TTL: util.Ptr(34),
+ Description: util.Ptr("updated"),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), validateCDNFederationUpdateFields(map[string]interface{}{"CName": "new.cname."})),
},
@@ -113,48 +118,32 @@ func TestCDNFederations(t *testing.T) {
EndpointId: GetFederationID(t, "booya.com."),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "cname": "booya.com.",
- "ttl": 34,
- "description": "fooya",
+ RequestBody: tc.CDNFederation{
+ CName: util.Ptr("booya.com."),
+ TTL: util.Ptr(34),
+ Description: util.Ptr("fooya"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetFederationID(t, "booya.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cname": "new.cname.",
- "ttl": 34,
- "description": "updated",
+ RequestBody: tc.CDNFederation{
+ CName: util.Ptr("new.cname."),
+ TTL: util.Ptr(34),
+ Description: util.Ptr("updated"),
},
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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- cdnFederation := tc.CDNFederation{}
-
- 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, &cdnFederation)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetCDNFederationsByName(cdnName, testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -163,14 +152,14 @@ func TestCDNFederations(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CreateCDNFederation(cdnFederation, cdnName, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CreateCDNFederation(testCase.RequestBody, cdnName, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.UpdateCDNFederation(cdnFederation, cdnName, testCase.EndpointId(), testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.UpdateCDNFederation(testCase.RequestBody, cdnName, testCase.EndpointId(), testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/cdn_queue_updates_test.go b/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
index a5cef60d2d..fb8e3bd243 100644
--- a/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
+++ b/traffic_ops/testing/api/v4/cdn_queue_updates_test.go
@@ -31,15 +31,13 @@ import (
func TestCDNQueueUpdates(t *testing.T) {
WithObjs(t, []TCObj{Types, CDNs, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, bool]{
"POST": {
"OK when VALID TYPE parameter": {
EndpointId: GetCDNID(t, "cdn1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"type": {"EDGE"}}},
- RequestBody: map[string]interface{}{
- "action": "queue",
- },
+ RequestBody: true,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateServersUpdatePending(GetCDNID(t, "cdn1")(), map[string]string{"type": "EDGE"})),
},
@@ -47,9 +45,7 @@ func TestCDNQueueUpdates(t *testing.T) {
EndpointId: GetCDNID(t, "cdn1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"profile": {"EDGE1"}}},
- RequestBody: map[string]interface{}{
- "action": "queue",
- },
+ RequestBody: true,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateServersUpdatePending(GetCDNID(t, "cdn1")(), map[string]string{"profileName": "EDGE1"})),
},
@@ -58,27 +54,13 @@ func TestCDNQueueUpdates(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
-
- var queueUpdate bool
- if testCase.RequestBody != nil {
- if action, ok := testCase.RequestBody["action"]; ok {
- if action == "queue" {
- queueUpdate = true
- } else if action == "dequeue" {
- queueUpdate = false
- } else {
- t.Errorf("Not a valid action: %v", action)
- }
- }
- }
-
switch method {
case "POST":
t.Run(name, func(t *testing.T) {
// Clear updates on all associated cdn servers to begin with
_, _, err := TOSession.QueueUpdatesForCDN(testCase.EndpointId(), false, client.RequestOptions{})
assert.RequireNoError(t, err, "Failed to clear updates for cdn %d", testCase.EndpointId())
- resp, reqInf, err := testCase.ClientSession.QueueUpdatesForCDN(testCase.EndpointId(), queueUpdate, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.QueueUpdatesForCDN(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp, tc.Alerts{}, err)
}
diff --git a/traffic_ops/testing/api/v4/cdnnotifications_test.go b/traffic_ops/testing/api/v4/cdnnotifications_test.go
index 9a81caea35..6f519a34cb 100644
--- a/traffic_ops/testing/api/v4/cdnnotifications_test.go
+++ b/traffic_ops/testing/api/v4/cdnnotifications_test.go
@@ -30,14 +30,16 @@ import (
func TestCDNNotifications(t *testing.T) {
WithObjs(t, []TCObj{CDNs, CDNNotifications}, func() {
- methodTests := utils.V4TestCase{
+
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
- utils.ResponseLengthGreaterOrEqual(1)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1)),
},
"OK when VALID CDN parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn2"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"cdn": {"cdn2"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(1),
validateCDNNotificationFields(map[string]interface{}{"Notification": "test notification: cdn2"})),
},
diff --git a/traffic_ops/testing/api/v4/cdns_test.go b/traffic_ops/testing/api/v4/cdns_test.go
index 59f2ee5a58..c8c1248767 100644
--- a/traffic_ops/testing/api/v4/cdns_test.go
+++ b/traffic_ops/testing/api/v4/cdns_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -41,7 +40,7 @@ func TestCDNs(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.CDN]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -105,41 +104,46 @@ func TestCDNs(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"BAD REQUEST when CDN ALREADY EXISTS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "cdn3",
- "dnssecEnabled": false,
- "domainName": "test.cdn3.net",
+ RequestBody: tc.CDN{
+ Name: "cdn3",
+ DNSSECEnabled: false,
+ DomainName: "test.cdn3.net",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when EMPTY NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "",
- "dnssecEnabled": false,
- "domainName": "test.noname.net",
+ RequestBody: tc.CDN{
+ Name: "",
+ DNSSECEnabled: false,
+ DomainName: "test.noname.net",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when EMPTY DOMAIN NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "nodomain",
- "dnssecEnabled": false,
- "domainName": "",
+ RequestBody: tc.CDN{
+ Name: "nodomain",
+ DNSSECEnabled: false,
+ DomainName: "",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"FORBIDDEN when READ ONLY USER": {
ClientSession: readOnlyUserSession,
- RequestBody: map[string]interface{}{
- "name": "readOnlyTest",
- "dnssecEnabled": false,
- "domainName": "test.ro",
+ RequestBody: tc.CDN{
+ Name: "readOnlyTest",
+ DNSSECEnabled: false,
+ DomainName: "test.ro",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
},
@@ -148,10 +152,10 @@ func TestCDNs(t *testing.T) {
"OK when VALID request": {
EndpointId: GetCDNID(t, "cdn1"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "dnssecEnabled": false,
- "domainName": "domain2",
- "name": "cdn1",
+ RequestBody: tc.CDN{
+ DNSSECEnabled: false,
+ DomainName: "domain2",
+ Name: "cdn1",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateCDNUpdateFields("cdn1", map[string]interface{}{"DomainName": "domain2"})),
@@ -160,10 +164,10 @@ func TestCDNs(t *testing.T) {
EndpointId: GetCDNID(t, "cdn1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "dnssecEnabled": false,
- "domainName": "newDomain",
- "name": "cdn1",
+ RequestBody: tc.CDN{
+ DNSSECEnabled: false,
+ DomainName: "newDomain",
+ Name: "cdn1",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
@@ -171,10 +175,10 @@ func TestCDNs(t *testing.T) {
EndpointId: GetCDNID(t, "cdn1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
- RequestBody: map[string]interface{}{
- "dnssecEnabled": false,
- "domainName": "newDomain",
- "name": "cdn1",
+ RequestBody: tc.CDN{
+ DNSSECEnabled: false,
+ DomainName: "newDomain",
+ Name: "cdn1",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
@@ -186,28 +190,12 @@ func TestCDNs(t *testing.T) {
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- cdn := tc.CDN{}
-
- 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, &cdn)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetCDNs(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -216,14 +204,14 @@ func TestCDNs(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateCDN(cdn, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateCDN(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateCDN(testCase.EndpointId(), cdn, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateCDN(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/coordinates_test.go b/traffic_ops/testing/api/v4/coordinates_test.go
index a56d912183..a1dbefddbe 100644
--- a/traffic_ops/testing/api/v4/coordinates_test.go
+++ b/traffic_ops/testing/api/v4/coordinates_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestCoordinates(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Coordinate]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -101,32 +100,37 @@ func TestCoordinates(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"BAD REQUEST when INVALID NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 1.1,
- "longitude": 2.2,
- "name": "",
+ RequestBody: tc.Coordinate{
+ Latitude: 1.1,
+ Longitude: 2.2,
+ Name: "",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID LATITUDE": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 20000,
- "longitude": 2.2,
- "name": "testlatitude",
+ RequestBody: tc.Coordinate{
+ Latitude: 20000,
+ Longitude: 2.2,
+ Name: "testlatitude",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID LONGITUDE": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 1.1,
- "longitude": 20000,
- "name": "testlongitude",
+ RequestBody: tc.Coordinate{
+ Latitude: 1.1,
+ Longitude: 20000,
+ Name: "testlongitude",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -135,20 +139,20 @@ func TestCoordinates(t *testing.T) {
"OK when VALID request": {
EndpointId: GetCoordinateID(t, "coordinate2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 7.7,
- "longitude": 8.8,
- "name": "coordinate2",
+ RequestBody: tc.Coordinate{
+ Latitude: 7.7,
+ Longitude: 8.8,
+ Name: "coordinate2",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateCoordinateUpdateCreateFields("coordinate2", map[string]interface{}{"Latitude": 7.7, "Longitude": 8.8})),
},
"NOT FOUND when INVALID ID parameter": {
EndpointId: func() int { return 111111 },
- RequestBody: map[string]interface{}{
- "latitude": 1.1,
- "longitude": 2.2,
- "name": "coordinate1",
+ RequestBody: tc.Coordinate{
+ Latitude: 1.1,
+ Longitude: 2.2,
+ Name: "coordinate1",
},
ClientSession: TOSession,
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
@@ -157,20 +161,20 @@ func TestCoordinates(t *testing.T) {
EndpointId: GetCoordinateID(t, "coordinate1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "latitude": 1.1,
- "longitude": 2.2,
- "name": "coordinate1",
+ RequestBody: tc.Coordinate{
+ Latitude: 1.1,
+ Longitude: 2.2,
+ Name: "coordinate1",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetCoordinateID(t, "coordinate1"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "latitude": 1.1,
- "longitude": 2.2,
- "name": "coordinate1",
+ RequestBody: tc.Coordinate{
+ Latitude: 1.1,
+ Longitude: 2.2,
+ Name: "coordinate1",
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -183,27 +187,11 @@ func TestCoordinates(t *testing.T) {
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- coordinate := tc.Coordinate{}
-
- 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, &coordinate)
- 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) {
@@ -214,14 +202,14 @@ func TestCoordinates(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateCoordinate(coordinate, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateCoordinate(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateCoordinate(testCase.EndpointId(), coordinate, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateCoordinate(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
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 512289661f..d0384b56b4 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,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestDeliveryServiceRequestComments(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.DeliveryServiceRequestComment]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -59,14 +58,19 @@ func TestDeliveryServiceRequestComments(t *testing.T) {
ClientSession: TOSession,
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), validateSortedDSRequestComments()),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"PUT": {
"OK when VALID request": {
EndpointId: GetDSRequestCommentId(t, "admin"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "deliveryServiceRequestId": GetDSRequestId(t, "test-ds1")(),
- "value": "updated comment",
+ RequestBody: tc.DeliveryServiceRequestComment{
+ DeliveryServiceRequestID: GetDSRequestId(t, "test-ds1")(),
+ Value: "updated comment",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
@@ -74,40 +78,24 @@ func TestDeliveryServiceRequestComments(t *testing.T) {
EndpointId: GetDSRequestCommentId(t, "admin"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{},
+ RequestBody: tc.DeliveryServiceRequestComment{},
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{}{},
+ RequestBody: tc.DeliveryServiceRequestComment{},
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)),
- },
- },
}
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":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetDeliveryServiceRequestComments(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -116,7 +104,7 @@ func TestDeliveryServiceRequestComments(t *testing.T) {
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateDeliveryServiceRequestComment(testCase.EndpointId(), comment, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateDeliveryServiceRequestComment(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
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 fa10faa669..f1b8d348f4 100644
--- a/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go
+++ b/traffic_ops/testing/api/v4/deliveryservices_required_capabilities_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"strconv"
@@ -25,6 +24,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"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"
@@ -38,28 +38,32 @@ func TestDeliveryServicesRequiredCapabilities(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.DeliveryServicesRequiredCapability]{
"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)),
+ 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)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when VALID DELIVERYSERVICEID parameter": {
ClientSession: TOSession,
- RequestOpts: client.RequestOptions{QueryParameters: url.Values{"deliveryServiceId": {"ds1"}}},
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"deliveryServiceId": {strconv.Itoa(GetDeliveryServiceId(t, "ds1")())}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1),
validateDSRCExpectedFields(map[string]interface{}{"DeliveryServiceId": "ds1"})),
},
"OK when VALID XMLID parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"xmlID": {"ds2"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"xmlID": {"ds2"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1),
validateDSRCExpectedFields(map[string]interface{}{"XMLID": "ds2"})),
},
"OK when VALID REQUIREDCAPABILITY parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"requiredCapability": {"bar"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"requiredCapability": {"bar"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1),
validateDSRCExpectedFields(map[string]interface{}{"RequiredCapability": "bar"})),
},
@@ -79,119 +83,123 @@ func TestDeliveryServicesRequiredCapabilities(t *testing.T) {
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), validateDSRCPagination("page")),
},
"BAD REQUEST when INVALID LIMIT parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"-2"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"-2"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID OFFSET parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "offset": {"0"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "offset": {"0"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID PAGE parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ },
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
"POST": {
"BAD REQUEST when REASSIGNING REQUIRED CAPABILITY to DELIVERY SERVICE": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "ds1")(),
- "requiredCapability": "foo",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds1")()),
+ RequiredCapability: util.StrPtr("foo"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when SERVERS DONT have CAPABILITY": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "test-ds-server-assignments")(),
- "requiredCapability": "disk",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "test-ds-server-assignments")()),
+ RequiredCapability: util.StrPtr("disk"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when DELIVERY SERVICE HAS TOPOLOGY where SERVERS DONT have CAPABILITY": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "ds-top-req-cap")(),
- "requiredCapability": "bar",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds-top-req-cap")()),
+ RequiredCapability: util.StrPtr("bar"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when DELIVERY SERVICE ID EMPTY": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "requiredCapability": "bar",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ RequiredCapability: util.StrPtr("bar"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when REQUIRED CAPABILITY EMPTY": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "ds1")(),
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"NOT FOUND when NON-EXISTENT REQUIRED CAPABILITY": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "ds1")(),
- "requiredCapability": "bogus",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds1")()),
+ RequiredCapability: util.StrPtr("bogus"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when NON-EXISTENT DELIVERY SERVICE ID": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": -1,
- "requiredCapability": "foo",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(-1),
+ RequiredCapability: util.StrPtr("foo"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"BAD REQUEST when INVALID DELIVERY SERVICE TYPE": {
- ClientSession: TOSession, RequestBody: map[string]interface{}{
- "deliveryServiceID": GetDeliveryServiceId(t, "anymap-ds")(),
- "requiredCapability": "foo",
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "anymap-ds")()),
+ RequiredCapability: util.StrPtr("foo"),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
},
"DELETE": {
"OK when VALID request": {
- EndpointId: GetDeliveryServiceId(t, "ds-top-req-cap"), ClientSession: TOSession,
- RequestBody: map[string]interface{}{"requiredCapability": "ram"},
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds-top-req-cap")()),
+ RequiredCapability: util.StrPtr("ram"),
+ },
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"NOT FOUND when NON-EXISTENT DELIVERYSERVICEID parameter": {
- EndpointId: func() int { return -1 }, ClientSession: TOSession,
- RequestBody: map[string]interface{}{"requiredCapability": "foo"},
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(-1),
+ RequiredCapability: util.StrPtr("foo"),
+ },
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when NON-EXISTENT REQUIREDCAPABILITY parameter": {
- EndpointId: GetDeliveryServiceId(t, "ds1"), ClientSession: TOSession,
- RequestBody: map[string]interface{}{"requiredCapability": "bogus"},
+ ClientSession: TOSession,
+ RequestBody: tc.DeliveryServicesRequiredCapability{
+ DeliveryServiceID: util.IntPtr(GetDeliveryServiceId(t, "ds1")()),
+ RequiredCapability: util.StrPtr("bogus"),
+ },
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- dsrc := tc.DeliveryServicesRequiredCapability{}
-
- if val, ok := testCase.RequestOpts.QueryParameters["deliveryServiceId"]; ok {
- if _, err := strconv.Atoi(val[0]); err != nil {
- testCase.RequestOpts.QueryParameters.Set("deliveryServiceId", strconv.Itoa(GetDeliveryServiceId(t, val[0])()))
- }
- }
-
- 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, &dsrc)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetDeliveryServicesRequiredCapabilities(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -200,14 +208,14 @@ func TestDeliveryServicesRequiredCapabilities(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateDeliveryServicesRequiredCapability(dsrc, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateDeliveryServicesRequiredCapability(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "DELETE":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.DeleteDeliveryServicesRequiredCapability(testCase.EndpointId(), testCase.RequestBody["requiredCapability"].(string), testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.DeleteDeliveryServicesRequiredCapability(*testCase.RequestBody.DeliveryServiceID, *testCase.RequestBody.RequiredCapability, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/divisions_test.go b/traffic_ops/testing/api/v4/divisions_test.go
index 23a3c62ca2..f3b4ece4f8 100644
--- a/traffic_ops/testing/api/v4/divisions_test.go
+++ b/traffic_ops/testing/api/v4/divisions_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestDivisions(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Division]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -101,13 +100,18 @@ func TestDivisions(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"PUT": {
"OK when VALID request": {
EndpointId: GetDivisionID(t, "cdn-div2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testdivision",
+ RequestBody: tc.Division{
+ Name: "testdivision",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateDivisionUpdateCreateFields("testdivision", map[string]interface{}{"Name": "testdivision"})),
@@ -116,16 +120,16 @@ func TestDivisions(t *testing.T) {
EndpointId: GetDivisionID(t, "division1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "name": "division1",
+ RequestBody: tc.Division{
+ Name: "division1",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetDivisionID(t, "division1"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "division1",
+ RequestBody: tc.Division{
+ Name: "division1",
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -143,29 +147,13 @@ func TestDivisions(t *testing.T) {
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- division := tc.Division{}
-
- 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, &division)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetDivisions(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -174,14 +162,14 @@ func TestDivisions(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateDivision(division, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateDivision(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateDivision(testCase.EndpointId(), division, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateDivision(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/federation_deliveryservices_test.go b/traffic_ops/testing/api/v4/federation_deliveryservices_test.go
index 54fe87907d..9873b46789 100644
--- a/traffic_ops/testing/api/v4/federation_deliveryservices_test.go
+++ b/traffic_ops/testing/api/v4/federation_deliveryservices_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -34,7 +33,7 @@ import (
func TestFederationsDeliveryServices(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, Topologies, ServiceCategories, DeliveryServices, CDNFederations, FederationDeliveryServices}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.FederationDSPost]{
"GET": {
"OK when VALID request": {
EndpointId: GetFederationID(t, "the.cname.com."),
@@ -112,16 +111,6 @@ func TestFederationsDeliveryServices(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- var dsID int
- fedDS := tc.FederationDSPost{}
-
- 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, &fedDS)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -132,6 +121,7 @@ func TestFederationsDeliveryServices(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
+ fedDS := testCase.RequestBody
alerts, reqInf, err := testCase.ClientSession.CreateFederationDeliveryServices(testCase.EndpointId(), fedDS.DSIDs, *fedDS.Replace, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
@@ -139,6 +129,7 @@ func TestFederationsDeliveryServices(t *testing.T) {
})
case "DELETE":
t.Run(name, func(t *testing.T) {
+ var dsID int
if val, ok := testCase.RequestOpts.QueryParameters["dsID"]; ok {
id, err := strconv.Atoi(val[0])
assert.RequireNoError(t, err, "Failed to convert dsID to an integer.")
diff --git a/traffic_ops/testing/api/v4/federation_federation_resolvers_test.go b/traffic_ops/testing/api/v4/federation_federation_resolvers_test.go
index 5c6fc2a8f7..339add0c13 100644
--- a/traffic_ops/testing/api/v4/federation_federation_resolvers_test.go
+++ b/traffic_ops/testing/api/v4/federation_federation_resolvers_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"testing"
@@ -29,7 +28,7 @@ import (
func TestFederationFederationResolvers(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, Tenants, CacheGroups, Statuses, Divisions, Regions, PhysLocations, Servers, Topologies, ServiceCategories, DeliveryServices, CDNFederations, FederationResolvers, FederationFederationResolvers}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.AssignFederationResolversRequest]{
"GET": {
"OK when VALID request AND RESOLVERS ASSIGNED": {
EndpointId: GetFederationID(t, "booya.com."),
@@ -46,40 +45,40 @@ func TestFederationFederationResolvers(t *testing.T) {
"OK when ASSIGNING ONE FEDERATION RESOLVER": {
EndpointId: GetFederationID(t, "the.cname.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "resolverIDs": []int{GetFederationResolverID(t, "1.2.3.4")()},
- "replace": false,
+ RequestBody: tc.AssignFederationResolversRequest{
+ FedResolverIDs: []int{GetFederationResolverID(t, "1.2.3.4")()},
+ Replace: false,
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when ASSIGNING MULTIPLE FEDERATION RESOLVERS": {
EndpointId: GetFederationID(t, "the.cname.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "resolverIDs": []int{
+ RequestBody: tc.AssignFederationResolversRequest{
+ FedResolverIDs: []int{
GetFederationResolverID(t, "1.2.3.4")(),
GetFederationResolverID(t, "0.0.0.0/12")(),
GetFederationResolverID(t, "::f1d0:f00d/123")(),
},
- "replace": false,
+ Replace: false,
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when REPLACING ALL FEDERATION RESOLVERS": {
EndpointId: GetFederationID(t, "the.cname.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "resolverIDs": []int{GetFederationResolverID(t, "dead::babe")()},
- "replace": true,
+ RequestBody: tc.AssignFederationResolversRequest{
+ FedResolverIDs: []int{GetFederationResolverID(t, "dead::babe")()},
+ Replace: true,
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"BAD REQUEST when FEDERATION DOESNT EXIST": {
EndpointId: func() int { return -1 },
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "resolverIDs": []int{GetFederationResolverID(t, "1.2.3.4")()},
- "replace": false,
+ RequestBody: tc.AssignFederationResolversRequest{
+ FedResolverIDs: []int{GetFederationResolverID(t, "1.2.3.4")()},
+ Replace: false,
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
@@ -89,15 +88,6 @@ func TestFederationFederationResolvers(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- frAssignment := tc.AssignFederationResolversRequest{}
-
- 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, &frAssignment)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -108,6 +98,7 @@ func TestFederationFederationResolvers(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
+ frAssignment := testCase.RequestBody
resp, reqInf, err := testCase.ClientSession.AssignFederationFederationResolver(testCase.EndpointId(), frAssignment.FedResolverIDs, frAssignment.Replace, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
diff --git a/traffic_ops/testing/api/v4/federation_resolvers_test.go b/traffic_ops/testing/api/v4/federation_resolvers_test.go
index 185a1b3e00..503af5faf5 100644
--- a/traffic_ops/testing/api/v4/federation_resolvers_test.go
+++ b/traffic_ops/testing/api/v4/federation_resolvers_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestFederationResolvers(t *testing.T) {
currentTime := time.Now().UTC().Add(-15 * time.Second)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.FederationResolver]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -112,14 +111,14 @@ func TestFederationResolvers(t *testing.T) {
"POST": {
"BAD REQUEST when MISSING IPADDRESS and TYPE FIELDS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{},
+ RequestBody: tc.FederationResolver{},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID IP ADDRESS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "ipAddress": "not a valid IP address",
- "typeId": GetTypeId(t, "RESOLVE4"),
+ RequestBody: tc.FederationResolver{
+ IPAddress: util.Ptr("not a valid IP address"),
+ TypeID: util.Ptr((uint)(GetTypeId(t, "RESOLVE4"))),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -136,17 +135,8 @@ func TestFederationResolvers(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- fr := tc.FederationResolver{}
-
- 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, &fr)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetFederationResolvers(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -155,7 +145,7 @@ func TestFederationResolvers(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CreateFederationResolver(fr, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CreateFederationResolver(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/federation_users_test.go b/traffic_ops/testing/api/v4/federation_users_test.go
index 8b5f9a14ab..351759bb42 100644
--- a/traffic_ops/testing/api/v4/federation_users_test.go
+++ b/traffic_ops/testing/api/v4/federation_users_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestFederationUsers(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.FederationUserPost]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
EndpointId: GetFederationID(t, "the.cname.com."),
@@ -103,45 +102,61 @@ func TestFederationUsers(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ EndpointId: GetFederationID(t, "the.cname.com."),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"OK when VALID request": {
EndpointId: GetFederationID(t, "google.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"userIds": []int{GetUserID(t, "readonlyuser")(), GetUserID(t, "disalloweduser")()}, "replace": false},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ RequestBody: tc.FederationUserPost{
+ IDs: []int{
+ GetUserID(t, "readonlyuser")(),
+ GetUserID(t, "disalloweduser")(),
+ },
+ Replace: util.Ptr(false),
+ },
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when REPLACING USERS": {
EndpointId: GetFederationID(t, "booya.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"userIds": []int{GetUserID(t, "readonlyuser")()}, "replace": true},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ RequestBody: tc.FederationUserPost{
+ IDs: []int{GetUserID(t, "readonlyuser")()},
+ Replace: util.Ptr(true),
+ },
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when ADDING USER": {
EndpointId: GetFederationID(t, "booya.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"userIds": []int{GetUserID(t, "disalloweduser")()}, "replace": false},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ RequestBody: tc.FederationUserPost{
+ IDs: []int{GetUserID(t, "disalloweduser")()},
+ Replace: util.Ptr(false),
+ },
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"BAD REQUEST when INVALID FEDERATION ID": {
EndpointId: func() int { return -1 },
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"userIds": []int{}, "replace": false},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
+ RequestBody: tc.FederationUserPost{
+ IDs: []int{},
+ Replace: util.Ptr(false),
+ },
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"BAD REQUEST when INVALID USER ID": {
EndpointId: GetFederationID(t, "the.cname.com."),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"userIds": []int{-1}, "replace": false},
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
- },
- },
- "GET AFTER CHANGES": {
- "OK when CHANGES made": {
- EndpointId: GetFederationID(t, "the.cname.com."),
- ClientSession: TOSession,
- RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ RequestBody: tc.FederationUserPost{
+ IDs: []int{-1},
+ Replace: util.Ptr(false),
+ },
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
},
}
@@ -149,17 +164,8 @@ func TestFederationUsers(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- federationUser := tc.FederationUserPost{}
-
- 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, &federationUser)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetFederationUsers(testCase.EndpointId(), testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -168,6 +174,7 @@ func TestFederationUsers(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
+ federationUser := testCase.RequestBody
alerts, reqInf, err := testCase.ClientSession.CreateFederationUsers(testCase.EndpointId(), federationUser.IDs, *federationUser.Replace, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
diff --git a/traffic_ops/testing/api/v4/federations_test.go b/traffic_ops/testing/api/v4/federations_test.go
index 8025b6ed0e..3a66f14fe5 100644
--- a/traffic_ops/testing/api/v4/federations_test.go
+++ b/traffic_ops/testing/api/v4/federations_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"sort"
"testing"
@@ -37,7 +36,7 @@ func TestFederations(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.DeliveryServiceFederationResolverMappingRequest]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -67,25 +66,28 @@ func TestFederations(t *testing.T) {
},
})),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"OK when VALID request": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "federations": []map[string]interface{}{
- {
- "deliveryService": "ds1",
- "mappings": map[string]interface{}{
- "resolve4": []string{"0.0.0.0"},
- "resolve6": []string{"::1"},
- },
+ RequestBody: tc.DeliveryServiceFederationResolverMappingRequest{
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "ds1",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"0.0.0.0"},
+ Resolve6: []string{"::1"},
},
- {
- "deliveryService": "ds2",
- "mappings": map[string]interface{}{
- "resolve4": []string{"1.2.3.4/28"},
- "resolve6": []string{"1234::/110"},
- },
+ },
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "ds2",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"1.2.3.4/28"},
+ Resolve6: []string{"1234::/110"},
},
},
},
@@ -93,14 +95,12 @@ func TestFederations(t *testing.T) {
},
"CONFLICT when INVALID DELIVERY SERVICE": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "federations": []map[string]interface{}{
- {
- "deliveryService": "aoeuhtns",
- "mappings": map[string]interface{}{
- "resolve4": []string{"1.2.3.4/28"},
- "resolve6": []string{"dead::beef", "f1d0::f00d/82"},
- },
+ RequestBody: tc.DeliveryServiceFederationResolverMappingRequest{
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "aoeuhtns",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"1.2.3.4/28"},
+ Resolve6: []string{"dead::beef", "f1d0::f00d/82"},
},
},
},
@@ -110,21 +110,19 @@ func TestFederations(t *testing.T) {
"PUT": {
"OK when VALID request": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "federations": []map[string]interface{}{
- {
- "deliveryService": "ds1",
- "mappings": map[string]interface{}{
- "resolve4": []string{"192.0.2.0/25", "192.0.2.128/25"},
- "resolve6": []string{"2001:db8::/33", "2001:db8:8000::/33"},
- },
+ RequestBody: tc.DeliveryServiceFederationResolverMappingRequest{
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "ds1",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"192.0.2.0/25", "192.0.2.128/25"},
+ Resolve6: []string{"2001:db8::/33", "2001:db8:8000::/33"},
},
- {
- "deliveryService": "ds2",
- "mappings": map[string]interface{}{
- "resolve4": []string{"192.0.2.0/25", "192.0.2.128/25"},
- "resolve6": []string{"2001:db8::/33", "2001:db8:8000::/33"},
- },
+ },
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "ds2",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"192.0.2.0/25", "192.0.2.128/25"},
+ Resolve6: []string{"2001:db8::/33", "2001:db8:8000::/33"},
},
},
},
@@ -144,45 +142,25 @@ func TestFederations(t *testing.T) {
},
"CONFLICT when INVALID DELIVERY SERVICE": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "federations": []map[string]interface{}{
- {
- "deliveryService": "aoeuhtns",
- "mappings": map[string]interface{}{
- "resolve4": []string{"1.2.3.4/28"},
- "resolve6": []string{"dead::beef", "f1d0::f00d/82"},
- },
+ RequestBody: tc.DeliveryServiceFederationResolverMappingRequest{
+ tc.DeliveryServiceFederationResolverMapping{
+ DeliveryService: "aoeuhtns",
+ Mappings: tc.ResolverMapping{
+ Resolve4: []string{"1.2.3.4/28"},
+ Resolve6: []string{"dead::beef", "f1d0::f00d/82"},
},
},
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusConflict)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- federation := tc.DeliveryServiceFederationResolverMappingRequest{}
-
- if testCase.RequestBody != nil {
- for _, federationRequest := range testCase.RequestBody {
- dat, err := json.Marshal(federationRequest)
- assert.RequireNoError(t, err, "Error occurred when marshalling request body: %v", err)
- err = json.Unmarshal(dat, &federation)
- assert.RequireNoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.AllFederations(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -191,14 +169,14 @@ func TestFederations(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.AddFederationResolverMappingsForCurrentUser(federation, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.AddFederationResolverMappingsForCurrentUser(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.ReplaceFederationResolverMappingsForCurrentUser(federation, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.ReplaceFederationResolverMappingsForCurrentUser(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/logs_test.go b/traffic_ops/testing/api/v4/logs_test.go
index d72f7e2df2..deca5a40e0 100644
--- a/traffic_ops/testing/api/v4/logs_test.go
+++ b/traffic_ops/testing/api/v4/logs_test.go
@@ -30,21 +30,22 @@ import (
func TestLogs(t *testing.T) {
WithObjs(t, []TCObj{Roles, Tenants, Users}, func() { // Objs added to create logs when this test is run alone
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
- utils.ResponseLengthGreaterOrEqual(1)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1)),
},
"OK when VALID USERNAME parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"username": {"admin"}}},
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"username": {"admin"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseLengthGreaterOrEqual(1),
validateLogsFields(map[string]interface{}{"User": "admin"})),
},
"RESPONSE LENGTH matches LIMIT parameter": {
- ClientSession: TOSession, RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"10"}}},
- Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
- utils.ResponseHasLength(10)),
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"10"}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), utils.ResponseHasLength(10)),
},
},
}
diff --git a/traffic_ops/testing/api/v4/origins_test.go b/traffic_ops/testing/api/v4/origins_test.go
index 0d63525055..3ed2801362 100644
--- a/traffic_ops/testing/api/v4/origins_test.go
+++ b/traffic_ops/testing/api/v4/origins_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"strconv"
@@ -26,6 +25,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"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"
@@ -40,7 +40,7 @@ func TestOrigins(t *testing.T) {
tenant4UserSession := utils.CreateV4Session(t, Config.TrafficOps.URL, "tenant4user", "pa$$word", Config.Default.Session.TimeoutInSecs)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Origin]{
"GET": {
"OK when VALID request": {
ClientSession: TOSession,
@@ -162,113 +162,113 @@ func TestOrigins(t *testing.T) {
"POST": {
"BAD REQUEST when ALREADY EXISTS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin1",
- "cachegroup": "originCachegroup",
- "Coordinate": "coordinate1",
- "deliveryService": "ds1",
- "fqdn": "origin1.example.com",
- "ipAddress": "1.2.3.4",
- "ip6Address": "dead:beef:cafe::42",
- "port": 1234,
- "Profile": "ATS_EDGE_TIER_CACHE",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
- "isPrimary": true,
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin1"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ Coordinate: util.Ptr("coordinate1"),
+ DeliveryService: util.Ptr("ds1"),
+ FQDN: util.Ptr("origin1.example.com"),
+ IPAddress: util.Ptr("1.2.3.4"),
+ IP6Address: util.Ptr("dead:beef:cafe::42"),
+ Port: util.Ptr(1234),
+ Profile: util.Ptr("ATS_EDGE_TIER_CACHE"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
+ IsPrimary: util.Ptr(true),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"FORBIDDEN when CHILD TENANT CREATES ORIGIN OUTSIDE TENANCY": {
ClientSession: tenant4UserSession,
- RequestBody: map[string]interface{}{
- "name": "originTenancyTest",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origintenancy.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant3")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("originTenancyTest"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origintenancy.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant3")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
},
"NOT FOUND when CACHEGROUP DOESNT EXIST": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testcg",
- "cachegroupId": 10000000,
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.cachegroupId.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testcg"),
+ CachegroupID: util.Ptr(10000000),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.cachegroupId.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when PROFILEID DOESNT EXIST": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testprofile",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.profileId.com",
- "profileId": 1000000,
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testprofile"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.profileId.com"),
+ ProfileID: util.Ptr(1000000),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when COORDINATE DOESNT EXIST": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testcoordinate",
- "coordinateId": 10000000,
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.coordinate.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testcoordinate"),
+ CoordinateID: util.Ptr(10000000),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.coordinate.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"FORBIDDEN when INVALID TENANT": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testtenant",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.tenant.com",
- "protocol": "http",
- "tenantId": 11111111,
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testtenant"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.tenant.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(11111111),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
},
"BAD REQUEST when INVALID PROTOCOL": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testprotocol",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.protocol.com",
- "protocol": "httttpppss",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testprotocol"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.protocol.com"),
+ Protocol: util.Ptr("httttpppss"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID IPV4 ADDRESS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testip",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "test.ip.com",
- "ipAddress": "311.255.323.412",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testip"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("test.ip.com"),
+ IPAddress: util.Ptr("311.255.323.412"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID IPV6 ADDRESS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testipv6",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin1.example.com",
- "ip6Address": "badipv6::addresss",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testipv6"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin1.example.com"),
+ IP6Address: util.Ptr("badipv6::addresss"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -277,17 +277,17 @@ func TestOrigins(t *testing.T) {
"OK when VALID request": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "multiOriginCachegroup",
- "Coordinate": "coordinate2",
- "deliveryService": "ds3",
- "fqdn": "originupdated.example.com",
- "ipAddress": "1.2.3.4",
- "ip6Address": "0000::1111",
- "port": 1234,
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant2")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("multiOriginCachegroup"),
+ Coordinate: util.Ptr("coordinate2"),
+ DeliveryService: util.Ptr("ds3"),
+ FQDN: util.Ptr("originupdated.example.com"),
+ IPAddress: util.Ptr("1.2.3.4"),
+ IP6Address: util.Ptr("0000::1111"),
+ Port: util.Ptr(1234),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant2")()),
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateOriginsUpdateCreateFields("origin2", map[string]interface{}{"Cachegroup": "multiOriginCachegroup", "Coordinate": "coordinate2", "DeliveryService": "ds3",
@@ -296,145 +296,145 @@ func TestOrigins(t *testing.T) {
"FORBIDDEN when CHILD TENANT updates PARENT TENANT ORIGIN": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: tenant4UserSession,
- RequestBody: map[string]interface{}{
- "name": "testtenancy",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "testtenancy.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testtenancy"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("testtenancy.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
},
"NOT FOUND when ORIGIN DOESNT EXIST": {
EndpointId: func() int { return 1111111 },
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "testid",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "testid.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("testid"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("testid.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"BAD REQUEST when DELIVERY SERVICE DOESNT EXIST": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "deliveryServiceId": 11111111,
- "fqdn": "origin2.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ DeliveryServiceID: util.Ptr(11111111),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"NOT FOUND when CACHEGROUP DOESNT EXIST": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroupId": 1111111,
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin2.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ CachegroupID: util.Ptr(1111111),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when PROFILEID DOESNT EXIST": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin2.example.com",
- "profileId": 11111111,
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ ProfileID: util.Ptr(11111111),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"NOT FOUND when COORDINATE DOESNT EXIST": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "coordinateId": 1111111,
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin2.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ CoordinateID: util.Ptr(1111111),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
"FORBIDDEN when INVALID TENANT": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin1",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin1.example.com",
- "protocol": "http",
- "tenantId": 1111111,
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin1"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin1.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(1111111),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusForbidden)),
},
"BAD REQUEST when INVALID PROTOCOL": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds1")(),
- "fqdn": "origin2.example.com",
- "protocol": "htttttpssss",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds1")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("htttttpssss"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID IPV4 ADDRESS": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds2")(),
- "fqdn": "origin2.example.com",
- "ipAddress": "300.254.123.1",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds2")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ IPAddress: util.Ptr("300.254.123.1"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID IPV6 ADDRESS": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds2")(),
- "fqdn": "origin2.example.com",
- "ip6Address": "test::42",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds2")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ IP6Address: util.Ptr("test::42"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID PORT": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryServiceId": GetDeliveryServiceId(t, "ds2")(),
- "fqdn": "origin2.example.com",
- "port": 80000,
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryServiceID: util.Ptr(GetDeliveryServiceId(t, "ds2")()),
+ FQDN: util.Ptr("origin2.example.com"),
+ Port: util.Ptr(80000),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -442,26 +442,26 @@ func TestOrigins(t *testing.T) {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryService": "ds2",
- "fqdn": "origin2.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryService: util.Ptr("ds2"),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetOriginID(t, "origin2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "origin2",
- "cachegroup": "originCachegroup",
- "deliveryService": "ds2",
- "fqdn": "origin2.example.com",
- "protocol": "http",
- "tenantId": GetTenantID(t, "tenant1")(),
+ RequestBody: tc.Origin{
+ Name: util.Ptr("origin2"),
+ Cachegroup: util.Ptr("originCachegroup"),
+ DeliveryService: util.Ptr("ds2"),
+ FQDN: util.Ptr("origin2.example.com"),
+ Protocol: util.Ptr("http"),
+ TenantID: util.Ptr(GetTenantID(t, "tenant1")()),
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -484,15 +484,6 @@ func TestOrigins(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- origin := tc.Origin{}
-
- 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, &origin)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -503,14 +494,14 @@ func TestOrigins(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CreateOrigin(origin, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CreateOrigin(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.UpdateOrigin(testCase.EndpointId(), origin, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.UpdateOrigin(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/phys_locations_test.go b/traffic_ops/testing/api/v4/phys_locations_test.go
index 40c14cd061..13f988fcc9 100644
--- a/traffic_ops/testing/api/v4/phys_locations_test.go
+++ b/traffic_ops/testing/api/v4/phys_locations_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestPhysLocations(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.PhysLocation]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -91,38 +90,43 @@ func TestPhysLocations(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"limit": {"1"}, "page": {"0"}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"OK when VALID request": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "address": "100 blah lane",
- "city": "foo",
- "comments": "comment",
- "email": "bar@foobar.com",
- "name": "testPhysicalLocation",
- "phone": "111-222-3333",
- "region": "region1",
- "regionId": GetRegionID(t, "region1")(),
- "shortName": "testLocation1",
- "state": "CO",
- "zip": "80602",
+ RequestBody: tc.PhysLocation{
+ Address: "100 blah lane",
+ City: "foo",
+ Comments: "comment",
+ Email: "bar@foobar.com",
+ Name: "testPhysicalLocation",
+ Phone: "111-222-3333",
+ RegionName: "region1",
+ RegionID: GetRegionID(t, "region1")(),
+ ShortName: "testLocation1",
+ State: "CO",
+ Zip: "80602",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validatePhysicalLocationUpdateCreateFields("testPhysicalLocation", map[string]interface{}{"Name": "testPhysicalLocation"})),
},
"BAD REQUEST when REGION ID does NOT MATCH REGION NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "address": "1234 southern way",
- "city": "Atlanta",
- "name": "HotAtlanta",
- "phone": "404-222-2222",
- "region": "region1",
- "regionId": GetRegionID(t, "cdn-region2")(),
- "shortName": "atlanta",
- "state": "GA",
- "zip": "30301",
+ RequestBody: tc.PhysLocation{
+ Address: "1234 southern way",
+ City: "Atlanta",
+ Name: "HotAtlanta",
+ Phone: "404-222-2222",
+ RegionName: "region1",
+ RegionID: GetRegionID(t, "cdn-region2")(),
+ ShortName: "atlanta",
+ State: "GA",
+ Zip: "30301",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -131,15 +135,15 @@ func TestPhysLocations(t *testing.T) {
"OK when VALID request": {
EndpointId: GetPhysicalLocationID(t, "HotAtlanta"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "address": "1234 southern way",
- "city": "NewCity",
- "name": "HotAtlanta",
- "phone": "404-222-2222",
- "regionId": GetRegionID(t, "region1")(),
- "shortName": "atlanta",
- "state": "GA",
- "zip": "30301",
+ RequestBody: tc.PhysLocation{
+ Address: "1234 southern way",
+ City: "NewCity",
+ Name: "HotAtlanta",
+ Phone: "404-222-2222",
+ RegionID: GetRegionID(t, "region1")(),
+ ShortName: "atlanta",
+ State: "GA",
+ Zip: "30301",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validatePhysicalLocationUpdateCreateFields("HotAtlanta", map[string]interface{}{"City": "NewCity"})),
@@ -148,28 +152,28 @@ func TestPhysLocations(t *testing.T) {
EndpointId: GetPhysicalLocationID(t, "HotAtlanta"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "address": "1234 southern way",
- "city": "Atlanta",
- "regionId": GetRegionID(t, "region1")(),
- "name": "HotAtlanta",
- "shortName": "atlanta",
- "state": "GA",
- "zip": "30301",
+ RequestBody: tc.PhysLocation{
+ Address: "1234 southern way",
+ City: "Atlanta",
+ RegionID: GetRegionID(t, "region1")(),
+ Name: "HotAtlanta",
+ ShortName: "atlanta",
+ State: "GA",
+ Zip: "30301",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetPhysicalLocationID(t, "HotAtlanta"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "address": "1234 southern way",
- "city": "Atlanta",
- "regionId": GetRegionID(t, "region1")(),
- "name": "HotAtlanta",
- "shortName": "atlanta",
- "state": "GA",
- "zip": "30301",
+ RequestBody: tc.PhysLocation{
+ Address: "1234 southern way",
+ City: "Atlanta",
+ RegionID: GetRegionID(t, "region1")(),
+ Name: "HotAtlanta",
+ ShortName: "atlanta",
+ State: "GA",
+ Zip: "30301",
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -182,29 +186,13 @@ func TestPhysLocations(t *testing.T) {
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- pl := tc.PhysLocation{}
-
- 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, &pl)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetPhysLocations(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -213,14 +201,14 @@ func TestPhysLocations(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreatePhysLocation(pl, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreatePhysLocation(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdatePhysLocation(testCase.EndpointId(), pl, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdatePhysLocation(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/ping_test.go b/traffic_ops/testing/api/v4/ping_test.go
index 1cce12c0ff..10e6b0a4bc 100644
--- a/traffic_ops/testing/api/v4/ping_test.go
+++ b/traffic_ops/testing/api/v4/ping_test.go
@@ -20,17 +20,20 @@ import (
"testing"
"github.com/apache/trafficcontrol/traffic_ops/testing/api/utils"
+ client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)
func TestPing(t *testing.T) {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
- ClientSession: TOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ ClientSession: TOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"OK when UNAUTHENTICATED": {
- ClientSession: NoAuthTOSession, Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ ClientSession: NoAuthTOSession,
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
},
}
diff --git a/traffic_ops/testing/api/v4/profiles_export_test.go b/traffic_ops/testing/api/v4/profiles_export_test.go
index 443b6415cb..c02efcc5cb 100644
--- a/traffic_ops/testing/api/v4/profiles_export_test.go
+++ b/traffic_ops/testing/api/v4/profiles_export_test.go
@@ -24,12 +24,13 @@ import (
"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 TestProfilesExport(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, struct{}]{
"GET": {
"OK when VALID request": {
EndpointId: GetProfileID(t, "EDGE1"),
diff --git a/traffic_ops/testing/api/v4/profiles_import_test.go b/traffic_ops/testing/api/v4/profiles_import_test.go
index 5f15eb21e3..2f21debea0 100644
--- a/traffic_ops/testing/api/v4/profiles_import_test.go
+++ b/traffic_ops/testing/api/v4/profiles_import_test.go
@@ -16,43 +16,42 @@
package v4
import (
- "encoding/json"
"net/http"
"reflect"
"testing"
"github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/lib/go-util"
"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 TestProfilesImport(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ProfileImportRequest]{
"POST": {
"OK when VALID request": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "importProfile": map[string]interface{}{
- "profile": map[string]interface{}{
- "name": "GLOBAL",
- "description": "Global Traffic Ops profile",
- "cdn": "cdn1",
- "type": "UNK_PROFILE",
+ RequestBody: tc.ProfileImportRequest{
+ Profile: tc.ProfileExportImportNullable{
+ Name: util.Ptr("GLOBAL"),
+ Description: util.Ptr("Global Traffic Ops profile"),
+ CDNName: util.Ptr("cdn1"),
+ Type: util.Ptr("UNK_PROFILE"),
+ },
+ Parameters: []tc.ProfileExportImportParameterNullable{
+ {
+ ConfigFile: util.Ptr("global"),
+ Name: util.Ptr("tm.instance_name"),
+ Value: util.Ptr("Traffic Ops CDN"),
},
- "parameters": []map[string]interface{}{
- {
- "config_file": "global",
- "name": "tm.instance_name",
- "value": "Traffic Ops CDN",
- },
- {
- "config_file": "global",
- "name": "tm.toolname",
- "value": "Traffic Ops",
- },
+ {
+ ConfigFile: util.Ptr("global"),
+ Name: util.Ptr("tm.toolname"),
+ Value: util.Ptr("Traffic Ops"),
},
},
},
@@ -62,20 +61,18 @@ func TestProfilesImport(t *testing.T) {
},
"BAD REQUEST when SPACE in PROFILE NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "importProfile": map[string]interface{}{
- "profile": map[string]interface{}{
- "name": "GLOBAL SPACES",
- "description": "Global Traffic Ops profile",
- "cdn": "cdn1",
- "type": "UNK_PROFILE",
- },
- "parameters": []map[string]interface{}{
- {
- "config_file": "global",
- "name": "tm.instance_name",
- "value": "Traffic Ops CDN",
- },
+ RequestBody: tc.ProfileImportRequest{
+ Profile: tc.ProfileExportImportNullable{
+ Name: util.Ptr("GLOBAL SPACES"),
+ Description: util.Ptr("Global Traffic Ops profile"),
+ CDNName: util.Ptr("cdn1"),
+ Type: util.Ptr("UNK_PROFILE"),
+ },
+ Parameters: []tc.ProfileExportImportParameterNullable{
+ {
+ ConfigFile: util.Ptr("global"),
+ Name: util.Ptr("tm.instance_name"),
+ Value: util.Ptr("Traffic Ops CDN"),
},
},
},
@@ -87,21 +84,10 @@ func TestProfilesImport(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- importProfile := tc.ProfileImportRequest{}
-
- if testCase.RequestBody != nil {
- if importProfileBody, ok := testCase.RequestBody["importProfile"]; ok {
- dat, err := json.Marshal(importProfileBody)
- assert.NoError(t, err, "Error occurred when marshalling request body: %v", err)
- err = json.Unmarshal(dat, &importProfile)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
- }
-
switch method {
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.ImportProfile(importProfile, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.ImportProfile(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/profiles_name_copy_test.go b/traffic_ops/testing/api/v4/profiles_name_copy_test.go
index ce05fd68c0..b4afff6b46 100644
--- a/traffic_ops/testing/api/v4/profiles_name_copy_test.go
+++ b/traffic_ops/testing/api/v4/profiles_name_copy_test.go
@@ -16,49 +16,48 @@
package v4
import (
- "encoding/json"
"net/http"
"testing"
"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"
+ client "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)
func TestProfilesNameCopy(t *testing.T) {
WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, ProfileParameters}, func() {
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ProfileCopy]{
"POST": {
"OK when VALID request": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "profileCopyFrom": "EDGE1",
- "name": "edge1-copy",
+ RequestBody: tc.ProfileCopy{
+ ExistingName: "EDGE1",
+ Name: "edge1-copy",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
},
"BAD REQUEST when NEW PROFILE NAME has SPACES": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "profileCopyFrom": "EDGE1",
- "name": "Profile Has Spaces",
+ RequestBody: tc.ProfileCopy{
+ ExistingName: "EDGE1",
+ Name: "Profile Has Spaces",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when PROFILE NAME ALREADY EXISTS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "profileCopyFrom": "EDGE1",
- "name": "EDGE2",
+ RequestBody: tc.ProfileCopy{
+ ExistingName: "EDGE1",
+ Name: "EDGE2",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"NOT FOUND when PROFILE to COPY FROM DOESNT EXIST": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "profileCopyFrom": "DOESNTEXIST",
- "name": "profileCopyFail",
+ RequestBody: tc.ProfileCopy{
+ ExistingName: "DOESNTEXIST",
+ Name: "profileCopyFail",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
@@ -68,19 +67,10 @@ func TestProfilesNameCopy(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- profileCopy := tc.ProfileCopy{}
-
- 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, &profileCopy)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CopyProfile(profileCopy, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CopyProfile(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/profiles_test.go b/traffic_ops/testing/api/v4/profiles_test.go
index 999f618c6c..d6a5c93955 100644
--- a/traffic_ops/testing/api/v4/profiles_test.go
+++ b/traffic_ops/testing/api/v4/profiles_test.go
@@ -16,7 +16,6 @@
package v4
import (
- "encoding/json"
"net/http"
"net/url"
"strconv"
@@ -38,7 +37,7 @@ func TestProfiles(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Profile]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -111,61 +110,53 @@ func TestProfiles(t *testing.T) {
"POST": {
"BAD REQUEST when NAME has SPACES": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "name has spaces test",
- "name": "name has space",
- "type": tc.CacheServerProfileType,
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Description: "name has spaces test",
+ Name: "name has space",
+ Type: tc.CacheServerProfileType,
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING ALL FIELDS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": 0,
- "description": "",
- "name": "",
- "type": "",
- },
- Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+ RequestBody: tc.Profile{},
+ Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID CDN ID": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": 0,
- "description": "description",
- "name": "badprofile",
- "type": tc.CacheServerProfileType,
+ RequestBody: tc.Profile{
+ CDNID: 0,
+ Description: "description",
+ Name: "badprofile",
+ Type: tc.CacheServerProfileType,
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING DESCRIPTION FIELD": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "",
- "name": "missing_description",
- "type": tc.CacheServerProfileType,
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Name: "missing_description",
+ Type: tc.CacheServerProfileType,
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING NAME FIELD": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "missing name",
- "name": "",
- "type": tc.CacheServerProfileType,
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Description: "missing name",
+ Type: tc.CacheServerProfileType,
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when MISSING TYPE FIELD": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "missing type",
- "name": "missing_type",
- "type": "",
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Description: "missing type",
+ Name: "missing_type",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
@@ -174,12 +165,12 @@ func TestProfiles(t *testing.T) {
"OK when VALID REQUEST": {
EndpointId: GetProfileID(t, "EDGE2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn2")(),
- "description": "edge2 description updated",
- "name": "EDGE2UPDATED",
- "routing_disabled": false,
- "type": "TR_PROFILE",
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn2")(),
+ Description: "edge2 description updated",
+ Name: "EDGE2UPDATED",
+ RoutingDisabled: false,
+ Type: "TR_PROFILE",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateProfilesUpdateCreateFields("EDGE2UPDATED",
@@ -190,24 +181,24 @@ func TestProfiles(t *testing.T) {
EndpointId: GetProfileID(t, "CCR1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "cdn1 description",
- "name": "CCR1",
- "routing_disabled": false,
- "type": "TR_PROFILE",
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Description: "cdn1 description",
+ Name: "CCR1",
+ RoutingDisabled: false,
+ Type: "TR_PROFILE",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetProfileID(t, "CCR1"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "cdn": GetCDNID(t, "cdn1")(),
- "description": "cdn1 description",
- "name": "CCR1",
- "routing_disabled": false,
- "type": "TR_PROFILE",
+ RequestBody: tc.Profile{
+ CDNID: GetCDNID(t, "cdn1")(),
+ Description: "cdn1 description",
+ Name: "CCR1",
+ RoutingDisabled: false,
+ Type: "TR_PROFILE",
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -218,15 +209,6 @@ func TestProfiles(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- profile := tc.Profile{}
-
- 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, &profile)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -237,14 +219,14 @@ func TestProfiles(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateProfile(profile, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateProfile(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateProfile(testCase.EndpointId(), profile, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateProfile(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/regions_test.go b/traffic_ops/testing/api/v4/regions_test.go
index 93a6559952..5c45668d6f 100644
--- a/traffic_ops/testing/api/v4/regions_test.go
+++ b/traffic_ops/testing/api/v4/regions_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -39,7 +38,7 @@ func TestRegions(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Region]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -122,10 +121,10 @@ func TestRegions(t *testing.T) {
"POST": {
"NOT FOUND when DIVISION DOESNT EXIST": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "invalidDivision",
- "division": 99999999,
- "divisionName": "doesntexist",
+ RequestBody: tc.Region{
+ Name: "invalidDivision",
+ Division: 99999999,
+ DivisionName: "doesntexist",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusNotFound)),
},
@@ -134,10 +133,10 @@ func TestRegions(t *testing.T) {
"OK when VALID request": {
EndpointId: GetRegionID(t, "cdn-region2"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "newName",
- "division": GetDivisionID(t, "cdn-div2")(),
- "divisionName": "cdn-div2",
+ RequestBody: tc.Region{
+ Name: "newName",
+ Division: GetDivisionID(t, "cdn-div2")(),
+ DivisionName: "cdn-div2",
},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateRegionsUpdateCreateFields("newName", map[string]interface{}{"Name": "newName"})),
@@ -146,20 +145,20 @@ func TestRegions(t *testing.T) {
EndpointId: GetRegionID(t, "region1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
- RequestBody: map[string]interface{}{
- "name": "newName",
- "division": GetDivisionID(t, "division1")(),
- "divisionName": "division1",
+ RequestBody: tc.Region{
+ Name: "newName",
+ Division: GetDivisionID(t, "division1")(),
+ DivisionName: "division1",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointId: GetRegionID(t, "region1"),
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "newName",
- "division": GetDivisionID(t, "division1")(),
- "divisionName": "division1",
+ RequestBody: tc.Region{
+ Name: "newName",
+ Division: GetDivisionID(t, "division1")(),
+ DivisionName: "division1",
},
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}}},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
@@ -187,16 +186,7 @@ func TestRegions(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- region := tc.Region{}
regionName := ""
-
- 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, ®ion)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -207,14 +197,14 @@ func TestRegions(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateRegion(region, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateRegion(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateRegion(testCase.EndpointId(), region, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateRegion(testCase.EndpointId(), testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/roles_test.go b/traffic_ops/testing/api/v4/roles_test.go
index c5124b4705..195eb0b2aa 100644
--- a/traffic_ops/testing/api/v4/roles_test.go
+++ b/traffic_ops/testing/api/v4/roles_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -38,7 +37,7 @@ func TestRoles(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.RoleV4]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
@@ -60,13 +59,18 @@ func TestRoles(t *testing.T) {
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"orderby": {"name"}, "sortOrder": {"desc"}}},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK), validateRoleDescSort()),
},
+ "OK when CHANGES made": {
+ ClientSession: TOSession,
+ RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfModifiedSince: {currentTimeRFC}}},
+ Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK)),
+ },
},
"POST": {
"BAD REQUEST when MISSING NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "description": "missing name",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Description: "missing name",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -75,9 +79,9 @@ func TestRoles(t *testing.T) {
},
"BAD REQUEST when MISSING DESCRIPTION": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "noDescription",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "noDescription",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -86,10 +90,10 @@ func TestRoles(t *testing.T) {
},
"BAD REQUEST when ROLE NAME ALREADY EXISTS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{
- "name": "new_admin",
- "description": "description",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "new_admin",
+ Description: "description",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -101,10 +105,10 @@ func TestRoles(t *testing.T) {
"OK when VALID request": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"update_role"}}},
- RequestBody: map[string]interface{}{
- "name": "new_name",
- "description": "new updated description",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "new_name",
+ Description: "new updated description",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -115,9 +119,9 @@ func TestRoles(t *testing.T) {
"BAD REQUEST when MISSING NAME": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"another_role"}}},
- RequestBody: map[string]interface{}{
- "description": "missing name",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Description: "missing name",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -127,9 +131,9 @@ func TestRoles(t *testing.T) {
"BAD REQUEST when MISSING DESCRIPTION": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"another_role"}}},
- RequestBody: map[string]interface{}{
- "name": "noDescription",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "noDescription",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -139,10 +143,10 @@ func TestRoles(t *testing.T) {
"BAD REQUEST when ADMIN ROLE": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"admin"}}},
- RequestBody: map[string]interface{}{
- "name": "adminUpdated",
- "description": "description",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "adminUpdated",
+ Description: "description",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -152,10 +156,10 @@ func TestRoles(t *testing.T) {
"NOT FOUND when ROLE DOESNT EXIST": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"doesntexist"}}},
- RequestBody: map[string]interface{}{
- "name": "doesntexist",
- "description": "description",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "doesntexist",
+ Description: "description",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -165,10 +169,10 @@ func TestRoles(t *testing.T) {
"BAD REQUEST when ROLE NAME ALREADY EXISTS": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"another_role"}}},
- RequestBody: map[string]interface{}{
- "name": "new_admin",
- "description": "description",
- "permissions": []string{
+ RequestBody: tc.RoleV4{
+ Name: "new_admin",
+ Description: "description",
+ Permissions: []string{
"all-read",
"all-write",
},
@@ -181,9 +185,9 @@ func TestRoles(t *testing.T) {
QueryParameters: url.Values{"name": {"another_role"}},
Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}},
},
- RequestBody: map[string]interface{}{
- "name": "another_role",
- "description": "super-user 3",
+ RequestBody: tc.RoleV4{
+ Name: "another_role",
+ Description: "super-user 3",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
@@ -193,9 +197,9 @@ func TestRoles(t *testing.T) {
QueryParameters: url.Values{"name": {"another_role"}},
Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}},
},
- RequestBody: map[string]interface{}{
- "name": "another_role",
- "description": "super-user 3",
+ RequestBody: tc.RoleV4{
+ Name: "another_role",
+ Description: "super-user 3",
},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
@@ -207,29 +211,13 @@ func TestRoles(t *testing.T) {
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
},
- "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)),
- },
- },
}
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- role := tc.RoleV4{}
-
- 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, &role)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
- case "GET", "GET AFTER CHANGES":
+ case "GET":
t.Run(name, func(t *testing.T) {
resp, reqInf, err := testCase.ClientSession.GetRoles(testCase.RequestOpts)
for _, check := range testCase.Expectations {
@@ -238,14 +226,14 @@ func TestRoles(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.CreateRole(role, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.CreateRole(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- alerts, reqInf, err := testCase.ClientSession.UpdateRole(testCase.RequestOpts.QueryParameters["name"][0], role, testCase.RequestOpts)
+ alerts, reqInf, err := testCase.ClientSession.UpdateRole(testCase.RequestOpts.QueryParameters["name"][0], testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, nil, alerts, err)
}
diff --git a/traffic_ops/testing/api/v4/server_capabilities_test.go b/traffic_ops/testing/api/v4/server_capabilities_test.go
index 6f0ef1cf4b..c34e926387 100644
--- a/traffic_ops/testing/api/v4/server_capabilities_test.go
+++ b/traffic_ops/testing/api/v4/server_capabilities_test.go
@@ -16,7 +16,6 @@ package v4
*/
import (
- "encoding/json"
"net/http"
"net/url"
"sort"
@@ -37,7 +36,7 @@ func TestServerCapabilities(t *testing.T) {
currentTime := time.Now().UTC().Add(-15 * time.Second)
currentTimeRFC := currentTime.Format(time.RFC1123)
- methodTests := utils.V4TestCase{
+ methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ServerCapability]{
"GET": {
"OK when VALID request": {
ClientSession: TOSession,
@@ -58,12 +57,12 @@ func TestServerCapabilities(t *testing.T) {
"POST": {
"BAD REQUEST when ALREADY EXISTS": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"name": "foo"},
+ RequestBody: tc.ServerCapability{Name: "foo"},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID NAME": {
ClientSession: TOSession,
- RequestBody: map[string]interface{}{"name": "b@dname"},
+ RequestBody: tc.ServerCapability{Name: "b@dname"},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
},
@@ -71,7 +70,7 @@ func TestServerCapabilities(t *testing.T) {
"OK when VALID request": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"blah"}}},
- RequestBody: map[string]interface{}{"name": "newname"},
+ RequestBody: tc.ServerCapability{Name: "newname"},
Expectations: utils.CkRequest(utils.NoError(), utils.HasStatus(http.StatusOK),
validateServerCapabilitiesUpdateFields(map[string]interface{}{"Name": "newname"}),
validateSSCFieldsOnServerCapabilityUpdate("newname", map[string]interface{}{"ServerCapability": "newname"})),
@@ -79,7 +78,7 @@ func TestServerCapabilities(t *testing.T) {
"BAD REQUEST when NAME DOESNT EXIST": {
ClientSession: TOSession,
RequestOpts: client.RequestOptions{QueryParameters: url.Values{"name": {"invalid"}}},
- RequestBody: map[string]interface{}{"name": "newname"},
+ RequestBody: tc.ServerCapability{Name: "newname"},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"PRECONDITION FAILED when updating with IMS & IUS Headers": {
@@ -88,7 +87,7 @@ func TestServerCapabilities(t *testing.T) {
QueryParameters: url.Values{"name": {"disk"}},
Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}},
},
- RequestBody: map[string]interface{}{"name": "newname"},
+ RequestBody: tc.ServerCapability{Name: "newname"},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
@@ -97,7 +96,7 @@ func TestServerCapabilities(t *testing.T) {
QueryParameters: url.Values{"name": {"disk"}},
Header: http.Header{rfc.IfMatch: {rfc.ETag(currentTime)}},
},
- RequestBody: map[string]interface{}{"name": "newname"},
+ RequestBody: tc.ServerCapability{Name: "newname"},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusPreconditionFailed)),
},
},
@@ -118,15 +117,6 @@ func TestServerCapabilities(t *testing.T) {
for method, testCases := range methodTests {
t.Run(method, func(t *testing.T) {
for name, testCase := range testCases {
- serverCapability := tc.ServerCapability{}
-
- 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, &serverCapability)
- assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
- }
-
switch method {
case "GET":
t.Run(name, func(t *testing.T) {
@@ -137,14 +127,14 @@ func TestServerCapabilities(t *testing.T) {
})
case "POST":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.CreateServerCapability(serverCapability, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.CreateServerCapability(testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}
})
case "PUT":
t.Run(name, func(t *testing.T) {
- resp, reqInf, err := testCase.ClientSession.UpdateServerCapability(testCase.RequestOpts.QueryParameters["name"][0], serverCapability, testCase.RequestOpts)
+ resp, reqInf, err := testCase.ClientSession.UpdateServerCapability(testCase.RequestOpts.QueryParameters["name"][0], testCase.RequestBody, testCase.RequestOpts)
for _, check := range testCase.Expectations {
check(t, reqInf, resp.Response, resp.Alerts, err)
}