From 876f0028a49bf14d31d85a6a78b2b27a49ae1c2a Mon Sep 17 00:00:00 2001 From: Laurent BAYE Date: Wed, 29 Mar 2023 23:53:07 +0200 Subject: [PATCH 1/2] feat(aql): Adding Pagination to AQL Query Builder --- .../client/aql/AqlQueryBuilder.java | 225 ++++++++++-------- .../client/aql/AqlQueryBuilderTest.java | 27 ++- 2 files changed, 153 insertions(+), 99 deletions(-) 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 b07a2bfa..954357fb 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 @@ -14,109 +14,138 @@ import static org.jfrog.artifactory.client.aql.AqlInclude.buildWithElements; public class AqlQueryBuilder { - private AqlRootElement root = new AqlRootElement(); - private AqlItem sort; - private AqlInclude include; + 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()); + return this; + } - public AqlQueryBuilder item(AqlItem item) { - root.putAll(item.value()); - return this; - } - - 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))); - } - return this; - } + 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))); + } + return this; + } + + public AqlQueryBuilder array(String key, AqlItem... items) { + if (isNotEmpty(items)) { + root.put(key, items); + } + return this; + } + + public AqlQueryBuilder and(AqlItem... items) { + if (isNotEmpty(items)) { + root.putAll(AqlItem.and((Object[])items).value()); + } + return this; + } + + public AqlQueryBuilder and(Collection items) { + return and(setToArray(items)); + } + + public AqlQueryBuilder or(AqlItem... items) { + if (isNotEmpty(items)) { + root.putAll(AqlItem.or((Object[])items).value()); + } + return this; + } + + public AqlQueryBuilder or(Collection items) { + return or(setToArray(items)); + } + + public AqlQueryBuilder include(String... elements) { + if (isNotEmpty(elements)) { + include = buildWithElements(elements); + } + return this; + } + + public AqlQueryBuilder asc(String... by) { + if (isNotEmpty(by)) { + this.sort = AqlItem.asc(by); + } + return this; + } + + public AqlQueryBuilder desc(String... by) { + if (isNotEmpty(by)) { + this.sort = AqlItem.desc(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) + getOffsetAsString(mapper) + getLimitAsString(mapper); + } catch (JsonProcessingException e) { + throw new AqlBuilderException("Error serializing object to json: ", e); + } + } + + private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : ""; + } + + 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) : ""; + } - public AqlQueryBuilder array(String key, AqlItem... items) { - if (isNotEmpty(items)) { - root.put(key, items); - } - return this; - } + private boolean hasInclude() { + return include != null && include.isNotEmpty(); + } - public AqlQueryBuilder and(AqlItem... items) { - if (isNotEmpty(items)) { - root.putAll(AqlItem.and((Object[]) items).value()); - } - return this; - } + private boolean hasSort() { + return sort != null && sort.isNotEmpty(); + } - public AqlQueryBuilder and(Collection items) { - return and(setToArray(items)); - } + private boolean hasLimit() { + return limit != null; + } - public AqlQueryBuilder or(AqlItem... items) { - if (isNotEmpty(items)) { - root.putAll(AqlItem.or((Object[]) items).value()); - } - return this; - } + private boolean hasOffset() { + return offset != null; + } - public AqlQueryBuilder or(Collection items) { - return or(setToArray(items)); - } + private boolean hasRoot() { + return root != null && root.isNotEmpty(); + } - public AqlQueryBuilder include(String... elements) { - if (isNotEmpty(elements)) { - include = buildWithElements(elements); - } - return this; - } - - public AqlQueryBuilder asc(String... by) { - if (isNotEmpty(by)) { - this.sort = AqlItem.asc(by); - } - return this; - } - - public AqlQueryBuilder desc(String... by) { - if (isNotEmpty(by)) { - this.sort = AqlItem.desc(by); - } - return this; - } - - public String build() { - try { - ObjectMapper mapper = new ObjectMapper(); - return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper); - } catch (JsonProcessingException e) { - throw new AqlBuilderException("Error serializing object to json: ", e); - } - } - - private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException { - return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : ""; - } - - private String getIncludeAsString() { - return hasInclude() ? include.toString() : ""; - } - - private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException { - return hasRoot() ? mapper.writeValueAsString(root) : ""; - } - - private boolean hasSort() { - return sort != null && sort.isNotEmpty(); - } - - private boolean hasInclude() { - return include != null && include.isNotEmpty(); - } - - private boolean hasRoot() { - return root != null && root.isNotEmpty(); - } - - private AqlItem[] setToArray(Collection items) { - return items.toArray(new AqlItem[0]); - } + private AqlItem[] setToArray(Collection items) { + return items.toArray(new AqlItem[0]); + } } 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 cd69b590..dddf6b94 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 @@ -143,6 +143,27 @@ public void orTest() { + "]})")); } + + @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 public void addNestedFilters() { final String result = new AqlQueryBuilder() @@ -190,6 +211,8 @@ public void variousElements() { .item(aqlItem("property", "value")) .include("name", "repo") .asc("name", "repo") + .offset(1) + .limit(2) .build(); assertThat(result, notNullValue()); @@ -206,7 +229,9 @@ public void variousElements() { + "\"property\":\"value\"" + "})" + ".include(\"name\",\"repo\")" - + ".sort({\"$asc\":[\"name\",\"repo\"]})")); + + ".sort({\"$asc\":[\"name\",\"repo\"]})" + + ".offset(1)" + + ".limit(2)")); } @Test From deb57ad598bbee03df1c322ce7989fdb57bb4246 Mon Sep 17 00:00:00 2001 From: yahavi Date: Tue, 7 Nov 2023 17:29:14 +0200 Subject: [PATCH 2/2] Reformat code --- .../client/aql/AqlQueryBuilder.java | 254 +++++++++--------- .../client/aql/AqlQueryBuilderTest.java | 8 +- 2 files changed, 131 insertions(+), 131 deletions(-) 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 954357fb..769ebde1 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 @@ -14,138 +14,138 @@ import static org.jfrog.artifactory.client.aql.AqlInclude.buildWithElements; 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()); - return this; - } + 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()); + return this; + } + + 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))); + } + return this; + } + + public AqlQueryBuilder array(String key, AqlItem... items) { + if (isNotEmpty(items)) { + root.put(key, items); + } + return this; + } + + public AqlQueryBuilder and(AqlItem... items) { + if (isNotEmpty(items)) { + root.putAll(AqlItem.and((Object[]) items).value()); + } + return this; + } + + public AqlQueryBuilder and(Collection items) { + return and(setToArray(items)); + } + + public AqlQueryBuilder or(AqlItem... items) { + if (isNotEmpty(items)) { + root.putAll(AqlItem.or((Object[]) items).value()); + } + return this; + } + + public AqlQueryBuilder or(Collection items) { + return or(setToArray(items)); + } + + public AqlQueryBuilder include(String... elements) { + if (isNotEmpty(elements)) { + include = buildWithElements(elements); + } + return this; + } + + public AqlQueryBuilder asc(String... by) { + if (isNotEmpty(by)) { + this.sort = AqlItem.asc(by); + } + return this; + } + + public AqlQueryBuilder desc(String... by) { + if (isNotEmpty(by)) { + this.sort = AqlItem.desc(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) + getOffsetAsString(mapper) + getLimitAsString(mapper); + } catch (JsonProcessingException e) { + throw new AqlBuilderException("Error serializing object to json: ", e); + } + } + + private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : ""; + } + + private String getIncludeAsString() { + return hasInclude() ? include.toString() : ""; + } + + private String getOffsetAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasOffset() ? ".offset(" + mapper.writeValueAsString(offset) + ")" : ""; + } - 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))); - } - return this; - } - - public AqlQueryBuilder array(String key, AqlItem... items) { - if (isNotEmpty(items)) { - root.put(key, items); - } - return this; - } - - public AqlQueryBuilder and(AqlItem... items) { - if (isNotEmpty(items)) { - root.putAll(AqlItem.and((Object[])items).value()); - } - return this; - } - - public AqlQueryBuilder and(Collection items) { - return and(setToArray(items)); - } - - public AqlQueryBuilder or(AqlItem... items) { - if (isNotEmpty(items)) { - root.putAll(AqlItem.or((Object[])items).value()); - } - return this; - } - - public AqlQueryBuilder or(Collection items) { - return or(setToArray(items)); - } - - public AqlQueryBuilder include(String... elements) { - if (isNotEmpty(elements)) { - include = buildWithElements(elements); - } - return this; - } - - public AqlQueryBuilder asc(String... by) { - if (isNotEmpty(by)) { - this.sort = AqlItem.asc(by); - } - return this; - } - - public AqlQueryBuilder desc(String... by) { - if (isNotEmpty(by)) { - this.sort = AqlItem.desc(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) + getOffsetAsString(mapper) + getLimitAsString(mapper); - } catch (JsonProcessingException e) { - throw new AqlBuilderException("Error serializing object to json: ", e); - } - } - - private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException { - return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : ""; - } - - 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 String getLimitAsString(ObjectMapper mapper) throws JsonProcessingException { + return hasLimit() ? ".limit(" + mapper.writeValueAsString(limit) + ")" : ""; + } - private boolean hasInclude() { - return include != null && include.isNotEmpty(); - } + 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 hasSort() { + return sort != null && sort.isNotEmpty(); + } - private boolean hasLimit() { - return limit != null; - } + private boolean hasLimit() { + return limit != null; + } - private boolean hasOffset() { - return offset != null; - } + private boolean hasOffset() { + return offset != null; + } - private boolean hasRoot() { - return root != null && root.isNotEmpty(); - } + private boolean hasRoot() { + return root != null && root.isNotEmpty(); + } - private AqlItem[] setToArray(Collection items) { - return items.toArray(new AqlItem[0]); - } + private AqlItem[] setToArray(Collection items) { + return items.toArray(new AqlItem[0]); + } } 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 dddf6b94..8f3588fc 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 @@ -147,8 +147,8 @@ public void orTest() { @Test public void limitTest() { String result = new AqlQueryBuilder() - .limit(123) - .build(); + .limit(123) + .build(); assertThat(result, notNullValue()); assertThat(result, is("items.find().limit(123)")); @@ -157,8 +157,8 @@ public void limitTest() { @Test public void offsetTest() { String result = new AqlQueryBuilder() - .offset(123) - .build(); + .offset(123) + .build(); assertThat(result, notNullValue()); assertThat(result, is("items.find().offset(123)"));