HTTP API Client: Update request 71/181671/2
authorMaciej Wereski <m.wereski@partner.samsung.com>
Fri, 10 Nov 2017 12:46:06 +0000 (13:46 +0100)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 19 Jun 2018 09:06:19 +0000 (11:06 +0200)
Change-Id: I7bac5be49bfd0f2a410720ec008c31c5d35cabd1
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
http/client/client.go
http/client/client_test.go

index cfad1aa..2922dc4 100644 (file)
@@ -176,7 +176,27 @@ func (client *BorutaClient) CloseRequest(reqID boruta.ReqID) error {
 // UpdateRequest prepares JSON with fields that should be changed for given
 // request ID.
 func (client *BorutaClient) UpdateRequest(reqInfo *boruta.ReqInfo) error {
-       return util.ErrNotImplemented
+       if reqInfo == nil {
+               return errors.New("nil reqInfo passed")
+       }
+       req, err := json.Marshal(&struct {
+               boruta.Priority
+               Deadline   time.Time
+               ValidAfter time.Time
+       }{
+               Priority:   reqInfo.Priority,
+               Deadline:   reqInfo.Deadline,
+               ValidAfter: reqInfo.ValidAfter,
+       })
+       if err != nil {
+               return err
+       }
+       path := client.url + "reqs/" + strconv.Itoa(int(reqInfo.ID))
+       resp, err := http.Post(path, contentType, bytes.NewReader(req))
+       if err != nil {
+               return err
+       }
+       return processResponse(resp, nil)
 }
 
 // GetRequestInfo queries Boruta server for details about given request ID.
index 08c4e26..4320263 100644 (file)
@@ -387,11 +387,67 @@ func TestCloseRequest(t *testing.T) {
 }
 
 func TestUpdateRequest(t *testing.T) {
-       assert, client := initTest(t, "")
-       assert.NotNil(client)
+       prefix := "update-req-"
+       path := "/api/v1/reqs/"
+
+       validAfter := time.Now()
+       deadline := validAfter.AddDate(0, 0, 2)
+       priority := Priority(4)
+
+       reqJSON := string(jsonMustMarshal(&struct {
+               Priority
+               Deadline   time.Time
+               ValidAfter time.Time
+       }{
+               Priority:   priority,
+               Deadline:   deadline,
+               ValidAfter: validAfter,
+       }))
+
+       reqinfo := req
+
+       reqinfo.Priority = priority
+       reqinfo.Deadline = deadline
+       reqinfo.ValidAfter = validAfter
 
+       tests := []*testCase{
+               &testCase{
+                       // valid request
+                       name:        prefix + "valid",
+                       path:        path + "1",
+                       json:        reqJSON,
+                       contentType: contentJSON,
+                       status:      http.StatusNoContent,
+               },
+               &testCase{
+                       // missing request
+                       name:        prefix + "missing",
+                       path:        path + "2",
+                       json:        reqJSON,
+                       contentType: contentJSON,
+                       status:      http.StatusNotFound,
+               },
+       }
+
+       srv := prepareServer(http.MethodPost, tests)
+       defer srv.Close()
+       assert, client := initTest(t, srv.URL)
+
+       // valid
+       reqinfo.ID = ReqID(1)
+       assert.Nil(client.UpdateRequest(&reqinfo))
+
+       // missing
+       reqinfo.ID = ReqID(2)
+       assert.Equal(errReqNotFound, client.UpdateRequest(&reqinfo))
+
+       // bad arguments
        err := client.UpdateRequest(nil)
-       assert.Equal(util.ErrNotImplemented, err)
+       assert.Equal(errors.New("nil reqInfo passed"), err)
+
+       // http.Post failure
+       client.url = "http://nosuchaddress.fail"
+       assert.NotNil(client.UpdateRequest(&reqinfo))
 }
 
 func TestGetRequestInfo(t *testing.T) {