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 java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.apache.archiva.indexer.search.RepositorySearch;
27 import org.apache.archiva.indexer.util.SearchUtil;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.database.ArchivaDAO;
30 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
31 import org.apache.maven.archiva.indexer.search.SearchResultHit;
32 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
33 import org.apache.maven.archiva.indexer.search.SearchResults;
34 import org.apache.maven.archiva.security.ArchivaXworkUser;
35 import org.apache.maven.archiva.security.UserRepositories;
36 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
37 import org.easymock.MockControl;
38 import org.easymock.classextension.MockClassControl;
40 import com.opensymphony.xwork2.Action;
45 public class SearchActionTest
46 extends PlexusInSpringTestCase
48 private SearchAction action;
50 private MockControl archivaConfigControl;
52 private ArchivaConfiguration archivaConfig;
54 private MockControl daoControl;
56 private ArchivaDAO dao;
58 private MockControl userReposControl;
60 private UserRepositories userRepos;
62 private MockControl archivaXworkUserControl;
64 private ArchivaXworkUser archivaXworkUser;
66 private MockControl searchControl;
68 private RepositorySearch search;
71 protected void setUp()
76 action = new SearchAction();
78 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
80 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
82 daoControl = MockControl.createControl( ArchivaDAO.class );
83 daoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
85 dao = ( ArchivaDAO ) daoControl.getMock();
87 userReposControl = MockControl.createControl( UserRepositories.class );
89 userRepos = ( UserRepositories ) userReposControl.getMock();
91 archivaXworkUserControl = MockClassControl.createControl( ArchivaXworkUser.class );
92 archivaXworkUserControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
94 archivaXworkUser = ( ArchivaXworkUser ) archivaXworkUserControl.getMock();
96 searchControl = MockControl.createControl( RepositorySearch.class );
97 searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
99 search = ( RepositorySearch ) searchControl.getMock();
101 action.setArchivaConfiguration( archivaConfig );
102 action.setArchivaXworkUser( archivaXworkUser );
103 action.setUserRepositories( userRepos );
104 action.setDao( dao );
105 action.setNexusSearch( search );
109 protected void tearDown()
115 public void testQuickSearch()
118 action.setQ( "archiva" );
119 action.setCurrentPage( 0 );
120 action.setSearchResultsOnly( false );
121 action.setCompleteQueryString( "" );
123 List<String> selectedRepos = new ArrayList<String>();
124 selectedRepos.add( "internal" );
125 selectedRepos.add( "snapshots" );
127 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
128 limits.setPageSize( 30 );
130 SearchResultHit hit = new SearchResultHit();
131 hit.setGroupId( "org.apache.archiva" );
132 hit.setArtifactId( "archiva-configuration" );
134 hit.addVersion( "1.0" );
135 hit.addVersion( "1.1" );
137 SearchResults results = new SearchResults();
138 results.setLimits( limits );
139 results.setTotalHits( 1 );
140 results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit );
142 List<String> versions = new ArrayList<String>();
143 versions.add( "1.0" );
144 versions.add( "1.1" );
146 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 3 );
148 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 );
150 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
152 daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions );
154 archivaXworkUserControl.replay();
155 userReposControl.replay();
156 searchControl.replay();
159 String result = action.quickSearch();
161 assertEquals( Action.SUCCESS, result );
163 archivaXworkUserControl.verify();
164 userReposControl.verify();
165 searchControl.verify();
169 public void testSearchWithinSearchResults()
172 // test filter of completeQueryString?
173 // test no need to filter completeQueryString?
176 public void testAdvancedSearch()
182 public void testSearchUserHasNoAccessToAnyRepository()
185 action.setQ( "archiva" );
186 action.setCurrentPage( 0 );
188 List<String> selectedRepos = new ArrayList<String>();
190 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user" );
192 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
194 archivaXworkUserControl.replay();
195 userReposControl.replay();
197 String result = action.quickSearch();
199 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
201 archivaXworkUserControl.verify();
202 userReposControl.verify();
205 public void testNoSearchHits()
208 action.setQ( "archiva" );
209 action.setCurrentPage( 0 );
210 action.setSearchResultsOnly( false );
211 action.setCompleteQueryString( "" );
213 List<String> selectedRepos = new ArrayList<String>();
214 selectedRepos.add( "internal" );
215 selectedRepos.add( "snapshots" );
217 SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() );
218 limits.setPageSize( 30 );
220 SearchResults results = new SearchResults();
222 archivaXworkUserControl.expectAndReturn( archivaXworkUser.getActivePrincipal( new HashMap() ), "user", 2 );
224 userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos );
226 searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results );
228 archivaXworkUserControl.replay();
229 userReposControl.replay();
230 searchControl.replay();
232 String result = action.quickSearch();
234 assertEquals( Action.INPUT, result );
236 archivaXworkUserControl.verify();
237 userReposControl.verify();
238 searchControl.verify();
241 // test pagination or just totalPages?
242 public void testPagination()
248 public void testFindArtifactWithOneHit()
254 public void testFindArtifactWithMultipleHits()
260 public void testFindArtifactNoChecksumSpecified()