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