1 package org.apache.maven.archiva.web.action;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
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;
44 import com.opensymphony.xwork2.Action;
49 public class SearchActionTest
50 extends PlexusInSpringTestCase
52 private SearchAction action;
54 private MockControl archivaConfigControl;
56 private ArchivaConfiguration archivaConfig;
58 private MockControl daoControl;
60 private ArchivaDAO dao;
62 private MockControl userReposControl;
64 private UserRepositories userRepos;
66 private MockControl archivaXworkUserControl;
68 private ArchivaXworkUser archivaXworkUser;
70 private MockControl searchControl;
72 private RepositorySearch search;
74 private MockControl artifactDaoControl;
76 private ArtifactDAO artifactDao;
79 protected void setUp()
84 action = new SearchAction();
86 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
87 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
89 daoControl = MockControl.createControl( ArchivaDAO.class );
90 daoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
91 dao = ( ArchivaDAO ) daoControl.getMock();
93 userReposControl = MockControl.createControl( UserRepositories.class );
94 userRepos = ( UserRepositories ) userReposControl.getMock();
96 archivaXworkUserControl = MockClassControl.createControl( ArchivaXworkUser.class );
97 archivaXworkUserControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
99 archivaXworkUser = ( ArchivaXworkUser ) archivaXworkUserControl.getMock();
101 searchControl = MockControl.createControl( RepositorySearch.class );
102 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
103 search = ( RepositorySearch ) searchControl.getMock();
105 artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
106 artifactDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
107 artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
109 action.setArchivaConfiguration( archivaConfig );
110 action.setArchivaXworkUser( archivaXworkUser );
111 action.setUserRepositories( userRepos );
112 action.setDao( dao );
113 action.setNexusSearch( search );
117 protected void tearDown()
125 public void testQuickSearch()
128 action.setQ( "archiva" );
129 action.setCurrentPage( 0 );
130 action.setSearchResultsOnly( false );
131 action.setCompleteQueryString( "" );
133 List<String> selectedRepos = new ArrayList<String>();
134 selectedRepos.add( "internal" );
135 selectedRepos.add( "snapshots" );
137 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
138 limits.setPageSize( 30 );
140 SearchResultHit hit = new SearchResultHit();
141 hit.setGroupId( "org.apache.archiva" );
142 hit.setArtifactId( "archiva-configuration" );
144 hit.addVersion( "1.0" );
145 hit.addVersion( "1.1" );
147 SearchResults results = new SearchResults();
148 results.setLimits( limits );
149 results.setTotalHits( 1 );
150 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
152 List<String> versions = new ArrayList<String>();
153 versions.add( "1.0" );
154 versions.add( "1.1" );
156 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 3 );
158 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
160 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
162 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
164 archivaXworkUserControl.replay();
165 userReposControl.replay();
166 searchControl.replay();
169 String result = action.quickSearch();
171 assertEquals( Action.SUCCESS, result );
172 assertEquals( 1, action.getTotalPages() );
173 assertEquals( 1, action.getResults().getTotalHits() );
175 archivaXworkUserControl.verify();
176 userReposControl.verify();
177 searchControl.verify();
181 private String getActivePrincipal()
183 return archivaXworkUser.getActivePrincipal( Collections.<String,Object>emptyMap() );
186 public void testSearchWithinSearchResults()
189 action.setQ( "archiva" );
190 action.setCurrentPage( 0 );
191 action.setSearchResultsOnly( true );
192 action.setCompleteQueryString( "org;apache" );
194 List<String> parsed = new ArrayList<String>();
196 parsed.add( "apache" );
198 List<String> selectedRepos = new ArrayList<String>();
199 selectedRepos.add( "internal" );
200 selectedRepos.add( "snapshots" );
202 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
203 limits.setPageSize( 30 );
205 SearchResultHit hit = new SearchResultHit();
206 hit.setGroupId( "org.apache.archiva" );
207 hit.setArtifactId( "archiva-configuration" );
209 hit.addVersion( "1.0" );
210 hit.addVersion( "1.1" );
212 SearchResults results = new SearchResults();
213 results.setLimits( limits );
214 results.setTotalHits( 1 );
215 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
217 List<String> versions = new ArrayList<String>();
218 versions.add( "1.0" );
219 versions.add( "1.1" );
221 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 3 );
223 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
225 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
227 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
229 archivaXworkUserControl.replay();
230 userReposControl.replay();
231 searchControl.replay();
234 String result = action.quickSearch();
236 assertEquals( Action.SUCCESS, result );
237 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
238 assertEquals( 1, action.getTotalPages() );
239 assertEquals( 1, action.getResults().getTotalHits() );
241 archivaXworkUserControl.verify();
242 userReposControl.verify();
243 searchControl.verify();
247 public void testQuickSearchUserHasNoAccessToAnyRepository()
250 action.setQ( "archiva" );
251 action.setCurrentPage( 0 );
253 List<String> selectedRepos = new ArrayList<String>();
255 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
257 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
259 archivaXworkUserControl.replay();
260 userReposControl.replay();
262 String result = action.quickSearch();
264 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
266 archivaXworkUserControl.verify();
267 userReposControl.verify();
270 public void testQuickSearchNoSearchHits()
273 action.setQ( "archiva" );
274 action.setCurrentPage( 0 );
275 action.setSearchResultsOnly( false );
276 action.setCompleteQueryString( "" );
278 List<String> selectedRepos = new ArrayList<String>();
279 selectedRepos.add( "internal" );
280 selectedRepos.add( "snapshots" );
282 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
283 limits.setPageSize( 30 );
285 SearchResults results = new SearchResults();
287 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 2 );
289 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
291 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
293 archivaXworkUserControl.replay();
294 userReposControl.replay();
295 searchControl.replay();
297 String result = action.quickSearch();
299 assertEquals( Action.INPUT, result );
301 archivaXworkUserControl.verify();
302 userReposControl.verify();
303 searchControl.verify();
306 // advanced/filtered search...
308 public void testAdvancedSearchOneRepository()
311 List<String> managedRepos = new ArrayList<String>();
312 managedRepos.add( "internal" );
313 managedRepos.add( "snapshots" );
315 action.setRepositoryId( "internal" );
316 action.setManagedRepositoryList( managedRepos );
317 action.setCurrentPage( 0 );
318 action.setRowCount( 30 );
319 action.setGroupId( "org" );
321 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
322 limits.setPageSize( 30 );
324 SearchResultHit hit = new SearchResultHit();
325 hit.setGroupId( "org.apache.archiva" );
326 hit.setArtifactId( "archiva-configuration" );
328 hit.addVersion( "1.0" );
329 hit.addVersion( "1.1" );
331 SearchResults results = new SearchResults();
332 results.setLimits( limits );
333 results.setTotalHits( 1 );
334 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
336 List<String> selectedRepos = new ArrayList<String>();
337 selectedRepos.add( "internal" );
338 selectedRepos.add( "snapshots" );
340 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
342 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
344 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
346 archivaXworkUserControl.replay();
347 searchControl.replay();
349 String result = action.filteredSearch();
351 assertEquals( Action.SUCCESS, result );
352 assertEquals( 1, action.getTotalPages() );
353 assertEquals( 1, action.getResults().getTotalHits() );
355 archivaXworkUserControl.verify();
356 searchControl.verify();
359 public void testAdvancedSearchAllRepositories()
362 List<String> managedRepos = new ArrayList<String>();
363 managedRepos.add( "internal" );
364 managedRepos.add( "snapshots" );
366 action.setRepositoryId( "all" );
367 action.setManagedRepositoryList( managedRepos );
368 action.setCurrentPage( 0 );
369 action.setRowCount( 30 );
370 action.setGroupId( "org" );
372 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
373 limits.setPageSize( 30 );
375 SearchResultHit hit = new SearchResultHit();
376 hit.setGroupId( "org.apache.archiva" );
377 hit.setArtifactId( "archiva-configuration" );
379 hit.addVersion( "1.0" );
380 hit.addVersion( "1.1" );
382 SearchResults results = new SearchResults();
383 results.setLimits( limits );
384 results.setTotalHits( 1 );
385 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
387 List<String> selectedRepos = new ArrayList<String>();
388 selectedRepos.add( "internal" );
390 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
392 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user", 2 );
394 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
396 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
398 archivaXworkUserControl.replay();
399 searchControl.replay();
400 userReposControl.replay();
402 String result = action.filteredSearch();
404 assertEquals( Action.SUCCESS, result );
405 assertEquals( 1, action.getTotalPages() );
406 assertEquals( 1, action.getResults().getTotalHits() );
408 archivaXworkUserControl.verify();
409 searchControl.verify();
410 userReposControl.verify();
413 public void testAdvancedSearchNoSearchHits()
416 List<String> managedRepos = new ArrayList<String>();
417 managedRepos.add( "internal" );
418 managedRepos.add( "snapshots" );
420 action.setRepositoryId( "internal" );
421 action.setManagedRepositoryList( managedRepos );
422 action.setCurrentPage( 0 );
423 action.setRowCount( 30 );
424 action.setGroupId( "org" );
426 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
427 limits.setPageSize( 30 );
429 SearchResults results = new SearchResults();
431 List<String> selectedRepos = new ArrayList<String>();
432 selectedRepos.add( "internal" );
433 selectedRepos.add( "snapshots" );
435 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
437 archivaXworkUserControl.expectAndReturn( getActivePrincipal(), "user" );
439 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
441 archivaXworkUserControl.replay();
442 searchControl.replay();
444 String result = action.filteredSearch();
446 assertEquals( Action.INPUT, result );
447 assertFalse( action.getActionErrors().isEmpty() );
448 assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
450 archivaXworkUserControl.verify();
451 searchControl.verify();
454 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
457 List<String> managedRepos = new ArrayList<String>();
459 action.setGroupId( "org.apache.archiva" );
460 action.setManagedRepositoryList( managedRepos );
462 String result = action.filteredSearch();
464 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
467 public void testAdvancedSearchNoSpecifiedCriteria()
470 List<String> managedRepos = new ArrayList<String>();
472 action.setManagedRepositoryList( managedRepos );
474 String result = action.filteredSearch();
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() );
484 public void testFindArtifactWithOneHit()
487 action.setQ( "afbcdeaadbcffceabbba1" );
489 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
490 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
491 null, "jar", "test-repo" ) );
493 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
495 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
498 artifactDaoControl.replay();
500 String result = action.findArtifact();
501 assertEquals( "artifact", result );
502 assertFalse( action.getDatabaseResults().isEmpty() );
503 assertEquals( 1, action.getDatabaseResults().size() );
506 artifactDaoControl.verify();
509 public void testFindArtifactWithMultipleHits()
512 action.setQ( "afbcdeaadbcffceabbba1" );
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" ) );
520 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
522 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
525 artifactDaoControl.replay();
527 String result = action.findArtifact();
528 assertEquals( "results", result );
529 assertFalse( action.getDatabaseResults().isEmpty() );
530 assertEquals( 2, action.getDatabaseResults().size() );
533 artifactDaoControl.verify();
536 public void testFindArtifactNoChecksumSpecified()
539 String result = action.findArtifact();
541 assertEquals( Action.INPUT, result );
542 assertFalse( action.getActionErrors().isEmpty() );
543 assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
546 public void testFindArtifactNoResults()
549 action.setQ( "afbcdeaadbcffceabbba1" );
551 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
553 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
555 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
558 artifactDaoControl.replay();
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() );
566 artifactDaoControl.verify();