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.metadata.repository.memory.TestMetadataResolver;
27 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 public class BrowseActionTest
37 extends AbstractActionTestCase
39 private static final String ACTION_HINT = "browseAction";
41 private BrowseAction action;
43 private static final List<String> GROUPS =
44 Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
47 private static final String OTHER_TEST_REPO = "other-repo";
49 public void testInstantiation()
51 assertFalse( action == (BrowseAction) getActionProxy( "/browse.action" ).getAction() );
54 public void testBrowse()
57 metadataResolver.setNamespaces( TEST_REPO, GROUPS );
59 String result = action.browse();
60 assertSuccessResult( result );
62 assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
63 assertNull( action.getProjectIds() );
64 assertNull( action.getProjectVersions() );
66 assertNull( action.getGroupId() );
67 assertNull( action.getArtifactId() );
68 assertNull( action.getRepositoryId() );
69 assertNull( action.getSharedModel() );
72 public void testBrowseNoObservableRepos()
75 setObservableRepos( Collections.<String>emptyList() );
77 String result = action.browse();
78 assertNoAccessResult( result );
80 assertNoOutputVariables();
83 public void testBrowseGroupNoObservableRepos()
86 setObservableRepos( Collections.<String>emptyList() );
87 String selectedGroupId = "org";
89 action.setGroupId( selectedGroupId );
90 String result = action.browseGroup();
91 assertNoAccessResult( result );
93 assertEquals( selectedGroupId, action.getGroupId() );
94 assertNull( action.getNamespaces() );
95 assertNull( action.getProjectIds() );
96 assertNull( action.getProjectVersions() );
97 assertNull( action.getArtifactId() );
98 assertNull( action.getRepositoryId() );
99 assertNull( action.getSharedModel() );
102 public void testBrowseArtifactNoObservableRepos()
105 setObservableRepos( Collections.<String>emptyList() );
106 String selectedGroupId = "org.apache";
107 String selectedArtifactId = "apache";
109 action.setGroupId( selectedGroupId );
110 action.setArtifactId( selectedArtifactId );
111 String result = action.browseArtifact();
112 assertNoAccessResult( result );
114 assertEquals( selectedGroupId, action.getGroupId() );
115 assertEquals( selectedArtifactId, action.getArtifactId() );
116 assertNull( action.getNamespaces() );
117 assertNull( action.getProjectIds() );
118 assertNull( action.getProjectVersions() );
119 assertNull( action.getRepositoryId() );
120 assertNull( action.getSharedModel() );
123 public void testBrowseGroupNoGroupId()
126 String result = action.browseGroup();
127 assertErrorResult( result );
128 assertNoOutputVariables();
131 public void testBrowseGroupNoArtifacts()
134 String selectedGroupId = "org";
135 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
137 metadataResolver.setNamespaces( TEST_REPO, groups );
138 action.setGroupId( selectedGroupId );
139 String result = action.browseGroup();
140 assertSuccessResult( result );
142 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
143 assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
144 assertNull( action.getProjectVersions() );
146 assertEquals( selectedGroupId, action.getGroupId() );
147 assertNull( action.getArtifactId() );
148 assertNull( action.getRepositoryId() );
149 assertNull( action.getSharedModel() );
152 public void testBrowseGroupWithArtifacts()
155 String artifacts = "apache";
156 String selectedGroupId = "org.apache";
157 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
159 metadataResolver.setNamespaces( TEST_REPO, groups );
160 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
161 action.setGroupId( selectedGroupId );
162 String result = action.browseGroup();
163 assertSuccessResult( result );
165 assertEquals( groups, action.getNamespaces() );
166 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
167 assertNull( action.getProjectVersions() );
169 assertEquals( selectedGroupId, action.getGroupId() );
170 assertNull( action.getArtifactId() );
171 assertNull( action.getRepositoryId() );
172 assertNull( action.getSharedModel() );
175 public void testBrowseWithCollapsedGroupsAndArtifacts()
178 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
180 metadataResolver.setNamespaces( TEST_REPO, groups );
181 // add an artifact in the tree to make sure "single" is not collapsed
182 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
184 String result = action.browse();
185 assertSuccessResult( result );
187 assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
188 assertNull( action.getProjectIds() );
189 assertNull( action.getProjectVersions() );
191 assertNull( action.getGroupId() );
192 assertNull( action.getArtifactId() );
193 assertNull( action.getRepositoryId() );
194 assertNull( action.getSharedModel() );
197 public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
200 setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
202 metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
203 metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
205 // add an artifact in the tree to make sure "single" is not collapsed
206 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
208 String result = action.browse();
209 assertSuccessResult( result );
211 assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
212 assertNull( action.getProjectIds() );
213 assertNull( action.getProjectVersions() );
215 assertNull( action.getGroupId() );
216 assertNull( action.getArtifactId() );
217 assertNull( action.getRepositoryId() );
218 assertNull( action.getSharedModel() );
221 public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
224 String artifacts = "apache";
225 String selectedGroupId = "org.apache";
226 List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
228 metadataResolver.setNamespaces( TEST_REPO, groups );
229 // add an artifact in the tree to make sure "single" is not collapsed
230 metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
232 action.setGroupId( selectedGroupId );
233 String result = action.browseGroup();
234 assertSuccessResult( result );
236 assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
237 assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
238 assertNull( action.getProjectVersions() );
240 assertEquals( selectedGroupId, action.getGroupId() );
241 assertNull( action.getArtifactId() );
242 assertNull( action.getRepositoryId() );
243 assertNull( action.getSharedModel() );
246 public void testBrowseArtifactNoGroupId()
249 String selectedArtifactId = "apache";
251 action.setArtifactId( selectedArtifactId );
252 String result = action.browseArtifact();
253 assertErrorResult( result );
255 assertNull( action.getNamespaces() );
256 assertNull( action.getProjectIds() );
257 assertNull( action.getProjectVersions() );
258 assertNull( action.getGroupId() );
259 assertEquals( selectedArtifactId, action.getArtifactId() );
260 assertNull( action.getRepositoryId() );
261 assertNull( action.getSharedModel() );
264 public void testBrowseArtifactNoArtifactId()
267 String selectedGroupId = "org.apache";
269 action.setGroupId( selectedGroupId );
270 String result = action.browseArtifact();
271 assertErrorResult( result );
273 assertNull( action.getNamespaces() );
274 assertNull( action.getProjectIds() );
275 assertNull( action.getProjectVersions() );
276 assertEquals( selectedGroupId, action.getGroupId() );
277 assertNull( action.getArtifactId() );
278 assertNull( action.getRepositoryId() );
279 assertNull( action.getSharedModel() );
282 public void testBrowseArtifact()
286 String selectedGroupId = "org.apache";
287 String selectedArtifactId = "apache";
289 List<String> versions = Arrays.asList( "1", "2", "3", "4" );
290 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
291 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
292 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
293 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
294 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
295 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
296 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
297 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
299 action.setGroupId( selectedGroupId );
300 action.setArtifactId( selectedArtifactId );
301 String result = action.browseArtifact();
302 assertSuccessResult( result );
304 assertEquals( selectedGroupId, action.getGroupId() );
305 assertEquals( selectedArtifactId, action.getArtifactId() );
306 assertNull( action.getRepositoryId() );
308 assertNull( action.getNamespaces() );
309 assertNull( action.getProjectIds() );
310 assertEquals( versions, action.getProjectVersions() );
312 ProjectVersionMetadata model = action.getSharedModel();
313 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
316 public void testBrowseArtifactWithSnapshots()
320 String selectedGroupId = "org.apache";
321 String selectedArtifactId = "apache";
323 List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
324 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
325 createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
326 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
327 createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
328 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
329 createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
330 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
331 createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
332 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
333 createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
334 metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
335 createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
337 action.setGroupId( selectedGroupId );
338 action.setArtifactId( selectedArtifactId );
339 String result = action.browseArtifact();
340 assertSuccessResult( result );
342 assertEquals( selectedGroupId, action.getGroupId() );
343 assertEquals( selectedArtifactId, action.getArtifactId() );
344 assertNull( action.getRepositoryId() );
346 assertNull( action.getNamespaces() );
347 assertNull( action.getProjectIds() );
348 assertEquals( versions, action.getProjectVersions() );
350 ProjectVersionMetadata model = action.getSharedModel();
351 assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
354 // TODO: test with restricted observable repos
355 // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
356 // Need to update the tests to verify this as models are currently the same
358 private void assertNoAccessResult( String result )
360 assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
361 assertEquals( 0, action.getActionErrors().size() );
362 assertEquals( 0, action.getActionMessages().size() );
365 private void assertSuccessResult( String result )
367 assertEquals( Action.SUCCESS, result );
368 assertEquals( 0, action.getActionErrors().size() );
369 assertEquals( 0, action.getActionMessages().size() );
372 private void assertErrorResult( String result )
374 assertEquals( Action.ERROR, result );
375 assertEquals( 1, action.getActionErrors().size() );
376 assertEquals( 0, action.getActionMessages().size() );
379 private void assertNoOutputVariables()
381 assertNull( action.getNamespaces() );
382 assertNull( action.getProjectIds() );
383 assertNull( action.getProjectVersions() );
384 assertNull( action.getGroupId() );
385 assertNull( action.getArtifactId() );
386 assertNull( action.getRepositoryId() );
387 assertNull( action.getSharedModel() );
390 protected void setUp()
394 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
395 //action = (BrowseAction) lookup( Action.class, ACTION_HINT );
396 action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
397 metadataResolver = new TestMetadataResolver();
398 RepositorySession repositorySession = mock( RepositorySession.class );
399 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
400 TestRepositorySessionFactory factory =
401 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
402 factory.setRepositorySession( repositorySession );
405 protected void tearDown()
409 setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );