Skip to content

Commit

Permalink
updated tests for profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rimashah25 committed Aug 18, 2023
1 parent 5e7b07f commit fc563a5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/go-tc/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type Profile struct {
Parameters []ParameterNullable `json:"params,omitempty"`
}

// ProfilesResponseV5 is a list of profiles returned by GET requests.
type ProfilesResponseV5 struct {
Response []ProfileV5 `json:"response"`
Alerts
}

// A ProfileV5 represents a set of configuration for a server or Delivery Service
// which may be reused to allow sharing configuration across the objects to
// which it is assigned. Note: Field LastUpdated represents RFC3339
Expand Down
4 changes: 2 additions & 2 deletions traffic_ops/testing/api/v5/cdn_locks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func TestCDNLocks(t *testing.T) {
}
},
"PROFILE POST": func(t *testing.T) {
profile := tc.Profile{}
profile := tc.ProfileV5{}
err = json.Unmarshal(dat, &profile)
assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
alerts, reqInf, err := testCase.ClientSession.CreateProfile(profile, testCase.RequestOpts)
Expand All @@ -625,7 +625,7 @@ func TestCDNLocks(t *testing.T) {
}
},
"PROFILE PUT": func(t *testing.T) {
profile := tc.Profile{}
profile := tc.ProfileV5{}
err = json.Unmarshal(dat, &profile)
assert.NoError(t, err, "Error occurred when unmarshalling request body: %v", err)
alerts, reqInf, err := testCase.ClientSession.UpdateProfile(testCase.EndpointID(), profile, testCase.RequestOpts)
Expand Down
24 changes: 12 additions & 12 deletions traffic_ops/testing/api/v5/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestProfiles(t *testing.T) {
currentTimeRFC := currentTime.Format(time.RFC1123)
tomorrow := currentTime.AddDate(0, 0, 1).Format(time.RFC1123)

methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.Profile]{
methodTests := utils.TestCase[client.Session, client.RequestOptions, tc.ProfileV5]{
"GET": {
"NOT MODIFIED when NO CHANGES made": {
ClientSession: TOSession,
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestProfiles(t *testing.T) {
"POST": {
"BAD REQUEST when NAME has SPACES": {
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Description: "name has spaces test",
Name: "name has space",
Expand All @@ -120,12 +120,12 @@ func TestProfiles(t *testing.T) {
},
"BAD REQUEST when MISSING ALL FIELDS": {
ClientSession: TOSession,
RequestBody: tc.Profile{},
RequestBody: tc.ProfileV5{},
Expectations: utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
},
"BAD REQUEST when INVALID CDN ID": {
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: 0,
Description: "description",
Name: "badprofile",
Expand All @@ -135,7 +135,7 @@ func TestProfiles(t *testing.T) {
},
"BAD REQUEST when MISSING DESCRIPTION FIELD": {
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Name: "missing_description",
Type: tc.CacheServerProfileType,
Expand All @@ -144,7 +144,7 @@ func TestProfiles(t *testing.T) {
},
"BAD REQUEST when MISSING NAME FIELD": {
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Description: "missing name",
Type: tc.CacheServerProfileType,
Expand All @@ -153,7 +153,7 @@ func TestProfiles(t *testing.T) {
},
"BAD REQUEST when MISSING TYPE FIELD": {
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Description: "missing type",
Name: "missing_type",
Expand All @@ -165,7 +165,7 @@ func TestProfiles(t *testing.T) {
"OK when VALID REQUEST": {
EndpointID: GetProfileID(t, "EDGE2"),
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn2")(),
Description: "edge2 description updated",
Name: "EDGE2UPDATED",
Expand All @@ -181,7 +181,7 @@ func TestProfiles(t *testing.T) {
EndpointID: GetProfileID(t, "CCR1"),
ClientSession: TOSession,
RequestOpts: client.RequestOptions{Header: http.Header{rfc.IfUnmodifiedSince: {currentTimeRFC}}},
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Description: "cdn1 description",
Name: "CCR1",
Expand All @@ -193,7 +193,7 @@ func TestProfiles(t *testing.T) {
"PRECONDITION FAILED when updating with IFMATCH ETAG Header": {
EndpointID: GetProfileID(t, "CCR1"),
ClientSession: TOSession,
RequestBody: tc.Profile{
RequestBody: tc.ProfileV5{
CDNID: GetCDNID(t, "cdn1")(),
Description: "cdn1 description",
Name: "CCR1",
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestProfiles(t *testing.T) {
func validateProfilesFields(expectedResp map[string]interface{}) utils.CkReqFunc {
return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ tc.Alerts, _ error) {
assert.RequireNotNil(t, resp, "Expected Profiles response to not be nil.")
profileResp := resp.([]tc.Profile)
profileResp := resp.([]tc.ProfileV5)
for field, expected := range expectedResp {
for _, profile := range profileResp {
switch field {
Expand Down Expand Up @@ -291,7 +291,7 @@ func validateProfilesUpdateCreateFields(name string, expectedResp map[string]int

func validateProfilesPagination(paginationParam string) utils.CkReqFunc {
return func(t *testing.T, _ toclientlib.ReqInf, resp interface{}, _ tc.Alerts, _ error) {
paginationResp := resp.([]tc.Profile)
paginationResp := resp.([]tc.ProfileV5)

opts := client.NewRequestOptions()
opts.QueryParameters.Set("orderby", "id")
Expand Down
51 changes: 51 additions & 0 deletions traffic_ops/traffic_ops_golang/dbhelpers/db_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,54 @@ func TestDivisionExists(t *testing.T) {
})
}
}

func TestProfileExists(t *testing.T) {
var testCases = []struct {
description string
id string
expectedError error
exists bool
}{
{
description: "Success: Get valid Profile",
id: "1",
expectedError: nil,
exists: true,
},
{
description: "Failure: Profile not in DB",
id: "5",
expectedError: sql.ErrNoRows,
exists: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mockDB.Close()

db := sqlx.NewDb(mockDB, "sqlmock")
defer db.Close()

mock.ExpectBegin()
rows := sqlmock.NewRows([]string{"EXISTS"})
if testCase.exists {
rows = rows.AddRow(1)
}
mock.ExpectQuery("SELECT").WillReturnRows(rows)
mock.ExpectCommit()

profileExists, err := ASNExists(db.MustBegin().Tx, testCase.id)
if testCase.exists != profileExists {
t.Errorf("Expected return exists: %t, actual %t", testCase.exists, profileExists)
}

if !errors.Is(err, testCase.expectedError) {
t.Errorf("getSCInfo expected: %s, actual: %s", testCase.expectedError, err)
}
})
}
}
2 changes: 1 addition & 1 deletion traffic_ops/traffic_ops_golang/profile/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func Read(w http.ResponseWriter, r *http.Request) {
profileInterfaces = append(profileInterfaces, profile)
}

api.WriteResp(w, r, profileList)
api.WriteResp(w, r, profileInterfaces)
return
}

Expand Down
4 changes: 2 additions & 2 deletions traffic_ops/v5-client/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (to *Session) GetParametersByProfileName(profileName string, opts RequestOp
}

// GetProfiles returns all Profiles stored in Traffic Ops.
func (to *Session) GetProfiles(opts RequestOptions) (tc.ProfilesResponse, toclientlib.ReqInf, error) {
var data tc.ProfilesResponse
func (to *Session) GetProfiles(opts RequestOptions) (tc.ProfilesResponseV5, toclientlib.ReqInf, error) {
var data tc.ProfilesResponseV5
reqInf, err := to.get(apiProfiles, opts, &data)
return data, reqInf, err
}
Expand Down

0 comments on commit fc563a5

Please sign in to comment.