Skip to content

Commit

Permalink
Merge pull request #44 from 3scale/fix-delete-product
Browse files Browse the repository at this point in the history
Fix delete product
  • Loading branch information
eguzki authored Mar 31, 2022
2 parents 38a0934 + 280d56c commit ac306cf
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 28 deletions.
40 changes: 27 additions & 13 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,46 +148,60 @@ func TestAdminPortalPathIsPreserved(t *testing.T) {
t.Errorf("expected trailing slash to be stripped")
}

verify:= func(req *http.Request, path string) {
verify := func(req *http.Request, path string) {
equals(t, host, req.URL.Hostname())
equals(t, port, req.URL.Port())
equals(t, scheme, req.URL.Scheme)
equals(t, path, req.URL.Path)
}


// Test path is preserved for GET
NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
_, err = NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req, "/example/admin/api/services.xml")
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(`<services></services>`))),
}
})).ListServices()
if err != nil {
t.Fatal(err)
}

// Test path is preserved for POST
NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
_, err = NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req, "/example/admin/api/services.xml")
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(`<service></service>`))),
}
})).CreateService("any")
if err != nil {
t.Fatal(err)
}

// Test path is preserved for DELETE
NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req,"/example/admin/api/services/any.xml")
err = NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req, "/example/admin/api/services/any.xml")
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
}
})).DeleteService("any")
if err != nil {
t.Fatal(err)
}

// Test path is preserved for PUT
NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req,"/example/admin/api/services/any.xml")
_, err = NewThreeScale(ap, "any", NewTestClient(func(req *http.Request) *http.Response {
verify(req, "/example/admin/api/services/any.xml")
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(`<service></service>`))),
}
})).UpdateService("any", NewParams())

if err != nil {
t.Fatal(err)
}
}

func equals(t *testing.T, exp, act interface{}) {
Expand Down
7 changes: 3 additions & 4 deletions client/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (c *ThreeScaleClient) UpdateMappingRule(svcId string, id string, params Par
}

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return m, err
}

defer resp.Body.Close()

err = handleXMLResp(resp, http.StatusOK, &m)
return m, err
}
Expand All @@ -85,11 +85,10 @@ func (c *ThreeScaleClient) DeleteMappingRule(svcId string, id string) error {
}

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return err
}
defer resp.Body.Close()

return handleXMLResp(resp, http.StatusOK, nil)
}
Expand Down
3 changes: 1 addition & 2 deletions client/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ func (c *ThreeScaleClient) DeleteProduct(id int64) error {
}
defer resp.Body.Close()

var empty struct{}
return handleJsonResp(resp, http.StatusOK, &empty)
return handleJsonResp(resp, http.StatusOK, nil)
}

// ListProducts List existing products
Expand Down
173 changes: 173 additions & 0 deletions client/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,3 +1004,176 @@ func TestReadProduct(t *testing.T) {
t.Fatalf("Expected %v; got %v", *product, *obj)
}
}

func TestCreateProduct(t *testing.T) {
var (
name string = "producttest1"
productID int64 = 98765
params Params = Params{}
)

httpClient := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.Path != productListResourceEndpoint {
t.Fatalf("Path does not match. Expected [%s]; got [%s]", productListResourceEndpoint, req.URL.Path)
}

if req.Method != http.MethodPost {
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodPost, req.Method)
}

product := &Product{
Element: ProductItem{
ID: productID,
Name: name,
},
}

responseBodyBytes, err := json.Marshal(product)
if err != nil {
t.Fatal(err)
}

return &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)),
Header: make(http.Header),
}
})

credential := "someAccessToken"
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient)
obj, err := c.CreateProduct(name, params)
if err != nil {
t.Fatal(err)
}

if obj == nil {
t.Fatal("CreateProduct returned nil")
}

if obj.Element.ID != productID {
t.Fatalf("obj ID does not match. Expected [%d]; got [%d]", productID, obj.Element.ID)
}

if obj.Element.Name != name {
t.Fatalf("obj name does not match. Expected [%s]; got [%s]", name, obj.Element.Name)
}
}

func TestUpdateProduct(t *testing.T) {
var (
productID int64 = 98765
endpoint = fmt.Sprintf(productResourceEndpoint, productID)
params = Params{"name": "newName"}
)

httpClient := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.Path != endpoint {
t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path)
}

