You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2018/04/06 20:45:57 UTC

[incubator-trafficcontrol] 07/15: added the API tests and TO Client methods for profileparameters

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

mitchell852 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit 3fdc4732dce4320383b080954ba5530bfed518e9
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Tue Apr 3 12:32:33 2018 -0600

    added the API tests and TO Client methods for profileparameters
---
 lib/go-tc/profile_parameters.go                    |  8 +--
 lib/go-tc/v13/profile_parameters.go                |  8 +--
 traffic_ops/client/v13/profile_parameter.go        | 60 ++++--------------
 .../testing/api/v13/profile_parameters_test.go     | 74 +++++++++++-----------
 traffic_ops/testing/api/v13/tc-fixtures.json       | 10 +++
 traffic_ops/testing/api/v13/traffic_control.go     | 34 +++++-----
 6 files changed, 84 insertions(+), 110 deletions(-)

diff --git a/lib/go-tc/profile_parameters.go b/lib/go-tc/profile_parameters.go
index a2d7dee..c31eb9f 100644
--- a/lib/go-tc/profile_parameters.go
+++ b/lib/go-tc/profile_parameters.go
@@ -28,16 +28,16 @@ type ProfileParametersResponse struct {
 type ProfileParameter struct {
 	LastUpdated TimeNoMod `json:"lastUpdated"`
 	Profile     string    `json:"profile"`
-	ProfileId   int       `json:"profileId"`
+	ProfileID   int       `json:"profileId"`
 	Parameter   string    `json:"parameter"`
-	ParameterId int       `json:"profileId"`
+	ParameterID int       `json:"profileId"`
 }
 
 // ProfileParameterNullable ...
 type ProfileParameterNullable struct {
 	LastUpdated *TimeNoMod `json:"lastUpdated" db:"last_updated"`
 	Profile     *string    `json:"profile" db:"profile"`
-	ProfileId   *int       `json:"profileId" db:"profile_id"`
+	ProfileID   *int       `json:"profileId" db:"profile_id"`
 	Parameter   *string    `json:"parameter" db:"parameter"`
-	ParameterId *int       `json:"parameterId" db:"parameter_id"`
+	ParameterID *int       `json:"parameterId" db:"parameter_id"`
 }
diff --git a/lib/go-tc/v13/profile_parameters.go b/lib/go-tc/v13/profile_parameters.go
index be3d1a2..954d6f0 100644
--- a/lib/go-tc/v13/profile_parameters.go
+++ b/lib/go-tc/v13/profile_parameters.go
@@ -30,16 +30,16 @@ type ProfileParametersResponse struct {
 type ProfileParameter struct {
 	LastUpdated tc.TimeNoMod `json:"lastUpdated"`
 	Profile     string       `json:"profile"`
-	ProfileId   int          `json:"profileId"`
+	ProfileID   int          `json:"profileId"`
 	Parameter   string       `json:"parameter"`
-	ParameterId int          `json:"profileId"`
+	ParameterID int          `json:"parameterId"`
 }
 
 // ProfileParameterNullable ...
 type ProfileParameterNullable struct {
 	LastUpdated *tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
 	Profile     *string       `json:"profile" db:"profile"`
-	ProfileId   *int          `json:"profileId" db:"profile_id"`
+	ProfileID   *int          `json:"profileId" db:"profile_id"`
 	Parameter   *string       `json:"parameter" db:"parameter"`
-	ParameterId *int          `json:"parameterId" db:"parameter_id"`
+	ParameterID *int          `json:"parameterId" db:"parameter_id"`
 }
diff --git a/traffic_ops/client/v13/profile_parameter.go b/traffic_ops/client/v13/profile_parameter.go
index aaa3fac..f4af45e 100644
--- a/traffic_ops/client/v13/profile_parameter.go
+++ b/traffic_ops/client/v13/profile_parameter.go
@@ -20,17 +20,19 @@ import (
 	"fmt"
 	"net"
 	"net/http"
-	"strconv"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc"
+	"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
 )
 
 const (
-	API_v13_Profile_Parameters = "/api/1.3/profile_parameters"
+	API_v13_Profile_Parameters = "/api/1.3/profileparameters"
+	ProfileIdQueryParam        = "profileId"
+	ParameterIdQueryParam      = "parameterId"
 )
 
 // Create a ProfileParameter
-func (to *Session) CreateProfileParameter(pp tc.ProfileParameter) (tc.Alerts, ReqInf, error) {
+func (to *Session) CreateProfileParameter(pp v13.ProfileParameter) (tc.Alerts, ReqInf, error) {
 
 	var remoteAddr net.Addr
 	reqBody, err := json.Marshal(pp)
@@ -48,28 +50,8 @@ func (to *Session) CreateProfileParameter(pp tc.ProfileParameter) (tc.Alerts, Re
 	return alerts, reqInf, nil
 }
 
-// Update a Profile Parameter by Profile
-func (to *Session) UpdateParameterByProfile(id int, pp tc.ProfileParameter) (tc.Alerts, ReqInf, error) {
-
-	var remoteAddr net.Addr
-	reqBody, err := json.Marshal(pp)
-	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
-	if err != nil {
-		return tc.Alerts{}, reqInf, err
-	}
-	URI := fmt.Sprintf("%s/%d", API_v13_Profile_Parameters, id)
-	resp, remoteAddr, err := to.request(http.MethodPut, URI, reqBody)
-	if err != nil {
-		return tc.Alerts{}, reqInf, err
-	}
-	defer resp.Body.Close()
-	var alerts tc.Alerts
-	err = json.NewDecoder(resp.Body).Decode(&alerts)
-	return alerts, reqInf, nil
-}
-
 // Returns a list of Profile Parameters
-func (to *Session) GetProfileParameters() ([]tc.ProfileParameter, ReqInf, error) {
+func (to *Session) GetProfileParameters() ([]v13.ProfileParameter, ReqInf, error) {
 	resp, remoteAddr, err := to.request(http.MethodGet, API_v13_Profile_Parameters, nil)
 	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
 	if err != nil {
@@ -77,32 +59,14 @@ func (to *Session) GetProfileParameters() ([]tc.ProfileParameter, ReqInf, error)
 	}
 	defer resp.Body.Close()
 
-	var data tc.ProfileParametersResponse
+	var data v13.ProfileParametersResponse
 	err = json.NewDecoder(resp.Body).Decode(&data)
 	return data.Response, reqInf, nil
 }
 
-// GET a Profile Parameter by the Profile
-func (to *Session) GetProfileParameterByIDs(profile int, parameter int) ([]tc.ProfileParameter, ReqInf, error) {
-	URI := fmt.Sprintf("%s?profile=%d&parameter=%d", API_v13_Profile_Parameters, profile)
-	resp, remoteAddr, err := to.request(http.MethodGet, URI, nil)
-	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
-	if err != nil {
-		return nil, reqInf, err
-	}
-	defer resp.Body.Close()
-
-	var data tc.ProfileParametersResponse
-	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
-		return nil, reqInf, err
-	}
-
-	return data.Response, reqInf, nil
-}
-
 // GET a Profile Parameter by the Parameter
-func (to *Session) GetProfileParameterByParameter(parameter int) ([]tc.ProfileParameter, ReqInf, error) {
-	URI := API_v13_Profile_Parameters + "?parameter=" + strconv.Itoa(parameter)
+func (to *Session) GetProfileParameterByQueryParams(queryParams string) ([]v13.ProfileParameter, ReqInf, error) {
+	URI := API_v13_Profile_Parameters + queryParams
 	resp, remoteAddr, err := to.request(http.MethodGet, URI, nil)
 	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
 	if err != nil {
@@ -110,7 +74,7 @@ func (to *Session) GetProfileParameterByParameter(parameter int) ([]tc.ProfilePa
 	}
 	defer resp.Body.Close()
 
-	var data tc.ProfileParametersResponse
+	var data v13.ProfileParametersResponse
 	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
 		return nil, reqInf, err
 	}
@@ -119,8 +83,8 @@ func (to *Session) GetProfileParameterByParameter(parameter int) ([]tc.ProfilePa
 }
 
 // DELETE a Parameter by Parameter
-func (to *Session) DeleteParameterByParameter(parameter int) (tc.Alerts, ReqInf, error) {
-	URI := fmt.Sprintf("%s/%d", API_v13_Profile_Parameters, parameter)
+func (to *Session) DeleteParameterByProfileParameter(profile int, parameter int) (tc.Alerts, ReqInf, error) {
+	URI := fmt.Sprintf("%s/profile/%d/parameter/%d", API_v13_Profile_Parameters, profile, parameter)
 	resp, remoteAddr, err := to.request(http.MethodDelete, URI, nil)
 	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
 	if err != nil {
diff --git a/traffic_ops/testing/api/v13/profile_parameters_test.go b/traffic_ops/testing/api/v13/profile_parameters_test.go
index e4b5f4f..00b8035 100644
--- a/traffic_ops/testing/api/v13/profile_parameters_test.go
+++ b/traffic_ops/testing/api/v13/profile_parameters_test.go
@@ -16,59 +16,57 @@
 package v13
 
 import (
+	"fmt"
 	"sync"
 	"testing"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-log"
-	tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+	"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
 )
 
+const queryParamFormat = "?profileId=%d&parameterId=%d"
+
 func TestProfileParameters(t *testing.T) {
 
+	CreateTestCDNs(t)
+	CreateTestTypes(t)
+	CreateTestParameters(t)
+	CreateTestProfiles(t)
 	CreateTestProfileParameters(t)
-	UpdateTestProfileParameters(t)
 	GetTestProfileParameters(t)
 	DeleteTestProfileParameters(t)
+	DeleteTestParameters(t)
+	DeleteTestProfiles(t)
+	DeleteTestTypes(t)
+	DeleteTestCDNs(t)
 
 }
 
 func CreateTestProfileParameters(t *testing.T) {
 
-	for _, pp := range testData.ProfileParameters {
-		resp, _, err := TOSession.CreateProfileParameter(pp)
-		log.Debugln("Response: ", resp)
-		if err != nil {
-			t.Errorf("could not CREATE profile parameters: %v\n", err)
-		}
-	}
-
-}
-
-func UpdateTestProfileParameters(t *testing.T) {
-
-	firstPP := testData.ProfileParameters[0]
-	// Retrieve the Parameter by profile so we can get the id for the Update
-	resp, _, err := TOSession.GetProfileParameterByParameter(firstPP.Profile)
+	firstProfile := testData.Profiles[0]
+	profileResp, _, err := TOSession.GetProfileByName(firstProfile.Name)
 	if err != nil {
-		t.Errorf("cannot GET Parameter by name: %v - %v\n", firstPP.Profile, err)
+		t.Errorf("cannot GET Profile by name: %v - %v\n", firstProfile.Name, err)
 	}
-	remotePP := resp[0]
-	expectedPP := 1
-	remotePP.Profile = expectedPP
-	var alert tc.Alerts
-	alert, _, err = TOSession.UpdateParameterByProfile(remotePP.Profile, remotePP)
+
+	firstParameter := testData.Parameters[0]
+	paramResp, _, err := TOSession.GetParameterByName(firstParameter.Name)
 	if err != nil {
-		t.Errorf("cannot UPDATE Parameter by profile: %v - %v\n", err, alert)
+		t.Errorf("cannot GET Parameter by name: %v - %v\n", firstParameter.Name, err)
 	}
 
-	// Retrieve the Parameter to check Parameter name got updated
-	resp, _, err = TOSession.GetProfileParameterByProfile(remotePP.Profile)
-	if err != nil {
-		t.Errorf("cannot GET Parameter by profile: %v - %v\n", firstPP.Profile, err)
+	profileID := profileResp[0].ID
+	parameterID := paramResp[0].ID
+
+	pp := v13.ProfileParameter{
+		ProfileID:   profileID,
+		ParameterID: parameterID,
 	}
-	respParameter := resp[0]
-	if respParameter.Profile != expectedPP {
-		t.Errorf("results do not match actual: %s, expected: %s\n", respParameter.Profile, expectedPP)
+	resp, _, err := TOSession.CreateProfileParameter(pp)
+	log.Debugln("Response: ", resp)
+	if err != nil {
+		t.Errorf("could not CREATE profile parameters: %v\n", err)
 	}
 
 }
@@ -76,7 +74,8 @@ func UpdateTestProfileParameters(t *testing.T) {
 func GetTestProfileParameters(t *testing.T) {
 
 	for _, pp := range testData.ProfileParameters {
-		resp, _, err := TOSession.GetProfileParameterByProfile(pp.Profile)
+		queryParams := fmt.Sprintf(queryParamFormat, pp.ProfileID, pp.ParameterID)
+		resp, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
 		if err != nil {
 			t.Errorf("cannot GET Parameter by name: %v - %v\n", err, resp)
 		}
@@ -105,27 +104,28 @@ func DeleteTestProfileParameters(t *testing.T) {
 	}
 }
 
-func DeleteTestProfileParameter(t *testing.T, pp tc.ProfileParameter) {
+func DeleteTestProfileParameter(t *testing.T, pp v13.ProfileParameter) {
 
+	queryParams := fmt.Sprintf(queryParamFormat, pp.ProfileID, pp.ParameterID)
 	// Retrieve the PtofileParameter by profile so we can get the id for the Update
-	resp, _, err := TOSession.GetProfileParameterByIDs(pp.Profile, pp.Parameter)
+	resp, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
 	if err != nil {
 		t.Errorf("cannot GET Parameter by profile: %v - %v\n", pp.Profile, err)
 	}
 	if len(resp) > 0 {
 		respPP := resp[0]
 
-		delResp, _, err := TOSession.DeleteProfileParameterByProfile(respPP.Profile)
+		delResp, _, err := TOSession.DeleteParameterByProfileParameter(respPP.ProfileID, respPP.ParameterID)
 		if err != nil {
 			t.Errorf("cannot DELETE Parameter by profile: %v - %v\n", err, delResp)
 		}
 
 		// Retrieve the Parameter to see if it got deleted
-		pls, _, err := TOSession.GetProfileParameterByIDs(pp.Profile)
+		pps, _, err := TOSession.GetProfileParameterByQueryParams(queryParams)
 		if err != nil {
 			t.Errorf("error deleting Parameter name: %s\n", err.Error())
 		}
-		if len(pls) > 0 {
+		if len(pps) > 0 {
 			t.Errorf("expected Parameter Name: %s and ConfigFile: %s to be deleted\n", pp.Profile, pp.Parameter)
 		}
 	}
diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json
index 27012b9..9462ee2 100644
--- a/traffic_ops/testing/api/v13/tc-fixtures.json
+++ b/traffic_ops/testing/api/v13/tc-fixtures.json
@@ -595,6 +595,16 @@
             "type": "ATS_PROFILE"
         }
     ],
+    "profileParameters": [
+        {
+            "profileId": 100,
+            "parameterId": 100
+        },
+        {
+            "profileId": 200,
+            "parameterId": 200
+        }
+    ],
     "regions": [
         {
             "divisionName": "division1",
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index 0036803..cba0b48 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -16,26 +16,26 @@
 package v13
 
 import (
-	tcapi "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+	v12 "github.com/apache/incubator-trafficcontrol/lib/go-tc"
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
 )
 
 // TrafficControl - maps to the tc-fixtures.json file
 type TrafficControl struct {
-	ASNs                           []tcapi.ASN                           `json:"asns"`
-	CDNs                           []v13.CDN                             `json:"cdns"`
-	CacheGroups                    []tcapi.CacheGroup                    `json:"cachegroups"`
-	DeliveryServiceRequests        []tcapi.DeliveryServiceRequest        `json:"deliveryServiceRequests"`
-	DeliveryServiceRequestComments []tcapi.DeliveryServiceRequestComment `json:"deliveryServiceRequestComments"`
-	DeliveryServices               []tcapi.DeliveryService               `json:"deliveryservices"`
-	Divisions                      []tcapi.Division                      `json:"divisions"`
-	Profiles                       []tcapi.Profile                       `json:"profiles"`
-	Parameters                     []tcapi.Parameter                     `json:"parameters"`
-	ProfileParameters              []tcapi.ProfileParameter              `json:"profileParameters"`
-	PhysLocations                  []tcapi.PhysLocation                  `json:"physLocations"`
-	Regions                        []tcapi.Region                        `json:"regions"`
-	Servers                        []v13.Server                          `json:"servers"`
-	Statuses                       []tcapi.Status                        `json:"statuses"`
-	Tenants                        []tcapi.Tenant                        `json:"tenants"`
-	Types                          []tcapi.Type                          `json:"types"`
+	ASNs                           []v12.ASN                           `json:"asns"`
+	CDNs                           []v13.CDN                           `json:"cdns"`
+	CacheGroups                    []v12.CacheGroup                    `json:"cachegroups"`
+	DeliveryServiceRequests        []v12.DeliveryServiceRequest        `json:"deliveryServiceRequests"`
+	DeliveryServiceRequestComments []v12.DeliveryServiceRequestComment `json:"deliveryServiceRequestComments"`
+	DeliveryServices               []v12.DeliveryService               `json:"deliveryservices"`
+	Divisions                      []v12.Division                      `json:"divisions"`
+	Profiles                       []v12.Profile                       `json:"profiles"`
+	Parameters                     []v12.Parameter                     `json:"parameters"`
+	ProfileParameters              []v13.ProfileParameter              `json:"profileParameters"`
+	PhysLocations                  []v12.PhysLocation                  `json:"physLocations"`
+	Regions                        []v12.Region                        `json:"regions"`
+	Servers                        []v13.Server                        `json:"servers"`
+	Statuses                       []v12.Status                        `json:"statuses"`
+	Tenants                        []v12.Tenant                        `json:"tenants"`
+	Types                          []v12.Type                          `json:"types"`
 }

-- 
To stop receiving notification emails like this one, please contact
mitchell852@apache.org.