]> source.dussan.org Git - archiva.git/blob
81183f43243ea04d286f830bf87e6866daf7766f
[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.UiConfiguration;
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.SearchService;
25 import org.fest.assertions.Assertions;
26 import org.junit.Test;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31
32 /**
33  * @author Olivier Lamy
34  */
35 public class SearchServiceTest
36     extends AbstractArchivaRestTest
37 {
38
39
40     @Test
41     public void quickSearchOnArtifactId()
42         throws Exception
43     {
44
45         String testRepoId = "test-repo";
46         // force guest user creation if not exists
47         if ( getUserService( authorizationHeader ).getGuestUser() == null )
48         {
49             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
50         }
51
52         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
53
54         SearchService searchService = getSearchService( authorizationHeader );
55
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
61
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 );
66
67         deleteTestRepo( testRepoId );
68     }
69
70     /**
71      * same search but with Guest user
72      *
73      * @throws Exception
74      */
75     @Test
76     public void quickSearchOnArtifactIdGuest()
77         throws Exception
78     {
79
80         String testRepoId = "test-repo";
81         // force guest user creation if not exists
82         if ( getUserService( authorizationHeader ).getGuestUser() == null )
83         {
84             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
85         }
86
87         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
88
89         SearchService searchService = getSearchService( null );
90
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
96
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 );
101
102         deleteTestRepo( testRepoId );
103     }
104
105     @Test
106     public void searchArtifactVersions()
107         throws Exception
108     {
109
110         String testRepoId = "test-repo";
111         // force guest user creation if not exists
112         if ( getUserService( authorizationHeader ).getGuestUser() == null )
113         {
114             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
115         }
116
117         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
118
119         // START SNIPPET: searchservice-artifact-versions
120         SearchService searchService = getSearchService( authorizationHeader );
121
122         List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );
123
124         // END SNIPPET: searchservice-artifact-versions
125
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 );
130
131         for ( Artifact artifact : artifacts )
132         {
133             log.info( "url:" + artifact.getUrl() );
134             String version = artifact.getVersion();
135             assertTrue( artifact.getUrl().contains( version ) );
136
137
138         }
139
140         deleteTestRepo( testRepoId );
141     }
142
143     @Test
144     public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier()
145         throws Exception
146     {
147
148         String testRepoId = "test-repo";
149         // force guest user creation if not exists
150         if ( getUserService( authorizationHeader ).getGuestUser() == null )
151         {
152             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
153         }
154
155         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
156
157         SearchService searchService = getSearchService( authorizationHeader );
158
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" );
164
165         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
166         // END SNIPPET: searchservice-with-classifier
167
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 );
172
173         deleteTestRepo( testRepoId );
174     }
175
176     @Test
177     public void searchWithSearchRequestBundleSymbolicNameOneVersion()
178         throws Exception
179     {
180
181         String testRepoId = "test-repo";
182         // force guest user creation if not exists
183         if ( getUserService( authorizationHeader ).getGuestUser() == null )
184         {
185             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
186         }
187
188         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
189
190         SearchService searchService = getSearchService( authorizationHeader );
191
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
196
197         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
198
199         assertNotNull( artifacts );
200         assertTrue(
201             " not 1 results for Bundle Symbolic Name org.apache.karaf.features.command but " + artifacts.size() + ":"
202                 + artifacts, artifacts.size() == 1 );
203
204         deleteTestRepo( testRepoId );
205     }
206
207     @Test
208     public void searchWithSearchRequestBundleSymbolicNameTwoVersion()
209         throws Exception
210     {
211
212         String testRepoId = "test-repo";
213         // force guest user creation if not exists
214         if ( getUserService( authorizationHeader ).getGuestUser() == null )
215         {
216             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
217         }
218         UiConfiguration uiConfiguration = new UiConfiguration();
219         uiConfiguration.setApplicationUrl( null );
220         getArchivaAdministrationService().setUiConfiguration( uiConfiguration );
221         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
222
223         SearchService searchService = getSearchService( authorizationHeader );
224
225         SearchRequest searchRequest = new SearchRequest();
226         searchRequest.setBundleSymbolicName( "org.apache.karaf.features.core" );
227
228         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
229
230         Assertions.assertThat( artifacts ).isNotNull().hasSize( 2 );
231
232         for ( Artifact artifact : artifacts )
233         {
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() );
239
240
241         }
242
243         deleteTestRepo( testRepoId );
244     }
245
246     @Test
247     public void searchWithSearchRequestExportPackageOneVersion()
248         throws Exception
249     {
250
251         String testRepoId = "test-repo";
252         // force guest user creation if not exists
253         if ( getUserService( authorizationHeader ).getGuestUser() == null )
254         {
255             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
256         }
257
258         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
259
260         SearchService searchService = getSearchService( authorizationHeader );
261
262         SearchRequest searchRequest = new SearchRequest();
263         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
264         searchRequest.setRepositories( Arrays.asList( testRepoId ) );
265
266         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
267
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 );
271
272         log.info( "artifact url " + artifacts.get( 0 ).getUrl() );
273         deleteTestRepo( testRepoId );
274     }
275
276     @Test
277     /**
278      * ensure we don't return response for an unknown repo
279      */
280     public void searchWithSearchUnknwownRepoId()
281         throws Exception
282     {
283
284         String testRepoId = "test-repo";
285         // force guest user creation if not exists
286         if ( getUserService( authorizationHeader ).getGuestUser() == null )
287         {
288             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
289         }
290
291         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
292
293         SearchService searchService = getSearchService( authorizationHeader );
294
295         SearchRequest searchRequest = new SearchRequest();
296         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
297         searchRequest.setRepositories( Arrays.asList( "tototititata" ) );
298
299         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
300
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 );
304
305         deleteTestRepo( testRepoId );
306     }
307
308     @Test
309     /**
310      * ensure we revert to all observable repos in case of no repo in the request
311      */
312     public void searchWithSearchNoRepos()
313         throws Exception
314     {
315
316         String testRepoId = "test-repo";
317         // force guest user creation if not exists
318         if ( getUserService( authorizationHeader ).getGuestUser() == null )
319         {
320             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
321         }
322
323         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
324
325         SearchService searchService = getSearchService( authorizationHeader );
326
327         SearchRequest searchRequest = new SearchRequest();
328         searchRequest.setBundleExportPackage( "org.apache.karaf.features.command.completers" );
329
330         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
331
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 );
335
336         log.info( "artifact url " + artifacts.get( 0 ).getUrl() );
337         deleteTestRepo( testRepoId );
338     }
339
340     @Test
341     public void getAllGroupIds()
342         throws Exception
343     {
344
345         String testRepoId = "test-repo";
346         // force guest user creation if not exists
347         if ( getUserService( authorizationHeader ).getGuestUser() == null )
348         {
349             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
350         }
351
352         createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
353
354         SearchService searchService = getSearchService( authorizationHeader );
355
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 );
362     }
363
364     @Test
365     /**
366      * test we don't return 2 artifacts pom + zip one
367      */
368     public void getSearchArtifactsWithOnlyClassifier()
369         throws Exception
370     {
371
372         String testRepoId = "test-repo";
373         // force guest user creation if not exists
374         if ( getUserService( authorizationHeader ).getGuestUser() == null )
375         {
376             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
377         }
378
379         createAndIndexRepo( testRepoId, "src/test/repo-with-classifier-only" );
380
381         SearchService searchService = getSearchService( authorizationHeader );
382
383         SearchRequest searchRequest =
384             new SearchRequest( "org.foo", "studio-all-update-site", null, null, null, Arrays.asList( "test-repo" ) );
385
386         List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );
387         log.info( "artifacts:" + artifacts );
388         assertEquals( 1, artifacts.size() );
389         deleteTestRepo( testRepoId );
390     }
391
392 }
393
394