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 String result = action.quickSearch();
155 assertEquals( Action.SUCCESS, result );
156 assertEquals( 1, action.getTotalPages() );
157 assertEquals( 1, action.getResults().getTotalHits() );
159 userReposControl.verify();
160 searchControl.verify();
164 public void testSearchWithinSearchResults()
167 action.setQ( "archiva" );
168 action.setCurrentPage( 0 );
169 action.setSearchResultsOnly( true );
170 action.setCompleteQueryString( "org;apache" );
172 List<String> parsed = new ArrayList<String>();
174 parsed.add( "apache" );
176 List<String> selectedRepos = new ArrayList<String>();
177 selectedRepos.add( "internal" );
178 selectedRepos.add( "snapshots" );
180 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
181 limits.setPageSize( 30 );
183 SearchResultHit hit = new SearchResultHit();
184 hit.setGroupId( "org.apache.archiva" );
185 hit.setArtifactId( "archiva-configuration" );
187 hit.addVersion( "1.0" );
188 hit.addVersion( "1.1" );
190 SearchResults results = new SearchResults();
191 results.setLimits( limits );
192 results.setTotalHits( 1 );
193 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
195 List<String> versions = new ArrayList<String>();
196 versions.add( "1.0" );
197 versions.add( "1.1" );
199 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
201 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
203 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
205 userReposControl.replay();
206 searchControl.replay();
209 String result = action.quickSearch();
211 assertEquals( Action.SUCCESS, result );
212 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
213 assertEquals( 1, action.getTotalPages() );
214 assertEquals( 1, action.getResults().getTotalHits() );
216 userReposControl.verify();
217 searchControl.verify();
221 public void testQuickSearchUserHasNoAccessToAnyRepository()
224 action.setQ( "archiva" );
225 action.setCurrentPage( 0 );
227 List<String> selectedRepos = new ArrayList<String>();
229 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
231 userReposControl.replay();
233 String result = action.quickSearch();
235 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
237 userReposControl.verify();
240 public void testQuickSearchNoSearchHits()
243 action.setQ( "archiva" );
244 action.setCurrentPage( 0 );
245 action.setSearchResultsOnly( false );
246 action.setCompleteQueryString( "" );
248 List<String> selectedRepos = new ArrayList<String>();
249 selectedRepos.add( "internal" );
250 selectedRepos.add( "snapshots" );
252 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
253 limits.setPageSize( 30 );
255 SearchResults results = new SearchResults();
257 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
259 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
261 userReposControl.replay();
262 searchControl.replay();
264 String result = action.quickSearch();
266 assertEquals( Action.INPUT, result );
268 userReposControl.verify();
269 searchControl.verify();
272 // advanced/filtered search...
274 public void testAdvancedSearchOneRepository()
277 List<String> managedRepos = new ArrayList<String>();
278 managedRepos.add( "internal" );
279 managedRepos.add( "snapshots" );
281 action.setRepositoryId( "internal" );
282 action.setManagedRepositoryList( managedRepos );
283 action.setCurrentPage( 0 );
284 action.setRowCount( 30 );
285 action.setGroupId( "org" );
287 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
288 limits.setPageSize( 30 );
290 SearchResultHit hit = new SearchResultHit();
291 hit.setGroupId( "org.apache.archiva" );
292 hit.setArtifactId( "archiva-configuration" );
294 hit.addVersion( "1.0" );
295 hit.addVersion( "1.1" );
297 SearchResults results = new SearchResults();
298 results.setLimits( limits );
299 results.setTotalHits( 1 );
300 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
302 List<String> selectedRepos = new ArrayList<String>();
303 selectedRepos.add( "internal" );
304 selectedRepos.add( "snapshots" );
306 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
308 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
310 searchControl.replay();
312 String result = action.filteredSearch();
314 assertEquals( Action.SUCCESS, result );
315 assertEquals( 1, action.getTotalPages() );
316 assertEquals( 1, action.getResults().getTotalHits() );
318 searchControl.verify();
321 public void testAdvancedSearchAllRepositories()
324 List<String> managedRepos = new ArrayList<String>();
325 managedRepos.add( "internal" );
326 managedRepos.add( "snapshots" );
328 action.setRepositoryId( "all" );
329 action.setManagedRepositoryList( managedRepos );
330 action.setCurrentPage( 0 );
331 action.setRowCount( 30 );
332 action.setGroupId( "org" );
334 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
335 limits.setPageSize( 30 );
337 SearchResultHit hit = new SearchResultHit();
338 hit.setGroupId( "org.apache.archiva" );
339 hit.setArtifactId( "archiva-configuration" );
341 hit.addVersion( "1.0" );
342 hit.addVersion( "1.1" );
344 SearchResults results = new SearchResults();
345 results.setLimits( limits );
346 results.setTotalHits( 1 );
347 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
349 List<String> selectedRepos = new ArrayList<String>();
350 selectedRepos.add( "internal" );
352 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
354 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
356 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
358 searchControl.replay();
359 userReposControl.replay();
361 String result = action.filteredSearch();
363 assertEquals( Action.SUCCESS, result );
364 assertEquals( 1, action.getTotalPages() );
365 assertEquals( 1, action.getResults().getTotalHits() );
367 searchControl.verify();
368 userReposControl.verify();
371 public void testAdvancedSearchNoSearchHits()
374 List<String> managedRepos = new ArrayList<String>();
375 managedRepos.add( "internal" );
376 managedRepos.add( "snapshots" );
378 action.setRepositoryId( "internal" );
379 action.setManagedRepositoryList( managedRepos );
380 action.setCurrentPage( 0 );
381 action.setRowCount( 30 );
382 action.setGroupId( "org" );
384 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
385 limits.setPageSize( 30 );
387 SearchResults results = new SearchResults();
389 List<String> selectedRepos = new ArrayList<String>();
390 selectedRepos.add( "internal" );
391 selectedRepos.add( "snapshots" );
393 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
395 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
397 searchControl.replay();
399 String result = action.filteredSearch();
401 assertEquals( Action.INPUT, result );
402 assertFalse( action.getActionErrors().isEmpty() );
403 assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
405 searchControl.verify();
408 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
411 List<String> managedRepos = new ArrayList<String>();
413 action.setGroupId( "org.apache.archiva" );
414 action.setManagedRepositoryList( managedRepos );
416 String result = action.filteredSearch();
418 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
421 public void testAdvancedSearchNoSpecifiedCriteria()
424 List<String> managedRepos = new ArrayList<String>();
426 action.setManagedRepositoryList( managedRepos );
428 String result = action.filteredSearch();
430 assertEquals( Action.INPUT, result );
431 assertFalse( action.getActionErrors().isEmpty() );
432 assertEquals( "Advanced Search - At least one search criteria must be provided.",
433 (String) action.getActionErrors().iterator().next() );
438 public void testFindArtifactWithOneHit()
441 action.setQ( "afbcdeaadbcffceabbba1" );
443 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
444 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
445 null, "jar", "test-repo" ) );
447 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
449 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
452 artifactDaoControl.replay();
454 String result = action.findArtifact();
455 assertEquals( "artifact", result );
456 assertFalse( action.getDatabaseResults().isEmpty() );
457 assertEquals( 1, action.getDatabaseResults().size() );
460 artifactDaoControl.verify();
463 public void testFindArtifactWithMultipleHits()
466 action.setQ( "afbcdeaadbcffceabbba1" );
468 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
469 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0",
470 null, "jar", "test-repo" ) );
471 dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-indexer", "1.0",
472 null, "jar", "test-repo" ) );
474 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
476 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
479 artifactDaoControl.replay();
481 String result = action.findArtifact();
482 assertEquals( "results", result );
483 assertFalse( action.getDatabaseResults().isEmpty() );
484 assertEquals( 2, action.getDatabaseResults().size() );
487 artifactDaoControl.verify();
490 public void testFindArtifactNoChecksumSpecified()
493 String result = action.findArtifact();
495 assertEquals( Action.INPUT, result );
496 assertFalse( action.getActionErrors().isEmpty() );
497 assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
500 public void testFindArtifactNoResults()
503 action.setQ( "afbcdeaadbcffceabbba1" );
505 List<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
507 daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao );
509 artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults );
512 artifactDaoControl.replay();
514 String result = action.findArtifact();
515 assertEquals( Action.INPUT, result );
516 assertFalse( action.getActionErrors().isEmpty() );
517 assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() );
520 artifactDaoControl.verify();