Skip to content

Commit

Permalink
Merge d24df0f into 77747dd
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Apr 18, 2018
2 parents 77747dd + d24df0f commit bd5900c
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 617 deletions.
99 changes: 36 additions & 63 deletions selvpcclient/resell/v2/floatingips/testing/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package testing

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
Expand All @@ -14,17 +11,13 @@ import (
)

func TestGetFloatingIP(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestGetFloatingIPResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
Expand All @@ -34,30 +27,32 @@ func TestGetFloatingIP(t *testing.T) {

expected := TestGetFloatingIPResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

func TestListFloatingIPs(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListFloatingIPsResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if actual == nil {
t.Fatal("didn't get floating ips")
}
Expand All @@ -71,17 +66,13 @@ func TestListFloatingIPs(t *testing.T) {
}

func TestListFloatingIPsSingle(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListFloatingIPsSingleResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client)
Expand All @@ -91,44 +82,23 @@ func TestListFloatingIPsSingle(t *testing.T) {

expected := TestListFloatingIPsSingleResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

func TestCreateFloatingIPs(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestCreateFloatingIPResponseRaw)

if r.Method != http.MethodPost {
t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method)
}

b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("unable to read the request body: %v", err)
}

var actualRequest interface{}
err = json.Unmarshal(b, &actualRequest)
if err != nil {
t.Errorf("unable to unmarshal the request body: %v", err)
}

var expectedRequest interface{}
err = json.Unmarshal([]byte(TestCreateFloatingIPOptsRaw), &expectedRequest)
if err != nil {
t.Errorf("unable to unmarshal expected raw response: %v", err)
}

if !reflect.DeepEqual(actualRequest, expectedRequest) {
t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest)
}
})
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
Expand All @@ -139,26 +109,29 @@ func TestCreateFloatingIPs(t *testing.T) {

expectedResponse := TestCreateFloatingIPResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actualResponse, expectedResponse) {
t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse)
}
}

func TestDeleteFloatingIP(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")

if r.Method != http.MethodDelete {
t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
"", http.MethodDelete, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
_, err := floatingips.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
}
99 changes: 36 additions & 63 deletions selvpcclient/resell/v2/licenses/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package testing

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
Expand All @@ -14,17 +11,13 @@ import (
)

func TestGetLicense(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/licenses/123123", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestGetLicenseResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/123123",
TestGetLicenseResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := licenses.Get(ctx, testEnv.Client, "123123")
Expand All @@ -34,30 +27,32 @@ func TestGetLicense(t *testing.T) {

expected := TestGetLicenseResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

func TestListLicenses(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListLicensesResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses",
TestListLicensesResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := licenses.List(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if actual == nil {
t.Fatal("didn't get licenses")
}
Expand All @@ -71,17 +66,13 @@ func TestListLicenses(t *testing.T) {
}

func TestListLicensesSingle(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestListLicensesSingleResponseRaw)

if r.Method != http.MethodGet {
t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses",
TestListLicensesSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
actual, _, err := licenses.List(ctx, testEnv.Client)
Expand All @@ -91,44 +82,23 @@ func TestListLicensesSingle(t *testing.T) {

expected := TestListLicensesSingleResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
}

func TestCreateLicenses(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, TestCreateLicenseResponseRaw)

if r.Method != http.MethodPost {
t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method)
}

b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("unable to read the request body: %v", err)
}

var actualRequest interface{}
err = json.Unmarshal(b, &actualRequest)
if err != nil {
t.Errorf("unable to unmarshal the request body: %v", err)
}

var expectedRequest interface{}
err = json.Unmarshal([]byte(TestCreateLicenseOptsRaw), &expectedRequest)
if err != nil {
t.Errorf("unable to unmarshal expected raw response: %v", err)
}

if !reflect.DeepEqual(actualRequest, expectedRequest) {
t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest)
}
})
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317",
TestCreateLicenseResponseRaw, TestCreateLicenseOptsRaw, http.MethodPost, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateLicenseOpts
Expand All @@ -139,26 +109,29 @@ func TestCreateLicenses(t *testing.T) {

expectedResponse := TestCreateLicenseResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actualResponse, expectedResponse) {
t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse)
}
}

func TestDeleteLicense(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testEnv.Mux.HandleFunc("/resell/v2/licenses/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")

if r.Method != http.MethodDelete {
t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method)
}
})
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/5232d5f3-4950-454b-bd41-78c5295622cd",
"", http.MethodDelete, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
_, err := licenses.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
}
Loading

0 comments on commit bd5900c

Please sign in to comment.