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.HashMap;
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( archivaXworkUser.getActivePrincipal( new HashMap() ), "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 public void testSearchWithinSearchResults()
184 action.setQ( "archiva" );
185 action.setCurrentPage( 0 );
186 action.setSearchResultsOnly( true );
187 action.setCompleteQueryString( "org;apache" );
189 List<String> parsed = new ArrayList<String>();
191 parsed.add( "apache" );
193 List<String> selectedRepos = new ArrayList<String>();
194 selectedRepos.add( "internal" );
195 selectedRepos.add( "snapshots" );
197 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
198 limits.setPageSize( 30 );
200 SearchResultHit hit = new SearchResultHit();
201 hit.setGroupId( "org.apache.archiva" );
202 hit.setArtifactId( "archiva-configuration" );
204 hit.addVersion( "1.0" );
205 hit.addVersion( "1.1" );
207 SearchResults results = new SearchResults();
208 results.setLimits( limits );
209 results.setTotalHits( 1 );
210 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
212 List<String> versions = new ArrayList<String>();
213 versions.add( "1.0" );
214 versions.add( "1.1" );
216 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 3 );
218 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
220 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
222 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
224 archivaXworkUserControl.replay();
225 userReposControl.replay();
226 searchControl.replay();
229 String result = action.quickSearch();
231 assertEquals( Action.SUCCESS, result );
232 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
233 assertEquals( 1, action.getTotalPages() );
234 assertEquals( 1, action.getResults().getTotalHits() );
236 archivaXworkUserControl.verify();
237 userReposControl.verify();
238 searchControl.verify();
242 public void testQuickSearchUserHasNoAccessToAnyRepository()
245 action.setQ( "archiva" );
246 action.setCurrentPage( 0 );
248 List<String> selectedRepos = new ArrayList<String>();
250 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
252 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
254 archivaXworkUserControl.replay();
255 userReposControl.replay();
257 String result = action.quickSearch();
259 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
261 archivaXworkUserControl.verify();
262 userReposControl.verify();
265 public void testQuickSearchNoSearchHits()
268 action.setQ( "archiva" );
269 action.setCurrentPage( 0 );
270 action.setSearchResultsOnly( false );
271 action.setCompleteQueryString( "" );
273 List<String> selectedRepos = new ArrayList<String>();
274 selectedRepos.add( "internal" );
275 selectedRepos.add( "snapshots" );
277 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
278 limits.setPageSize( 30 );
280 SearchResults results = new SearchResults();
282 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 2 );
284 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
286 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
288 archivaXworkUserControl.replay();
289 userReposControl.replay();
290 searchControl.replay();
292 String result = action.quickSearch();
294 assertEquals( Action.INPUT, result );
296 archivaXworkUserControl.verify();
297 userReposControl.verify();
298 searchControl.verify();
301 // advanced/filtered search...
303 public void testAdvancedSearchOneRepository()
306 List<String> managedRepos = new ArrayList<String>();
307 managedRepos.add( "internal" );
308 managedRepos.add( "snapshots" );
310 action.setRepositoryId( "internal" );
311 action.setManagedRepositoryList( managedRepos );
312 action.setCurrentPage( 0 );
313 action.setRowCount( 30 );
314 action.setGroupId( "org" );
316 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
317 limits.setPageSize( 30 );
319 SearchResultHit hit = new SearchResultHit();
320 hit.setGroupId( "org.apache.archiva" );
321 hit.setArtifactId( "archiva-configuration" );
323 hit.addVersion( "1.0" );
324 hit.addVersion( "1.1" );
326 SearchResults results = new SearchResults();
327 results.setLimits( limits );
328 results.setTotalHits( 1 );
329 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
331 List<String> selectedRepos = new ArrayList<String>();
332 selectedRepos.add( "internal" );
333 selectedRepos.add( "snapshots" );
335 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
337 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
339 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
341 archivaXworkUserControl.replay();
342 searchControl.replay();
344 String result = action.filteredSearch();
346 assertEquals( Action.SUCCESS, result );
347 assertEquals( 1, action.getTotalPages() );
348 assertEquals( 1, action.getResults().getTotalHits() );
350 archivaXworkUserControl.verify();
351 searchControl.verify();
354 public void testAdvancedSearchAllRepositories()
357 List<String> managedRepos = new ArrayList<String>();
358 managedRepos.add( "internal" );
359 managedRepos.add( "snapshots" );
361 action.setRepositoryId( "all" );
362 action.setManagedRepositoryList( managedRepos );
363 action.setCurrentPage( 0 );
364 action.setRowCount( 30 );
365 action.setGroupId( "org" );
367 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
368 limits.setPageSize( 30 );
370 SearchResultHit hit = new SearchResultHit();
371 hit.setGroupId( "org.apache.archiva" );
372 hit.setArtifactId( "archiva-configuration" );
374 hit.addVersion( "1.0" );
375 hit.addVersion( "1.1" );
377 SearchResults results = new SearchResults();
378 results.setLimits( limits );
379 results.setTotalHits( 1 );
380 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
382 List<String> selectedRepos = new ArrayList<String>();
383 selectedRepos.add( "internal" );
385 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
387 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 2 );
389 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
391 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
393 archivaXworkUserControl.replay();
394 searchControl.replay();
395 userReposControl.replay();
397 String result = action.filteredSearch();
399 assertEquals( Action.SUCCESS, result );
400 assertEquals( 1, action.getTotalPages() );
401 assertEquals( 1, action.getResults().getTotalHits() );
403 archivaXworkUserControl.verify();
404 searchControl.verify();
405 userReposControl.verify();
408 public void testAdvancedSearchNoSearchHits()
411 List<String> managedRepos = new ArrayList<String>();
412 managedRepos.add( "internal" );
413 managedRepos.add( "snapshots" );
415 action.setRepositoryId( "internal" );
416 action.setManagedRepositoryList( managedRepos );
417 action.setCurrentPage( 0 );
418 action.setRowCount( 30 );
419 action.setGroupId( "org" );
421 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
422 limits.setPageSize( 30 );
424 SearchResults results = new SearchResults();
426 List<String> selectedRepos = new ArrayList<String>();
427 selectedRepos.add( "internal" );
428 selectedRepos.add( "snapshots" );
430 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
432 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
434 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
436 archivaXworkUserControl.replay();
437 searchControl.replay();
439 String result = action.filteredSearch();
441 assertEquals( Action.INPUT, result );
442 assertFalse( action.getActionErrors().isEmpty() );
443 assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
445 archivaXworkUserControl.verify();
446 searchControl.verify();
449 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
452 List<String> managedRepos = new ArrayList<String>();
454 action.setGroupId( "org.apache.archiva" );
455 action.setManagedRepositoryList( managedRepos );
457 String result = action.filteredSearch();
459 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
462 public void testAdvancedSearchNoSpecifiedCriteria()
465 List<String> managedRepos = new ArrayList<String>();
467 action.setManagedRepositoryList( managedRepos );
469 String result = action.filteredSearch();
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() );
479 public void testFindArtifactWithOneHit()
482 action.setQ( "afbcdeaadbcffceabbba1" );
484 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
485 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
486 null, "jar", "test-repo" ) );
488 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
490 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
493 artifactDaoControl.replay();
495 String result = action.findArtifact();
496 assertEquals( "artifact", result );
497 assertFalse( action.getDatabaseResults().isEmpty() );
498 assertEquals( 1, action.getDatabaseResults().size() );
501 artifactDaoControl.verify();
504 public void testFindArtifactWithMultipleHits()
507 action.setQ( "afbcdeaadbcffceabbba1" );
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" ) );
515 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
517 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
520 artifactDaoControl.replay();
522 String result = action.findArtifact();
523 assertEquals( "results", result );
524 assertFalse( action.getDatabaseResults().isEmpty() );
525 assertEquals( 2, action.getDatabaseResults().size() );
528 artifactDaoControl.verify();
531 public void testFindArtifactNoChecksumSpecified()
534 String result = action.findArtifact();
536 assertEquals( Action.INPUT, result );
537 assertFalse( action.getActionErrors().isEmpty() );
538 assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
541 public void testFindArtifactNoResults()
544 action.setQ( "afbcdeaadbcffceabbba1" );
546 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
548 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
550 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
553 artifactDaoControl.replay();
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() );
561 artifactDaoControl.verify();