Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions broker/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,35 @@ func CollectAboutData(fullCount int64, offset int32, limit int32, r *http.Reques
}
limit64 := int64(limit)
offset64 := int64(offset)
lastOffset := int64(0)
if fullCount > 0 {
lastOffset = ((fullCount - 1) / limit64) * limit64
}
if fullCount > limit64 {
lastOffset := ((fullCount - 1) / limit64) * limit64
urlValues := r.URL.Query()
urlValues["offset"] = []string{strconv.FormatInt(lastOffset, 10)}
link := ToLinkUrlValues(r, urlValues)
about.LastLink = &link
if offset64 != lastOffset {
urlValues := r.URL.Query()
urlValues["offset"] = []string{strconv.FormatInt(lastOffset, 10)}
link := ToLinkUrlValues(r, urlValues)
about.LastLink = &link
}
}
if offset64 > 0 {
urlValues := r.URL.Query()
urlValues["offset"] = []string{"0"}
firstLink := ToLinkUrlValues(r, urlValues)
about.FirstLink = &firstLink

pOffset := offset64 - limit64
if pOffset < 0 {
pOffset = 0
}
urlValues := r.URL.Query()
if pOffset > lastOffset {
pOffset = lastOffset
}
urlValues = r.URL.Query()
urlValues["offset"] = []string{strconv.FormatInt(pOffset, 10)}
link := ToLinkUrlValues(r, urlValues)
about.PrevLink = &link
prevLink := ToLinkUrlValues(r, urlValues)
about.PrevLink = &prevLink
}
if fullCount > offset64+limit64 {
noffset := offset64 + limit64
Expand Down
74 changes: 74 additions & 0 deletions broker/api/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

Expand All @@ -25,3 +26,76 @@ func TestGetSymbolForRequest(t *testing.T) {
assert.Equal(t, "tenant mapping must be specified", err.Error())
assert.Equal(t, "", resolved)
}

func TestCollectAboutDataLastLink(t *testing.T) {
reqOffset0 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=0", nil)
reqOffset10 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=10", nil)
reqOffset20 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=20", nil)
reqOffset1000 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=1000", nil)

// First page (count=21, limit=10, offset=0): prevLink omitted, next/last present.
about := CollectAboutData(21, 0, 10, reqOffset0)
assert.Equal(t, int64(21), about.Count)
assert.Nil(t, about.FirstLink)
assert.Nil(t, about.PrevLink)
assert.NotNil(t, about.NextLink)
assert.Contains(t, *about.NextLink, "offset=10")
assert.Contains(t, *about.NextLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.LastLink)
assert.Contains(t, *about.LastLink, "offset=20")
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")

// Not last page (count=21, limit=10, offset=10): all links present
about = CollectAboutData(21, 10, 10, reqOffset10)
assert.Equal(t, int64(21), about.Count)
assert.NotNil(t, about.FirstLink)
assert.Contains(t, *about.FirstLink, "offset=0")
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.PrevLink)
assert.Contains(t, *about.PrevLink, "offset=0")
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.NextLink)
assert.Contains(t, *about.NextLink, "offset=20")
assert.Contains(t, *about.NextLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.LastLink)
assert.Contains(t, *about.LastLink, "offset=20")
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")

// Last page (count=20, limit=10, offset=10): lastLink and nextLink should be omitted.
about = CollectAboutData(20, 10, 10, reqOffset10)
assert.Equal(t, int64(20), about.Count)
assert.NotNil(t, about.FirstLink)
assert.Contains(t, *about.FirstLink, "offset=0")
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.PrevLink)
assert.Contains(t, *about.PrevLink, "offset=0")
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
assert.Nil(t, about.NextLink)
assert.Nil(t, about.LastLink)

// Last partial page (count=21, limit=10, offset=20): lastLink and nextLink should be omitted.
about = CollectAboutData(21, 20, 10, reqOffset20)
assert.Equal(t, int64(21), about.Count)
assert.NotNil(t, about.FirstLink)
assert.Contains(t, *about.FirstLink, "offset=0")
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.PrevLink)
assert.Contains(t, *about.PrevLink, "offset=10")
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
assert.Nil(t, about.NextLink)
assert.Nil(t, about.LastLink)

// Out-of-range page (count=21, limit=10, offset=1000): prevLink and lastLink should be present.
about = CollectAboutData(21, 1000, 10, reqOffset1000)
assert.Equal(t, int64(21), about.Count)
assert.NotNil(t, about.FirstLink)
assert.Contains(t, *about.FirstLink, "offset=0")
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
assert.NotNil(t, about.PrevLink)
assert.Contains(t, *about.PrevLink, "offset=20")
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
assert.Nil(t, about.NextLink)
assert.NotNil(t, about.LastLink)
assert.Contains(t, *about.LastLink, "offset=20")
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")
}
Comment thread
jakub-id marked this conversation as resolved.
3 changes: 3 additions & 0 deletions broker/oapi/open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ components:
prevLink:
type: string
description: Link to the previous page of results
firstLink:
type: string
description: Link to the first page of results
lastLink:
type: string
description: Link to the last page of results
Expand Down
Loading