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.configuration.ArchivaConfiguration;
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.archiva.metadata.model.ArtifactMetadata;
32 import org.apache.archiva.metadata.repository.MetadataRepository;
33 import org.apache.archiva.metadata.repository.RepositorySession;
34 import org.apache.archiva.security.UserRepositories;
35 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
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);
136 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
138 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
140 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
142 userReposControl.replay();
143 searchControl.replay();
145 action.setPrincipal("user");
146 String result = action.quickSearch();
148 assertEquals(Action.SUCCESS, result);
149 assertEquals("total pages not 1", 1, action.getTotalPages());
150 assertEquals("totol hits not 1", 1, action.getResults().getTotalHits());
152 userReposControl.verify();
153 searchControl.verify();
156 public void testSearchWithinSearchResults()
159 action.setQ("archiva");
160 action.setCurrentPage(0);
161 action.setSearchResultsOnly(true);
162 action.setCompleteQueryString("org;apache");
164 List<String> parsed = new ArrayList<String>();
166 parsed.add("apache");
168 List<String> selectedRepos = new ArrayList<String>();
169 selectedRepos.add("internal");
170 selectedRepos.add("snapshots");
172 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
173 limits.setPageSize(30);
175 SearchResultHit hit = new SearchResultHit();
176 hit.setGroupId("org.apache.archiva");
177 hit.setArtifactId("archiva-configuration");
179 hit.addVersion("1.0");
180 hit.addVersion("1.1");
182 SearchResults results = new SearchResults();
183 results.setLimits(limits);
184 results.setTotalHits(1);
185 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
186 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
187 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
189 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, parsed), results);
191 userReposControl.replay();
192 searchControl.replay();
194 action.setPrincipal("user");
195 String result = action.quickSearch();
197 assertEquals(Action.SUCCESS, result);
198 assertEquals("org;apache;archiva", action.getCompleteQueryString());
199 assertEquals(1, action.getTotalPages());
200 assertEquals(1, action.getResults().getTotalHits());
202 userReposControl.verify();
203 searchControl.verify();
206 public void testQuickSearchUserHasNoAccessToAnyRepository()
209 action.setQ("archiva");
210 action.setCurrentPage(0);
212 List<String> selectedRepos = new ArrayList<String>();
214 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
216 userReposControl.replay();
218 action.setPrincipal("user");
219 String result = action.quickSearch();
221 assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
223 userReposControl.verify();
226 public void testQuickSearchNoSearchHits()
229 action.setQ("archiva");
230 action.setCurrentPage(0);
231 action.setSearchResultsOnly(false);
232 action.setCompleteQueryString("");
234 List<String> selectedRepos = new ArrayList<String>();
235 selectedRepos.add("internal");
236 selectedRepos.add("snapshots");
238 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
239 limits.setPageSize(30);
241 SearchResults results = new SearchResults();
243 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
245 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
247 userReposControl.replay();
248 searchControl.replay();
250 action.setPrincipal("user");
251 String result = action.quickSearch();
253 assertEquals(Action.INPUT, result);
255 userReposControl.verify();
256 searchControl.verify();
259 // advanced/filtered search...
261 public void testAdvancedSearchOneRepository()
264 List<String> managedRepos = new ArrayList<String>();
265 managedRepos.add("internal");
266 managedRepos.add("snapshots");
268 action.setRepositoryId("internal");
269 action.setManagedRepositoryList(managedRepos);
270 action.setCurrentPage(0);
271 action.setRowCount(30);
272 action.setGroupId("org");
274 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
275 limits.setPageSize(30);
277 SearchResultHit hit = new SearchResultHit();
278 hit.setGroupId("org.apache.archiva");
279 hit.setArtifactId("archiva-configuration");
281 hit.addVersion("1.0");
282 hit.addVersion("1.1");
284 SearchResults results = new SearchResults();
285 results.setLimits(limits);
286 results.setTotalHits(1);
287 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
289 List<String> selectedRepos = new ArrayList<String>();
290 selectedRepos.add("internal");
291 selectedRepos.add("snapshots");
293 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
295 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
297 searchControl.replay();
299 String result = action.filteredSearch();
301 assertEquals(Action.SUCCESS, result);
302 assertEquals(1, action.getTotalPages());
303 assertEquals(1, action.getResults().getTotalHits());
305 searchControl.verify();
308 public void testAdvancedSearchAllRepositories()
311 List<String> managedRepos = new ArrayList<String>();
312 managedRepos.add("internal");
313 managedRepos.add("snapshots");
315 action.setRepositoryId("all");
316 action.setManagedRepositoryList(managedRepos);
317 action.setCurrentPage(0);
318 action.setRowCount(30);
319 action.setGroupId("org");
321 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
322 limits.setPageSize(30);
324 SearchResultHit hit = new SearchResultHit();
325 hit.setGroupId("org.apache.archiva");
326 hit.setArtifactId("archiva-configuration");
328 hit.addVersion("1.0");
329 hit.addVersion("1.1");
331 SearchResults results = new SearchResults();
332 results.setLimits(limits);
333 results.setTotalHits(1);
334 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
336 List<String> selectedRepos = new ArrayList<String>();
337 selectedRepos.add("internal");
339 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
341 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
343 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
345 searchControl.replay();
346 userReposControl.replay();
348 action.setPrincipal("user");
349 String result = action.filteredSearch();
351 assertEquals(Action.SUCCESS, result);
352 assertEquals(1, action.getTotalPages());
353 assertEquals(1, action.getResults().getTotalHits());
355 searchControl.verify();
356 userReposControl.verify();
359 public void testAdvancedSearchNoSearchHits()
362 List<String> managedRepos = new ArrayList<String>();
363 managedRepos.add("internal");
364 managedRepos.add("snapshots");
366 action.setRepositoryId("internal");
367 action.setManagedRepositoryList(managedRepos);
368 action.setCurrentPage(0);
369 action.setRowCount(30);
370 action.setGroupId("org");
372 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
373 limits.setPageSize(30);
375 SearchResults results = new SearchResults();
377 List<String> selectedRepos = new ArrayList<String>();
378 selectedRepos.add("internal");
379 selectedRepos.add("snapshots");
381 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
383 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
385 searchControl.replay();
387 String result = action.filteredSearch();
389 assertEquals(Action.INPUT, result);
390 assertFalse(action.getActionErrors().isEmpty());
391 assertEquals("No results found", (String) action.getActionErrors().iterator().next());
393 searchControl.verify();
396 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
399 List<String> managedRepos = new ArrayList<String>();
401 action.setGroupId("org.apache.archiva");
402 action.setManagedRepositoryList(managedRepos);
404 String result = action.filteredSearch();
406 assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
409 public void testAdvancedSearchNoSpecifiedCriteria()
412 List<String> managedRepos = new ArrayList<String>();
414 action.setManagedRepositoryList(managedRepos);
416 String result = action.filteredSearch();
418 assertEquals(Action.INPUT, result);
419 assertFalse(action.getActionErrors().isEmpty());
420 assertEquals("Advanced Search - At least one search criteria must be provided.",
421 (String) action.getActionErrors().iterator().next());
425 public void testFindArtifactWithOneHit()
428 action.setQ(TEST_CHECKSUM);
430 MockControl control = MockControl.createControl(MetadataRepository.class);
431 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
432 when(session.getRepository()).thenReturn(metadataRepository);
434 ArtifactMetadata artifact = createArtifact("archiva-configuration", "1.0");
435 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
436 Collections.singletonList(artifact));
438 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
439 Collections.singletonList(TEST_REPO));
442 userReposControl.replay();
444 String result = action.findArtifact();
445 assertEquals("artifact", result);
446 assertEquals(1, action.getDatabaseResults().size());
447 assertEquals(artifact, action.getDatabaseResults().get(0));
450 userReposControl.verify();
453 public void testFindArtifactWithMultipleHits()
456 action.setQ(TEST_CHECKSUM);
458 MockControl control = MockControl.createControl(MetadataRepository.class);
459 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
460 when(session.getRepository()).thenReturn(metadataRepository);
462 List<ArtifactMetadata> artifacts =
463 Arrays.asList(createArtifact("archiva-configuration", "1.0"), createArtifact("archiva-indexer", "1.0"));
464 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM), artifacts);
466 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
467 Collections.singletonList(TEST_REPO));
470 userReposControl.replay();
472 String result = action.findArtifact();
473 assertEquals("results", result);
474 assertFalse(action.getDatabaseResults().isEmpty());
475 assertEquals(2, action.getDatabaseResults().size());
478 userReposControl.verify();
481 public void testFindArtifactNoChecksumSpecified()
484 String result = action.findArtifact();
486 assertEquals(Action.INPUT, result);
487 assertFalse(action.getActionErrors().isEmpty());
488 assertEquals("Unable to search for a blank checksum", (String) action.getActionErrors().iterator().next());
491 public void testFindArtifactNoResults()
494 action.setQ(TEST_CHECKSUM);
496 MockControl control = MockControl.createControl(MetadataRepository.class);
497 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
498 when(session.getRepository()).thenReturn(metadataRepository);
500 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
501 Collections.<ArtifactMetadata>emptyList());
503 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
504 Collections.singletonList(TEST_REPO));
507 userReposControl.replay();
509 String result = action.findArtifact();
510 assertEquals(Action.INPUT, result);
511 assertFalse(action.getActionErrors().isEmpty());
512 assertEquals("No results found", (String) action.getActionErrors().iterator().next());
515 userReposControl.verify();
518 private ArtifactMetadata createArtifact(String project, String version)
520 ArtifactMetadata metadata = new ArtifactMetadata();
521 metadata.setNamespace("org.apache.archiva");
522 metadata.setProject(project);
523 metadata.setProjectVersion(version);
524 metadata.setVersion(version);
525 metadata.setRepositoryId(TEST_REPO);
526 metadata.setId(project + "-" + version + ".jar");