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