Skip to content

Commit 867b8aa

Browse files
committed
refactor: preparing search for multiple backends
Also added IT tests for search
1 parent 3bc1b63 commit 867b8aa

File tree

6 files changed

+158
-58
lines changed

6 files changed

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

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)