Skip to content

Commit ee286ad

Browse files
authored
Support $match and $nmatch in AqlQueryBuilder (#370)
1 parent 777f4c9 commit ee286ad

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

services/src/main/java/org/jfrog/artifactory/client/aql/AqlItem.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class AqlItem {
1111
private static final String DESC = "$desc";
1212
private static final String OR = "$or";
1313
private static final String AND = "$and";
14+
private static final String MATCH = "$match";
15+
private static final String NOT_MATCH = "$nmatch";
1416

1517
private Map<String, Object> item;
1618

@@ -39,6 +41,14 @@ public static AqlItem desc(String... items) {
3941
return new AqlItem(DESC, items);
4042
}
4143

44+
public static AqlItem match(String key, String pattern) {
45+
return new AqlItem(key, new AqlItem(MATCH, pattern));
46+
}
47+
48+
public static AqlItem notMatch(String key, String pattern) {
49+
return new AqlItem(key, new AqlItem(NOT_MATCH, pattern));
50+
}
51+
4252
@JsonIgnore
4353
public boolean isNotEmpty() {
4454
return item != null && !item.isEmpty();

services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public AqlQueryBuilder or(AqlItem... items) {
5858
return this;
5959
}
6060

61+
public AqlQueryBuilder match(String key, String pattern) {
62+
if (key != null) {
63+
root.putAll(AqlItem.match(key, pattern).value());
64+
}
65+
return this;
66+
}
67+
68+
public AqlQueryBuilder notMatch(String key, String pattern) {
69+
if (key != null) {
70+
root.putAll(AqlItem.notMatch(key, pattern).value());
71+
}
72+
return this;
73+
}
74+
6175
public AqlQueryBuilder or(Collection<AqlItem> items) {
6276
return or(setToArray(items));
6377
}

services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.hamcrest.core.IsNull.notNullValue;
1010
import static org.jfrog.artifactory.client.aql.AqlItem.aqlItem;
1111
import static org.jfrog.artifactory.client.aql.AqlItem.or;
12+
import static org.jfrog.artifactory.client.aql.AqlItem.match;
1213
import static org.testng.Assert.assertEquals;
1314
import static org.testng.Assert.assertTrue;
1415

@@ -143,14 +144,42 @@ public void orTest() {
143144
+ "]})"));
144145
}
145146

147+
148+
@Test
149+
public void matchTest() {
150+
String result = new AqlQueryBuilder()
151+
.match("repo", "myrepo*")
152+
.build();
153+
154+
assertThat(result, notNullValue());
155+
assertThat(result, is("items.find("
156+
+ "{\"repo\":"
157+
+ "{\"$match\":\"myrepo*\"}"
158+
+ "})"));
159+
}
160+
161+
@Test
162+
public void notMatchTest() {
163+
String result = new AqlQueryBuilder()
164+
.notMatch("repo", "myrepo*")
165+
.build();
166+
167+
assertThat(result, notNullValue());
168+
assertThat(result, is("items.find("
169+
+ "{\"repo\":"
170+
+ "{\"$nmatch\":\"myrepo*\"}"
171+
+ "})"));
172+
}
173+
146174
@Test
147175
public void addNestedFilters() {
148176
final String result = new AqlQueryBuilder()
149177
.and(
150178
or(
151179
aqlItem("repo", "myrepo1"),
152180
aqlItem("repo", "myrepo2"),
153-
aqlItem("repo", "myrepo3")
181+
aqlItem("repo", "myrepo3"),
182+
match("repo", "myotherrepo*")
154183
),
155184
or(
156185
aqlItem("name", "maven-metadata1.xml"),
@@ -165,8 +194,10 @@ public void addNestedFilters() {
165194
+ "{\"$or\":["
166195
+ "{\"repo\":\"myrepo1\"},"
167196
+ "{\"repo\":\"myrepo2\"},"
168-
+ "{\"repo\":\"myrepo3\"}"
169-
+ "]},"
197+
+ "{\"repo\":\"myrepo3\"},"
198+
+ "{\"repo\":"
199+
+ "{\"$match\":\"myotherrepo*\"}"
200+
+ "}]},"
170201
+ "{\"$or\":["
171202
+ "{\"name\":\"maven-metadata1.xml\"},"
172203
+ "{\"name\":\"maven-metadata2.xml\"}"

0 commit comments

Comments
 (0)