Skip to content

Commit

Permalink
Support $match and $nmatch in AqlQueryBuilder (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
sailingcat authored Nov 7, 2023
1 parent 777f4c9 commit ee286ad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 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 @@ -58,6 +58,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 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,42 @@ 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 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 +194,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 Down

0 comments on commit ee286ad

Please sign in to comment.