Skip to content

Commit

Permalink
unbind activedoc from product
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Dec 2, 2020
1 parent a92ef49 commit e6cd4cc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
34 changes: 34 additions & 0 deletions client/activedocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
56 changes: 56 additions & 0 deletions client/activedocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e6cd4cc

Please sign in to comment.