]> source.dussan.org Git - archiva.git/blob
7d9f45e30ca7c81c02d8b86c606637d590add211
[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.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;
28 import org.apache.archiva.scheduler.indexing.DefaultDownloadRemoteIndexScheduler;
29 import org.springframework.context.support.GenericApplicationContext;
30
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37
38 public class BrowseActionTest
39     extends AbstractActionTestCase
40 {
41     private static final String ACTION_HINT = "browseAction";
42
43     private BrowseAction action;
44
45     private static final List<String> GROUPS =
46         Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
47                        "repeat.repeat" );
48
49     private static final String OTHER_TEST_REPO = "other-repo";
50
51     protected void setUp()
52         throws Exception
53     {
54         super.setUp();
55         setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
56         action = (BrowseAction) getActionProxy( "/browse.action" ).getAction();
57         metadataResolver = new TestMetadataResolver();
58         RepositorySession repositorySession = mock( RepositorySession.class );
59         when( repositorySession.getResolver() ).thenReturn( metadataResolver );
60         TestRepositorySessionFactory factory =
61             applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
62         factory.setRepositorySession( repositorySession );
63     }
64
65     protected void tearDown()
66         throws Exception
67     {
68         super.tearDown();
69         applicationContext.getBean( DefaultDownloadRemoteIndexScheduler.class ).shutdown();
70         setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
71     }
72
73     public void testInstantiation()
74     {
75         assertFalse( action == getActionProxy( "/browse.action" ).getAction() );
76     }
77
78     public void testBrowse()
79         throws Exception
80     {
81         metadataResolver.setNamespaces( TEST_REPO, GROUPS );
82
83         String result = action.browse();
84         assertSuccessResult( result );
85
86         assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
87         assertNull( action.getProjectIds() );
88         assertNull( action.getProjectVersions() );
89
90         assertNull( action.getGroupId() );
91         assertNull( action.getArtifactId() );
92         assertNull( action.getRepositoryId() );
93         assertNull( action.getSharedModel() );
94     }
95
96     public void testBrowseNoObservableRepos()
97         throws Exception
98     {
99         setObservableRepos( Collections.<String>emptyList() );
100
101         String result = action.browse();
102         assertNoAccessResult( result );
103
104         assertNoOutputVariables();
105     }
106
107     public void testBrowseGroupNoObservableRepos()
108         throws Exception
109     {
110         setObservableRepos( Collections.<String>emptyList() );
111         String selectedGroupId = "org";
112
113         action.setGroupId( selectedGroupId );
114         String result = action.browseGroup();
115         assertNoAccessResult( result );
116
117         assertEquals( selectedGroupId, action.getGroupId() );
118         assertNull( action.getNamespaces() );
119         assertNull( action.getProjectIds() );
120         assertNull( action.getProjectVersions() );
121         assertNull( action.getArtifactId() );
122         assertNull( action.getRepositoryId() );
123         assertNull( action.getSharedModel() );
124     }
125
126     public void testBrowseArtifactNoObservableRepos()
127         throws Exception
128     {
129         setObservableRepos( Collections.<String>emptyList() );
130         String selectedGroupId = "org.apache";
131         String selectedArtifactId = "apache";
132
133         action.setGroupId( selectedGroupId );
134         action.setArtifactId( selectedArtifactId );
135         String result = action.browseArtifact();
136         assertNoAccessResult( result );
137
138         assertEquals( selectedGroupId, action.getGroupId() );
139         assertEquals( selectedArtifactId, action.getArtifactId() );
140         assertNull( action.getNamespaces() );
141         assertNull( action.getProjectIds() );
142         assertNull( action.getProjectVersions() );
143         assertNull( action.getRepositoryId() );
144         assertNull( action.getSharedModel() );
145     }
146
147     public void testBrowseGroupNoGroupId()
148         throws Exception
149     {
150         String result = action.browseGroup();
151         assertErrorResult( result );
152         assertNoOutputVariables();
153     }
154
155     public void testBrowseGroupNoArtifacts()
156         throws Exception
157     {
158         String selectedGroupId = "org";
159         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
160
161         metadataResolver.setNamespaces( TEST_REPO, groups );
162         action.setGroupId( selectedGroupId );
163         String result = action.browseGroup();
164         assertSuccessResult( result );
165
166         assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
167         assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
168         assertNull( action.getProjectVersions() );
169
170         assertEquals( selectedGroupId, action.getGroupId() );
171         assertNull( action.getArtifactId() );
172         assertNull( action.getRepositoryId() );
173         assertNull( action.getSharedModel() );
174     }
175
176     public void testBrowseGroupWithArtifacts()
177         throws Exception
178     {
179         String artifacts = "apache";
180         String selectedGroupId = "org.apache";
181         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
182
183         metadataResolver.setNamespaces( TEST_REPO, groups );
184         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, artifacts, new ProjectVersionMetadata() );
185         action.setGroupId( selectedGroupId );
186         String result = action.browseGroup();
187         assertSuccessResult( result );
188
189         assertEquals( groups, action.getNamespaces() );
190         assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
191         assertNull( action.getProjectVersions() );
192
193         assertEquals( selectedGroupId, action.getGroupId() );
194         assertNull( action.getArtifactId() );
195         assertNull( action.getRepositoryId() );
196         assertNull( action.getSharedModel() );
197     }
198
199     public void testBrowseWithCollapsedGroupsAndArtifacts()
200         throws Exception
201     {
202         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
203
204         metadataResolver.setNamespaces( TEST_REPO, groups );
205         // add an artifact in the tree to make sure "single" is not collapsed
206         metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
207
208         String result = action.browse();
209         assertSuccessResult( result );
210
211         assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
212         assertNull( action.getProjectIds() );
213         assertNull( action.getProjectVersions() );
214
215         assertNull( action.getGroupId() );
216         assertNull( action.getArtifactId() );
217         assertNull( action.getRepositoryId() );
218         assertNull( action.getSharedModel() );
219     }
220
221     public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
222         throws Exception
223     {
224         setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
225
226         metadataResolver.setNamespaces( TEST_REPO, Arrays.asList( "org.apache.archiva", "org.apache" ) );
227         metadataResolver.setNamespaces( OTHER_TEST_REPO, Arrays.asList( "org.codehaus.plexus", "org.codehaus" ) );
228
229         // add an artifact in the tree to make sure "single" is not collapsed
230         metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
231
232         String result = action.browse();
233         assertSuccessResult( result );
234
235         assertEquals( Collections.singletonList( "org" ), action.getNamespaces() );
236         assertNull( action.getProjectIds() );
237         assertNull( action.getProjectVersions() );
238
239         assertNull( action.getGroupId() );
240         assertNull( action.getArtifactId() );
241         assertNull( action.getRepositoryId() );
242         assertNull( action.getSharedModel() );
243     }
244
245     public void testBrowseGroupWithCollapsedGroupsAndArtifacts()
246         throws Exception
247     {
248         String artifacts = "apache";
249         String selectedGroupId = "org.apache";
250         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
251
252         metadataResolver.setNamespaces( TEST_REPO, groups );
253         // add an artifact in the tree to make sure "single" is not collapsed
254         metadataResolver.setProjectVersion( TEST_REPO, "org.apache", "apache", new ProjectVersionMetadata() );
255
256         action.setGroupId( selectedGroupId );
257         String result = action.browseGroup();
258         assertSuccessResult( result );
259
260         assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
261         assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
262         assertNull( action.getProjectVersions() );
263
264         assertEquals( selectedGroupId, action.getGroupId() );
265         assertNull( action.getArtifactId() );
266         assertNull( action.getRepositoryId() );
267         assertNull( action.getSharedModel() );
268     }
269
270     public void testBrowseArtifactNoGroupId()
271         throws Exception
272     {
273         String selectedArtifactId = "apache";
274
275         action.setArtifactId( selectedArtifactId );
276         String result = action.browseArtifact();
277         assertErrorResult( result );
278
279         assertNull( action.getNamespaces() );
280         assertNull( action.getProjectIds() );
281         assertNull( action.getProjectVersions() );
282         assertNull( action.getGroupId() );
283         assertEquals( selectedArtifactId, action.getArtifactId() );
284         assertNull( action.getRepositoryId() );
285         assertNull( action.getSharedModel() );
286     }
287
288     public void testBrowseArtifactNoArtifactId()
289         throws Exception
290     {
291         String selectedGroupId = "org.apache";
292
293         action.setGroupId( selectedGroupId );
294         String result = action.browseArtifact();
295         assertErrorResult( result );
296
297         assertNull( action.getNamespaces() );
298         assertNull( action.getProjectIds() );
299         assertNull( action.getProjectVersions() );
300         assertEquals( selectedGroupId, action.getGroupId() );
301         assertNull( action.getArtifactId() );
302         assertNull( action.getRepositoryId() );
303         assertNull( action.getSharedModel() );
304     }
305
306     public void testBrowseArtifact()
307         throws Exception
308
309     {
310         String selectedGroupId = "org.apache";
311         String selectedArtifactId = "apache";
312
313         List<String> versions = Arrays.asList( "1", "2", "3", "4" );
314         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
315                                             createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
316         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
317                                             createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
318         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
319                                             createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
320         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
321                                             createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
322
323         action.setGroupId( selectedGroupId );
324         action.setArtifactId( selectedArtifactId );
325         String result = action.browseArtifact();
326         assertSuccessResult( result );
327
328         assertEquals( selectedGroupId, action.getGroupId() );
329         assertEquals( selectedArtifactId, action.getArtifactId() );
330         assertNull( action.getRepositoryId() );
331
332         assertNull( action.getNamespaces() );
333         assertNull( action.getProjectIds() );
334         assertEquals( versions, action.getProjectVersions() );
335
336         ProjectVersionMetadata model = action.getSharedModel();
337         assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
338     }
339
340     public void testBrowseArtifactWithSnapshots()
341         throws Exception
342
343     {
344         String selectedGroupId = "org.apache";
345         String selectedArtifactId = "apache";
346
347         List<String> versions = Arrays.asList( "1", "2", "3", "4-SNAPSHOT", "4", "5-SNAPSHOT" );
348         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
349                                             createProjectModel( selectedGroupId, selectedArtifactId, "1" ) );
350         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
351                                             createProjectModel( selectedGroupId, selectedArtifactId, "2" ) );
352         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
353                                             createProjectModel( selectedGroupId, selectedArtifactId, "3" ) );
354         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
355                                             createProjectModel( selectedGroupId, selectedArtifactId, "4-SNAPSHOT" ) );
356         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
357                                             createProjectModel( selectedGroupId, selectedArtifactId, "4" ) );
358         metadataResolver.setProjectVersion( TEST_REPO, selectedGroupId, selectedArtifactId,
359                                             createProjectModel( selectedGroupId, selectedArtifactId, "5-SNAPSHOT" ) );
360
361         action.setGroupId( selectedGroupId );
362         action.setArtifactId( selectedArtifactId );
363         String result = action.browseArtifact();
364         assertSuccessResult( result );
365
366         assertEquals( selectedGroupId, action.getGroupId() );
367         assertEquals( selectedArtifactId, action.getArtifactId() );
368         assertNull( action.getRepositoryId() );
369
370         assertNull( action.getNamespaces() );
371         assertNull( action.getProjectIds() );
372         assertEquals( versions, action.getProjectVersions() );
373
374         ProjectVersionMetadata model = action.getSharedModel();
375         assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
376     }
377
378     // TODO: test with restricted observable repos
379     // TODO: current behaviour is to ignore values that differ between models - instead, pick the latest and use that.
380     //       Need to update the tests to verify this as models are currently the same
381
382     private void assertNoAccessResult( String result )
383     {
384         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
385         assertEquals( 0, action.getActionErrors().size() );
386         assertEquals( 0, action.getActionMessages().size() );
387     }
388
389     private void assertSuccessResult( String result )
390     {
391         assertEquals( Action.SUCCESS, result );
392         assertEquals( 0, action.getActionErrors().size() );
393         assertEquals( 0, action.getActionMessages().size() );
394     }
395
396     private void assertErrorResult( String result )
397     {
398         assertEquals( Action.ERROR, result );
399         assertEquals( 1, action.getActionErrors().size() );
400         assertEquals( 0, action.getActionMessages().size() );
401     }
402
403     private void assertNoOutputVariables()
404     {
405         assertNull( action.getNamespaces() );
406         assertNull( action.getProjectIds() );
407         assertNull( action.getProjectVersions() );
408         assertNull( action.getGroupId() );
409         assertNull( action.getArtifactId() );
410         assertNull( action.getRepositoryId() );
411         assertNull( action.getSharedModel() );
412     }
413
414
415 }