]> source.dussan.org Git - archiva.git/blob
b01a3d8b0d7bcd8acff5f67711b21c14e282798f
[archiva.git] /
1 package org.apache.maven.archiva.web.action;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25
26 import org.apache.archiva.indexer.search.RepositorySearch;
27 import org.apache.archiva.indexer.search.SearchFields;
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.database.ArchivaDAO;
31 import org.apache.maven.archiva.database.ArtifactDAO;
32 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
33 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
34 import org.apache.maven.archiva.indexer.search.SearchResultHit;
35 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
36 import org.apache.maven.archiva.indexer.search.SearchResults;
37 import org.apache.maven.archiva.model.ArchivaArtifact;
38 import org.apache.maven.archiva.security.ArchivaXworkUser;
39 import org.apache.maven.archiva.security.UserRepositories;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 import org.easymock.MockControl;
42 import org.easymock.classextension.MockClassControl;
43
44 import com.opensymphony.xwork2.Action;
45
46 /**
47  * 
48  */
49 public class SearchActionTest
50     extends PlexusInSpringTestCase 
51 {
52     private SearchAction action;
53     
54     private MockControl archivaConfigControl;
55     
56     private ArchivaConfiguration archivaConfig;
57     
58     private MockControl daoControl;
59     
60     private ArchivaDAO dao;
61     
62     private MockControl userReposControl;
63     
64     private UserRepositories userRepos;
65     
66     private MockControl archivaXworkUserControl;
67     
68     private ArchivaXworkUser archivaXworkUser;
69     
70     private MockControl searchControl;
71     
72     private RepositorySearch search;
73     
74     private MockControl artifactDaoControl;
75     
76     private ArtifactDAO artifactDao;
77     
78     @Override
79     protected void setUp() 
80         throws Exception
81     {
82         super.setUp();
83         
84         action = new SearchAction();
85         
86         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );        
87         archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
88         
89         daoControl = MockControl.createControl( ArchivaDAO.class );
90         daoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );        
91         dao = ( ArchivaDAO ) daoControl.getMock();
92         
93         userReposControl = MockControl.createControl( UserRepositories.class );        
94         userRepos = ( UserRepositories ) userReposControl.getMock();
95         
96         archivaXworkUserControl = MockClassControl.createControl( ArchivaXworkUser.class );
97         archivaXworkUserControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
98         
99         archivaXworkUser = ( ArchivaXworkUser ) archivaXworkUserControl.getMock();
100         
101         searchControl = MockControl.createControl( RepositorySearch.class );
102         searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
103         search = ( RepositorySearch ) searchControl.getMock();
104         
105         artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
106         artifactDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
107         artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
108                 
109         action.setArchivaConfiguration( archivaConfig );
110         action.setArchivaXworkUser( archivaXworkUser );
111         action.setUserRepositories( userRepos );
112         action.setDao( dao );
113         action.setNexusSearch( search );
114     }
115     
116     @Override
117     protected void tearDown()
118         throws Exception
119     {
120         super.tearDown();
121     }
122     
123     // quick search...
124     
125     public void testQuickSearch()
126         throws Exception
127     {           
128         action.setQ( "archiva" );
129         action.setCurrentPage( 0 );
130         action.setSearchResultsOnly( false );
131         action.setCompleteQueryString( "" );
132         
133         List<String> selectedRepos = new ArrayList<String>();
134         selectedRepos.add( "internal" );
135         selectedRepos.add( "snapshots" );
136         
137         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
138         limits.setPageSize( 30 );
139         
140         SearchResultHit hit = new SearchResultHit();
141         hit.setGroupId( "org.apache.archiva" );
142         hit.setArtifactId( "archiva-configuration" );
143         hit.setUrl( "url" );
144         hit.addVersion( "1.0" );
145         hit.addVersion( "1.1" );        
146         
147         SearchResults results = new SearchResults();
148         results.setLimits( limits );
149         results.setTotalHits( 1 );
150         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
151         
152         List<String> versions = new ArrayList<String>();
153         versions.add( "1.0" );
154         versions.add( "1.1" );
155         
156         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 3 );
157                 
158         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
159         
160         searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
161                 
162         daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
163                 
164         archivaXworkUserControl.replay();
165         userReposControl.replay();
166         searchControl.replay();
167         daoControl.replay();
168         
169         String result = action.quickSearch();
170         
171         assertEquals( Action.SUCCESS, result );      
172         assertEquals( 1, action.getTotalPages() );
173         assertEquals( 1, action.getResults().getTotalHits() );
174         
175         archivaXworkUserControl.verify();
176         userReposControl.verify();
177         searchControl.verify();
178         daoControl.verify();
179     }
180     
181     public void testSearchWithinSearchResults()
182         throws Exception
183     {
184         action.setQ( "archiva" );
185         action.setCurrentPage( 0 );
186         action.setSearchResultsOnly( true );
187         action.setCompleteQueryString( "org;apache" );
188         
189         List<String> parsed = new ArrayList<String>();
190         parsed.add( "org" );
191         parsed.add( "apache" );
192         
193         List<String> selectedRepos = new ArrayList<String>();
194         selectedRepos.add( "internal" );
195         selectedRepos.add( "snapshots" );
196         
197         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
198         limits.setPageSize( 30 );
199         
200         SearchResultHit hit = new SearchResultHit();
201         hit.setGroupId( "org.apache.archiva" );
202         hit.setArtifactId( "archiva-configuration" );
203         hit.setUrl( "url" );
204         hit.addVersion( "1.0" );
205         hit.addVersion( "1.1" );        
206         
207         SearchResults results = new SearchResults();
208         results.setLimits( limits );
209         results.setTotalHits( 1 );
210         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
211         
212         List<String> versions = new ArrayList<String>();
213         versions.add( "1.0" );
214         versions.add( "1.1" );
215         
216         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 3 );
217                 
218         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
219         
220         searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
221                 
222         daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
223                 
224         archivaXworkUserControl.replay();
225         userReposControl.replay();
226         searchControl.replay();
227         daoControl.replay();
228         
229         String result = action.quickSearch();
230         
231         assertEquals( Action.SUCCESS, result );
232         assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
233         assertEquals( 1, action.getTotalPages() );
234         assertEquals( 1, action.getResults().getTotalHits() );
235         
236         archivaXworkUserControl.verify();
237         userReposControl.verify();
238         searchControl.verify();
239         daoControl.verify();
240     }        
241     
242     public void testQuickSearchUserHasNoAccessToAnyRepository()
243         throws Exception
244     {
245         action.setQ( "archiva" );
246         action.setCurrentPage( 0 );
247         
248         List<String> selectedRepos = new ArrayList<String>();
249         
250         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
251         
252         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
253         
254         archivaXworkUserControl.replay();
255         userReposControl.replay();
256         
257         String result = action.quickSearch();
258         
259         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );        
260         
261         archivaXworkUserControl.verify();
262         userReposControl.verify();        
263     }
264     
265     public void testQuickSearchNoSearchHits()
266         throws Exception
267     {
268         action.setQ( "archiva" );
269         action.setCurrentPage( 0 );
270         action.setSearchResultsOnly( false );
271         action.setCompleteQueryString( "" );
272         
273         List<String> selectedRepos = new ArrayList<String>();
274         selectedRepos.add( "internal" );
275         selectedRepos.add( "snapshots" );
276         
277         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
278         limits.setPageSize( 30 );
279                 
280         SearchResults results = new SearchResults();
281         
282         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 2 );
283                 
284         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
285         
286         searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
287         
288         archivaXworkUserControl.replay();
289         userReposControl.replay();
290         searchControl.replay();
291         
292         String result = action.quickSearch();
293         
294         assertEquals( Action.INPUT, result );        
295         
296         archivaXworkUserControl.verify();
297         userReposControl.verify();
298         searchControl.verify();
299     }
300         
301     // advanced/filtered search...
302     
303     public void testAdvancedSearchOneRepository()
304         throws Exception
305     {
306         List<String> managedRepos = new ArrayList<String>();
307         managedRepos.add( "internal" );
308         managedRepos.add( "snapshots" );
309         
310         action.setRepositoryId( "internal" );
311         action.setManagedRepositoryList( managedRepos );
312         action.setCurrentPage( 0 );
313         action.setRowCount( 30 );
314         action.setGroupId( "org" );
315         
316         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
317         limits.setPageSize( 30 );
318         
319         SearchResultHit hit = new SearchResultHit();
320         hit.setGroupId( "org.apache.archiva" );
321         hit.setArtifactId( "archiva-configuration" );
322         hit.setUrl( "url" );
323         hit.addVersion( "1.0" );
324         hit.addVersion( "1.1" );        
325         
326         SearchResults results = new SearchResults();
327         results.setLimits( limits );
328         results.setTotalHits( 1 );
329         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
330         
331         List<String> selectedRepos = new ArrayList<String>();
332         selectedRepos.add( "internal" );
333         selectedRepos.add( "snapshots" );
334         
335         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
336         
337         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
338         
339         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
340         
341         archivaXworkUserControl.replay();
342         searchControl.replay();
343         
344         String result = action.filteredSearch();
345         
346         assertEquals( Action.SUCCESS, result );
347         assertEquals( 1, action.getTotalPages() );
348         assertEquals( 1, action.getResults().getTotalHits() );
349         
350         archivaXworkUserControl.verify();
351         searchControl.verify();
352     }
353     
354     public void testAdvancedSearchAllRepositories()
355         throws Exception
356     {   
357         List<String> managedRepos = new ArrayList<String>();
358         managedRepos.add( "internal" );
359         managedRepos.add( "snapshots" );
360         
361         action.setRepositoryId( "all" );
362         action.setManagedRepositoryList( managedRepos );
363         action.setCurrentPage( 0 );
364         action.setRowCount( 30 );
365         action.setGroupId( "org" );
366         
367         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
368         limits.setPageSize( 30 );
369         
370         SearchResultHit hit = new SearchResultHit();
371         hit.setGroupId( "org.apache.archiva" );
372         hit.setArtifactId( "archiva-configuration" );
373         hit.setUrl( "url" );
374         hit.addVersion( "1.0" );
375         hit.addVersion( "1.1" );        
376         
377         SearchResults results = new SearchResults();
378         results.setLimits( limits );
379         results.setTotalHits( 1 );
380         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
381         
382         List<String> selectedRepos = new ArrayList<String>();
383         selectedRepos.add( "internal" );
384         
385         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
386         
387         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 2 );
388         
389         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
390         
391         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
392         
393         archivaXworkUserControl.replay();
394         searchControl.replay();
395         userReposControl.replay();
396         
397         String result = action.filteredSearch();
398         
399         assertEquals( Action.SUCCESS, result );
400         assertEquals( 1, action.getTotalPages() );
401         assertEquals( 1, action.getResults().getTotalHits() );
402         
403         archivaXworkUserControl.verify();
404         searchControl.verify();
405         userReposControl.verify();
406     }
407         
408     public void testAdvancedSearchNoSearchHits()
409         throws Exception
410     {
411         List<String> managedRepos = new ArrayList<String>();
412         managedRepos.add( "internal" );
413         managedRepos.add( "snapshots" );
414         
415         action.setRepositoryId( "internal" );
416         action.setManagedRepositoryList( managedRepos );
417         action.setCurrentPage( 0 );
418         action.setRowCount( 30 );
419         action.setGroupId( "org" );
420         
421         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
422         limits.setPageSize( 30 );
423                 
424         SearchResults results = new SearchResults();
425         
426         List<String> selectedRepos = new ArrayList<String>();
427         selectedRepos.add( "internal" );
428         selectedRepos.add( "snapshots" );
429         
430         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
431         
432         archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
433         
434         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
435         
436         archivaXworkUserControl.replay();
437         searchControl.replay();
438         
439         String result = action.filteredSearch();
440         
441         assertEquals( Action.INPUT, result );
442         assertFalse( action.getActionErrors().isEmpty() );
443         assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
444         
445         archivaXworkUserControl.verify();
446         searchControl.verify();
447     }
448     
449     public void testAdvancedSearchUserHasNoAccessToAnyRepository()
450         throws Exception
451     {
452         List<String> managedRepos = new ArrayList<String>();
453         
454         action.setGroupId( "org.apache.archiva" );
455         action.setManagedRepositoryList( managedRepos );
456         
457         String result = action.filteredSearch();
458         
459         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
460     }
461     
462     public void testAdvancedSearchNoSpecifiedCriteria()
463         throws Exception
464     {
465         List<String> managedRepos = new ArrayList<String>();
466         
467         action.setManagedRepositoryList( managedRepos );
468         
469         String result = action.filteredSearch();
470         
471         assertEquals( Action.INPUT, result );
472         assertFalse( action.getActionErrors().isEmpty() );
473         assertEquals( "Advanced Search - At least one search criteria must be provided.",
474                       (String) action.getActionErrors().iterator().next() );
475     }
476     
477     // find artifact..
478     
479     public void testFindArtifactWithOneHit()
480         throws Exception
481     {
482         action.setQ( "afbcdeaadbcffceabbba1" );
483         
484         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
485         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
486                                            null, "jar", "test-repo" ) );
487         
488         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
489         
490         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
491         
492         daoControl.replay();
493         artifactDaoControl.replay();
494         
495         String result = action.findArtifact();
496         assertEquals( "artifact", result );
497         assertFalse( action.getDatabaseResults().isEmpty() );
498         assertEquals( 1, action.getDatabaseResults().size() );
499         
500         daoControl.verify();
501         artifactDaoControl.verify();
502     }
503         
504     public void testFindArtifactWithMultipleHits()
505         throws Exception
506     {
507         action.setQ( "afbcdeaadbcffceabbba1" );
508         
509         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
510         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
511                                            null, "jar", "test-repo" ) );
512         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-indexer", "1.0",
513                                             null, "jar", "test-repo" ) );
514         
515         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
516         
517         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
518         
519         daoControl.replay();
520         artifactDaoControl.replay();
521         
522         String result = action.findArtifact();
523         assertEquals( "results", result );
524         assertFalse( action.getDatabaseResults().isEmpty() );
525         assertEquals( 2, action.getDatabaseResults().size() );
526         
527         daoControl.verify();
528         artifactDaoControl.verify();
529     }
530     
531     public void testFindArtifactNoChecksumSpecified()
532         throws Exception
533     {
534         String result = action.findArtifact();
535         
536         assertEquals( Action.INPUT, result );
537         assertFalse( action.getActionErrors().isEmpty() );
538         assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
539     }
540     
541     public void testFindArtifactNoResults()
542         throws Exception
543     {
544         action.setQ( "afbcdeaadbcffceabbba1" );
545         
546         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
547         
548         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
549         
550         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
551         
552         daoControl.replay();
553         artifactDaoControl.replay();
554         
555         String result = action.findArtifact();
556         assertEquals( Action.INPUT, result );
557         assertFalse( action.getActionErrors().isEmpty() );
558         assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() );
559         
560         daoControl.verify();
561         artifactDaoControl.verify();
562     }
563 }