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.google.common.collect.Lists;
23 import com.opensymphony.xwork2.Action;
24 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.webtest.memory.TestMetadataResolver;
27 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
28 import org.apache.archiva.scheduler.indexing.DefaultDownloadRemoteIndexScheduler;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import org.junit.Test;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 public class BrowseActionTest
39 extends AbstractActionTestCase
41 private static final String ACTION_HINT = "browseAction";
43 private BrowseAction action;
45 private static final List<String> GROUPS =
46 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
49 private static final String OTHER_TEST_REPO = "other-repo";
56 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
57 action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
58 metadataResolver = new TestMetadataResolver();
59 RepositorySession repositorySession = mock( RepositorySession.class );
60 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
61 TestRepositorySessionFactory factory =
62 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
63 factory.setRepositorySession( repositorySession );
67 public void tearDown()
71 applicationContext.getBean( DefaultDownloadRemoteIndexScheduler.class ).shutdown();
72 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
76 public void testInstantiation()
78 assertFalse( action == getActionProxy( "/browse.action" ).getAction() );
82 public void testBrowse()
85 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
87 String result = action.browse();
88 assertSuccessResult( result );
90 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
91 assertNull( action.getProjectIds() );
92 assertNull( action.getProjectVersions() );
94 assertNull( action.getGroupId() );
95 assertNull( action.getArtifactId() );
96 assertNull( action.getRepositoryId() );
97 assertNull( action.getSharedModel() );
101 public void testBrowseNoObservableRepos()
104 setObservableRepos( Collections.<String>emptyList() );
106 String result = action.browse();
107 assertNoAccessResult( result );
109 assertNoOutputVariables();
113 public void testBrowseGroupNoObservableRepos()
116 setObservableRepos( Collections.<String>emptyList() );
117 String selectedGroupId = "org";
119 action.setGroupId( selectedGroupId );
120 String result = action.browseGroup();
121 assertNoAccessResult( result );
123 assertEquals( selectedGroupId, action.getGroupId() );
124 assertNull( action.getNamespaces() );
125 assertNull( action.getProjectIds() );
126 assertNull( action.getProjectVersions() );
127 assertNull( action.getArtifactId() );
128 assertNull( action.getRepositoryId() );
129 assertNull( action.getSharedModel() );
133 public void testBrowseArtifactNoObservableRepos()
136 setObservableRepos( Collections.<String>emptyList() );
137 String selectedGroupId = "org.apache";
138 String selectedArtifactId = "apache";
140 action.setGroupId( selectedGroupId );
141 action.setArtifactId( selectedArtifactId );
142 String result = action.browseArtifact();
143 assertNoAccessResult( result );
145 assertEquals( selectedGroupId, action.getGroupId() );
146 assertEquals( selectedArtifactId, action.getArtifactId() );
147 assertNull( action.getNamespaces() );
148 assertNull( action.getProjectIds() );
149 assertNull( action.getProjectVersions() );
150 assertNull( action.getRepositoryId() );
151 assertNull( action.getSharedModel() );
155 public void testBrowseGroupNoGroupId()
158 String result = action.browseGroup();
159 assertErrorResult( result );
160 assertNoOutputVariables();
164 public void testBrowseGroupNoArtifacts()
167 String selectedGroupId = "org";
168 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
170 metadataResolver.setNamespaces( TEST_REPO, groups );
171 action.setGroupId( selectedGroupId );
172 String result = action.browseGroup();
173 assertSuccessResult( result );
175 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
176 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
177 assertNull( action.getProjectVersions() );
179 assertEquals( selectedGroupId, action.getGroupId() );
180 assertNull( action.getArtifactId() );
181 assertNull( action.getRepositoryId() );
182 assertNull( action.getSharedModel() );
186 public void testBrowseGroupWithArtifacts()
189 String artifacts = "apache";
190 String selectedGroupId = "org.apache";
191 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
193 metadataResolver.setNamespaces( TEST_REPO, groups );
194 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
195 action.setGroupId( selectedGroupId );
196 String result = action.browseGroup();
197 assertSuccessResult( result );
199 assertEquals( groups, action.getNamespaces() );
200 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
201 assertNull( action.getProjectVersions() );
203 assertEquals( selectedGroupId, action.getGroupId() );
204 assertNull( action.getArtifactId() );
205 assertNull( action.getRepositoryId() );
206 assertNull( action.getSharedModel() );
210 public void testBrowseWithCollapsedGroupsAndArtifacts()
213 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
215 metadataResolver.setNamespaces( TEST_REPO, groups );
216 // add an artifact in the tree to make sure "single" is not collapsed
217 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
219 String result = action.browse();
220 assertSuccessResult( result );
222 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
223 assertNull( action.getProjectIds() );
224 assertNull( action.getProjectVersions() );
226 assertNull( action.getGroupId() );
227 assertNull( action.getArtifactId() );
228 assertNull( action.getRepositoryId() );
229 assertNull( action.getSharedModel() );
233 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
236 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
238 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
239 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
241 // add an artifact in the tree to make sure "single" is not collapsed
242 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
244 String result = action.browse();
245 assertSuccessResult( result );
247 assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
248 assertNull( action.getProjectIds() );
249 assertNull( action.getProjectVersions() );
251 assertNull( action.getGroupId() );
252 assertNull( action.getArtifactId() );
253 assertNull( action.getRepositoryId() );
254 assertNull( action.getSharedModel() );
258 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
261 String artifacts = "apache";
262 String selectedGroupId = "org.apache";
263 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
265 metadataResolver.setNamespaces( TEST_REPO, groups );
266 // add an artifact in the tree to make sure "single" is not collapsed
267 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
269 action.setGroupId( selectedGroupId );
270 String result = action.browseGroup();
271 assertSuccessResult( result );
273 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
274 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
275 assertNull( action.getProjectVersions() );
277 assertEquals( selectedGroupId, action.getGroupId() );
278 assertNull( action.getArtifactId() );
279 assertNull( action.getRepositoryId() );
280 assertNull( action.getSharedModel() );
284 public void testBrowseArtifactNoGroupId()
287 String selectedArtifactId = "apache";
289 action.setArtifactId( selectedArtifactId );
290 String result = action.browseArtifact();
291 assertErrorResult( result );
293 assertNull( action.getNamespaces() );
294 assertNull( action.getProjectIds() );
295 assertNull( action.getProjectVersions() );
296 assertNull( action.getGroupId() );
297 assertEquals( selectedArtifactId, action.getArtifactId() );
298 assertNull( action.getRepositoryId() );
299 assertNull( action.getSharedModel() );
303 public void testBrowseArtifactNoArtifactId()
306 String selectedGroupId = "org.apache";
308 action.setGroupId( selectedGroupId );
309 String result = action.browseArtifact();
310 assertErrorResult( result );
312 assertNull( action.getNamespaces() );
313 assertNull( action.getProjectIds() );
314 assertNull( action.getProjectVersions() );
315 assertEquals( selectedGroupId, action.getGroupId() );
316 assertNull( action.getArtifactId() );
317 assertNull( action.getRepositoryId() );
318 assertNull( action.getSharedModel() );
322 public void testBrowseArtifact()
326 String selectedGroupId = "org.apache";
327 String selectedArtifactId = "apache";
329 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
330 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
331 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
332 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
333 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
334 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
335 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
336 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
337 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
339 action.setGroupId( selectedGroupId );
340 action.setArtifactId( selectedArtifactId );
341 String result = action.browseArtifact();
342 assertSuccessResult( result );
344 assertEquals( selectedGroupId, action.getGroupId() );
345 assertEquals( selectedArtifactId, action.getArtifactId() );
346 assertNull( action.getRepositoryId() );
348 assertNull( action.getNamespaces() );
349 assertNull( action.getProjectIds() );
350 assertEquals( versions, action.getProjectVersions() );
352 ProjectVersionMetadata model = action.getSharedModel();
353 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
357 public void testBrowseArtifactWithSnapshots()
361 String selectedGroupId = "org.apache";
362 String selectedArtifactId = "apache";
364 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
365 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
366 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
367 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
368 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
369 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
370 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
371 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
372 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
373 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
374 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
375 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
376 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
378 action.setGroupId( selectedGroupId );
379 action.setArtifactId( selectedArtifactId );
380 String result = action.browseArtifact();
381 assertSuccessResult( result );
383 assertEquals( selectedGroupId, action.getGroupId() );
384 assertEquals( selectedArtifactId, action.getArtifactId() );
385 assertNull( action.getRepositoryId() );
387 assertNull( action.getNamespaces() );
388 assertNull( action.getProjectIds() );
389 assertEquals( versions, action.getProjectVersions() );
391 ProjectVersionMetadata model = action.getSharedModel();
392 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
395 // TODO: test with restricted observable repos
396 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
397 // Need to update the tests to verify this as models are currently the same
399 private void assertNoAccessResult( String result )
401 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
402 assertEquals( 0, action.getActionErrors().size() );
403 assertEquals( 0, action.getActionMessages().size() );
406 private void assertSuccessResult( String result )
408 assertEquals( Action.SUCCESS, result );
409 assertEquals( 0, action.getActionErrors().size() );
410 assertEquals( 0, action.getActionMessages().size() );
413 private void assertErrorResult( String result )
415 assertEquals( Action.ERROR, result );
416 assertEquals( 1, action.getActionErrors().size() );
417 assertEquals( 0, action.getActionMessages().size() );
420 private void assertNoOutputVariables()
422 assertNull( action.getNamespaces() );
423 assertNull( action.getProjectIds() );
424 assertNull( action.getProjectVersions() );
425 assertNull( action.getGroupId() );
426 assertNull( action.getArtifactId() );
427 assertNull( action.getRepositoryId() );
428 assertNull( action.getSharedModel() );