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 File targetRepo = 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, targetRepo );
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 File targetRepo = 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, targetRepo );
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 File targetRepo = 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, targetRepo );
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 File targetRepo = 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, targetRepo );
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 File targetRepo = 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, targetRepo );
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 File targetRepo = createAndIndexRepo( testRepoId );
210 SearchService searchService = getSearchService( authorizationHeader );
212 SearchRequest searchRequest = new SearchRequest();
213 searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
215 List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
217 assertNotNull( artifacts );
218 assertTrue( " not 1 results for Bundle ExportPackage org.apache.karaf.features.command.completers but "
219 + artifacts.size() + ":" + artifacts, artifacts.size() == 1 );
221 log.info( "artifcat url " + artifacts.get( 0 ).getUrl() );
222 deleteTestRepo( testRepoId, targetRepo );
226 public void getAllGroupIds()
230 String testRepoId = "test-repo";
231 // force guest user creation if not exists
232 if ( getUserService( authorizationHeader ).getGuestUser() == null )
234 assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
237 File targetRepo = createAndIndexRepo( testRepoId );
239 SearchService searchService = getSearchService( authorizationHeader );
241 Collection<String> groupIds = searchService.getAllGroupIds( Arrays.asList( testRepoId ) ).getGroupIds();
242 log.info( "groupIds " + groupIds );
243 assertFalse( groupIds.isEmpty() );
244 assertTrue( groupIds.contains( "commons-cli") );
245 assertTrue( groupIds.contains( "org.apache.felix" ) );
246 deleteTestRepo( testRepoId, targetRepo );
249 private File createAndIndexRepo( String testRepoId )
252 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
254 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, true );
256 File targetRepo = new File( "target/test-origin-repo" );
257 if ( targetRepo.exists() )
259 FileUtils.deleteDirectory( targetRepo );
261 assertFalse( targetRepo.exists() );
262 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), targetRepo );
264 ManagedRepository managedRepository = new ManagedRepository();
265 managedRepository.setId( testRepoId );
266 managedRepository.setName( "test repo" );
268 managedRepository.setLocation( targetRepo.getPath() );
269 managedRepository.setIndexDirectory( "target/.index-" + Long.toString( new Date().getTime() ) );
271 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
272 service.addManagedRepository( managedRepository );
274 getRoleManagementService( authorizationHeader ).assignTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
276 getRepositoriesService(authorizationHeader).scanRepositoryNow( testRepoId, true );
281 private void deleteTestRepo( String id, File targetRepo )
284 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
286 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );