1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.beans.UiConfiguration;
22 import org.apache.archiva.maven2.model.Artifact;
23 import org.apache.archiva.rest.api.model.SearchRequest;
24 import org.apache.archiva.rest.api.services.SearchService;
25 import org.fest.assertions.api.Assertions;
26 import org.junit.Test;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
33 * @author Olivier Lamy
35 public class SearchServiceTest
36 extends AbstractArchivaRestTest
41 public void quickSearchOnArtifactId()
45 String testRepoId = "test-repo";
46 // force guest user creation if not exists
47 if ( getUserService( authorizationHeader ).getGuestUser() == null )
49 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
52 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
54 SearchService searchService = getSearchService( authorizationHeader );
56 // START SNIPPET: quick-search
57 List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
58 // return all artifacts with groupId OR artifactId OR version OR packaging OR className
59 // NOTE : only artifacts with classifier empty are returned
60 // END SNIPPET: quick-search
62 assertNotNull( artifacts );
63 assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
64 artifacts.size() == 6 );
65 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
67 deleteTestRepo( testRepoId );
71 * same search but with Guest user
76 public void quickSearchOnArtifactIdGuest()
80 String testRepoId = "test-repo";
81 // force guest user creation if not exists
82 if ( getUserService( authorizationHeader ).getGuestUser() == null )
84 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
87 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
89 SearchService searchService = getSearchService( null );
91 // START SNIPPET: quick-search
92 List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
93 // return all artifacts with groupId OR artifactId OR version OR packaging OR className
94 // NOTE : only artifacts with classifier empty are returned
95 // END SNIPPET: quick-search
97 assertNotNull( artifacts );
98 assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
99 artifacts.size() == 6 );
100 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
102 deleteTestRepo( testRepoId );
106 public void searchArtifactVersions()
110 String testRepoId = "test-repo";
111 // force guest user creation if not exists
112 if ( getUserService( authorizationHeader ).getGuestUser() == null )
114 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
117 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
119 // START SNIPPET: searchservice-artifact-versions
120 SearchService searchService = getSearchService( authorizationHeader );
122 List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
124 // END SNIPPET: searchservice-artifact-versions
126 assertNotNull( artifacts );
127 assertTrue( " not 13 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
128 artifacts.size() == 13 );
129 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
131 for ( Artifact artifact : artifacts )
133 log.info( "url: {}", artifact.getUrl() );
134 String version = artifact.getVersion();
135 assertTrue( artifact.getUrl().contains( version ) );
140 deleteTestRepo( testRepoId );
144 public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
148 String testRepoId = "test-repo";
149 // force guest user creation if not exists
150 if ( getUserService( authorizationHeader ).getGuestUser() == null )
152 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
155 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
157 SearchService searchService = getSearchService( authorizationHeader );
159 // START SNIPPET: searchservice-with-classifier
160 SearchRequest searchRequest = new SearchRequest();
161 searchRequest.setGroupId( "commons-logging" );
162 searchRequest.setArtifactId( "commons-logging" );
163 searchRequest.setClassifier( "sources" );
165 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
166 // END SNIPPET: searchservice-with-classifier
168 assertNotNull( artifacts );
169 assertTrue( " not 2 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
170 artifacts.size() == 2 );
171 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
173 deleteTestRepo( testRepoId );
177 public void searchWithSearchRequestBundleSymbolicNameOneVersion()
181 String testRepoId = "test-repo";
182 // force guest user creation if not exists
183 if ( getUserService( authorizationHeader ).getGuestUser() == null )
185 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
188 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
190 SearchService searchService = getSearchService( authorizationHeader );
192 // START SNIPPET: searchservice-with-osgi
193 SearchRequest searchRequest = new SearchRequest();
194 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.command" );
195 // END SNIPPET: searchservice-with-osgi
197 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
199 assertNotNull( artifacts );
201 " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
202 + artifacts, artifacts.size() == 1 );
204 deleteTestRepo( testRepoId );
208 public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
212 String testRepoId = "test-repo";
213 // force guest user creation if not exists
214 if ( getUserService( authorizationHeader ).getGuestUser() == null )
216 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
218 UiConfiguration uiConfiguration = new UiConfiguration();
219 uiConfiguration.setApplicationUrl( null );
220 getArchivaAdministrationService().setUiConfiguration( uiConfiguration );
221 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
223 SearchService searchService = getSearchService( authorizationHeader );
225 SearchRequest searchRequest = new SearchRequest();
226 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
228 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
230 Assertions.assertThat( artifacts ).isNotNull().hasSize( 2 );
232 for ( Artifact artifact : artifacts )
234 log.info( "url: {}", artifact.getUrl() );
235 String version = artifact.getVersion();
236 assertEquals( "http://localhost:" + port
237 + "/repository/test-repo/org/apache/karaf/features/org.apache.karaf.features.core/"
238 + version + "/org.apache.karaf.features.core-" + version + ".jar", artifact.getUrl() );
243 deleteTestRepo( testRepoId );
247 public void searchWithSearchRequestExportPackageOneVersion()
251 String testRepoId = "test-repo";
252 // force guest user creation if not exists
253 if ( getUserService( authorizationHeader ).getGuestUser() == null )
255 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
258 createAndIndexRepo( testRepoId, getBasedir() + "/src/test/repo-with-osgi" );
260 SearchService searchService = getSearchService( authorizationHeader );
262 SearchRequest searchRequest = new SearchRequest();
263 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
264 searchRequest.setRepositories( Arrays.asList( testRepoId ) );
266 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
268 assertNotNull( artifacts );
269 assertTrue( " not 1 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
270 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
272 log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
273 deleteTestRepo( testRepoId );
278 * ensure we don't return response for an unknown repo
280 public void searchWithSearchUnknwownRepoId()
284 String testRepoId = "test-repo";
285 // force guest user creation if not exists
286 if ( getUserService( authorizationHeader ).getGuestUser() == null )
288 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
291 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
293 SearchService searchService = getSearchService( authorizationHeader );
295 SearchRequest searchRequest = new SearchRequest();
296 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
297 searchRequest.setRepositories( Arrays.asList( "tototititata" ) );
299 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
301 assertNotNull( artifacts );
302 assertTrue( " not 0 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
303 + artifacts.size() + ":" + artifacts, artifacts.size() == 0 );
305 deleteTestRepo( testRepoId );
310 * ensure we revert to all observable repos in case of no repo in the request
312 public void searchWithSearchNoRepos()
316 String testRepoId = "test-repo";
317 // force guest user creation if not exists
318 if ( getUserService( authorizationHeader ).getGuestUser() == null )
320 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
323 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
325 SearchService searchService = getSearchService( authorizationHeader );
327 SearchRequest searchRequest = new SearchRequest();
328 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
330 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
332 assertNotNull( artifacts );
333 assertTrue( " not 0 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
334 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
336 log.info( "artifact url {}", artifacts.get( 0 ).getUrl() );
337 deleteTestRepo( testRepoId );
341 public void getAllGroupIds()
345 String testRepoId = "test-repo";
346 // force guest user creation if not exists
347 if ( getUserService( authorizationHeader ).getGuestUser() == null )
349 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
352 createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
354 SearchService searchService = getSearchService( authorizationHeader );
356 Collection<String> groupIds = searchService.getAllGroupIds( Arrays.asList( testRepoId ) ).getGroupIds();
357 log.info( "groupIds {}", groupIds );
358 assertFalse( groupIds.isEmpty() );
359 assertTrue( groupIds.contains( "commons-cli" ) );
360 assertTrue( groupIds.contains( "org.apache.felix" ) );
361 deleteTestRepo( testRepoId );
366 * test we don't return 2 artifacts pom + zip one
368 public void getSearchArtifactsWithOnlyClassifier()
372 String testRepoId = "test-repo";
373 // force guest user creation if not exists
374 if ( getUserService( authorizationHeader ).getGuestUser() == null )
376 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
379 createAndIndexRepo( testRepoId, "src/test/repo-with-classifier-only" );
381 SearchService searchService = getSearchService( authorizationHeader );
383 SearchRequest searchRequest =
384 new SearchRequest( "org.foo", "studio-all-update-site", null, null, null, Arrays.asList( "test-repo" ) );
386 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
387 log.info( "artifacts: {}", artifacts );
388 assertEquals( 1, artifacts.size() );
389 deleteTestRepo( testRepoId );