if req.Method != http.MethodPut {
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodGet, req.Method)
}

product := &Product{
Element: ProductItem{
ID: productID,
Name: "newName",
},
}

responseBodyBytes, err := json.Marshal(product)
if err != nil {
t.Fatal(err)
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)),
Header: make(http.Header),
}
})

credential := "someAccessToken"
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient)
obj, err := c.UpdateProduct(productID, params)
if err != nil {
t.Fatal(err)
}

if obj == nil {
t.Fatal("product returned nil")
}

if obj.Element.ID != productID {
t.Fatalf("obj ID does not match. Expected [%d]; got [%d]", productID, obj.Element.ID)
}

if obj.Element.Name != "newName" {
t.Fatalf("obj name does not match. Expected [%s]; got [%s]", "newName", obj.Element.Name)
}
}

func TestDeleteProduct(t *testing.T) {
var (
productID int64 = 98765
endpoint = fmt.Sprintf(productResourceEndpoint, productID)
)

httpClient := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.Path != endpoint {
t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path)
}

if req.Method != http.MethodDelete {
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodDelete, req.Method)
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("")),
Header: make(http.Header),
}
})

credential := "someAccessToken"
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient)
err := c.DeleteProduct(productID)
if err != nil {
t.Fatal(err)
}
}

func TestListProducts(t *testing.T) {
httpClient := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.Path != productListResourceEndpoint {
t.Fatalf("Path does not match. Expected [%s]; got [%s]", backendListResourceEndpoint, req.URL.Path)
}

if req.Method != http.MethodGet {
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodGet, req.Method)
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(helperLoadBytes(t, "product_list_fixture.json"))),
Header: make(http.Header),
}
})

credential := "someAccessToken"
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient)
productList, err := c.ListProducts()
if err != nil {
t.Fatal(err)
}

if productList == nil {
t.Fatal("product list returned nil")
}

if len(productList.Products) != 2 {
t.Fatalf("Then number of products does not match. Expected [%d]; got [%d]", 2, len(productList.Products))
}
}
17 changes: 8 additions & 9 deletions client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func (c *ThreeScaleClient) ReadProxy(svcID string) (Proxy, error) {
req.URL.RawQuery = values.Encode()

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return p, err
}
defer resp.Body.Close()

err = handleXMLResp(resp, http.StatusOK, &p)
return p, err
Expand Down Expand Up @@ -74,12 +73,12 @@ func (c *ThreeScaleClient) UpdateProxy(svcId string, params Params) (Proxy, erro
}

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return p, err
}

defer resp.Body.Close()

err = handleXMLResp(resp, http.StatusOK, &p)
return p, err
}
Expand All @@ -100,12 +99,12 @@ func (c *ThreeScaleClient) ListProxyConfig(svcId string, env string) (ProxyConfi
req.URL.RawQuery = values.Encode()

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return pc, err
}

defer resp.Body.Close()

err = handleJsonResp(resp, http.StatusOK, &pc)
return pc, err
}
Expand All @@ -125,12 +124,12 @@ func (c *ThreeScaleClient) PromoteProxyConfig(svcId string, env string, version
}

resp, err := c.httpClient.Do(req)
defer resp.Body.Close()

if err != nil {
return pe, err
}

defer resp.Body.Close()

err = handleJsonResp(resp, http.StatusCreated, &pe)
return pe, err
}
Expand All @@ -151,7 +150,7 @@ func (c *ThreeScaleClient) getProxyConfig(endpoint string) (ProxyConfigElement,
if err != nil {
return pc, err
}
timeTaken := time.Now().Sub(start)
timeTaken := time.Since(start)
if c.afterResponse != nil {
c.afterResponse(resp.StatusCode, timeTaken)
}
Expand Down
24 changes: 24 additions & 0 deletions client/testdata/product_list_fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"services": [
{
"service": {
"id": 45498,
"name": "Product 01",
"system_name": "product_01",
"description": "",
"created_at": "2019-11-29T17:18:02+01:00",
"updated_at": "2020-03-25T15:07:22+01:00"
}
},
{
"service": {
"id": 55498,
"name": "Product 02",
"system_name": "product_02",
"description": "",
"created_at": "2019-11-29T17:18:02+01:00",
"updated_at": "2020-03-25T15:07:22+01:00"
}
}
]
}

0 comments on commit ac306cf

Please sign in to comment.