diff --git a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java index 667eea8e..f549b97a 100644 --- a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java +++ b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java @@ -17,6 +17,8 @@ public class AqlQueryBuilder { private AqlRootElement root = new AqlRootElement(); private AqlItem sort; private AqlInclude include; + private Integer limit; + private Integer offset; public AqlQueryBuilder item(AqlItem item) { root.putAll(item.value()); @@ -26,9 +28,9 @@ public AqlQueryBuilder item(AqlItem item) { public AqlQueryBuilder elements(AqlItem... items) { if (isNotEmpty(items)) { root.putAll(Arrays.stream(items) - .map(item -> item.value().entrySet()) - .flatMap(Collection::stream) - .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))); + .map(item -> item.value().entrySet()) + .flatMap(Collection::stream) + .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))); } return this; } @@ -97,10 +99,21 @@ public AqlQueryBuilder desc(String... by) { return this; } + public AqlQueryBuilder limit(int limit) { + this.limit = limit; + return this; + } + + public AqlQueryBuilder offset(int offset) { + this.offset = offset; + return this; + } + public String build() { try { ObjectMapper mapper = new ObjectMapper(); - return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper); + return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString( + mapper) + getOffsetAsString(mapper) + getLimitAsString(mapper); } catch (JsonProcessingException e) { throw new AqlBuilderException("Error serializing object to json: ", e); } @@ -114,16 +127,32 @@ private String getIncludeAsString() { return hasInclude() ? include.toString() : ""; } + private String getOffsetAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasOffset() ? ".offset(" + mapper.writeValueAsString(offset) + ")" : ""; + } + + private String getLimitAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasLimit() ? ".limit(" + mapper.writeValueAsString(limit) + ")" : ""; + } + private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException { return hasRoot() ? mapper.writeValueAsString(root) : ""; } + private boolean hasInclude() { + return include != null && include.isNotEmpty(); + } + private boolean hasSort() { return sort != null && sort.isNotEmpty(); } - private boolean hasInclude() { - return include != null && include.isNotEmpty(); + private boolean hasLimit() { + return limit != null; + } + + private boolean hasOffset() { + return offset != null; } private boolean hasRoot() { diff --git a/services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java b/services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java index d590bc52..0d484301 100644 --- a/services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java +++ b/services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java @@ -144,31 +144,51 @@ public void orTest() { + "]})")); } - @Test public void matchTest() { String result = new AqlQueryBuilder() - .match("repo", "myrepo*") - .build(); + .match("repo", "myrepo*") + .build(); assertThat(result, notNullValue()); assertThat(result, is("items.find(" - + "{\"repo\":" - + "{\"$match\":\"myrepo*\"}" - + "})")); + + "{\"repo\":" + + "{\"$match\":\"myrepo*\"}" + + "})")); } @Test public void notMatchTest() { String result = new AqlQueryBuilder() - .notMatch("repo", "myrepo*") - .build(); + .notMatch("repo", "myrepo*") + .build(); assertThat(result, notNullValue()); assertThat(result, is("items.find(" - + "{\"repo\":" - + "{\"$nmatch\":\"myrepo*\"}" - + "})")); + + "{\"repo\":" + + "{\"$nmatch\":\"myrepo*\"}" + + "})")); + } + + + @Test + public void limitTest() { + String result = new AqlQueryBuilder() + .limit(123) + .build(); + + assertThat(result, notNullValue()); + assertThat(result, is("items.find().limit(123)")); + } + + @Test + public void offsetTest() { + String result = new AqlQueryBuilder() + .offset(123) + .build(); + + assertThat(result, notNullValue()); + assertThat(result, is("items.find().offset(123)")); } @Test @@ -221,6 +241,8 @@ public void variousElements() { .item(aqlItem("property", "value")) .include("name", "repo") .asc("name", "repo") + .offset(1) + .limit(2) .build(); assertThat(result, notNullValue()); @@ -237,7 +259,9 @@ public void variousElements() { + "\"property\":\"value\"" + "})" + ".include(\"name\",\"repo\")" - + ".sort({\"$asc\":[\"name\",\"repo\"]})")); + + ".sort({\"$asc\":[\"name\",\"repo\"]})" + + ".offset(1)" + + ".limit(2)")); } @Test