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

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

commit 6b07fd7e5ff4ed062deb5e489d7a96e083fd4062
Author: Dewayne Richardson <dewr...@apache.org>
AuthorDate: Thu Feb 22 12:09:50 2018 -0700

    added DeleteByID TO client function
    
    moved package line up
    
    added division creation to link up foreign keys
    
    removed debug
---
 traffic_ops/client/division.go                     |  28 ++++--
 traffic_ops/testing/api/v13/cdns_test.go           |   4 +-
 .../api/v13/deliveryservice_requests_test.go       |   4 +-
 traffic_ops/testing/api/v13/division_test.go       | 105 +++++++++++++++++++++
 traffic_ops/testing/api/v13/region_test.go         |  18 +++-
 traffic_ops/testing/api/v13/status_test.go         |   4 +-
 traffic_ops/testing/api/v13/traffic_ops_test.go    |   4 +-
 7 files changed, 148 insertions(+), 19 deletions(-)

diff --git a/traffic_ops/client/division.go b/traffic_ops/client/division.go
index 28832f9..617ff51 100644
--- a/traffic_ops/client/division.go
+++ b/traffic_ops/client/division.go
@@ -25,7 +25,7 @@ import (
 )
 
 const (
-       API_v2_Divisions = "/api/1.3/divisions"
+       API_v13_Divisions = "/api/1.3/divisions"
 )
 
 // Create a Division
