Skip to content

Commit 6bc4008

Browse files
committed
refactor: preparing search for multiple backends
Also added IT tests for search
1 parent d337473 commit 6bc4008

File tree

6 files changed

+158
-60
lines changed

6 files changed

+158
-60
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.codejive.jpm.search;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.codejive.jpm.search.Search;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.EnumSource;
12+
import org.junit.jupiter.params.provider.FieldSource;
13+
14+
public class SearchIT {
15+
@ParameterizedTest
16+
@EnumSource(Search.Backends.class)
17+
void testSearchSingleTerm(Search.Backends backend) throws IOException {
18+
Search s = Search.getBackend(backend);
19+
Search.SearchResult res = s.findArtifacts("httpclient", 10);
20+
assertThat(res.count).isGreaterThan(0);
21+
assertThat(res.artifacts).isNotEmpty();
22+
}
23+
24+
@ParameterizedTest
25+
@EnumSource(Search.Backends.class)
26+
void testSearchDoubleTerm(Search.Backends backend) throws IOException {
27+
Search s = Search.getBackend(backend);
28+
Search.SearchResult res = s.findArtifacts("apache:httpclient", 10);
29+
assertThat(res.count).isGreaterThan(0);
30+
assertThat(res.artifacts).isNotEmpty();
31+
}
32+
33+
@ParameterizedTest
34+
@EnumSource(Search.Backends.class)
35+
void testSearchTripleTerm(Search.Backends backend) throws IOException {
36+
Search s = Search.getBackend(backend);
37+
Search.SearchResult res = s.findArtifacts("org.apache.httpcomponents:httpclient:", 10);
38+
assertThat(res.count).isGreaterThan(0);
39+
assertThat(res.artifacts).isNotEmpty();
40+
assertThat(res.artifacts).allMatch(a -> "org.apache.httpcomponents".equals(a.getGroupId()));
41+
assertThat(res.artifacts).allMatch(a -> "httpclient".equals(a.getArtifactId()));
42+
}
43+
}

