1 package org.apache.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.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.indexer.search.RepositorySearch;
25 import org.apache.archiva.indexer.search.SearchFields;
26 import org.apache.archiva.indexer.search.SearchResultHit;
27 import org.apache.archiva.indexer.search.SearchResultLimits;
28 import org.apache.archiva.indexer.search.SearchResults;
29 import org.apache.archiva.indexer.util.SearchUtil;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.repository.MetadataRepository;
32 import org.apache.archiva.metadata.repository.RepositorySession;
33 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
34 import org.apache.archiva.security.UserRepositories;
35 import org.apache.archiva.configuration.ArchivaConfiguration;
36 import org.easymock.MockControl;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.List;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
49 public class SearchActionTest
50 extends AbstractActionTestCase
52 private SearchAction action;
54 private MockControl userReposControl;
56 private UserRepositories userRepos;
58 private MockControl searchControl;
60 private MockControl repoAdminControl;
62 private ManagedRepositoryAdmin managedRepositoryAdmin;
64 private RepositorySearch search;
66 private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1";
68 private static final String TEST_REPO = "test-repo";
70 private static final String GUEST = "guest";
72 private RepositorySession session;
75 protected void setUp()
80 action = new SearchAction();
82 session = mock( RepositorySession.class );
83 //TestRepositorySessionFactory factory = (TestRepositorySessionFactory) lookup( RepositorySessionFactory.class );
84 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
85 factory.setRepositorySession( session );
86 action.setRepositorySessionFactory( factory );
88 MockControl archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
89 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
91 userReposControl = MockControl.createControl( UserRepositories.class );
92 userRepos = (UserRepositories) userReposControl.getMock();
94 searchControl = MockControl.createControl( RepositorySearch.class );
95 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
96 search = (RepositorySearch) searchControl.getMock();
98 repoAdminControl = MockControl.createControl( ManagedRepositoryAdmin.class );
99 managedRepositoryAdmin = (ManagedRepositoryAdmin) repoAdminControl.getMock();
101 //( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfig );
103 action.setManagedRepositoryAdmin( managedRepositoryAdmin );
104 action.setUserRepositories( userRepos );
105 action.setNexusSearch( search );
110 public void testQuickSearch()
113 action.setQ( "archiva" );
114 action.setCurrentPage( 0 );
115 action.setSearchResultsOnly( false );
116 action.setCompleteQueryString( "" );
118 List<String> selectedRepos = new ArrayList<String>();
119 selectedRepos.add( "internal" );
120 selectedRepos.add( "snapshots" );
122 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
123 limits.setPageSize( 30 );
125 SearchResultHit hit = new SearchResultHit();
126 hit.setGroupId( "org.apache.archiva" );
127 hit.setArtifactId( "archiva-configuration" );
129 hit.addVersion( "1.0" );
130 hit.addVersion( "1.1" );
132 SearchResults results = new SearchResults();
133 results.setLimits( limits );
134 results.setTotalHits( 1 );
135 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration", null, "jar" ), hit );
137 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
139 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
141 userReposControl.replay();
142 searchControl.replay();
144 action.setPrincipal( "user" );
145 String result = action.quickSearch();
147 assertEquals( Action.SUCCESS, result );
148 assertEquals( 1, action.getTotalPages() );
149 assertEquals( 1, action.getResults().getTotalHits() );
151 userReposControl.verify();
152 searchControl.verify();
155 public void testSearchWithinSearchResults()
158 action.setQ( "archiva" );
159 action.setCurrentPage( 0 );
160 action.setSearchResultsOnly( true );
161 action.setCompleteQueryString( "org;apache" );
163 List<String> parsed = new ArrayList<String>();
165 parsed.add( "apache" );
167 List<String> selectedRepos = new ArrayList<String>();
168 selectedRepos.add( "internal" );
169 selectedRepos.add( "snapshots" );
171 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
172 limits.setPageSize( 30 );
174 SearchResultHit hit = new SearchResultHit();
175 hit.setGroupId( "org.apache.archiva" );
176 hit.setArtifactId( "archiva-configuration" );
178 hit.addVersion( "1.0" );
179 hit.addVersion( "1.1" );
181 SearchResults results = new SearchResults();
182 results.setLimits( limits );
183 results.setTotalHits( 1 );
184 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration", null, "jar" ), hit );
186 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
188 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
190 userReposControl.replay();
191 searchControl.replay();
193 action.setPrincipal( "user" );
194 String result = action.quickSearch();
196 assertEquals( Action.SUCCESS, result );
197 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
198 assertEquals( 1, action.getTotalPages() );
199 assertEquals( 1, action.getResults().getTotalHits() );
201 userReposControl.verify();
202 searchControl.verify();
205 public void testQuickSearchUserHasNoAccessToAnyRepository()
208 action.setQ( "archiva" );
209 action.setCurrentPage( 0 );
211 List<String> selectedRepos = new ArrayList<String>();
213 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
215 userReposControl.replay();
217 action.setPrincipal( "user" );
218 String result = action.quickSearch();
220 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
222 userReposControl.verify();
225 public void testQuickSearchNoSearchHits()
228 action.setQ( "archiva" );
229 action.setCurrentPage( 0 );
230 action.setSearchResultsOnly( false );
231 action.setCompleteQueryString( "" );
233 List<String> selectedRepos = new ArrayList<String>();
234 selectedRepos.add( "internal" );
235 selectedRepos.add( "snapshots" );
237 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
238 limits.setPageSize( 30 );
240 SearchResults results = new SearchResults();
242 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
244 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
246 userReposControl.replay();
247 searchControl.replay();
249 action.setPrincipal( "user" );
250 String result = action.quickSearch();
252 assertEquals( Action.INPUT, result );
254 userReposControl.verify();
255 searchControl.verify();
258 // advanced/filtered search...
260 public void testAdvancedSearchOneRepository()
263 List<String> managedRepos = new ArrayList<String>();
264 managedRepos.add( "internal" );
265 managedRepos.add( "snapshots" );
267 action.setRepositoryId( "internal" );
268 action.setManagedRepositoryList( managedRepos );
269 action.setCurrentPage( 0 );
270 action.setRowCount( 30 );
271 action.setGroupId( "org" );
273 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
274 limits.setPageSize( 30 );
276 SearchResultHit hit = new SearchResultHit();
277 hit.setGroupId( "org.apache.archiva" );
278 hit.setArtifactId( "archiva-configuration" );
280 hit.addVersion( "1.0" );
281 hit.addVersion( "1.1" );
283 SearchResults results = new SearchResults();
284 results.setLimits( limits );
285 results.setTotalHits( 1 );
286 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration", null, "jar" ), hit );
288 List<String> selectedRepos = new ArrayList<String>();
289 selectedRepos.add( "internal" );
290 selectedRepos.add( "snapshots" );
292 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
294 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
296 searchControl.replay();
298 String result = action.filteredSearch();
300 assertEquals( Action.SUCCESS, result );
301 assertEquals( 1, action.getTotalPages() );
302 assertEquals( 1, action.getResults().getTotalHits() );
304 searchControl.verify();
307 public void testAdvancedSearchAllRepositories()
310 List<String> managedRepos = new ArrayList<String>();
311 managedRepos.add( "internal" );
312 managedRepos.add( "snapshots" );
314 action.setRepositoryId( "all" );
315 action.setManagedRepositoryList( managedRepos );
316 action.setCurrentPage( 0 );
317 action.setRowCount( 30 );
318 action.setGroupId( "org" );
320 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
321 limits.setPageSize( 30 );
323 SearchResultHit hit = new SearchResultHit();
324 hit.setGroupId( "org.apache.archiva" );
325 hit.setArtifactId( "archiva-configuration" );
327 hit.addVersion( "1.0" );
328 hit.addVersion( "1.1" );
330 SearchResults results = new SearchResults();
331 results.setLimits( limits );
332 results.setTotalHits( 1 );
333 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration", null, "jar" ), hit );
335 List<String> selectedRepos = new ArrayList<String>();
336 selectedRepos.add( "internal" );
338 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
340 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
342 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
344 searchControl.replay();
345 userReposControl.replay();
347 action.setPrincipal( "user" );
348 String result = action.filteredSearch();
350 assertEquals( Action.SUCCESS, result );
351 assertEquals( 1, action.getTotalPages() );
352 assertEquals( 1, action.getResults().getTotalHits() );
354 searchControl.verify();
355 userReposControl.verify();
358 public void testAdvancedSearchNoSearchHits()
361 List<String> managedRepos = new ArrayList<String>();
362 managedRepos.add( "internal" );
363 managedRepos.add( "snapshots" );
365 action.setRepositoryId( "internal" );
366 action.setManagedRepositoryList( managedRepos );
367 action.setCurrentPage( 0 );
368 action.setRowCount( 30 );
369 action.setGroupId( "org" );
371 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
372 limits.setPageSize( 30 );
374 SearchResults results = new SearchResults();
376 List<String> selectedRepos = new ArrayList<String>();
377 selectedRepos.add( "internal" );
378 selectedRepos.add( "snapshots" );
380 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
382 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
384 searchControl.replay();
386 String result = action.filteredSearch();
388 assertEquals( Action.INPUT, result );
389 assertFalse( action.getActionErrors().isEmpty() );
390 assertEquals( "No results found", (String) action.getActionErrors().iterator().next() );
392 searchControl.verify();
395 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
398 List<String> managedRepos = new ArrayList<String>();
400 action.setGroupId( "org.apache.archiva" );
401 action.setManagedRepositoryList( managedRepos );
403 String result = action.filteredSearch();
405 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
408 public void testAdvancedSearchNoSpecifiedCriteria()
411 List<String> managedRepos = new ArrayList<String>();
413 action.setManagedRepositoryList( managedRepos );
415 String result = action.filteredSearch();
417 assertEquals( Action.INPUT, result );
418 assertFalse( action.getActionErrors().isEmpty() );
419 assertEquals( "Advanced Search - At least one search criteria must be provided.",
420 (String) action.getActionErrors().iterator().next() );
424 public void testFindArtifactWithOneHit()
427 action.setQ( TEST_CHECKSUM );
429 MockControl control = MockControl.createControl( MetadataRepository.class );
430 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
431 when( session.getRepository() ).thenReturn( metadataRepository );
433 ArtifactMetadata artifact = createArtifact( "archiva-configuration", "1.0" );
434 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
435 Collections.singletonList( artifact ) );
437 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
438 Collections.singletonList( TEST_REPO ) );
441 userReposControl.replay();
443 String result = action.findArtifact();
444 assertEquals( "artifact", result );
445 assertEquals( 1, action.getDatabaseResults().size() );
446 assertEquals( artifact, action.getDatabaseResults().get( 0 ) );
449 userReposControl.verify();
452 public void testFindArtifactWithMultipleHits()
455 action.setQ( TEST_CHECKSUM );
457 MockControl control = MockControl.createControl( MetadataRepository.class );
458 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
459 when( session.getRepository() ).thenReturn( metadataRepository );
461 List<ArtifactMetadata> artifacts = Arrays.asList( createArtifact( "archiva-configuration", "1.0" ),
462 createArtifact( "archiva-indexer", "1.0" ) );
463 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), artifacts );
465 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
466 Collections.singletonList( TEST_REPO ) );
469 userReposControl.replay();
471 String result = action.findArtifact();
472 assertEquals( "results", result );
473 assertFalse( action.getDatabaseResults().isEmpty() );
474 assertEquals( 2, action.getDatabaseResults().size() );
477 userReposControl.verify();
480 public void testFindArtifactNoChecksumSpecified()
483 String result = action.findArtifact();
485 assertEquals( Action.INPUT, result );
486 assertFalse( action.getActionErrors().isEmpty() );
487 assertEquals( "Unable to search for a blank checksum", (String) action.getActionErrors().iterator().next() );
490 public void testFindArtifactNoResults()
493 action.setQ( TEST_CHECKSUM );
495 MockControl control = MockControl.createControl( MetadataRepository.class );
496 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
497 when( session.getRepository() ).thenReturn( metadataRepository );
499 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
500 Collections.<ArtifactMetadata>emptyList() );
502 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
503 Collections.singletonList( TEST_REPO ) );
506 userReposControl.replay();
508 String result = action.findArtifact();
509 assertEquals( Action.INPUT, result );
510 assertFalse( action.getActionErrors().isEmpty() );
511 assertEquals( "No results found", (String) action.getActionErrors().iterator().next() );
514 userReposControl.verify();
517 private ArtifactMetadata createArtifact( String project, String version )
519 ArtifactMetadata metadata = new ArtifactMetadata();
520 metadata.setNamespace( "org.apache.archiva" );
521 metadata.setProject( project );
522 metadata.setProjectVersion( version );
523 metadata.setVersion( version );
524 metadata.setRepositoryId( TEST_REPO );
525 metadata.setId( project + "-" + version + ".jar" );