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.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.archiva.security.UserRepositories;
35 import org.easymock.MockControl;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
48 public class SearchActionTest
49 extends AbstractActionTestCase
51 private SearchAction action;
53 private MockControl userReposControl;
55 private UserRepositories userRepos;
57 private MockControl searchControl;
59 private RepositorySearch search;
61 private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1";
63 private static final String TEST_REPO = "test-repo";
65 private static final String GUEST = "guest";
67 private RepositorySession session;
70 protected void setUp()
75 action = new SearchAction();
77 session = mock( RepositorySession.class );
78 //TestRepositorySessionFactory factory = (TestRepositorySessionFactory) lookup( RepositorySessionFactory.class );
79 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
80 factory.setRepositorySession( session );
81 action.setRepositorySessionFactory( factory );
83 MockControl archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
84 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.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 action.setArchivaConfiguration( archivaConfig );
94 action.setUserRepositories( userRepos );
95 action.setNexusSearch( search );
100 public void testQuickSearch()
103 action.setQ( "archiva" );
104 action.setCurrentPage( 0 );
105 action.setSearchResultsOnly( false );
106 action.setCompleteQueryString( "" );
108 List<String> selectedRepos = new ArrayList<String>();
109 selectedRepos.add( "internal" );
110 selectedRepos.add( "snapshots" );
112 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
113 limits.setPageSize( 30 );
115 SearchResultHit hit = new SearchResultHit();
116 hit.setGroupId( "org.apache.archiva" );
117 hit.setArtifactId( "archiva-configuration" );
119 hit.addVersion( "1.0" );
120 hit.addVersion( "1.1" );
122 SearchResults results = new SearchResults();
123 results.setLimits( limits );
124 results.setTotalHits( 1 );
125 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
127 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
129 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
131 userReposControl.replay();
132 searchControl.replay();
134 action.setPrincipal( "user" );
135 String result = action.quickSearch();
137 assertEquals( Action.SUCCESS, result );
138 assertEquals( 1, action.getTotalPages() );
139 assertEquals( 1, action.getResults().getTotalHits() );
141 userReposControl.verify();
142 searchControl.verify();
145 public void testSearchWithinSearchResults()
148 action.setQ( "archiva" );
149 action.setCurrentPage( 0 );
150 action.setSearchResultsOnly( true );
151 action.setCompleteQueryString( "org;apache" );
153 List<String> parsed = new ArrayList<String>();
155 parsed.add( "apache" );
157 List<String> selectedRepos = new ArrayList<String>();
158 selectedRepos.add( "internal" );
159 selectedRepos.add( "snapshots" );
161 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
162 limits.setPageSize( 30 );
164 SearchResultHit hit = new SearchResultHit();
165 hit.setGroupId( "org.apache.archiva" );
166 hit.setArtifactId( "archiva-configuration" );
168 hit.addVersion( "1.0" );
169 hit.addVersion( "1.1" );
171 SearchResults results = new SearchResults();
172 results.setLimits( limits );
173 results.setTotalHits( 1 );
174 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
176 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
178 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results );
180 userReposControl.replay();
181 searchControl.replay();
183 action.setPrincipal( "user" );
184 String result = action.quickSearch();
186 assertEquals( Action.SUCCESS, result );
187 assertEquals( "org;apache;archiva", action.getCompleteQueryString() );
188 assertEquals( 1, action.getTotalPages() );
189 assertEquals( 1, action.getResults().getTotalHits() );
191 userReposControl.verify();
192 searchControl.verify();
195 public void testQuickSearchUserHasNoAccessToAnyRepository()
198 action.setQ( "archiva" );
199 action.setCurrentPage( 0 );
201 List<String> selectedRepos = new ArrayList<String>();
203 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
205 userReposControl.replay();
207 action.setPrincipal( "user" );
208 String result = action.quickSearch();
210 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
212 userReposControl.verify();
215 public void testQuickSearchNoSearchHits()
218 action.setQ( "archiva" );
219 action.setCurrentPage( 0 );
220 action.setSearchResultsOnly( false );
221 action.setCompleteQueryString( "" );
223 List<String> selectedRepos = new ArrayList<String>();
224 selectedRepos.add( "internal" );
225 selectedRepos.add( "snapshots" );
227 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
228 limits.setPageSize( 30 );
230 SearchResults results = new SearchResults();
232 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
234 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
236 userReposControl.replay();
237 searchControl.replay();
239 action.setPrincipal( "user" );
240 String result = action.quickSearch();
242 assertEquals( Action.INPUT, result );
244 userReposControl.verify();
245 searchControl.verify();
248 // advanced/filtered search...
250 public void testAdvancedSearchOneRepository()
253 List<String> managedRepos = new ArrayList<String>();
254 managedRepos.add( "internal" );
255 managedRepos.add( "snapshots" );
257 action.setRepositoryId( "internal" );
258 action.setManagedRepositoryList( managedRepos );
259 action.setCurrentPage( 0 );
260 action.setRowCount( 30 );
261 action.setGroupId( "org" );
263 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
264 limits.setPageSize( 30 );
266 SearchResultHit hit = new SearchResultHit();
267 hit.setGroupId( "org.apache.archiva" );
268 hit.setArtifactId( "archiva-configuration" );
270 hit.addVersion( "1.0" );
271 hit.addVersion( "1.1" );
273 SearchResults results = new SearchResults();
274 results.setLimits( limits );
275 results.setTotalHits( 1 );
276 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
278 List<String> selectedRepos = new ArrayList<String>();
279 selectedRepos.add( "internal" );
280 selectedRepos.add( "snapshots" );
282 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
284 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
286 searchControl.replay();
288 String result = action.filteredSearch();
290 assertEquals( Action.SUCCESS, result );
291 assertEquals( 1, action.getTotalPages() );
292 assertEquals( 1, action.getResults().getTotalHits() );
294 searchControl.verify();
297 public void testAdvancedSearchAllRepositories()
300 List<String> managedRepos = new ArrayList<String>();
301 managedRepos.add( "internal" );
302 managedRepos.add( "snapshots" );
304 action.setRepositoryId( "all" );
305 action.setManagedRepositoryList( managedRepos );
306 action.setCurrentPage( 0 );
307 action.setRowCount( 30 );
308 action.setGroupId( "org" );
310 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
311 limits.setPageSize( 30 );
313 SearchResultHit hit = new SearchResultHit();
314 hit.setGroupId( "org.apache.archiva" );
315 hit.setArtifactId( "archiva-configuration" );
317 hit.addVersion( "1.0" );
318 hit.addVersion( "1.1" );
320 SearchResults results = new SearchResults();
321 results.setLimits( limits );
322 results.setTotalHits( 1 );
323 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
325 List<String> selectedRepos = new ArrayList<String>();
326 selectedRepos.add( "internal" );
328 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
330 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
332 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
334 searchControl.replay();
335 userReposControl.replay();
337 action.setPrincipal( "user" );
338 String result = action.filteredSearch();
340 assertEquals( Action.SUCCESS, result );
341 assertEquals( 1, action.getTotalPages() );
342 assertEquals( 1, action.getResults().getTotalHits() );
344 searchControl.verify();
345 userReposControl.verify();
348 public void testAdvancedSearchNoSearchHits()
351 List<String> managedRepos = new ArrayList<String>();
352 managedRepos.add( "internal" );
353 managedRepos.add( "snapshots" );
355 action.setRepositoryId( "internal" );
356 action.setManagedRepositoryList( managedRepos );
357 action.setCurrentPage( 0 );
358 action.setRowCount( 30 );
359 action.setGroupId( "org" );
361 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
362 limits.setPageSize( 30 );
364 SearchResults results = new SearchResults();
366 List<String> selectedRepos = new ArrayList<String>();
367 selectedRepos.add( "internal" );
368 selectedRepos.add( "snapshots" );
370 SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos );
372 searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results );
374 searchControl.replay();
376 String result = action.filteredSearch();
378 assertEquals( Action.INPUT, result );
379 assertFalse( action.getActionErrors().isEmpty() );
380 assertEquals( "No results found", (String) action.getActionErrors().iterator().next() );
382 searchControl.verify();
385 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
388 List<String> managedRepos = new ArrayList<String>();
390 action.setGroupId( "org.apache.archiva" );
391 action.setManagedRepositoryList( managedRepos );
393 String result = action.filteredSearch();
395 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
398 public void testAdvancedSearchNoSpecifiedCriteria()
401 List<String> managedRepos = new ArrayList<String>();
403 action.setManagedRepositoryList( managedRepos );
405 String result = action.filteredSearch();
407 assertEquals( Action.INPUT, result );
408 assertFalse( action.getActionErrors().isEmpty() );
409 assertEquals( "Advanced Search - At least one search criteria must be provided.",
410 (String) action.getActionErrors().iterator().next() );
414 public void testFindArtifactWithOneHit()
417 action.setQ( TEST_CHECKSUM );
419 MockControl control = MockControl.createControl( MetadataRepository.class );
420 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
421 when( session.getRepository() ).thenReturn( metadataRepository );
423 ArtifactMetadata artifact = createArtifact( "archiva-configuration", "1.0" );
424 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
425 Collections.singletonList( artifact ) );
427 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
428 Collections.singletonList( TEST_REPO ) );
431 userReposControl.replay();
433 String result = action.findArtifact();
434 assertEquals( "artifact", result );
435 assertEquals( 1, action.getDatabaseResults().size() );
436 assertEquals( artifact, action.getDatabaseResults().get( 0 ) );
439 userReposControl.verify();
442 public void testFindArtifactWithMultipleHits()
445 action.setQ( TEST_CHECKSUM );
447 MockControl control = MockControl.createControl( MetadataRepository.class );
448 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
449 when( session.getRepository() ).thenReturn( metadataRepository );
451 List<ArtifactMetadata> artifacts = Arrays.asList( createArtifact( "archiva-configuration", "1.0" ),
452 createArtifact( "archiva-indexer", "1.0" ) );
453 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), artifacts );
455 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
456 Collections.singletonList( TEST_REPO ) );
459 userReposControl.replay();
461 String result = action.findArtifact();
462 assertEquals( "results", result );
463 assertFalse( action.getDatabaseResults().isEmpty() );
464 assertEquals( 2, action.getDatabaseResults().size() );
467 userReposControl.verify();
470 public void testFindArtifactNoChecksumSpecified()
473 String result = action.findArtifact();
475 assertEquals( Action.INPUT, result );
476 assertFalse( action.getActionErrors().isEmpty() );
477 assertEquals( "Unable to search for a blank checksum", (String) action.getActionErrors().iterator().next() );
480 public void testFindArtifactNoResults()
483 action.setQ( TEST_CHECKSUM );
485 MockControl control = MockControl.createControl( MetadataRepository.class );
486 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
487 when( session.getRepository() ).thenReturn( metadataRepository );
489 control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ),
490 Collections.<ArtifactMetadata>emptyList() );
492 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ),
493 Collections.singletonList( TEST_REPO ) );
496 userReposControl.replay();
498 String result = action.findArtifact();
499 assertEquals( Action.INPUT, result );
500 assertFalse( action.getActionErrors().isEmpty() );
501 assertEquals( "No results found", (String) action.getActionErrors().iterator().next() );
504 userReposControl.verify();
507 private ArtifactMetadata createArtifact( String project, String version )
509 ArtifactMetadata metadata = new ArtifactMetadata();
510 metadata.setNamespace( "org.apache.archiva" );
511 metadata.setProject( project );
512 metadata.setProjectVersion( version );
513 metadata.setVersion( version );
514 metadata.setRepositoryId( TEST_REPO );
515 metadata.setId( project + "-" + version + ".jar" );