From e6cd4cc6b3a5dc64d1a02f30aa392037d8c98258 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Wed, 2 Dec 2020 18:31:49 +0100 Subject: [PATCH] unbind activedoc from product --- client/activedocs.go | 34 ++++++++++++++++++++++++ client/activedocs_test.go | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/client/activedocs.go b/client/activedocs.go index c4e82fb..5eb95a8 100644 --- a/client/activedocs.go +++ b/client/activedocs.go @@ -126,3 +126,37 @@ func (c *ThreeScaleClient) DeleteActiveDoc(id int64) error { return handleJsonResp(resp, http.StatusOK, nil) } + +// UnbindActiveDocFromProduct removes product relationship from activedoc object +func (c *ThreeScaleClient) UnbindActiveDocFromProduct(id int64) (*ActiveDoc, error) { + endpoint := fmt.Sprintf(activeDocEndpoint, id) + + data := struct { + ID int64 `json:"id"` + ServiceID *int64 `json:"service_id"` + }{ + id, + nil, + } + + bodyArr, err := json.Marshal(data) + if err != nil { + return nil, err + } + body := bytes.NewReader(bodyArr) + + req, err := c.buildUpdateJSONReq(endpoint, body) + if err != nil { + return nil, err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + respObj := &ActiveDoc{} + err = handleJsonResp(resp, http.StatusOK, respObj) + return respObj, err +} diff --git a/client/activedocs_test.go b/client/activedocs_test.go index 65c7e1a..5593ffa 100644 --- a/client/activedocs_test.go +++ b/client/activedocs_test.go @@ -278,3 +278,59 @@ func TestDeleteActiveDocs(t *testing.T) { t.Fatal(err) } } + +func TestUnbindActiveDocFromProduct(t *testing.T) { + var ( + adID1 int64 = 1 + name1 = "ActiveDoc1" + body = "{}" + endpoint = fmt.Sprintf(activeDocEndpoint, adID1) + activeDoc = ActiveDoc{ + Element: ActiveDocItem{ + ID: &adID1, + Name: &name1, + Body: &body, + SystemName: &name1, + }, + } + ) + + 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.MethodPut, req.Method) + } + + responseBodyBytes, err := json.Marshal(activeDoc) + 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) + resp, err := c.UnbindActiveDocFromProduct(adID1) + if err != nil { + t.Fatal(err) + } + + if resp == nil { + t.Fatal("response was nil") + } + + if !reflect.DeepEqual(*resp, activeDoc) { + got, _ := json.Marshal(*resp) + expected, _ := json.Marshal(activeDoc) + t.Logf("Expected %s; got %s", string(expected), string(got)) + t.Fatalf("Expected %v; got %v", expected, got) + } +}