Skip to content

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Nov 8, 2023
2 parents a81140d + de29d0b commit a217465
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class AqlItem {
private static final String DESC = "$desc";
private static final String OR = "$or";
private static final String AND = "$and";
private static final String MATCH = "$match";
private static final String NOT_MATCH = "$nmatch";

private Map<String, Object> item;

Expand Down Expand Up @@ -39,6 +41,14 @@ public static AqlItem desc(String... items) {
return new AqlItem(DESC, items);
}

public static AqlItem match(String key, String pattern) {
return new AqlItem(key, new AqlItem(MATCH, pattern));
}

public static AqlItem notMatch(String key, String pattern) {
return new AqlItem(key, new AqlItem(NOT_MATCH, pattern));
}

@JsonIgnore
public boolean isNotEmpty() {
return item != null && !item.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
Expand Down Expand Up @@ -58,6 +60,20 @@ public AqlQueryBuilder or(AqlItem... items) {
return this;
}

public AqlQueryBuilder match(String key, String pattern) {
if (key != null) {
root.putAll(AqlItem.match(key, pattern).value());
}
return this;
}

public AqlQueryBuilder notMatch(String key, String pattern) {
if (key != null) {
root.putAll(AqlItem.notMatch(key, pattern).value());
}
return this;
}

public AqlQueryBuilder or(Collection<AqlItem> items) {
return or(setToArray(items));
}
Expand All @@ -83,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);
}
Expand All @@ -100,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.hamcrest.core.IsNull.notNullValue;
import static org.jfrog.artifactory.client.aql.AqlItem.aqlItem;
import static org.jfrog.artifactory.client.aql.AqlItem.or;
import static org.jfrog.artifactory.client.aql.AqlItem.match;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -143,14 +144,62 @@ public void orTest() {
+ "]})"));
}

@Test
public void matchTest() {
String result = new AqlQueryBuilder()
.match("repo", "myrepo*")
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find("
+ "{\"repo\":"
+ "{\"$match\":\"myrepo*\"}"
+ "})"));
}

@Test
public void notMatchTest() {
String result = new AqlQueryBuilder()
.notMatch("repo", "myrepo*")
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find("
+ "{\"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
public void addNestedFilters() {
final String result = new AqlQueryBuilder()
.and(
or(
aqlItem("repo", "myrepo1"),
aqlItem("repo", "myrepo2"),
aqlItem("repo", "myrepo3")
aqlItem("repo", "myrepo3"),
match("repo", "myotherrepo*")
),
or(
aqlItem("name", "maven-metadata1.xml"),
Expand All @@ -165,8 +214,10 @@ public void addNestedFilters() {
+ "{\"$or\":["
+ "{\"repo\":\"myrepo1\"},"
+ "{\"repo\":\"myrepo2\"},"
+ "{\"repo\":\"myrepo3\"}"
+ "]},"
+ "{\"repo\":\"myrepo3\"},"
+ "{\"repo\":"
+ "{\"$match\":\"myotherrepo*\"}"
+ "}]},"
+ "{\"$or\":["
+ "{\"name\":\"maven-metadata1.xml\"},"
+ "{\"name\":\"maven-metadata2.xml\"}"
Expand All @@ -190,6 +241,8 @@ public void variousElements() {
.item(aqlItem("property", "value"))
.include("name", "repo")
.asc("name", "repo")
.offset(1)
.limit(2)
.build();

assertThat(result, notNullValue());
Expand All @@ -206,7 +259,9 @@ public void variousElements() {
+ "\"property\":\"value\""
+ "})"
+ ".include(\"name\",\"repo\")"
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"));
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"
+ ".offset(1)"
+ ".limit(2)"));
}

@Test
Expand Down

0 comments on commit a217465

Please sign in to comment.