From ee286ad7443c5bd1db14212675a8269428339000 Mon Sep 17 00:00:00 2001 From: sailingcat Date: Tue, 7 Nov 2023 16:18:52 +0100 Subject: [PATCH] Support $match and $nmatch in AqlQueryBuilder (#370) --- .../jfrog/artifactory/client/aql/AqlItem.java | 10 +++++ .../client/aql/AqlQueryBuilder.java | 14 +++++++ .../client/aql/AqlQueryBuilderTest.java | 37 +++++++++++++++++-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlItem.java b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlItem.java index ca6522e2..135283b4 100644 --- a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlItem.java +++ b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlItem.java @@ -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 item; @@ -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(); 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..667eea8e 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 @@ -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 items) { return or(setToArray(items)); } 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..d590bc52 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 @@ -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; @@ -143,6 +144,33 @@ 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() @@ -150,7 +178,8 @@ public void addNestedFilters() { or( aqlItem("repo", "myrepo1"), aqlItem("repo", "myrepo2"), - aqlItem("repo", "myrepo3") + aqlItem("repo", "myrepo3"), + match("repo", "myotherrepo*") ), or( aqlItem("name", "maven-metadata1.xml"), @@ -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\"}"