@@ -37,7 +37,7 @@ func (to *Session) CreateDivision(division tc.Division) 
(tc.Alerts, ReqInf, erro
        if err != nil {
                return tc.Alerts{}, reqInf, err
        }
-       resp, remoteAddr, err := to.request(http.MethodPost, API_v2_Divisions, 
reqBody)
+       resp, remoteAddr, err := to.request(http.MethodPost, API_v13_Divisions, 
reqBody)
        if err != nil {
                return tc.Alerts{}, reqInf, err
        }
@@ -56,7 +56,7 @@ func (to *Session) UpdateDivisionByID(id int, division 
tc.Division) (tc.Alerts,
        if err != nil {
                return tc.Alerts{}, reqInf, err
        }
-       route := fmt.Sprintf("%s/%d", API_v2_Divisions, id)
+       route := fmt.Sprintf("%s/%d", API_v13_Divisions, id)
        resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody)
        if err != nil {
                return tc.Alerts{}, reqInf, err
@@ -69,7 +69,7 @@ func (to *Session) UpdateDivisionByID(id int, division 
tc.Division) (tc.Alerts,
 
 // Returns a list of Divisions
 func (to *Session) GetDivisions() ([]tc.Division, ReqInf, error) {
-       resp, remoteAddr, err := to.request(http.MethodGet, API_v2_Divisions, 
nil)
+       resp, remoteAddr, err := to.request(http.MethodGet, API_v13_Divisions, 
nil)
        reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
        if err != nil {
                return nil, reqInf, err
@@ -83,7 +83,7 @@ func (to *Session) GetDivisions() ([]tc.Division, ReqInf, 
error) {
 
 // GET a Division by the Division id
 func (to *Session) GetDivisionByID(id int) ([]tc.Division, ReqInf, error) {
-       route := fmt.Sprintf("%s/%d", API_v2_Divisions, id)
+       route := fmt.Sprintf("%s/%d", API_v13_Divisions, id)
        resp, remoteAddr, err := to.request(http.MethodGet, route, nil)
        reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
        if err != nil {
@@ -101,7 +101,7 @@ func (to *Session) GetDivisionByID(id int) ([]tc.Division, 
ReqInf, error) {
 
 // GET a Division by the Division name
 func (to *Session) GetDivisionByName(name string) ([]tc.Division, ReqInf, 
error) {
-       url := fmt.Sprintf("%s?name=%s", API_v2_Divisions, name)
+       url := fmt.Sprintf("%s?name=%s", API_v13_Divisions, name)
        resp, remoteAddr, err := to.request(http.MethodGet, url, nil)
        reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
        if err != nil {
@@ -117,9 +117,23 @@ func (to *Session) GetDivisionByName(name string) 
([]tc.Division, ReqInf, error)
        return data.Response, reqInf, nil
 }
 
+// DELETE a Division by Division id
+func (to *Session) DeleteDivisionByID(id int) (tc.Alerts, ReqInf, error) {
+       route := fmt.Sprintf("%s/%d", API_v13_Divisions, id)
+       resp, remoteAddr, err := to.request(http.MethodDelete, route, nil)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       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
+}
+
 // DELETE a Division by Division name
 func (to *Session) DeleteDivisionByName(name string) (tc.Alerts, ReqInf, 
error) {
-       route := fmt.Sprintf("%s/name/%s", API_v2_Divisions, name)
+       route := fmt.Sprintf("%s/name/%s", API_v13_Divisions, name)
        resp, remoteAddr, err := to.request(http.MethodDelete, route, nil)
        reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
        if err != nil {
diff --git a/traffic_ops/testing/api/v13/cdns_test.go 
b/traffic_ops/testing/api/v13/cdns_test.go
index 1a7d9a8..2aba83b 100644
--- a/traffic_ops/testing/api/v13/cdns_test.go
+++ b/traffic_ops/testing/api/v13/cdns_test.go
@@ -1,3 +1,5 @@
+package v13
+
 /*
 
    Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +15,6 @@
    limitations under the License.
 */
 
-package v13
-
 import (
        "testing"
 
diff --git a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go 
b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
index 193f03c..9648e3c 100644
--- a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
+++ b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
@@ -1,3 +1,5 @@
+package v13
+
 /*
 
    Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +15,6 @@
    limitations under the License.
 */
 
-package v13
-
 import (
        "strings"
        "testing"
diff --git a/traffic_ops/testing/api/v13/division_test.go 
b/traffic_ops/testing/api/v13/division_test.go
new file mode 100644
index 0000000..954fe7f
--- /dev/null
+++ b/traffic_ops/testing/api/v13/division_test.go
@@ -0,0 +1,105 @@
+package v13
+
+/*
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+       "testing"
+
+       "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+)
+
+func TestDivisions(t *testing.T) {
+
+       CreateTestDivisions(t)
+       UpdateTestDivisions(t)
+       GetTestDivisions(t)
+       DeleteTestDivisions(t)
+
+}
+
+func CreateTestDivisions(t *testing.T) {
+       for _, division := range testData.Divisions {
+               resp, _, err := TOSession.CreateDivision(division)
+               log.Debugln("Response: ", resp)
+               if err != nil {
+                       t.Errorf("could not CREATE division: %v\n", err)
+               }
+       }
+}
+
+func UpdateTestDivisions(t *testing.T) {
+
+       firstDivision := testData.Divisions[0]
+       // Retrieve the Division by division so we can get the id for the Update
+       resp, _, err := TOSession.GetDivisionByName(firstDivision.Name)
+       if err != nil {
+               t.Errorf("cannot GET Division by division: %v - %v\n", 
firstDivision.Name, err)
+       }
+       remoteDivision := resp[0]
+       expectedDivision := "division-test"
+       remoteDivision.Name = expectedDivision
+       var alert tc.Alerts
+       alert, _, err = TOSession.UpdateDivisionByID(remoteDivision.ID, 
remoteDivision)
+       if err != nil {
+               t.Errorf("cannot UPDATE Division by id: %v - %v\n", err, alert)
+       }
+
+       // Retrieve the Division to check division got updated
+       resp, _, err = TOSession.GetDivisionByID(remoteDivision.ID)
+       if err != nil {
+               t.Errorf("cannot GET Division by division: %v - %v\n", 
firstDivision.Name, err)
+       }
+       respDivision := resp[0]
+       if respDivision.Name != expectedDivision {
+               t.Errorf("results do not match actual: %s, expected: %s\n", 
respDivision.Name, expectedDivision)
+       }
+
+}
+
+func GetTestDivisions(t *testing.T) {
+       for _, division := range testData.Divisions {
+               resp, _, err := TOSession.GetDivisionByName(division.Name)
+               if err != nil {
+                       t.Errorf("cannot GET Division by division: %v - %v\n", 
err, resp)
+               }
+       }
+}
+
+func DeleteTestDivisions(t *testing.T) {
+
+       division := testData.Divisions[1]
+       // Retrieve the Division by name so we can get the id
+       resp, _, err := TOSession.GetDivisionByName(division.Name)
+       if err != nil {
+               t.Errorf("cannot GET Division by name: %v - %v\n", 
division.Name, err)
+       }
+       respDivision := resp[0]
+
+       delResp, _, err := TOSession.DeleteDivisionByID(respDivision.ID)
+       if err != nil {
+               t.Errorf("cannot DELETE Division by division: %v - %v\n", err, 
delResp)
+       }
+
+       // Retrieve the Division to see if it got deleted
+       divisionResp, _, err := TOSession.GetDivisionByName(division.Name)
+       if err != nil {
+               t.Errorf("error deleting Division division: %s\n", err.Error())
+       }
+       if len(divisionResp) > 0 {
+               t.Errorf("expected Division : %s to be deleted\n", 
division.Name)
+       }
+}
diff --git a/traffic_ops/testing/api/v13/region_test.go 
b/traffic_ops/testing/api/v13/region_test.go
index 3ac07c2..4d3397a 100644
--- a/traffic_ops/testing/api/v13/region_test.go
+++ b/traffic_ops/testing/api/v13/region_test.go
@@ -1,3 +1,5 @@
+package v13
+
 /*
 
    Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,10 +15,7 @@
    limitations under the License.
 */
 
-package v13
-
 import (
-       "fmt"
        "testing"
 
        "github.com/apache/incubator-trafficcontrol/lib/go-log"
@@ -25,6 +24,8 @@ import (
 
 func TestRegions(t *testing.T) {
 
+       CreateTestDivisions(t)
+
        CreateTestRegions(t)
        UpdateTestRegions(t)
        GetTestRegions(t)
@@ -33,8 +34,17 @@ func TestRegions(t *testing.T) {
 }
 
 func CreateTestRegions(t *testing.T) {
+
+       // Retrieve the Division by name so we can get the ID
+       division := testData.Divisions[0]
+       resp, _, err := TOSession.GetDivisionByName(division.Name)
+       if err != nil {
+               t.Errorf("cannot GET Division by name: %v - %v\n", 
division.Name, err)
+       }
+       respDivision := resp[0]
+
        for _, region := range testData.Regions {
-               fmt.Printf("region ---> %v\n", region)
+               region.Division = respDivision.ID
                resp, _, err := TOSession.CreateRegion(region)
                log.Debugln("Response: ", resp)
                if err != nil {
diff --git a/traffic_ops/testing/api/v13/status_test.go 
b/traffic_ops/testing/api/v13/status_test.go
index 199bfc4..6478001 100644
--- a/traffic_ops/testing/api/v13/status_test.go
+++ b/traffic_ops/testing/api/v13/status_test.go
@@ -1,3 +1,5 @@
+package v13
+
 /*
 
    Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +15,6 @@
    limitations under the License.
 */
 
-package v13
-
 import (
        "testing"
 
diff --git a/traffic_ops/testing/api/v13/traffic_ops_test.go 
b/traffic_ops/testing/api/v13/traffic_ops_test.go
index c7e25ff..4800836 100644
--- a/traffic_ops/testing/api/v13/traffic_ops_test.go
+++ b/traffic_ops/testing/api/v13/traffic_ops_test.go
@@ -1,3 +1,5 @@
+package v13
+
 /*
 
    Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +15,6 @@
    limitations under the License.
 */
 
-package v13
-
 import (
        "database/sql"
        "flag"

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

Reply via email to