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.ManagedRepository;
22 import org.apache.archiva.rest.api.model.Artifact;
23 import org.apache.archiva.rest.api.model.SearchRequest;
24 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
25 import org.apache.archiva.rest.api.services.SearchService;
26 import org.apache.archiva.security.common.ArchivaRoleConstants;
27 import org.apache.commons.io.FileUtils;
28 import org.junit.Test;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.List;
37 * @author Olivier Lamy
39 public class SearchServiceTest
40 extends AbstractArchivaRestTest
45 public void quickSearchOnArtifactId()
49 String testRepoId = "test-repo";
50 // force guest user creation if not exists
51 if ( getUserService( authorizationHeader ).getGuestUser() == null )
53 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
56 createAndIndexRepo( testRepoId );
58 SearchService searchService = getSearchService( authorizationHeader );
60 // START SNIPPET: quick-search
61 List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
62 // return all artifacts with groupId OR artifactId OR version OR packaging OR className
63 // NOTE : only artifacts with classifier empty are returned
64 // END SNIPPET: quick-search
66 assertNotNull( artifacts );
67 assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
68 artifacts.size() == 6 );
69 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
71 deleteTestRepo( testRepoId );
75 public void searchArtifactVersions()
79 String testRepoId = "test-repo";
80 // force guest user creation if not exists
81 if ( getUserService( authorizationHeader ).getGuestUser() == null )
83 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
86 createAndIndexRepo( testRepoId );
88 // START SNIPPET: searchservice-artifact-versions
89 SearchService searchService = getSearchService( authorizationHeader );
91 List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
93 // END SNIPPET: searchservice-artifact-versions
95 assertNotNull( artifacts );
96 assertTrue( " not 3 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
97 artifacts.size() == 13 );
98 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
100 deleteTestRepo( testRepoId );
104 public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
108 String testRepoId = "test-repo";
109 // force guest user creation if not exists
110 if ( getUserService( authorizationHeader ).getGuestUser() == null )
112 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
115 createAndIndexRepo( testRepoId );
117 SearchService searchService = getSearchService( authorizationHeader );
119 // START SNIPPET: searchservice-with-classifier
120 SearchRequest searchRequest = new SearchRequest();
121 searchRequest.setGroupId( "commons-logging" );
122 searchRequest.setArtifactId( "commons-logging" );
123 searchRequest.setClassifier( "sources" );
125 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
126 // END SNIPPET: searchservice-with-classifier
128 assertNotNull( artifacts );
129 assertTrue( " not 2 results for commons-logging search but " + artifacts.size() + ":" + artifacts,
130 artifacts.size() == 2 );
131 log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts );
133 deleteTestRepo( testRepoId );
137 public void searchWithSearchRequestBundleSymbolicNameOneVersion()
141 String testRepoId = "test-repo";
142 // force guest user creation if not exists
143 if ( getUserService( authorizationHeader ).getGuestUser() == null )
145 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
148 createAndIndexRepo( testRepoId );
150 SearchService searchService = getSearchService( authorizationHeader );
152 // START SNIPPET: searchservice-with-osgi
153 SearchRequest searchRequest = new SearchRequest();
154 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.command" );
155 // END SNIPPET: searchservice-with-osgi
157 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
159 assertNotNull( artifacts );
161 " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
162 + artifacts, artifacts.size() == 1 );
164 deleteTestRepo( testRepoId );
168 public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
172 String testRepoId = "test-repo";
173 // force guest user creation if not exists
174 if ( getUserService( authorizationHeader ).getGuestUser() == null )
176 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
179 createAndIndexRepo( testRepoId );
181 SearchService searchService = getSearchService( authorizationHeader );
183 SearchRequest searchRequest = new SearchRequest();
184 searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
186 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
188 assertNotNull( artifacts );
190 " not 2 results for Bundle Symbolic Name org.apache.karaf.features.core but " + artifacts.size() + ":"
191 + artifacts, artifacts.size() == 2 );
193 deleteTestRepo( testRepoId );
197 public void searchWithSearchRequestExportPackageOneVersion()
201 String testRepoId = "test-repo";
202 // force guest user creation if not exists
203 if ( getUserService( authorizationHeader ).getGuestUser() == null )
205 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
208 createAndIndexRepo( testRepoId );
210 SearchService searchService = getSearchService( authorizationHeader );
212 SearchRequest searchRequest = new SearchRequest();
213 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
214 searchRequest.setRepositories( Arrays.asList( testRepoId ) );
216 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
218 assertNotNull( artifacts );
219 assertTrue( " not 1 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
220 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
222 log.info( "artifact url " + artifacts.get( 0 ).getUrl() );
223 deleteTestRepo( testRepoId );
227 public void getAllGroupIds()
231 String testRepoId = "test-repo";
232 // force guest user creation if not exists
233 if ( getUserService( authorizationHeader ).getGuestUser() == null )
235 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
238 createAndIndexRepo( testRepoId );
240 SearchService searchService = getSearchService( authorizationHeader );
242 Collection<String> groupIds = searchService.getAllGroupIds( Arrays.asList( testRepoId ) ).getGroupIds();
243 log.info( "groupIds " + groupIds );
244 assertFalse( groupIds.isEmpty() );
245 assertTrue( groupIds.contains( "commons-cli" ) );
246 assertTrue( groupIds.contains( "org.apache.felix" ) );
247 deleteTestRepo( testRepoId );
250 private void createAndIndexRepo( String testRepoId )
253 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
255 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
257 //File targetRepo = new File( "target/test-origin-repo" );
258 //if ( targetRepo.exists() )
260 // FileUtils.deleteDirectory( targetRepo );
262 //assertFalse( targetRepo.exists() );
263 //FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), targetRepo );
265 ManagedRepository managedRepository = new ManagedRepository();
266 managedRepository.setId( testRepoId );
267 managedRepository.setName( "test repo" );
269 managedRepository.setLocation( new File( "src/test/repo-with-osgi" ).getPath() );
270 managedRepository.setIndexDirectory( "target/.index-" + Long.toString( new Date().getTime() ) );
272 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
273 service.addManagedRepository( managedRepository );
275 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
276 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
278 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
283 private void deleteTestRepo( String id )
286 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
288 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );