From db6f3ef022d72629b7d94e28c51817b37516e795 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Wed, 30 Mar 2022 16:11:52 +0200 Subject: [PATCH 1/3] fix delete product --- client/product.go | 3 +- client/product_test.go | 173 ++++++++++++++++++++++ client/testdata/product_list_fixture.json | 24 +++ 3 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 client/testdata/product_list_fixture.json diff --git a/client/product.go b/client/product.go index b08e7b4..92ebe6b 100644 --- a/client/product.go +++ b/client/product.go @@ -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 diff --git a/client/product_test.go b/client/product_test.go index be48312..85d1b9f 100644 --- a/client/product_test.go +++ b/client/product_test.go @@ -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)) + } +} diff --git a/client/testdata/product_list_fixture.json b/client/testdata/product_list_fixture.json new file mode 100644 index 0000000..f8abfdd --- /dev/null +++ b/client/testdata/product_list_fixture.json @@ -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" + } + } + ] +} From 343f0e0e9c309129c19cd5400b09d472510780d6 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Wed, 30 Mar 2022 16:22:03 +0200 Subject: [PATCH 2/3] fix lint issues --- client/client_test.go | 28 +++++++++++++++++++--------- client/mapping.go | 7 +++---- client/proxy.go | 17 ++++++++--------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 076c85a..7171b87 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -148,46 +148,56 @@ 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(""))), } })).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(""))), } })).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(""))), } })).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(""))), } })).UpdateService("any", NewParams()) - + if err != nil { + t.Fatal(err) + } } func equals(t *testing.T, exp, act interface{}) { diff --git a/client/mapping.go b/client/mapping.go index 6e16a2c..9b6d991 100644 --- a/client/mapping.go +++ b/client/mapping.go @@ -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 } @@ -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) } diff --git a/client/proxy.go b/client/proxy.go index dd62f37..457a8fa 100644 --- a/client/proxy.go +++ b/client/proxy.go @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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) } From 280d56c86e548a755e11db6a9180f5ae9861f935 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Wed, 30 Mar 2022 16:55:43 +0200 Subject: [PATCH 3/3] fix client tests --- client/client_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 7171b87..7b2c54f 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -159,7 +159,8 @@ func TestAdminPortalPathIsPreserved(t *testing.T) { _, 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(``))), } })).ListServices() if err != nil { @@ -170,7 +171,8 @@ func TestAdminPortalPathIsPreserved(t *testing.T) { _, 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(``))), } })).CreateService("any") if err != nil { @@ -181,7 +183,8 @@ func TestAdminPortalPathIsPreserved(t *testing.T) { 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 { @@ -192,7 +195,8 @@ func TestAdminPortalPathIsPreserved(t *testing.T) { _, 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(``))), } })).UpdateService("any", NewParams()) if err != nil {