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;
42 import org.junit.Test;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
50 public class SearchActionTest
51 extends AbstractActionTestCase
53 private SearchAction action;
55 private MockControl userReposControl;
57 private UserRepositories userRepos;
59 private MockControl searchControl;
61 private MockControl repoAdminControl;
63 private ManagedRepositoryAdmin managedRepositoryAdmin;
65 private RepositorySearch search;
67 private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1";
69 private static final String TEST_REPO = "test-repo";
71 private static final String GUEST = "guest";
73 private RepositorySession session;
81 action = new SearchAction();
83 session = mock(RepositorySession.class);
84 //TestRepositorySessionFactory factory = (TestRepositorySessionFactory) lookup( RepositorySessionFactory.class );
85 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
86 factory.setRepositorySession(session);
87 action.setRepositorySessionFactory(factory);
89 MockControl archivaConfigControl = MockControl.createControl(ArchivaConfiguration.class);
90 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
92 userReposControl = MockControl.createControl(UserRepositories.class);
93 userRepos = (UserRepositories) userReposControl.getMock();
95 searchControl = MockControl.createControl(RepositorySearch.class);
96 searchControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
97 search = (RepositorySearch) searchControl.getMock();
99 repoAdminControl = MockControl.createControl(ManagedRepositoryAdmin.class);
100 managedRepositoryAdmin = (ManagedRepositoryAdmin) repoAdminControl.getMock();
102 //( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfig );
104 action.setManagedRepositoryAdmin(managedRepositoryAdmin);
105 action.setUserRepositories(userRepos);
106 action.setNexusSearch(search);
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", null, "jar"), hit);
138 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
140 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
142 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
144 userReposControl.replay();
145 searchControl.replay();
147 action.setPrincipal("user");
148 String result = action.quickSearch();
150 assertEquals(Action.SUCCESS, result);
151 assertEquals("total pages not 1", 1, action.getTotalPages());
152 assertEquals("totol hits not 1", 1, action.getResults().getTotalHits());
154 userReposControl.verify();
155 searchControl.verify();
159 public void testSearchWithinSearchResults()
162 action.setQ("archiva");
163 action.setCurrentPage(0);
164 action.setSearchResultsOnly(true);
165 action.setCompleteQueryString("org;apache");
167 List<String> parsed = new ArrayList<String>();
169 parsed.add("apache");
171 List<String> selectedRepos = new ArrayList<String>();
172 selectedRepos.add("internal");
173 selectedRepos.add("snapshots");
175 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
176 limits.setPageSize(30);
178 SearchResultHit hit = new SearchResultHit();
179 hit.setGroupId("org.apache.archiva");
180 hit.setArtifactId("archiva-configuration");
182 hit.addVersion("1.0");
183 hit.addVersion("1.1");
185 SearchResults results = new SearchResults();
186 results.setLimits(limits);
187 results.setTotalHits(1);
188 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
189 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
190 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
192 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, parsed), results);
194 userReposControl.replay();
195 searchControl.replay();
197 action.setPrincipal("user");
198 String result = action.quickSearch();
200 assertEquals(Action.SUCCESS, result);
201 assertEquals("org;apache;archiva", action.getCompleteQueryString());
202 assertEquals(1, action.getTotalPages());
203 assertEquals(1, action.getResults().getTotalHits());
205 userReposControl.verify();
206 searchControl.verify();
210 public void testQuickSearchUserHasNoAccessToAnyRepository()
213 action.setQ("archiva");
214 action.setCurrentPage(0);
216 List<String> selectedRepos = new ArrayList<String>();
218 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
220 userReposControl.replay();
222 action.setPrincipal("user");
223 String result = action.quickSearch();
225 assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
227 userReposControl.verify();
231 public void testQuickSearchNoSearchHits()
234 action.setQ("archiva");
235 action.setCurrentPage(0);
236 action.setSearchResultsOnly(false);
237 action.setCompleteQueryString("");
239 List<String> selectedRepos = new ArrayList<String>();
240 selectedRepos.add("internal");
241 selectedRepos.add("snapshots");
243 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
244 limits.setPageSize(30);
246 SearchResults results = new SearchResults();
248 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
250 searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
252 userReposControl.replay();
253 searchControl.replay();
255 action.setPrincipal("user");
256 String result = action.quickSearch();
258 assertEquals(Action.INPUT, result);
260 userReposControl.verify();
261 searchControl.verify();
264 // advanced/filtered search...
267 public void testAdvancedSearchOneRepository()
270 List<String> managedRepos = new ArrayList<String>();
271 managedRepos.add("internal");
272 managedRepos.add("snapshots");
274 action.setRepositoryId("internal");
275 action.setManagedRepositoryList(managedRepos);
276 action.setCurrentPage(0);
277 action.setRowCount(30);
278 action.setGroupId("org");
280 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
281 limits.setPageSize(30);
283 SearchResultHit hit = new SearchResultHit();
284 hit.setGroupId("org.apache.archiva");
285 hit.setArtifactId("archiva-configuration");
287 hit.addVersion("1.0");
288 hit.addVersion("1.1");
290 SearchResults results = new SearchResults();
291 results.setLimits(limits);
292 results.setTotalHits(1);
293 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
295 List<String> selectedRepos = new ArrayList<String>();
296 selectedRepos.add("internal");
297 selectedRepos.add("snapshots");
299 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
301 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
303 searchControl.replay();
305 String result = action.filteredSearch();
307 assertEquals(Action.SUCCESS, result);
308 assertEquals(1, action.getTotalPages());
309 assertEquals(1, action.getResults().getTotalHits());
311 searchControl.verify();
315 public void testAdvancedSearchAllRepositories()
318 List<String> managedRepos = new ArrayList<String>();
319 managedRepos.add("internal");
320 managedRepos.add("snapshots");
322 action.setRepositoryId("all");
323 action.setManagedRepositoryList(managedRepos);
324 action.setCurrentPage(0);
325 action.setRowCount(30);
326 action.setGroupId("org");
328 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
329 limits.setPageSize(30);
331 SearchResultHit hit = new SearchResultHit();
332 hit.setGroupId("org.apache.archiva");
333 hit.setArtifactId("archiva-configuration");
335 hit.addVersion("1.0");
336 hit.addVersion("1.1");
338 SearchResults results = new SearchResults();
339 results.setLimits(limits);
340 results.setTotalHits(1);
341 results.addHit(SearchUtil.getHitId("org.apache.archiva", "archiva-configuration", null, "jar"), hit);
343 List<String> selectedRepos = new ArrayList<String>();
344 selectedRepos.add("internal");
346 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
348 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
350 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
352 searchControl.replay();
353 userReposControl.replay();
355 action.setPrincipal("user");
356 String result = action.filteredSearch();
358 assertEquals(Action.SUCCESS, result);
359 assertEquals(1, action.getTotalPages());
360 assertEquals(1, action.getResults().getTotalHits());
362 searchControl.verify();
363 userReposControl.verify();
367 public void testAdvancedSearchNoSearchHits()
370 List<String> managedRepos = new ArrayList<String>();
371 managedRepos.add("internal");
372 managedRepos.add("snapshots");
374 action.setRepositoryId("internal");
375 action.setManagedRepositoryList(managedRepos);
376 action.setCurrentPage(0);
377 action.setRowCount(30);
378 action.setGroupId("org");
380 SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
381 limits.setPageSize(30);
383 SearchResults results = new SearchResults();
385 List<String> selectedRepos = new ArrayList<String>();
386 selectedRepos.add("internal");
387 selectedRepos.add("snapshots");
389 SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
391 searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
393 searchControl.replay();
395 String result = action.filteredSearch();
397 assertEquals(Action.INPUT, result);
398 assertFalse(action.getActionErrors().isEmpty());
399 assertEquals("No results found", (String) action.getActionErrors().iterator().next());
401 searchControl.verify();
405 public void testAdvancedSearchUserHasNoAccessToAnyRepository()
408 List<String> managedRepos = new ArrayList<String>();
410 action.setGroupId("org.apache.archiva");
411 action.setManagedRepositoryList(managedRepos);
413 String result = action.filteredSearch();
415 assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
419 public void testAdvancedSearchNoSpecifiedCriteria()
422 List<String> managedRepos = new ArrayList<String>();
424 action.setManagedRepositoryList(managedRepos);
426 String result = action.filteredSearch();
428 assertEquals(Action.INPUT, result);
429 assertFalse(action.getActionErrors().isEmpty());
430 assertEquals("Advanced Search - At least one search criteria must be provided.",
431 (String) action.getActionErrors().iterator().next());
436 public void testFindArtifactWithOneHit()
439 action.setQ(TEST_CHECKSUM);
441 MockControl control = MockControl.createControl(MetadataRepository.class);
442 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
443 when(session.getRepository()).thenReturn(metadataRepository);
445 ArtifactMetadata artifact = createArtifact("archiva-configuration", "1.0");
446 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
447 Collections.singletonList(artifact));
449 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
450 Collections.singletonList(TEST_REPO));
453 userReposControl.replay();
455 String result = action.findArtifact();
456 assertEquals("artifact", result);
457 assertEquals(1, action.getDatabaseResults().size());
458 assertEquals(artifact, action.getDatabaseResults().get(0));
461 userReposControl.verify();
465 public void testFindArtifactWithMultipleHits()
468 action.setQ(TEST_CHECKSUM);
470 MockControl control = MockControl.createControl(MetadataRepository.class);
471 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
472 when(session.getRepository()).thenReturn(metadataRepository);
474 List<ArtifactMetadata> artifacts =
475 Arrays.asList(createArtifact("archiva-configuration", "1.0"), createArtifact("archiva-indexer", "1.0"));
476 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM), artifacts);
478 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
479 Collections.singletonList(TEST_REPO));
482 userReposControl.replay();
484 String result = action.findArtifact();
485 assertEquals("results", result);
486 assertFalse(action.getDatabaseResults().isEmpty());
487 assertEquals(2, action.getDatabaseResults().size());
490 userReposControl.verify();
494 public void testFindArtifactNoChecksumSpecified()
497 String result = action.findArtifact();
499 assertEquals(Action.INPUT, result);
500 assertFalse(action.getActionErrors().isEmpty());
501 assertEquals("Unable to search for a blank checksum", (String) action.getActionErrors().iterator().next());
505 public void testFindArtifactNoResults()
508 action.setQ(TEST_CHECKSUM);
510 MockControl control = MockControl.createControl(MetadataRepository.class);
511 MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
512 when(session.getRepository()).thenReturn(metadataRepository);
514 control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
515 Collections.<ArtifactMetadata>emptyList());
517 userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
518 Collections.singletonList(TEST_REPO));
521 userReposControl.replay();
523 String result = action.findArtifact();
524 assertEquals(Action.INPUT, result);
525 assertFalse(action.getActionErrors().isEmpty());
526 assertEquals("No results found", (String) action.getActionErrors().iterator().next());
529 userReposControl.verify();
532 private ArtifactMetadata createArtifact(String project, String version)
534 ArtifactMetadata metadata = new ArtifactMetadata();
535 metadata.setNamespace("org.apache.archiva");
536 metadata.setProject(project);
537 metadata.setProjectVersion(version);
538 metadata.setVersion(version);
539 metadata.setRepositoryId(TEST_REPO);
540 metadata.setId(project + "-" + version + ".jar");