]> source.dussan.org Git - archiva.git/blob
68296ed97103347b82298f7e358d7d2655966169
[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.Collections;
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.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResultLimits;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.indexer.util.SearchUtil;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.database.ArchivaDAO;
34 import org.apache.maven.archiva.database.ArtifactDAO;
35 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
36 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
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( getActivePrincipal(), "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     private String getActivePrincipal()
182     {
183         return archivaXworkUser.getActivePrincipal( Collections.<String,Object>emptyMap() );
184     }
185     
186     public void testSearchWithinSearchResults()
187         throws Exception
188     {
189         action.setQ( "archiva" );
190         action.setCurrentPage( 0 );
191         action.setSearchResultsOnly( true );
192         action.setCompleteQueryString( "org;apache" );
193         
194         List<String> parsed = new ArrayList<String>();
195         parsed.add( "org" );
196         parsed.add( "apache" );
197         
198         List<String> selectedRepos = new ArrayList<String>();
199         selectedRepos.add( "internal" );
200         selectedRepos.add( "snapshots" );
201         
202         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
203         limits.setPageSize( 30 );
204         
205         SearchResultHit hit = new SearchResultHit();
206         hit.setGroupId( "org.apache.archiva" );
207         hit.setArtifactId( "archiva-configuration" );
208         hit.setUrl( "url" );
209         hit.addVersion( "1.0" );
210         hit.addVersion( "1.1" );        
211         
212         SearchResults results = new SearchResults();
213         results.setLimits( limits );
214         results.setTotalHits( 1 );
215         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
216         
217         List<String> versions = new ArrayList<String>();
218         versions.add( "1.0" );
219         versions.add( "1.1" );
220         
221         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 3 );
222                 
223         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
224         
225         searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
226                 
227         daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
228                 
229         archivaXworkUserControl.replay();
230         userReposControl.replay();
231         searchControl.replay();
232         daoControl.replay();
233         
234         String result = action.quickSearch();
235         
236         assertEquals( Action.SUCCESS, result );
237         assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
238         assertEquals( 1, action.getTotalPages() );
239         assertEquals( 1, action.getResults().getTotalHits() );
240         
241         archivaXworkUserControl.verify();
242         userReposControl.verify();
243         searchControl.verify();
244         daoControl.verify();
245     }        
246     
247     public void testQuickSearchUserHasNoAccessToAnyRepository()
248         throws Exception
249     {
250         action.setQ( "archiva" );
251         action.setCurrentPage( 0 );
252         
253         List<String> selectedRepos = new ArrayList<String>();
254         
255         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
256         
257         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
258         
259         archivaXworkUserControl.replay();
260         userReposControl.replay();
261         
262         String result = action.quickSearch();
263         
264         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );        
265         
266         archivaXworkUserControl.verify();
267         userReposControl.verify();        
268     }
269     
270     public void testQuickSearchNoSearchHits()
271         throws Exception
272     {
273         action.setQ( "archiva" );
274         action.setCurrentPage( 0 );
275         action.setSearchResultsOnly( false );
276         action.setCompleteQueryString( "" );
277         
278         List<String> selectedRepos = new ArrayList<String>();
279         selectedRepos.add( "internal" );
280         selectedRepos.add( "snapshots" );
281         
282         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
283         limits.setPageSize( 30 );
284                 
285         SearchResults results = new SearchResults();
286         
287         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 2 );
288                 
289         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
290         
291         searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
292         
293         archivaXworkUserControl.replay();
294         userReposControl.replay();
295         searchControl.replay();
296         
297         String result = action.quickSearch();
298         
299         assertEquals( Action.INPUT, result );        
300         
301         archivaXworkUserControl.verify();
302         userReposControl.verify();
303         searchControl.verify();
304     }
305         
306     // advanced/filtered search...
307     
308     public void testAdvancedSearchOneRepository()
309         throws Exception
310     {
311         List<String> managedRepos = new ArrayList<String>();
312         managedRepos.add( "internal" );
313         managedRepos.add( "snapshots" );
314         
315         action.setRepositoryId( "internal" );
316         action.setManagedRepositoryList( managedRepos );
317         action.setCurrentPage( 0 );
318         action.setRowCount( 30 );
319         action.setGroupId( "org" );
320         
321         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
322         limits.setPageSize( 30 );
323         
324         SearchResultHit hit = new SearchResultHit();
325         hit.setGroupId( "org.apache.archiva" );
326         hit.setArtifactId( "archiva-configuration" );
327         hit.setUrl( "url" );
328         hit.addVersion( "1.0" );
329         hit.addVersion( "1.1" );        
330         
331         SearchResults results = new SearchResults();
332         results.setLimits( limits );
333         results.setTotalHits( 1 );
334         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
335         
336         List<String> selectedRepos = new ArrayList<String>();
337         selectedRepos.add( "internal" );
338         selectedRepos.add( "snapshots" );
339         
340         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
341         
342         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
343         
344         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
345         
346         archivaXworkUserControl.replay();
347         searchControl.replay();
348         
349         String result = action.filteredSearch();
350         
351         assertEquals( Action.SUCCESS, result );
352         assertEquals( 1, action.getTotalPages() );
353         assertEquals( 1, action.getResults().getTotalHits() );
354         
355         archivaXworkUserControl.verify();
356         searchControl.verify();
357     }
358     
359     public void testAdvancedSearchAllRepositories()
360         throws Exception
361     {   
362         List<String> managedRepos = new ArrayList<String>();
363         managedRepos.add( "internal" );
364         managedRepos.add( "snapshots" );
365         
366         action.setRepositoryId( "all" );
367         action.setManagedRepositoryList( managedRepos );
368         action.setCurrentPage( 0 );
369         action.setRowCount( 30 );
370         action.setGroupId( "org" );
371         
372         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
373         limits.setPageSize( 30 );
374         
375         SearchResultHit hit = new SearchResultHit();
376         hit.setGroupId( "org.apache.archiva" );
377         hit.setArtifactId( "archiva-configuration" );
378         hit.setUrl( "url" );
379         hit.addVersion( "1.0" );
380         hit.addVersion( "1.1" );        
381         
382         SearchResults results = new SearchResults();
383         results.setLimits( limits );
384         results.setTotalHits( 1 );
385         results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
386         
387         List<String> selectedRepos = new ArrayList<String>();
388         selectedRepos.add( "internal" );
389         
390         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
391         
392         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 2 );
393         
394         userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
395         
396         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
397         
398         archivaXworkUserControl.replay();
399         searchControl.replay();
400         userReposControl.replay();
401         
402         String result = action.filteredSearch();
403         
404         assertEquals( Action.SUCCESS, result );
405         assertEquals( 1, action.getTotalPages() );
406         assertEquals( 1, action.getResults().getTotalHits() );
407         
408         archivaXworkUserControl.verify();
409         searchControl.verify();
410         userReposControl.verify();
411     }
412         
413     public void testAdvancedSearchNoSearchHits()
414         throws Exception
415     {
416         List<String> managedRepos = new ArrayList<String>();
417         managedRepos.add( "internal" );
418         managedRepos.add( "snapshots" );
419         
420         action.setRepositoryId( "internal" );
421         action.setManagedRepositoryList( managedRepos );
422         action.setCurrentPage( 0 );
423         action.setRowCount( 30 );
424         action.setGroupId( "org" );
425         
426         SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
427         limits.setPageSize( 30 );
428                 
429         SearchResults results = new SearchResults();
430         
431         List<String> selectedRepos = new ArrayList<String>();
432         selectedRepos.add( "internal" );
433         selectedRepos.add( "snapshots" );
434         
435         SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
436         
437         archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
438         
439         searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
440         
441         archivaXworkUserControl.replay();
442         searchControl.replay();
443         
444         String result = action.filteredSearch();
445         
446         assertEquals( Action.INPUT, result );
447         assertFalse( action.getActionErrors().isEmpty() );
448         assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
449         
450         archivaXworkUserControl.verify();
451         searchControl.verify();
452     }
453     
454     public void testAdvancedSearchUserHasNoAccessToAnyRepository()
455         throws Exception
456     {
457         List<String> managedRepos = new ArrayList<String>();
458         
459         action.setGroupId( "org.apache.archiva" );
460         action.setManagedRepositoryList( managedRepos );
461         
462         String result = action.filteredSearch();
463         
464         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
465     }
466     
467     public void testAdvancedSearchNoSpecifiedCriteria()
468         throws Exception
469     {
470         List<String> managedRepos = new ArrayList<String>();
471         
472         action.setManagedRepositoryList( managedRepos );
473         
474         String result = action.filteredSearch();
475         
476         assertEquals( Action.INPUT, result );
477         assertFalse( action.getActionErrors().isEmpty() );
478         assertEquals( "Advanced Search - At least one search criteria must be provided.",
479                       (String) action.getActionErrors().iterator().next() );
480     }
481     
482     // find artifact..
483     
484     public void testFindArtifactWithOneHit()
485         throws Exception
486     {
487         action.setQ( "afbcdeaadbcffceabbba1" );
488         
489         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
490         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
491                                            null, "jar", "test-repo" ) );
492         
493         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
494         
495         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
496         
497         daoControl.replay();
498         artifactDaoControl.replay();
499         
500         String result = action.findArtifact();
501         assertEquals( "artifact", result );
502         assertFalse( action.getDatabaseResults().isEmpty() );
503         assertEquals( 1, action.getDatabaseResults().size() );
504         
505         daoControl.verify();
506         artifactDaoControl.verify();
507     }
508         
509     public void testFindArtifactWithMultipleHits()
510         throws Exception
511     {
512         action.setQ( "afbcdeaadbcffceabbba1" );
513         
514         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
515         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
516                                            null, "jar", "test-repo" ) );
517         dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-indexer", "1.0",
518                                             null, "jar", "test-repo" ) );
519         
520         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
521         
522         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
523         
524         daoControl.replay();
525         artifactDaoControl.replay();
526         
527         String result = action.findArtifact();
528         assertEquals( "results", result );
529         assertFalse( action.getDatabaseResults().isEmpty() );
530         assertEquals( 2, action.getDatabaseResults().size() );
531         
532         daoControl.verify();
533         artifactDaoControl.verify();
534     }
535     
536     public void testFindArtifactNoChecksumSpecified()
537         throws Exception
538     {
539         String result = action.findArtifact();
540         
541         assertEquals( Action.INPUT, result );
542         assertFalse( action.getActionErrors().isEmpty() );
543         assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
544     }
545     
546     public void testFindArtifactNoResults()
547         throws Exception
548     {
549         action.setQ( "afbcdeaadbcffceabbba1" );
550         
551         List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
552         
553         daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
554         
555         artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
556         
557         daoControl.replay();
558         artifactDaoControl.replay();
559         
560         String result = action.findArtifact();
561         assertEquals( Action.INPUT, result );
562         assertFalse( action.getActionErrors().isEmpty() );
563         assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() );
564         
565         daoControl.verify();
566         artifactDaoControl.verify();
567     }
568 }