]> source.dussan.org Git - archiva.git/blob
c1e0fa464b68b82cbad90608de7cbe5423092c18
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
29
30 import java.io.File;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.List;
35
36 /**
37  * @author Olivier Lamy
38  */
39 public class SearchServiceTest
40     extends AbstractArchivaRestTest
41 {
42
43
44     @Test
45     public void quickSearchOnArtifactId()
46         throws Exception
47     {
48
49         String testRepoId = "test-repo";
50         // force guest user creation if not exists
51         if ( getUserService( authorizationHeader ).getGuestUser() == null )
52         {
53             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
54         }
55
56         File targetRepo = createAndIndexRepo( testRepoId );
57
58         SearchService searchService = getSearchService( authorizationHeader );
59
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
65
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 );
70
71         deleteTestRepo( testRepoId, targetRepo );
72     }
73
74     @Test
75     public void searchArtifactVersions()
76         throws Exception
77     {
78
79         String testRepoId = "test-repo";
80         // force guest user creation if not exists
81         if ( getUserService( authorizationHeader ).getGuestUser() == null )
82         {
83             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
84         }
85
86         File targetRepo = createAndIndexRepo( testRepoId );
87
88         // START SNIPPET: searchservice-artifact-versions
89         SearchService searchService = getSearchService( authorizationHeader );
90
91         List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
92
93         // END SNIPPET: searchservice-artifact-versions
94
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 );
99
100         deleteTestRepo( testRepoId, targetRepo );
101     }
102
103     @Test
104     public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
105         throws Exception
106     {
107
108         String testRepoId = "test-repo";
109         // force guest user creation if not exists
110         if ( getUserService( authorizationHeader ).getGuestUser() == null )
111         {
112             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
113         }
114
115         File targetRepo = createAndIndexRepo( testRepoId );
116
117         SearchService searchService = getSearchService( authorizationHeader );
118
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" );
124
125         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
126         // END SNIPPET: searchservice-with-classifier
127
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 );
132
133         deleteTestRepo( testRepoId, targetRepo );
134     }
135
136     @Test
137     public void searchWithSearchRequestBundleSymbolicNameOneVersion()
138         throws Exception
139     {
140
141         String testRepoId = "test-repo";
142         // force guest user creation if not exists
143         if ( getUserService( authorizationHeader ).getGuestUser() == null )
144         {
145             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
146         }
147
148         File targetRepo = createAndIndexRepo( testRepoId );
149
150         SearchService searchService = getSearchService( authorizationHeader );
151
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
156
157         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
158
159         assertNotNull( artifacts );
160         assertTrue(
161             " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
162                 + artifacts, artifacts.size() == 1 );
163
164         deleteTestRepo( testRepoId, targetRepo );
165     }
166
167     @Test
168     public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
169         throws Exception
170     {
171
172         String testRepoId = "test-repo";
173         // force guest user creation if not exists
174         if ( getUserService( authorizationHeader ).getGuestUser() == null )
175         {
176             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
177         }
178
179         File targetRepo = createAndIndexRepo( testRepoId );
180
181         SearchService searchService = getSearchService( authorizationHeader );
182
183         SearchRequest searchRequest = new SearchRequest();
184         searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
185
186         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
187
188         assertNotNull( artifacts );
189         assertTrue(
190             " not 2 results for Bundle Symbolic Name org.apache.karaf.features.core but " + artifacts.size() + ":"
191                 + artifacts, artifacts.size() == 2 );
192
193         deleteTestRepo( testRepoId, targetRepo );
194     }
195
196     @Test
197     public void searchWithSearchRequestExportPackageOneVersion()
198         throws Exception
199     {
200
201         String testRepoId = "test-repo";
202         // force guest user creation if not exists
203         if ( getUserService( authorizationHeader ).getGuestUser() == null )
204         {
205             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
206         }
207
208         File targetRepo = createAndIndexRepo( testRepoId );
209
210         SearchService searchService = getSearchService( authorizationHeader );
211
212         SearchRequest searchRequest = new SearchRequest();
213         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
214
215         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
216
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 );
220
221         log.info( "artifcat url " + artifacts.get( 0 ).getUrl() );
222         deleteTestRepo( testRepoId, targetRepo );
223     }
224
225     @Test
226     public void getAllGroupIds()
227         throws Exception
228     {
229
230         String testRepoId = "test-repo";
231         // force guest user creation if not exists
232         if ( getUserService( authorizationHeader ).getGuestUser() == null )
233         {
234             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
235         }
236
237         File targetRepo = createAndIndexRepo( testRepoId );
238
239         SearchService searchService = getSearchService( authorizationHeader );
240
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 );
247     }
248
249     private File createAndIndexRepo( String testRepoId )
250         throws Exception
251     {
252         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
253         {
254             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, true );
255         }
256         File targetRepo = new File( "target/test-origin-repo" );
257         if ( targetRepo.exists() )
258         {
259             FileUtils.deleteDirectory( targetRepo );
260         }
261         assertFalse( targetRepo.exists() );
262         FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), targetRepo );
263
264         ManagedRepository managedRepository = new ManagedRepository();
265         managedRepository.setId( testRepoId );
266         managedRepository.setName( "test repo" );
267
268         managedRepository.setLocation( targetRepo.getPath() );
269         managedRepository.setIndexDirectory( "target/.index-" + Long.toString( new Date().getTime() ) );
270
271         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
272         service.addManagedRepository( managedRepository );
273
274         getRoleManagementService( authorizationHeader ).assignTemplatedRole(ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
275
276         getRepositoriesService(authorizationHeader).scanRepositoryNow( testRepoId, true );
277
278         return targetRepo;
279     }
280
281     private void deleteTestRepo( String id, File targetRepo )
282         throws Exception
283     {
284         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
285         {
286             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
287         }
288
289     }
290
291
292 }
293
294