Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $match and $nmatch in AqlQueryBuilder #370

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading