]> source.dussan.org Git - archiva.git/blob
55774dc512b637c5fee91f82c15d3d7834f5af31
[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         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 );
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         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 );
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         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 );
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         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 );
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         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 );
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         createAndIndexRepo( testRepoId );
209
210         SearchService searchService = getSearchService( authorizationHeader );
211
212         SearchRequest searchRequest = new SearchRequest();
213         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
214         searchRequest.setRepositories( Arrays.asList( testRepoId ) );
215
216         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
217
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 );
221
222         log.info( "artifact url " + artifacts.get( 0 ).getUrl() );
223         deleteTestRepo( testRepoId );
224     }
225
226     @Test
227     public void getAllGroupIds()
228         throws Exception
229     {
230
231         String testRepoId = "test-repo";
232         // force guest user creation if not exists
233         if ( getUserService( authorizationHeader ).getGuestUser() == null )
234         {
235             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
236         }
237
238         createAndIndexRepo( testRepoId );
239
240         SearchService searchService = getSearchService( authorizationHeader );
241
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 );
248     }
249
250     private void createAndIndexRepo( String testRepoId )
251         throws Exception
252     {
253         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
254         {
255             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
256         }
257         //File targetRepo = new File( "target/test-origin-repo" );
258         //if ( targetRepo.exists() )
259         //{
260         //    FileUtils.deleteDirectory( targetRepo );
261         //}
262         //assertFalse( targetRepo.exists() );
263         //FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), targetRepo );
264
265         ManagedRepository managedRepository = new ManagedRepository();
266         managedRepository.setId( testRepoId );
267         managedRepository.setName( "test repo" );
268
269         managedRepository.setLocation( new File( "src/test/repo-with-osgi" ).getPath() );
270         managedRepository.setIndexDirectory( "target/.index-" + Long.toString( new Date().getTime() ) );
271
272         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
273         service.addManagedRepository( managedRepository );
274
275         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
276             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
277
278         getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
279
280         //return targetRepo;
281     }
282
283     private void deleteTestRepo( String id )
284         throws Exception
285     {
286         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
287         {
288             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
289         }
290
291     }
292
293
294 }
295
296