HTTP API Client: List and filter requests 73/181673/8
authorMaciej Wereski <m.wereski@partner.samsung.com>
Fri, 10 Nov 2017 14:05:57 +0000 (15:05 +0100)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Mon, 9 Jul 2018 12:55:05 +0000 (14:55 +0200)
Change-Id: I550ebfd6e87b916e15adf39519568b18828d6ae2
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
http/client/client.go
http/client/client_test.go

index b218473..0de4078 100644 (file)
@@ -219,9 +219,19 @@ func (client *BorutaClient) GetRequestInfo(reqID boruta.ReqID) (boruta.ReqInfo,
 
 // ListRequests queries Boruta server for list of requests that match given
 // filter. Filter may be empty or nil to get list of all requests.
-func (client *BorutaClient) ListRequests(filter boruta.ListFilter) (
-       []boruta.ReqInfo, error) {
-       return nil, util.ErrNotImplemented
+func (client *BorutaClient) ListRequests(filter boruta.ListFilter) ([]boruta.ReqInfo, error) {
+       req, err := json.Marshal(filter)
+       if err != nil {
+               return nil, err
+       }
+       resp, err := http.Post(client.url+"reqs/list", contentType,
+               bytes.NewReader(req))
+       if err != nil {
+               return nil, err
+       }
+       list := new([]boruta.ReqInfo)
+       err = processResponse(resp, list)
+       return *list, err
 }
 
 // AcquireWorker queries Boruta server for information required to access
index 2b680e2..2eda6df 100644 (file)
@@ -47,6 +47,13 @@ type testCase struct {
        header http.Header
 }
 
+// chanFilter is used to mock failure of JSON marshalling in ListRequests.
+type chanFilter chan bool
+
+func (f chanFilter) Match(_ *ReqInfo) bool {
+       return false
+}
+
 type dummyReadCloser int
 
 func (r dummyReadCloser) Close() error {
@@ -505,12 +512,93 @@ func TestGetRequestInfo(t *testing.T) {
 }
 
 func TestListRequests(t *testing.T) {
-       assert, client := initTest(t, "")
-       assert.NotNil(client)
+       prefix := "filter-reqs-"
+       path := "/api/v1/reqs/list"
+
+       // from api/v1 TestListRequestsHandler
+       deadline, _ := time.Parse(dateLayout, "2222-12-31T00:00:00Z")
+       validAfter, _ := time.Parse(dateLayout, "1683-09-12T00:00:00Z")
+       reqs := []ReqInfo{
+               {ID: 1, Priority: (HiPrio + LoPrio) / 2, State: WAIT,
+                       Deadline: deadline, ValidAfter: validAfter},
+               {ID: 2, Priority: (HiPrio+LoPrio)/2 + 1, State: WAIT,
+                       Deadline: deadline, ValidAfter: validAfter},
+               {ID: 3, Priority: (HiPrio + LoPrio) / 2, State: CANCEL,
+                       Deadline: deadline, ValidAfter: validAfter},
+               {ID: 4, Priority: (HiPrio+LoPrio)/2 + 1, State: CANCEL,
+                       Deadline: deadline, ValidAfter: validAfter},
+       }
+
+       missingFilter := util.NewRequestFilter("INPROGRESS", "2")
+       missingHeader := make(http.Header)
+       missingHeader.Set("Boruta-Request-Count", "0")
+       validFilter := util.NewRequestFilter("WAIT", "")
+       validHeader := make(http.Header)
+       validHeader.Set("Boruta-Request-Count", "2")
+       nilHeader := make(http.Header)
+       nilHeader.Set("Boruta-Request-Count", "4")
+
+       tests := []*testCase{
+               &testCase{
+                       // valid filter
+                       name:        prefix + "valid-filter",
+                       path:        path,
+                       json:        string(jsonMustMarshal(validFilter)),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      validHeader,
+               },
+               &testCase{
+                       // nil filter - list all
+                       name:        prefix + "nil",
+                       path:        path,
+                       json:        string(jsonMustMarshal(nil)),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      nilHeader,
+               },
+               &testCase{
+                       // no requests matched
+                       name:        prefix + "nomatch-all",
+                       path:        path,
+                       json:        string(jsonMustMarshal(missingFilter)),
+                       contentType: contentJSON,
+                       status:      http.StatusOK,
+                       header:      missingHeader,
+               },
+       }
+
+       srv := prepareServer(http.MethodPost, tests)
+       defer srv.Close()
+       assert, client := initTest(t, srv.URL)
+
+       // nil filter
+       list, err := client.ListRequests(nil)
+       assert.Nil(err)
+       assert.Equal(reqs, list)
+
+       // valid filter
+       list, err = client.ListRequests(validFilter)
+       assert.Nil(err)
+       assert.Equal(reqs[:2], list)
+
+       // no requests matched
+       list, err = client.ListRequests(missingFilter)
+       assert.Nil(err)
+       assert.Equal([]ReqInfo{}, list)
+
+       // json.Marshal failure
+       var typeError *json.UnsupportedTypeError
+       list, err = client.ListRequests(make(chanFilter))
+       assert.Empty(list)
+       assert.IsType(typeError, err)
+
+       // http.Post failure
+       client.url = "http://nosuchaddress.fail"
+       list, err = client.ListRequests(nil)
+       assert.Zero(list)
+       assert.NotNil(err)
 
-       reqInfo, err := client.ListRequests(nil)
-       assert.Nil(reqInfo)
-       assert.Equal(util.ErrNotImplemented, err)
 }
 
 func TestAcquireWorker(t *testing.T) {