]> source.dussan.org Git - archiva.git/blob
38c0305cc108aac93e2d56463b1e43e6d2bb948c
[archiva.git] /
1 package org.apache.archiva.web.action;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
37
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;
43
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
47 /**
48  *
49  */
50 public class SearchActionTest
51     extends AbstractActionTestCase
52 {
53     private SearchAction action;
54
55     private MockControl userReposControl;
56
57     private UserRepositories userRepos;
58
59     private MockControl searchControl;
60
61     private MockControl repoAdminControl;
62
63     private ManagedRepositoryAdmin managedRepositoryAdmin;
64
65     private RepositorySearch search;
66
67     private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1";
68
69     private static final String TEST_REPO = "test-repo";
70
71     private static final String GUEST = "guest";
72
73     private RepositorySession session;
74
75     @Override
76     public void setUp()
77         throws Exception
78     {
79         super.setUp();
80
81         action = new SearchAction();
82
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);
88
89         MockControl archivaConfigControl = MockControl.createControl(ArchivaConfiguration.class);
90         ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
91
92         userReposControl = MockControl.createControl(UserRepositories.class);
93         userRepos = (UserRepositories) userReposControl.getMock();
94
95         searchControl = MockControl.createControl(RepositorySearch.class);
96         searchControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
97         search = (RepositorySearch) searchControl.getMock();
98
99         repoAdminControl = MockControl.createControl(ManagedRepositoryAdmin.class);
100         managedRepositoryAdmin = (ManagedRepositoryAdmin) repoAdminControl.getMock();
101
102         //( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfig );
103
104         action.setManagedRepositoryAdmin(managedRepositoryAdmin);
105         action.setUserRepositories(userRepos);
106         action.setNexusSearch(search);
107     }
108
109     // quick search...
110
111     @Test
112     public void testQuickSearch()
113         throws Exception
114     {
115         action.setQ("archiva");
116         action.setCurrentPage(0);
117         action.setSearchResultsOnly(false);
118         action.setCompleteQueryString("");
119
120         List<String> selectedRepos = new ArrayList<String>();
121         selectedRepos.add("internal");
122         selectedRepos.add("snapshots");
123
124         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
125         limits.setPageSize(30);
126
127         SearchResultHit hit = new SearchResultHit();
128         hit.setGroupId("org.apache.archiva");
129         hit.setArtifactId("archiva-configuration");
130         hit.setUrl("url");
131         hit.addVersion("1.0");
132         hit.addVersion("1.1");
133
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() );
139
140         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
141
142         searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
143
144         userReposControl.replay();
145         searchControl.replay();
146
147         action.setPrincipal("user");
148         String result = action.quickSearch();
149
150         assertEquals(Action.SUCCESS, result);
151         assertEquals("total pages not 1", 1, action.getTotalPages());
152         assertEquals("totol hits not 1", 1, action.getResults().getTotalHits());
153
154         userReposControl.verify();
155         searchControl.verify();
156     }
157
158     @Test
159     public void testSearchWithinSearchResults()
160         throws Exception
161     {
162         action.setQ("archiva");
163         action.setCurrentPage(0);
164         action.setSearchResultsOnly(true);
165         action.setCompleteQueryString("org;apache");
166
167         List<String> parsed = new ArrayList<String>();
168         parsed.add("org");
169         parsed.add("apache");
170
171         List<String> selectedRepos = new ArrayList<String>();
172         selectedRepos.add("internal");
173         selectedRepos.add("snapshots");
174
175         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
176         limits.setPageSize(30);
177
178         SearchResultHit hit = new SearchResultHit();
179         hit.setGroupId("org.apache.archiva");
180         hit.setArtifactId("archiva-configuration");
181         hit.setUrl("url");
182         hit.addVersion("1.0");
183         hit.addVersion("1.1");
184
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);
191
192         searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, parsed), results);
193
194         userReposControl.replay();
195         searchControl.replay();
196
197         action.setPrincipal("user");
198         String result = action.quickSearch();
199
200         assertEquals(Action.SUCCESS, result);
201         assertEquals("org;apache;archiva", action.getCompleteQueryString());
202         assertEquals(1, action.getTotalPages());
203         assertEquals(1, action.getResults().getTotalHits());
204
205         userReposControl.verify();
206         searchControl.verify();
207     }
208
209     @Test
210     public void testQuickSearchUserHasNoAccessToAnyRepository()
211         throws Exception
212     {
213         action.setQ("archiva");
214         action.setCurrentPage(0);
215
216         List<String> selectedRepos = new ArrayList<String>();
217
218         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
219
220         userReposControl.replay();
221
222         action.setPrincipal("user");
223         String result = action.quickSearch();
224
225         assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
226
227         userReposControl.verify();
228     }
229
230     @Test
231     public void testQuickSearchNoSearchHits()
232         throws Exception
233     {
234         action.setQ("archiva");
235         action.setCurrentPage(0);
236         action.setSearchResultsOnly(false);
237         action.setCompleteQueryString("");
238
239         List<String> selectedRepos = new ArrayList<String>();
240         selectedRepos.add("internal");
241         selectedRepos.add("snapshots");
242
243         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
244         limits.setPageSize(30);
245
246         SearchResults results = new SearchResults();
247
248         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
249
250         searchControl.expectAndReturn(search.search("user", selectedRepos, "archiva", limits, null), results);
251
252         userReposControl.replay();
253         searchControl.replay();
254
255         action.setPrincipal("user");
256         String result = action.quickSearch();
257
258         assertEquals(Action.INPUT, result);
259
260         userReposControl.verify();
261         searchControl.verify();
262     }
263
264     // advanced/filtered search...
265
266     @Test
267     public void testAdvancedSearchOneRepository()
268         throws Exception
269     {
270         List<String> managedRepos = new ArrayList<String>();
271         managedRepos.add("internal");
272         managedRepos.add("snapshots");
273
274         action.setRepositoryId("internal");
275         action.setManagedRepositoryList(managedRepos);
276         action.setCurrentPage(0);
277         action.setRowCount(30);
278         action.setGroupId("org");
279
280         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
281         limits.setPageSize(30);
282
283         SearchResultHit hit = new SearchResultHit();
284         hit.setGroupId("org.apache.archiva");
285         hit.setArtifactId("archiva-configuration");
286         hit.setUrl("url");
287         hit.addVersion("1.0");
288         hit.addVersion("1.1");
289
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);
294
295         List<String> selectedRepos = new ArrayList<String>();
296         selectedRepos.add("internal");
297         selectedRepos.add("snapshots");
298
299         SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
300
301         searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
302
303         searchControl.replay();
304
305         String result = action.filteredSearch();
306
307         assertEquals(Action.SUCCESS, result);
308         assertEquals(1, action.getTotalPages());
309         assertEquals(1, action.getResults().getTotalHits());
310
311         searchControl.verify();
312     }
313
314     @Test
315     public void testAdvancedSearchAllRepositories()
316         throws Exception
317     {
318         List<String> managedRepos = new ArrayList<String>();
319         managedRepos.add("internal");
320         managedRepos.add("snapshots");
321
322         action.setRepositoryId("all");
323         action.setManagedRepositoryList(managedRepos);
324         action.setCurrentPage(0);
325         action.setRowCount(30);
326         action.setGroupId("org");
327
328         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
329         limits.setPageSize(30);
330
331         SearchResultHit hit = new SearchResultHit();
332         hit.setGroupId("org.apache.archiva");
333         hit.setArtifactId("archiva-configuration");
334         hit.setUrl("url");
335         hit.addVersion("1.0");
336         hit.addVersion("1.1");
337
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);
342
343         List<String> selectedRepos = new ArrayList<String>();
344         selectedRepos.add("internal");
345
346         SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
347
348         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds("user"), selectedRepos);
349
350         searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
351
352         searchControl.replay();
353         userReposControl.replay();
354
355         action.setPrincipal("user");
356         String result = action.filteredSearch();
357
358         assertEquals(Action.SUCCESS, result);
359         assertEquals(1, action.getTotalPages());
360         assertEquals(1, action.getResults().getTotalHits());
361
362         searchControl.verify();
363         userReposControl.verify();
364     }
365
366     @Test
367     public void testAdvancedSearchNoSearchHits()
368         throws Exception
369     {
370         List<String> managedRepos = new ArrayList<String>();
371         managedRepos.add("internal");
372         managedRepos.add("snapshots");
373
374         action.setRepositoryId("internal");
375         action.setManagedRepositoryList(managedRepos);
376         action.setCurrentPage(0);
377         action.setRowCount(30);
378         action.setGroupId("org");
379
380         SearchResultLimits limits = new SearchResultLimits(action.getCurrentPage());
381         limits.setPageSize(30);
382
383         SearchResults results = new SearchResults();
384
385         List<String> selectedRepos = new ArrayList<String>();
386         selectedRepos.add("internal");
387         selectedRepos.add("snapshots");
388
389         SearchFields searchFields = new SearchFields("org", null, null, null, null, selectedRepos);
390
391         searchControl.expectAndReturn(search.search("user", searchFields, limits), results);
392
393         searchControl.replay();
394
395         String result = action.filteredSearch();
396
397         assertEquals(Action.INPUT, result);
398         assertFalse(action.getActionErrors().isEmpty());
399         assertEquals("No results found", (String) action.getActionErrors().iterator().next());
400
401         searchControl.verify();
402     }
403
404     @Test
405     public void testAdvancedSearchUserHasNoAccessToAnyRepository()
406         throws Exception
407     {
408         List<String> managedRepos = new ArrayList<String>();
409
410         action.setGroupId("org.apache.archiva");
411         action.setManagedRepositoryList(managedRepos);
412
413         String result = action.filteredSearch();
414
415         assertEquals(GlobalResults.ACCESS_TO_NO_REPOS, result);
416     }
417
418     @Test
419     public void testAdvancedSearchNoSpecifiedCriteria()
420         throws Exception
421     {
422         List<String> managedRepos = new ArrayList<String>();
423
424         action.setManagedRepositoryList(managedRepos);
425
426         String result = action.filteredSearch();
427
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());
432     }
433
434     // find artifact..
435     @Test
436     public void testFindArtifactWithOneHit()
437         throws Exception
438     {
439         action.setQ(TEST_CHECKSUM);
440
441         MockControl control = MockControl.createControl(MetadataRepository.class);
442         MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
443         when(session.getRepository()).thenReturn(metadataRepository);
444
445         ArtifactMetadata artifact = createArtifact("archiva-configuration", "1.0");
446         control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
447                                 Collections.singletonList(artifact));
448
449         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
450                                          Collections.singletonList(TEST_REPO));
451
452         control.replay();
453         userReposControl.replay();
454
455         String result = action.findArtifact();
456         assertEquals("artifact", result);
457         assertEquals(1, action.getDatabaseResults().size());
458         assertEquals(artifact, action.getDatabaseResults().get(0));
459
460         control.verify();
461         userReposControl.verify();
462     }
463
464     @Test
465     public void testFindArtifactWithMultipleHits()
466         throws Exception
467     {
468         action.setQ(TEST_CHECKSUM);
469
470         MockControl control = MockControl.createControl(MetadataRepository.class);
471         MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
472         when(session.getRepository()).thenReturn(metadataRepository);
473
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);
477
478         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
479                                          Collections.singletonList(TEST_REPO));
480
481         control.replay();
482         userReposControl.replay();
483
484         String result = action.findArtifact();
485         assertEquals("results", result);
486         assertFalse(action.getDatabaseResults().isEmpty());
487         assertEquals(2, action.getDatabaseResults().size());
488
489         control.verify();
490         userReposControl.verify();
491     }
492
493     @Test
494     public void testFindArtifactNoChecksumSpecified()
495         throws Exception
496     {
497         String result = action.findArtifact();
498
499         assertEquals(Action.INPUT, result);
500         assertFalse(action.getActionErrors().isEmpty());
501         assertEquals("Unable to search for a blank checksum", (String) action.getActionErrors().iterator().next());
502     }
503
504     @Test
505     public void testFindArtifactNoResults()
506         throws Exception
507     {
508         action.setQ(TEST_CHECKSUM);
509
510         MockControl control = MockControl.createControl(MetadataRepository.class);
511         MetadataRepository metadataRepository = (MetadataRepository) control.getMock();
512         when(session.getRepository()).thenReturn(metadataRepository);
513
514         control.expectAndReturn(metadataRepository.getArtifactsByChecksum(TEST_REPO, TEST_CHECKSUM),
515                                 Collections.<ArtifactMetadata>emptyList());
516
517         userReposControl.expectAndReturn(userRepos.getObservableRepositoryIds(GUEST),
518                                          Collections.singletonList(TEST_REPO));
519
520         control.replay();
521         userReposControl.replay();
522
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());
527
528         control.verify();
529         userReposControl.verify();
530     }
531
532     private ArtifactMetadata createArtifact(String project, String version)
533     {
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");
541         return metadata;
542     }
543 }