You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by da...@apache.org on 2018/02/26 16:45:09 UTC

[incubator-trafficcontrol] 03/17: updated for the API test cases

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 808f4236fe08f7fc7a836137cf0619e77a16d460
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Tue Feb 20 14:28:27 2018 -0700

    updated for the API test cases
---
 traffic_ops/client/phys_location.go           | 132 ++++++++++++++++++++++++++
 traffic_ops/testing/api/phys_location_test.go | 108 +++++++++++++++++++++
 traffic_ops/testing/api/tc-fixtures.json      |  38 ++++++++
 3 files changed, 278 insertions(+)

diff --git a/traffic_ops/client/phys_location.go b/traffic_ops/client/phys_location.go
new file mode 100644
index 0000000..f0b0f15
--- /dev/null
+++ b/traffic_ops/client/phys_location.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_PHYS_LOCATIONS = "/api/1.3/phys_locations"
+)
+
+// Create a PhysLocation
+func (to *Session) CreatePhysLocation(physLocation tc.PhysLocation) (tc.Alerts, ReqInf, error) {
+
+	var remoteAddr net.Addr
+	reqBody, err := json.Marshal(physLocation)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return tc.Alerts{}, reqInf, err
+	}
+	resp, remoteAddr, err := to.request(http.MethodPost, API_v13_PHYS_LOCATIONS, 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 PhysLocation by ID
+func (to *Session) UpdatePhysLocationByID(id int, physLocation tc.PhysLocation) (tc.Alerts, ReqInf, error) {
+
+	var remoteAddr net.Addr
+	reqBody, err := json.Marshal(physLocation)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return tc.Alerts{}, reqInf, err
+	}
+	route := fmt.Sprintf("%s/%d", API_v13_PHYS_LOCATIONS, 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 physLocations
+func (to *Session) GetPhysLocations() ([]tc.PhysLocation, ReqInf, error) {
+	resp, remoteAddr, err := to.request(http.MethodGet, API_v13_PHYS_LOCATIONS, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return nil, reqInf, err
+	}
+	defer resp.Body.Close()
+
+	var data tc.PhysLocationsResponse
+	err = json.NewDecoder(resp.Body).Decode(&data)
+	return data.Response, reqInf, nil
+}
+
+// GET a PhysLocation by the PhysLocation id
+func (to *Session) GetPhysLocationByID(id int) ([]tc.PhysLocation, ReqInf, error) {
+	route := fmt.Sprintf("%s/%d", API_v13_PHYS_LOCATIONS, 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.PhysLocationsResponse
+	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
+// GET a PhysLocation by the PhysLocation name
+func (to *Session) GetPhysLocationByName(name string) ([]tc.PhysLocation, ReqInf, error) {
+	url := fmt.Sprintf("%s?name=%s", API_v13_PHYS_LOCATIONS, 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.PhysLocationsResponse
+	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
+		return nil, reqInf, err
+	}
+
+	return data.Response, reqInf, nil
+}
+
+// DELETE a PhysLocation by ID
+func (to *Session) DeletePhysLocationByID(id int) (tc.Alerts, ReqInf, error) {
+	route := fmt.Sprintf("%s/%d", API_v13_PHYS_LOCATIONS, 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/phys_location_test.go b/traffic_ops/testing/api/phys_location_test.go
new file mode 100644
index 0000000..2cccf4a
--- /dev/null
+++ b/traffic_ops/testing/api/phys_location_test.go
@@ -0,0 +1,108 @@
+/*
+
+   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 TestPhysLocations(t *testing.T) {
+
+	CreateTestPhysLocations(t)
+	//UpdateTestPhysLocations(t)
+	//GetTestPhysLocations(t)
+	//DeleteTestPhysLocations(t)
+
+}
+
+func CreateTestPhysLocations(t *testing.T) {
+	for _, physLocation := range testData.PhysLocations {
+		fmt.Printf("physLocation ---> %v\n", physLocation)
+		resp, _, err := TOSession.CreatePhysLocation(physLocation)
+		log.Debugln("Response: ", resp)
+		if err != nil {
+			t.Errorf("could not CREATE physLocation: %v\n", err)
+		}
+	}
+}
+
+func UpdateTestPhysLocations(t *testing.T) {
+
+	firstPhysLocation := testData.PhysLocations[0]
+	// Retrieve the PhysLocation by physLocation so we can get the id for the Update
+	resp, _, err := TOSession.GetPhysLocationByName(firstPhysLocation.Name)
+	if err != nil {
+		t.Errorf("cannot GET PhysLocation by physLocation: %v - %v\n", firstPhysLocation.Name, err)
+	}
+	remotePhysLocation := resp[0]
+	expectedPhysLocation := "OFFLINE-TEST"
+	remotePhysLocation.Name = expectedPhysLocation
+	var alert tc.Alerts
+	alert, _, err = TOSession.UpdatePhysLocationByID(remotePhysLocation.ID, remotePhysLocation)
+	if err != nil {
+		t.Errorf("cannot UPDATE PhysLocation by id: %v - %v\n", err, alert)
+	}
+
+	// Retrieve the PhysLocation to check physLocation got updated
+	resp, _, err = TOSession.GetPhysLocationByID(remotePhysLocation.ID)
+	if err != nil {
+		t.Errorf("cannot GET PhysLocation by physLocation: %v - %v\n", firstPhysLocation.Name, err)
+	}
+	respPhysLocation := resp[0]
+	if respPhysLocation.Name != expectedPhysLocation {
+		t.Errorf("results do not match actual: %s, expected: %s\n", respPhysLocation.Name, expectedPhysLocation)
+	}
+
+}
+
+func GetTestPhysLocations(t *testing.T) {
+	for _, physLocation := range testData.PhysLocations {
+		resp, _, err := TOSession.GetPhysLocationByName(physLocation.Name)
+		if err != nil {
+			t.Errorf("cannot GET PhysLocation by physLocation: %v - %v\n", err, resp)
+		}
+	}
+}
+
+func DeleteTestPhysLocations(t *testing.T) {
+
+	physLocation := testData.PhysLocations[1]
+	// Retrieve the PhysLocation by name so we can get the id
+	resp, _, err := TOSession.GetPhysLocationByName(physLocation.Name)
+	if err != nil {
+		t.Errorf("cannot GET PhysLocation by name: %v - %v\n", physLocation.Name, err)
+	}
+	respPhysLocation := resp[0]
+
+	delResp, _, err := TOSession.DeletePhysLocationByID(respPhysLocation.ID)
+	if err != nil {
+		t.Errorf("cannot DELETE PhysLocation by physLocation: %v - %v\n", err, delResp)
+	}
+
+	// Retrieve the PhysLocation to see if it got deleted
+	physLocationResp, _, err := TOSession.GetPhysLocationByName(physLocation.Name)
+	if err != nil {
+		t.Errorf("error deleting PhysLocation: %s\n", err.Error())
+	}
+	if len(physLocationResp) > 0 {
+		t.Errorf("expected PhysLocation: %s to be deleted\n", physLocation.Name)
+	}
+
+}
diff --git a/traffic_ops/testing/api/tc-fixtures.json b/traffic_ops/testing/api/tc-fixtures.json
index 2d3a65b..38df6f6 100644
--- a/traffic_ops/testing/api/tc-fixtures.json
+++ b/traffic_ops/testing/api/tc-fixtures.json
@@ -86,6 +86,44 @@
             "name": "division2"
         }
     ],
+    "physLocations": [
+        {
+            "address": "1234 mile high circle",
+            "name": "Denver",
+            "comments": "comments 1",
+            "email": "test1@email.com",
+            "shortName": "denver",
+            "poc": "Point Of Contact1",
+            "phone": "303-111-1111",
+            "city": "Denver",
+            "state": "CO",
+            "zip": "80202"
+        },
+        {
+            "address": "1234 green way",
+            "name": "Boulder",
+            "comments": "comments 2",
+            "email": "test2@email.com",
+            "shortName": "boulder",
+            "poc": "Point Of Contact1",
+            "phone": "303-222-2222",
+            "city": "Boulder",
+            "state": "CO",
+            "zip": "80301"
+        },
+        {
+            "address": "1234 green way",
+            "name": "HotAtlanta",
+            "comments": "comments 3",
+            "email": "test2@email.com",
+            "shortName": "boulder",
+            "poc": "Point Of Contact1",
+            "phone": "404-222-2222",
+            "city": "Atlanta",
+            "state": "GA",
+            "zip": "30301"
+        }
+    ],
     "regions": [
         {
             "divisionName": "division1",

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