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 3743b143af53729559d805ae2710eaafd751927e
Author: Dewayne Richardson <dewr...@apache.org>
AuthorDate: Tue Feb 20 09:41:24 2018 -0700

    added API tests for /regions
---
 traffic_ops/client/region.go           | 132 +++++++++++++++++++++++++++++++++
 traffic_ops/testing/api/region_test.go | 107 ++++++++++++++++++++++++++
 2 files changed, 239 insertions(+)

diff --git a/traffic_ops/client/region.go b/traffic_ops/client/region.go
new file mode 100644
index 0000000..f3e39f1
--- /dev/null
+++ b/traffic_ops/client/region.go
@@ -0,0 +1,132 @@
+/*
+
+   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.
+*/
+
+package client
+
+import (
+       "encoding/json"
+       "fmt"
+       "net"
+       "net/http"
+
+       "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+)
+
+const (
+       API_v13_REGIONS = "/api/1.3/regions"
+)
+
+// Create a Region
+func (to *Session) CreateRegion(region tc.Region) (tc.Alerts, ReqInf, error) {
+
+       var remoteAddr net.Addr
+       reqBody, err := json.Marshal(region)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return tc.Alerts{}, reqInf, err
+       }
+       resp, remoteAddr, err := to.request(http.MethodPost, API_v13_REGIONS, 
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
+}
+
+// Update a Region by ID
+func (to *Session) UpdateRegionByID(id int, region tc.Region) (tc.Alerts, 
ReqInf, error) {
+
+       var remoteAddr net.Addr
+       reqBody, err := json.Marshal(region)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return tc.Alerts{}, reqInf, err
+       }
+       route := fmt.Sprintf("%s/%d", API_v13_REGIONS, id)
+       resp, remoteAddr, err := to.request(http.MethodPut, route, 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 regions
+func (to *Session) GetRegions() ([]tc.Region, ReqInf, error) {
+       resp, remoteAddr, err := to.request(http.MethodGet, API_v13_REGIONS, 
nil)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return nil, reqInf, err
+       }
+       defer resp.Body.Close()
+
+       var data tc.RegionsResponse
+       err = json.NewDecoder(resp.Body).Decode(&data)
+       return data.Response, reqInf, nil
+}
+
+// GET a Region by the Region id
+func (to *Session) GetRegionByID(id int) ([]tc.Region, ReqInf, error) {
+       route := fmt.Sprintf("%s/%d", API_v13_REGIONS, id)
+       resp, remoteAddr, err := to.request(http.MethodGet, route, nil)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return nil, reqInf, err
+       }
+       defer resp.Body.Close()
+
+       var data tc.RegionsResponse
+       if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+               return nil, reqInf, err
+       }
+
+       return data.Response, reqInf, nil
+}
+
+// GET a Region by the Region name
+func (to *Session) GetRegionByName(name string) ([]tc.Region, ReqInf, error) {
+       url := fmt.Sprintf("%s?name=%s", API_v13_REGIONS, name)
+       resp, remoteAddr, err := to.request(http.MethodGet, url, nil)
+       reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: 
remoteAddr}
+       if err != nil {
+               return nil, reqInf, err
+       }
+       defer resp.Body.Close()
+
+       var data tc.RegionsResponse
+       if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+               return nil, reqInf, err
+       }
+
+       return data.Response, reqInf, nil
+}
+
+// DELETE a Region by ID
+func (to *Session) DeleteRegionByID(id int) (tc.Alerts, ReqInf, error) {
+       route := fmt.Sprintf("%s/%d", API_v13_REGIONS, 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
+}
diff --git a/traffic_ops/testing/api/region_test.go 
b/traffic_ops/testing/api/region_test.go
new file mode 100644
index 0000000..2d0d78a
--- /dev/null
+++ b/traffic_ops/testing/api/region_test.go
@@ -0,0 +1,107 @@
+/*
+
+   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.
+*/
+
+package api
+
+import (
+       "fmt"
+       "testing"
+
+       "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+)
+
+func TestRegions(t *testing.T) {
+
+       CreateTestRegions(t)
+       UpdateTestRegions(t)
+       GetTestRegions(t)
+       DeleteTestRegions(t)
+
+}
+
+func CreateTestRegions(t *testing.T) {
+       for _, region := range testData.Regions {
+               fmt.Printf("region ---> %v\n", region)
+               resp, _, err := TOSession.CreateRegion(region)
+               log.Debugln("Response: ", resp)
+               if err != nil {
+                       t.Errorf("could not CREATE region: %v\n", err)
+               }
+       }
+}
+
+func UpdateTestRegions(t *testing.T) {
+
+       firstRegion := testData.Regions[0]
+       // Retrieve the Region by region so we can get the id for the Update
+       resp, _, err := TOSession.GetRegionByName(firstRegion.Name)
+       if err != nil {
+               t.Errorf("cannot GET Region by region: %v - %v\n", 
firstRegion.Name, err)
+       }
+       remoteRegion := resp[0]
+       expectedRegion := "OFFLINE-TEST"
+       remoteRegion.Name = expectedRegion
+       var alert tc.Alerts
+       alert, _, err = TOSession.UpdateRegionByID(remoteRegion.ID, 
remoteRegion)
+       if err != nil {
+               t.Errorf("cannot UPDATE Region by id: %v - %v\n", err, alert)
+       }
+
+       // Retrieve the Region to check region got updated
+       resp, _, err = TOSession.GetRegionByID(remoteRegion.ID)
+       if err != nil {
+               t.Errorf("cannot GET Region by region: %v - %v\n", 
firstRegion.Name, err)
+       }
+       respRegion := resp[0]
+       if respRegion.Name != expectedRegion {
+               t.Errorf("results do not match actual: %s, expected: %s\n", 
respRegion.Name, expectedRegion)
+       }
+
+}
+
+func GetTestRegions(t *testing.T) {
+       for _, region := range testData.Regions {
+               resp, _, err := TOSession.GetRegionByName(region.Name)
+               if err != nil {
+                       t.Errorf("cannot GET Region by region: %v - %v\n", err, 
resp)
+               }
+       }
+}
+
+func DeleteTestRegions(t *testing.T) {
+
+       region := testData.Regions[1]
+       // Retrieve the Region by name so we can get the id
+       resp, _, err := TOSession.GetRegionByName(region.Name)
+       if err != nil {
+               t.Errorf("cannot GET Region by name: %v - %v\n", region.Name, 
err)
+       }
+       respRegion := resp[0]
+
+       delResp, _, err := TOSession.DeleteRegionByID(respRegion.ID)
+       if err != nil {
+               t.Errorf("cannot DELETE Region by region: %v - %v\n", err, 
delResp)
+       }
+
+       // Retrieve the Region to see if it got deleted
+       regionResp, _, err := TOSession.GetRegionByName(region.Name)
+       if err != nil {
+               t.Errorf("error deleting Region region: %s\n", err.Error())
+       }
+       if len(regionResp) > 0 {
+               t.Errorf("expected Region : %s to be deleted\n", region.Name)
+       }
+}

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

Reply via email to