src/main/java/org/codejive/jpm/Jpm.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.*;
66
import java.util.stream.Collectors;
77
import org.codejive.jpm.config.AppInfo;
8+
import org.codejive.jpm.search.Search;
89
import org.codejive.jpm.util.*;
910
import org.eclipse.aether.artifact.Artifact;
1011
import org.eclipse.aether.resolution.DependencyResolutionException;
@@ -122,10 +123,11 @@ public SyncResult copy(String[] artifactNames, Map<String, String> repos, boolea
122123
public String[] search(String artifactPattern, int count) throws IOException {
123124
List<Artifact> artifacts = new ArrayList<>();
124125
int max = count <= 0 || count > 200 ? 200 : count;
125-
SearchResult result = SearchUtils.findArtifacts(artifactPattern, max);
126+
Search s = Search.getBackend(null);
127+
Search.SearchResult result = s.findArtifacts(artifactPattern, max);
126128
while (result != null) {
127129
artifacts.addAll(result.artifacts);
128-
result = count <= 0 ? SearchUtils.findNextArtifacts(result) : null;
130+
result = count <= 0 ? s.findNextArtifacts(result) : null;
129131
}
130132
return artifacts.stream().map(Jpm::artifactGav).toArray(String[]::new);
131133
}

src/main/java/org/codejive/jpm/Main.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//DEPS org.yaml:snakeyaml:2.4
55
//DEPS org.jline:jline-console-ui:3.30.5 org.jline:jline-terminal-jni:3.30.5
66
//DEPS org.slf4j:slf4j-api:2.0.17 org.slf4j:slf4j-simple:2.0.17
7-
//SOURCES Jpm.java config/AppInfo.java util/CommandsParser.java util/FileUtils.java util/Resolver.java
8-
//SOURCES util/ScriptUtils.java util/SearchResult.java util/SearchUtils.java util/SyncResult.java util/Version.java
7+
//SOURCES Jpm.java config/AppInfo.java search/Search.java search/SearchSmoRestImpl.java util/CommandsParser.java
8+
//SOURCES util/FileUtils.java util/Resolver.java util/ScriptUtils.java util/SyncResult.java util/Version.java
99
// spotless:on
1010

1111
package org.codejive.jpm;
@@ -45,16 +45,16 @@
4545
versionProvider = Version.class,
4646
description = "Simple command line tool for managing Maven artifacts",
4747
subcommands = {
48-
Main.Copy.class,
4948
Main.Search.class,
5049
Main.Install.class,
50+
Main.Copy.class,
5151
Main.PrintPath.class,
52-
Main.Exec.class,
5352
Main.Do.class,
5453
Main.Clean.class,
5554
Main.Build.class,
5655
Main.Run.class,
57-
Main.Test.class
56+
Main.Test.class,
57+
Main.Exec.class
5858
})
5959
public class Main {
6060

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.codejive.jpm.search;
2+
3+
import java.io.IOException;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import org.eclipse.aether.artifact.Artifact;
7+
8+
public interface Search {
9+
10+
/**
11+
* Find artifacts matching the given pattern. This will return the first page of results. If the
12+
* pattern to search for is a simple name (there are no colons in the string), the search will
13+
* match any part of an artifact's group or name. If there's a single colon, the search will
14+
* match any part of the group id and artifact id separately. If there are two colons, the
15+
* search will match the group id and artifact id exactly, and will return the artifact's
16+
* versions.
17+
*
18+
* @param artifactPattern The pattern to search for.
19+
* @param count The maximum number of results to return.
20+
* @return The search result as an instance of {@link SearchResult}.
21+
* @throws IOException If an error occurred during the search.
22+
*/
23+
SearchResult findArtifacts(String artifactPattern, int count) throws IOException;
24+
25+
/**
26+
* Find the next page of artifacts. This takes a {@link SearchResult} returned by a previous
27+
* call to {@link #findArtifacts(String, int)} and returns the next page of results.
28+
*
29+
* @param prevResult The previous search result.
30+
* @return The next search result as an instance of {@link SearchResult}.
31+
* @throws IOException If an error occurred during the search.
32+
*/
33+
SearchResult findNextArtifacts(SearchResult prevResult) throws IOException;
34+
35+
enum Backends {
36+
SMO_REST("smo-rest");
37+
38+
public final String label;
39+
40+
Backends(String label) {
41+
this.label = label;
42+
}
43+
}
44+
45+
static Search getBackend(Backends backend) {
46+
if (backend == Backends.SMO_REST) {
47+
return new SearchSmoRestImpl();
48+
}
49+
return new SearchSmoRestImpl();
50+
}
51+
52+
/**
53+
* Hold the result of a search while also functioning as a kind of bookmark for paging purposes.
54+
*/
55+
class SearchResult {
56+
/** The artifacts that matched the search query. */
57+
public final List<? extends Artifact> artifacts;
58+
59+
/** The search query that produced this result. */
60+
public final String query;
61+
62+
/** The index of the first artifact in this result relative to the total result set. */
63+
public final int start;
64+
65+
/** The maximum number of results to return */
66+
public final int count;
67+
68+
/** The total number of artifacts that matched the search query. */
69+
public final int total;
70+
71+
/**
72+
* Create a new search result.
73+
*
74+
* @param artifacts The artifacts that matched the search query.
75+
* @param query The search query that produced this result.
76+
* @param start The index of the first artifact in this result relative to the total result
77+
* set.
78+
* @param count The maximum number of results to return.
79+
* @param total The total number of artifacts that matched the search query.
80+
*/
81+
public SearchResult(
82+
List<? extends Artifact> artifacts, String query, int start, int count, int total) {
83+
this.artifacts = Collections.unmodifiableList(artifacts);
84+
this.query = query;
85+
this.start = start;
86+
this.count = count;
87+
this.total = total;
88+
}
89+
}
90+
}

src/main/java/org/codejive/jpm/util/SearchUtils.java renamed to src/main/java/org/codejive/jpm/search/SearchSmoRestImpl.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.codejive.jpm.util;
1+
package org.codejive.jpm.search;
22

33
import java.io.IOException;
44
import java.io.InputStream;
@@ -10,6 +10,7 @@
1010
import org.apache.http.client.methods.HttpGet;
1111
import org.apache.http.impl.client.CloseableHttpClient;
1212
import org.apache.http.impl.client.HttpClients;
13+
import org.codejive.jpm.util.Version;
1314
import org.eclipse.aether.artifact.DefaultArtifact;
1415
import org.yaml.snakeyaml.DumperOptions;
1516
import org.yaml.snakeyaml.LoaderOptions;
@@ -18,7 +19,7 @@
1819
import org.yaml.snakeyaml.representer.Representer;
1920

2021
/** Utility class for searching Maven artifacts. */
21-
public class SearchUtils {
22+
public class SearchSmoRestImpl implements Search {
2223

2324
/**
2425
* Find artifacts matching the given pattern. This will return the first page of results. If the
@@ -30,31 +31,33 @@ public class SearchUtils {
3031
*
3132
* @param artifactPattern The pattern to search for.
3233
* @param count The maximum number of results to return.
33-
* @return The search result as an instance of {@link SearchResult}.
34+
* @return The search result as an instance of {@link Search.SearchResult}.
3435
* @throws IOException If an error occurred during the search.
3536
*/
36-
public static SearchResult findArtifacts(String artifactPattern, int count) throws IOException {
37+
public Search.SearchResult findArtifacts(String artifactPattern, int count) throws IOException {
3738
return select(artifactPattern, 0, count);
3839
}
3940

4041
/**
41-
* Find the next page of artifacts. This takes a {@link SearchResult} returned by a previous
42-
* call to {@link #findArtifacts(String, int)} and returns the next page of results.
42+
* Find the next page of artifacts. This takes a {@link Search.SearchResult} returned by a
43+
* previous call to {@link #findArtifacts(String, int)} and returns the next page of results.
4344
*
4445
* @param prevResult The previous search result.
45-
* @return The next search result as an instance of {@link SearchResult}.
46+
* @return The next search result as an instance of {@link Search.SearchResult}.
4647
* @throws IOException If an error occurred during the search.
4748
*/
48-
public static SearchResult findNextArtifacts(SearchResult prevResult) throws IOException {
49+
public Search.SearchResult findNextArtifacts(Search.SearchResult prevResult)
50+
throws IOException {
4951
if (prevResult.start + prevResult.count >= prevResult.total) {
5052
return null;
5153
}
52-
SearchResult result =
54+
Search.SearchResult result =
5355
select(prevResult.query, prevResult.start + prevResult.count, prevResult.count);
5456
return result.artifacts.isEmpty() ? null : result;
5557
}
5658

57-
private static SearchResult select(String query, int start, int count) throws IOException {
59+
private static Search.SearchResult select(String query, int start, int count)
60+
throws IOException {
5861
String[] parts = query.split(":", -1);
5962
String finalQuery;
6063
if (parts.length >= 3) {
@@ -106,9 +109,10 @@ private static SearchResult select(String query, int start, int count) throws IO
106109
List<DefaultArtifact> artifacts =
107110
result.response.docs.stream()
108111
.filter(d -> acceptDoc(d, parts))
109-
.map(SearchUtils::toArtifact)
112+
.map(SearchSmoRestImpl::toArtifact)
110113
.collect(Collectors.toList());
111-
return new SearchResult(artifacts, query, start, count, result.response.numFound);
114+
return new Search.SearchResult(
115+
artifacts, query, start, count, result.response.numFound);
112116
}
113117
}
114118
}

src/main/java/org/codejive/jpm/util/SearchResult.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)