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.List;
25 import org.apache.archiva.indexer.search.RepositorySearch;
26 import org.apache.archiva.indexer.search.SearchFields;
27 import org.apache.archiva.indexer.search.SearchResultHit;
28 import org.apache.archiva.indexer.search.SearchResultLimits;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.indexer.util.SearchUtil;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.database.ArchivaDAO;
33 import org.apache.maven.archiva.database.ArtifactDAO;
34 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
35 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
36 import org.apache.maven.archiva.model.ArchivaArtifact;
37 import org.apache.maven.archiva.security.UserRepositories;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
39 import org.easymock.MockControl;
41 import com.opensymphony.xwork2.Action;
46 public class SearchActionTest
47 extends PlexusInSpringTestCase
49 private SearchAction action;
51 private MockControl archivaConfigControl;
53 private ArchivaConfiguration archivaConfig;
55 private MockControl daoControl;
57 private ArchivaDAO dao;
59 private MockControl userReposControl;
61 private UserRepositories userRepos;
63 private MockControl searchControl;
65 private RepositorySearch search;
67 private MockControl artifactDaoControl;
69 private ArtifactDAO artifactDao;
72 protected void setUp()
77 action = new SearchAction();
79 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
80 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
82 daoControl = MockControl.createControl( ArchivaDAO.class );
83 daoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
84 dao = ( ArchivaDAO ) daoControl.getMock();
86 userReposControl = MockControl.createControl( UserRepositories.class );
87 userRepos = ( UserRepositories ) userReposControl.getMock();
89 searchControl = MockControl.createControl( RepositorySearch.class );
90 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
91 search = ( RepositorySearch ) searchControl.getMock();
93 artifactDaoControl = MockControl.createControl( ArtifactDAO.class );
94 artifactDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
95 artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock();
97 action.setArchivaConfiguration( archivaConfig );
98 action.setUserRepositories( userRepos );
100 action.setNexusSearch( search );
104 protected void tearDown()
112 public void testQuickSearch()
115 action.setQ( "archiva" );
116 action.setCurrentPage( 0 );
117 action.setSearchResultsOnly( false );
118 action.setCompleteQueryString( "" );
120 List<String> selectedRepos = new ArrayList<String>();
121 selectedRepos.add( "internal" );
122 selectedRepos.add( "snapshots" );
124 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
125 limits.setPageSize( 30 );
127 SearchResultHit hit = new SearchResultHit();
128 hit.setGroupId( "org.apache.archiva" );
129 hit.setArtifactId( "archiva-configuration" );
131 hit.addVersion( "1.0" );
132 hit.addVersion( "1.1" );
134 SearchResults results = new SearchResults();
135 results.setLimits( limits );
136 results.setTotalHits( 1 );
137 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
139 List<String> versions = new ArrayList<String>();
140 versions.add( "1.0" );
141 versions.add( "1.1" );
143 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
145 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
147 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
149 userReposControl.replay();
150 searchControl.replay();
153 action.setPrincipal( "user" );
154 String result = action.quickSearch();
156 assertEquals( Action.SUCCESS, result );
157 assertEquals( 1, action.getTotalPages() );
158 assertEquals( 1, action.getResults().getTotalHits() );
160 userReposControl.verify();
161 searchControl.verify();
165 public void testSearchWithinSearchResults()
168 action.setQ( "archiva" );
169 action.setCurrentPage( 0 );
170 action.setSearchResultsOnly( true );
171 action.setCompleteQueryString( "org;apache" );
173 List<String> parsed = new ArrayList<String>();
175 parsed.add( "apache" );
177 List<String> selectedRepos = new ArrayList<String>();
178 selectedRepos.add( "internal" );
179 selectedRepos.add( "snapshots" );
181 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
182 limits.setPageSize( 30 );
184 SearchResultHit hit = new SearchResultHit();
185 hit.setGroupId( "org.apache.archiva" );
186 hit.setArtifactId( "archiva-configuration" );
188 hit.addVersion( "1.0" );
189 hit.addVersion( "1.1" );
191 SearchResults results = new SearchResults();
192 results.setLimits( limits );
193 results.setTotalHits( 1 );
194 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
196 List<String> versions = new ArrayList<String>();
197 versions.add( "1.0" );
198 versions.add( "1.1" );
200 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
202 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
204 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
206 userReposControl.replay();
207 searchControl.replay();
210 action.setPrincipal( "user" );
211 String result = action.quickSearch();
213 assertEquals( Action.SUCCESS, result );
214 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
215 assertEquals( 1, action.getTotalPages() );
216 assertEquals( 1, action.getResults().getTotalHits() );
218 userReposControl.verify();
219 searchControl.verify();
223 public void testQuickSearchUserHasNoAccessToAnyRepository()
226 action.setQ( "archiva" );
227 action.setCurrentPage( 0 );
229 List<String> selectedRepos = new ArrayList<String>();
231 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
233 userReposControl.replay();
235 action.setPrincipal( "user" );
236 String result = action.quickSearch();
238 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
240 userReposControl.verify();
243 public void testQuickSearchNoSearchHits()
246 action.setQ( "archiva" );
247 action.setCurrentPage( 0 );
248 action.setSearchResultsOnly( false );
249 action.setCompleteQueryString( "" );
251 List<String> selectedRepos = new ArrayList<String>();
252 selectedRepos.add( "internal" );
253 selectedRepos.add( "snapshots" );
255 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
256 limits.setPageSize( 30 );
258 SearchResults results = new SearchResults();
260 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
262 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
264 userReposControl.replay();
265 searchControl.replay();
267 action.setPrincipal( "user" );
268 String result = action.quickSearch();
270 assertEquals( Action.INPUT, result );
272 userReposControl.verify();
273 searchControl.verify();
276 // advanced/filtered search...
278 public void testAdvancedSearchOneRepository()
281 List<String> managedRepos = new ArrayList<String>();
282 managedRepos.add( "internal" );
283 managedRepos.add( "snapshots" );
285 action.setRepositoryId( "internal" );
286 action.setManagedRepositoryList( managedRepos );
287 action.setCurrentPage( 0 );
288 action.setRowCount( 30 );
289 action.setGroupId( "org" );
291 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
292 limits.setPageSize( 30 );
294 SearchResultHit hit = new SearchResultHit();
295 hit.setGroupId( "org.apache.archiva" );
296 hit.setArtifactId( "archiva-configuration" );
298 hit.addVersion( "1.0" );
299 hit.addVersion( "1.1" );
301 SearchResults results = new SearchResults();
302 results.setLimits( limits );
303 results.setTotalHits( 1 );
304 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
306 List<String> selectedRepos = new ArrayList<String>();
307 selectedRepos.add( "internal" );
308 selectedRepos.add( "snapshots" );
310 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
312 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
314 searchControl.replay();
316 String result = action.filteredSearch();
318 assertEquals( Action.SUCCESS, result );
319 assertEquals( 1, action.getTotalPages() );
320 assertEquals( 1, action.getResults().getTotalHits() );
322 searchControl.verify();
325 public void testAdvancedSearchAllRepositories()
328 List<String> managedRepos = new ArrayList<String>();
329 managedRepos.add( "internal" );
330 managedRepos.add( "snapshots" );
332 action.setRepositoryId( "all" );
333 action.setManagedRepositoryList( managedRepos );
334 action.setCurrentPage( 0 );
335 action.setRowCount( 30 );
336 action.setGroupId( "org" );
338 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
339 limits.setPageSize( 30 );
341 SearchResultHit hit = new SearchResultHit();
342 hit.setGroupId( "org.apache.archiva" );
343 hit.setArtifactId( "archiva-configuration" );
345 hit.addVersion( "1.0" );
346 hit.addVersion( "1.1" );
348 SearchResults results = new SearchResults();
349 results.setLimits( limits );
350 results.setTotalHits( 1 );
351 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
353 List<String> selectedRepos = new ArrayList<String>();
354 selectedRepos.add( "internal" );
356 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
358 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
360 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
362 searchControl.replay();
363 userReposControl.replay();
365 action.setPrincipal( "user" );
366 String result = action.filteredSearch();
368 assertEquals( Action.SUCCESS, result );
369 assertEquals( 1, action.getTotalPages() );
370 assertEquals( 1, action.getResults().getTotalHits() );
372 searchControl.verify();
373 userReposControl.verify();
376 public void testAdvancedSearchNoSearchHits()
379 List<String> managedRepos = new ArrayList<String>();
380 managedRepos.add( "internal" );
381 managedRepos.add( "snapshots" );
383 action.setRepositoryId( "internal" );
384 action.setManagedRepositoryList( managedRepos );
385 action.setCurrentPage( 0 );
386 action.setRowCount( 30 );
387 action.setGroupId( "org" );
389 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
390 limits.setPageSize( 30 );
392 SearchResults results = new SearchResults();
394 List<String> selectedRepos = new ArrayList<String>();
395 selectedRepos.add( "internal" );
396 selectedRepos.add( "snapshots" );
398 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
400 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
402 searchControl.replay();
404 String result = action.filteredSearch();
406 assertEquals( Action.INPUT, result );
407 assertFalse( action.getActionErrors().isEmpty() );
408 assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
410 searchControl.verify();
413 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
416 List<String> managedRepos = new ArrayList<String>();
418 action.setGroupId( "org.apache.archiva" );
419 action.setManagedRepositoryList( managedRepos );
421 String result = action.filteredSearch();
423 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
426 public void testAdvancedSearchNoSpecifiedCriteria()
429 List<String> managedRepos = new ArrayList<String>();
431 action.setManagedRepositoryList( managedRepos );
433 String result = action.filteredSearch();
435 assertEquals( Action.INPUT, result );
436 assertFalse( action.getActionErrors().isEmpty() );
437 assertEquals( "Advanced Search - At least one search criteria must be provided.",
438 (String) action.getActionErrors().iterator().next() );
443 public void testFindArtifactWithOneHit()
446 action.setQ( "afbcdeaadbcffceabbba1" );
448 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
449 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
450 null, "jar", "test-repo" ) );
452 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
454 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
457 artifactDaoControl.replay();
459 String result = action.findArtifact();
460 assertEquals( "artifact", result );
461 assertFalse( action.getDatabaseResults().isEmpty() );
462 assertEquals( 1, action.getDatabaseResults().size() );
465 artifactDaoControl.verify();
468 public void testFindArtifactWithMultipleHits()
471 action.setQ( "afbcdeaadbcffceabbba1" );
473 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
474 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
475 null, "jar", "test-repo" ) );
476 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-indexer", "1.0",
477 null, "jar", "test-repo" ) );
479 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
481 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
484 artifactDaoControl.replay();
486 String result = action.findArtifact();
487 assertEquals( "results", result );
488 assertFalse( action.getDatabaseResults().isEmpty() );
489 assertEquals( 2, action.getDatabaseResults().size() );
492 artifactDaoControl.verify();
495 public void testFindArtifactNoChecksumSpecified()
498 String result = action.findArtifact();
500 assertEquals( Action.INPUT, result );
501 assertFalse( action.getActionErrors().isEmpty() );
502 assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
505 public void testFindArtifactNoResults()
508 action.setQ( "afbcdeaadbcffceabbba1" );
510 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
512 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
514 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
517 artifactDaoControl.replay();
519 String result = action.findArtifact();
520 assertEquals( Action.INPUT, result );
521 assertFalse( action.getActionErrors().isEmpty() );
522 assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() );
525 artifactDaoControl.verify();