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 com.opensymphony.xwork2.Action;
23 import org.apache.archiva.indexer.search.RepositorySearch;
24 import org.apache.archiva.indexer.search.SearchFields;
25 import org.apache.archiva.indexer.search.SearchResultHit;
26 import org.apache.archiva.indexer.search.SearchResultLimits;
27 import org.apache.archiva.indexer.search.SearchResults;
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.security.UserRepositories;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import org.easymock.MockControl;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
44 public class SearchActionTest
45 extends PlexusInSpringTestCase
47 private SearchAction action;
49 private MockControl userReposControl;
51 private UserRepositories userRepos;
53 private MockControl searchControl;
55 private RepositorySearch search;
57 private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1";
59 private static final String TEST_REPO = "test-repo";
61 private static final String GUEST = "guest";
64 protected void setUp()
69 action = new SearchAction();
71 MockControl archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
72 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
74 userReposControl = MockControl.createControl( UserRepositories.class );
75 userRepos = ( UserRepositories ) userReposControl.getMock();
77 searchControl = MockControl.createControl( RepositorySearch.class );
78 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
79 search = ( RepositorySearch ) searchControl.getMock();
81 action.setArchivaConfiguration( archivaConfig );
82 action.setUserRepositories( userRepos );
83 action.setNexusSearch( search );
87 protected void tearDown()
95 public void testQuickSearch()
98 action.setQ( "archiva" );
99 action.setCurrentPage( 0 );
100 action.setSearchResultsOnly( false );
101 action.setCompleteQueryString( "" );
103 List<String> selectedRepos = new ArrayList<String>();
104 selectedRepos.add( "internal" );
105 selectedRepos.add( "snapshots" );
107 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
108 limits.setPageSize( 30 );
110 SearchResultHit hit = new SearchResultHit();
111 hit.setGroupId( "org.apache.archiva" );
112 hit.setArtifactId( "archiva-configuration" );
114 hit.addVersion( "1.0" );
115 hit.addVersion( "1.1" );
117 SearchResults results = new SearchResults();
118 results.setLimits( limits );
119 results.setTotalHits( 1 );
120 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
122 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
124 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
126 userReposControl.replay();
127 searchControl.replay();
129 action.setPrincipal( "user" );
130 String result = action.quickSearch();
132 assertEquals( Action.SUCCESS, result );
133 assertEquals( 1, action.getTotalPages() );
134 assertEquals( 1, action.getResults().getTotalHits() );
136 userReposControl.verify();
137 searchControl.verify();
140 public void testSearchWithinSearchResults()
143 action.setQ( "archiva" );
144 action.setCurrentPage( 0 );
145 action.setSearchResultsOnly( true );
146 action.setCompleteQueryString( "org;apache" );
148 List<String> parsed = new ArrayList<String>();
150 parsed.add( "apache" );
152 List<String> selectedRepos = new ArrayList<String>();
153 selectedRepos.add( "internal" );
154 selectedRepos.add( "snapshots" );
156 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
157 limits.setPageSize( 30 );
159 SearchResultHit hit = new SearchResultHit();
160 hit.setGroupId( "org.apache.archiva" );
161 hit.setArtifactId( "archiva-configuration" );
163 hit.addVersion( "1.0" );
164 hit.addVersion( "1.1" );
166 SearchResults results = new SearchResults();
167 results.setLimits( limits );
168 results.setTotalHits( 1 );
169 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
171 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
173 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
175 userReposControl.replay();
176 searchControl.replay();
178 action.setPrincipal( "user" );
179 String result = action.quickSearch();
181 assertEquals( Action.SUCCESS, result );
182 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
183 assertEquals( 1, action.getTotalPages() );
184 assertEquals( 1, action.getResults().getTotalHits() );
186 userReposControl.verify();
187 searchControl.verify();
190 public void testQuickSearchUserHasNoAccessToAnyRepository()
193 action.setQ( "archiva" );
194 action.setCurrentPage( 0 );
196 List<String> selectedRepos = new ArrayList<String>();
198 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
200 userReposControl.replay();
202 action.setPrincipal( "user" );
203 String result = action.quickSearch();
205 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
207 userReposControl.verify();
210 public void testQuickSearchNoSearchHits()
213 action.setQ( "archiva" );
214 action.setCurrentPage( 0 );
215 action.setSearchResultsOnly( false );
216 action.setCompleteQueryString( "" );
218 List<String> selectedRepos = new ArrayList<String>();
219 selectedRepos.add( "internal" );
220 selectedRepos.add( "snapshots" );
222 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
223 limits.setPageSize( 30 );
225 SearchResults results = new SearchResults();
227 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
229 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
231 userReposControl.replay();
232 searchControl.replay();
234 action.setPrincipal( "user" );
235 String result = action.quickSearch();
237 assertEquals( Action.INPUT, result );
239 userReposControl.verify();
240 searchControl.verify();
243 // advanced/filtered search...
245 public void testAdvancedSearchOneRepository()
248 List<String> managedRepos = new ArrayList<String>();
249 managedRepos.add( "internal" );
250 managedRepos.add( "snapshots" );
252 action.setRepositoryId( "internal" );
253 action.setManagedRepositoryList( managedRepos );
254 action.setCurrentPage( 0 );
255 action.setRowCount( 30 );
256 action.setGroupId( "org" );
258 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
259 limits.setPageSize( 30 );
261 SearchResultHit hit = new SearchResultHit();
262 hit.setGroupId( "org.apache.archiva" );
263 hit.setArtifactId( "archiva-configuration" );
265 hit.addVersion( "1.0" );
266 hit.addVersion( "1.1" );
268 SearchResults results = new SearchResults();
269 results.setLimits( limits );
270 results.setTotalHits( 1 );
271 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
273 List<String> selectedRepos = new ArrayList<String>();
274 selectedRepos.add( "internal" );
275 selectedRepos.add( "snapshots" );
277 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
279 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
281 searchControl.replay();
283 String result = action.filteredSearch();
285 assertEquals( Action.SUCCESS, result );
286 assertEquals( 1, action.getTotalPages() );
287 assertEquals( 1, action.getResults().getTotalHits() );
289 searchControl.verify();
292 public void testAdvancedSearchAllRepositories()
295 List<String> managedRepos = new ArrayList<String>();
296 managedRepos.add( "internal" );
297 managedRepos.add( "snapshots" );
299 action.setRepositoryId( "all" );
300 action.setManagedRepositoryList( managedRepos );
301 action.setCurrentPage( 0 );
302 action.setRowCount( 30 );
303 action.setGroupId( "org" );
305 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
306 limits.setPageSize( 30 );
308 SearchResultHit hit = new SearchResultHit();
309 hit.setGroupId( "org.apache.archiva" );
310 hit.setArtifactId( "archiva-configuration" );
312 hit.addVersion( "1.0" );
313 hit.addVersion( "1.1" );
315 SearchResults results = new SearchResults();
316 results.setLimits( limits );
317 results.setTotalHits( 1 );
318 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
320 List<String> selectedRepos = new ArrayList<String>();
321 selectedRepos.add( "internal" );
323 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
325 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
327 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
329 searchControl.replay();
330 userReposControl.replay();
332 action.setPrincipal( "user" );
333 String result = action.filteredSearch();
335 assertEquals( Action.SUCCESS, result );
336 assertEquals( 1, action.getTotalPages() );
337 assertEquals( 1, action.getResults().getTotalHits() );
339 searchControl.verify();
340 userReposControl.verify();
343 public void testAdvancedSearchNoSearchHits()
346 List<String> managedRepos = new ArrayList<String>();
347 managedRepos.add( "internal" );
348 managedRepos.add( "snapshots" );
350 action.setRepositoryId( "internal" );
351 action.setManagedRepositoryList( managedRepos );
352 action.setCurrentPage( 0 );
353 action.setRowCount( 30 );
354 action.setGroupId( "org" );
356 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
357 limits.setPageSize( 30 );
359 SearchResults results = new SearchResults();
361 List<String> selectedRepos = new ArrayList<String>();
362 selectedRepos.add( "internal" );
363 selectedRepos.add( "snapshots" );
365 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
367 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
369 searchControl.replay();
371 String result = action.filteredSearch();
373 assertEquals( Action.INPUT, result );
374 assertFalse( action.getActionErrors().isEmpty() );
375 assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() );
377 searchControl.verify();
380 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
383 List<String> managedRepos = new ArrayList<String>();
385 action.setGroupId( "org.apache.archiva" );
386 action.setManagedRepositoryList( managedRepos );
388 String result = action.filteredSearch();
390 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
393 public void testAdvancedSearchNoSpecifiedCriteria()
396 List<String> managedRepos = new ArrayList<String>();
398 action.setManagedRepositoryList( managedRepos );
400 String result = action.filteredSearch();
402 assertEquals( Action.INPUT, result );
403 assertFalse( action.getActionErrors().isEmpty() );
404 assertEquals( "Advanced Search - At least one search criteria must be provided.",
405 (String) action.getActionErrors().iterator().next() );
409 public void testFindArtifactWithOneHit()
412 action.setQ( TEST_CHECKSUM );
414 MockControl control = MockControl.createControl( MetadataRepository.class );
415 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
416 action.setMetadataRepository( metadataRepository );
418 ArtifactMetadata artifact = createArtifact( "archiva-configuration", "1.0" );
419 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
420 Collections.singletonList( artifact ) );
422 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
423 Collections.singletonList( TEST_REPO ) );
426 userReposControl.replay();
428 String result = action.findArtifact();
429 assertEquals( "artifact", result );
430 assertEquals( 1, action.getDatabaseResults().size() );
431 assertEquals( artifact, action.getDatabaseResults().get( 0 ) );
434 userReposControl.verify();
437 public void testFindArtifactWithMultipleHits()
440 action.setQ( TEST_CHECKSUM );
442 MockControl control = MockControl.createControl( MetadataRepository.class );
443 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
444 action.setMetadataRepository( metadataRepository );
446 List<ArtifactMetadata> artifacts = Arrays.asList( createArtifact( "archiva-configuration", "1.0" ),
447 createArtifact( "archiva-indexer", "1.0" ) );
448 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), artifacts );
450 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
451 Collections.singletonList( TEST_REPO ) );
454 userReposControl.replay();
456 String result = action.findArtifact();
457 assertEquals( "results", result );
458 assertFalse( action.getDatabaseResults().isEmpty() );
459 assertEquals( 2, action.getDatabaseResults().size() );
462 userReposControl.verify();
465 public void testFindArtifactNoChecksumSpecified()
468 String result = action.findArtifact();
470 assertEquals( Action.INPUT, result );
471 assertFalse( action.getActionErrors().isEmpty() );
472 assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() );
475 public void testFindArtifactNoResults()
478 action.setQ( TEST_CHECKSUM );
480 MockControl control = MockControl.createControl( MetadataRepository.class );
481 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
482 action.setMetadataRepository( metadataRepository );
484 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
485 Collections.<ArtifactMetadata>emptyList() );
487 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
488 Collections.singletonList( TEST_REPO ) );
491 userReposControl.replay();
493 String result = action.findArtifact();
494 assertEquals( Action.INPUT, result );
495 assertFalse( action.getActionErrors().isEmpty() );
496 assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() );
499 userReposControl.verify();
502 private ArtifactMetadata createArtifact( String project, String version )
504 ArtifactMetadata metadata = new ArtifactMetadata();
505 metadata.setNamespace( "org.apache.archiva" );
506 metadata.setProject( project );
507 metadata.setProjectVersion( version );
508 metadata.setVersion( version );
509 metadata.setRepositoryId( TEST_REPO );
510 metadata.setId( project + "-" + version + ".jar" );