Skip to content

Commit

Permalink
add limit
Browse files Browse the repository at this point in the history
fix Count header name
  • Loading branch information
wistefan committed Apr 23, 2021
1 parent 811ed8d commit dad96b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
6 changes: 3 additions & 3 deletions api/full_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ paths:
- $ref: '#/components/parameters/csf'
- $ref: '#/components/parameters/pageSize'
- $ref: '#/components/parameters/pageAnchor'
- $ref: '#/components/parameters/limit'
- $ref: '#/components/parameters/components-parameters-options'
- $ref: '#/components/parameters/lastN'
responses:
Expand Down Expand Up @@ -1070,6 +1071,7 @@ paths:
- $ref: '#/components/parameters/link'
- $ref: '#/components/parameters/pageSize'
- $ref: '#/components/parameters/pageAnchor'
- $ref: '#/components/parameters/limit'
- $ref: '#/components/parameters/components-parameters-options'
- $ref: '#/components/parameters/lastN'
requestBody:
Expand Down Expand Up @@ -1185,7 +1187,6 @@ components:
type: integer
minimum: 1
maximum: 100
default: 100
pageAnchor:
name: pageAnchor
description: Size of the page to be returned
Expand All @@ -1203,7 +1204,6 @@ components:
type: integer
minimum: 1
maximum: 100
default: 100
offset:
name: offset
description: Pagination offset
Expand Down Expand Up @@ -1485,7 +1485,7 @@ components:
- type: array
items:
oneOf:
- type: string)
- type: string
- type: number
- type: boolean
- type: object
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/fiware/mintaka/rest/TemporalApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ public class TemporalApiController implements TemporalRetrievalApi {
private static final String TEMPORAL_VALUES_OPTION = "temporalValues";
private static final String COUNT_OPTION = "count";
private static final String LINK_HEADER_TEMPLATE = "<%s>; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json\"";
private static final String NGSILD_RESULTS_COUNT_HEADER = "NGSILD-Results-Count";
private static final String PAGE_SIZE_HEADER = "Page-Size";
private static final String NEXT_PAGE_HEADER = "Next-Page";
private static final String PREVIOUS_PAGE_HEADER = "Previous-Page";
private static final String LINK_HEADER = "Link";

public static final String COMMA_SEPERATOR = ",";
public static final String TIMERELATION_ERROR_MSG_TEMPLATE = "The given timestamp type is not supported: %s";


private final EntityTemporalService entityTemporalService;
private final LdContextCache contextCache;
private final QueryParser queryParser;
Expand All @@ -99,6 +104,7 @@ public HttpResponse<Object> queryTemporalEntities(
@Nullable @Size(min = 1) String csf,
Integer pageSize,
URI pageAnchor,
Integer limit,
@Nullable String options,
@Nullable @Min(1) Integer lastN) {

Expand All @@ -117,6 +123,8 @@ public HttpResponse<Object> queryTemporalEntities(
Optional<QueryTerm> optionalQuery = Optional.ofNullable(q).map(queryString -> queryParser.toTerm(queryString, contextUrls));
Optional<GeoQuery> optionalGeoQuery = getGeometryQuery(georel, geometry, coordinates, expandedGeoProperty);

// if pagesize is null, set it to limit, even though limit might also be null.
pageSize = getPageSize(pageSize, limit);

LimitableResult<List<EntityTemporalVO>> limitableResult = entityTemporalService.getEntitiesWithQuery(
optionalIdList,
Expand Down Expand Up @@ -155,34 +163,41 @@ public HttpResponse<Object> queryTemporalEntities(
mutableHttpResponse = HttpResponse.ok(entityTemporalListVO);
}
paginationInformation.ifPresent(pi -> {
mutableHttpResponse.header("Page-Size", String.valueOf(pi.getPageSize()));
pi.getNextPage().ifPresent(np -> mutableHttpResponse.header("Next-Page", np));
pi.getPreviousPage().ifPresent(pp -> mutableHttpResponse.header("Previous-Page", pp));
mutableHttpResponse.header(PAGE_SIZE_HEADER, String.valueOf(pi.getPageSize()));
pi.getNextPage().ifPresent(np -> mutableHttpResponse.header(NEXT_PAGE_HEADER, np));
pi.getPreviousPage().ifPresent(pp -> mutableHttpResponse.header(PREVIOUS_PAGE_HEADER, pp));
});

if (isCountOptionSet(options)) {
Number totalCount = entityTemporalService.countMatchingEntities(optionalIdList, optionalIdPattern, expandedTypes, optionalQuery, optionalGeoQuery, timeQuery);
mutableHttpResponse.header("NGSILD-Total-Count", totalCount.toString());
mutableHttpResponse.header(NGSILD_RESULTS_COUNT_HEADER, totalCount.toString());
}

if (acceptType == AcceptType.JSON) {
mutableHttpResponse.header("Link", getLinkHeader(contextUrls));
mutableHttpResponse.header(LINK_HEADER, getLinkHeader(contextUrls));
}
return mutableHttpResponse;
}

private Integer getPageSize(Integer pageSize, Integer limit) {
Integer requestedPageSize = Optional.ofNullable(pageSize).orElse(limit);
return Optional.ofNullable(requestedPageSize).orElse(DEFAULT_LIMIT);
}

@Override
public HttpResponse<Object> queryTemporalEntitiesOnPost(
@NotNull QueryVO queryVO,
@Nullable String link,
@Nullable @Min(1) @Max(100) Integer pageSize,
@Nullable URI pageAnchor,
@Nullable @Min(1) @Max(100) Integer limit,
@Nullable String options,
@Nullable @Min(1) Integer lastN) {

Optional<EntityInfoVO> entityInfoVO = Optional.ofNullable(queryVO.entities());
Optional<GeoQueryVO> geoQueryVO = Optional.ofNullable(queryVO.geoQ());
Optional<TemporalQueryVO> temporalQueryVO = Optional.ofNullable(queryVO.temporalQ());

return queryTemporalEntities(link,
entityInfoVO.map(EntityInfoVO::getId).map(this::idToString).orElse(null),
entityInfoVO.map(EntityInfoVO::getIdPattern).orElse(null),
Expand All @@ -196,7 +211,7 @@ public HttpResponse<Object> queryTemporalEntitiesOnPost(
temporalQueryVO.map(TemporalQueryVO::getTimeproperty).orElse(null),
temporalQueryVO.map(TemporalQueryVO::timeAt).orElse(null),
temporalQueryVO.map(TemporalQueryVO::endTimeAt).orElse(null),
queryVO.csf(), pageSize, pageAnchor, options, lastN);
queryVO.csf(), pageSize, pageAnchor, limit, options, lastN);
}

private String idToString(Object id) {
Expand Down
20 changes: 11 additions & 9 deletions src/test/java/org/fiware/mintaka/QueryingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,9 @@ public void testTempWithNgsiQueryNoAttrInReqTimeFrameInTempValues(RequestType re
}

@DisplayName("Test count option")
@Test
public void testCountOptionSet() {
@ParameterizedTest
@ValueSource(strings = {"pageSize", "limit"})
public void testCountOptionSet(String paginationParameterName) {
MutableHttpRequest getRequest = getGetRequest(
Optional.empty(),
Optional.of(".*"),
Expand All @@ -821,23 +822,24 @@ public void testCountOptionSet() {

getRequest.getParameters()
.add("options", "count")
.add("pageSize", "2");
.add(paginationParameterName, "2");

HttpResponse<List<Map<String, Object>>> entryListResponse = mintakaTestClient.toBlocking().exchange(getRequest, List.class);
assertEquals(HttpStatus.OK, entryListResponse.getStatus(), "The request should succeed.");
assertNotNull(entryListResponse.getHeaders().get("NGSILD-Total-Count"), "Count should be present");
assertEquals("5", entryListResponse.getHeaders().get("NGSILD-Total-Count"), "5 entites should match.");
assertNotNull(entryListResponse.getHeaders().get("NGSILD-Results-Count"), "Count should be present");
assertEquals("5", entryListResponse.getHeaders().get("NGSILD-Results-Count"), "5 entites should match.");
}

@DisplayName("Retrieve query results with entity pagination")
@Test
public void testEntityPagination() {
@ParameterizedTest
@ValueSource(strings = {"pageSize", "limit"})
public void testEntityPagination(String paginationParameterName) {
MutableHttpRequest getRequest = HttpRequest.GET("/temporal/entities/");
getRequest.getParameters()
.add("options", "temporalValues")
.add("idPattern", ".*")
.add("attrs", "temperature")
.add("pageSize", "2");
.add(paginationParameterName, "2");
List<String> allReturnedEntities = new ArrayList<>();
HttpResponse<List<Map<String, Object>>> entryListResponse = mintakaTestClient.toBlocking().exchange(getRequest, List.class);
assertEquals(HttpStatus.OK, entryListResponse.getStatus(), "The request should succeed.");
Expand All @@ -862,7 +864,7 @@ public void testEntityPagination() {
.add("options", "temporalValues")
.add("idPattern", ".*")
.add("attrs", "temperature")
.add("pageSize", "2")
.add(paginationParameterName, "2")
.add("pageAnchor", nextPageAnchor);
entryListResponse = mintakaTestClient.toBlocking().exchange(getRequest, List.class);
assertEquals(HttpStatus.OK, entryListResponse.getStatus(), "The request should succeed.");
Expand Down

0 comments on commit dad96b1

Please sign in to comment.