]> source.dussan.org Git - archiva.git/blob
ea9a976ad913821b6e256b8a8f2ebeaa104d98bf
[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
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 public class BrowseActionTest
37     extends AbstractActionTestCase
38 {
39     private static final String ACTION_HINT = "browseAction";
40
41     private BrowseAction action;
42
43     private static final List<String> GROUPS =
44         Arrays.asList( "org.apache.archiva", "commons-lang", "org.apache.maven", "com.sun", "com.oracle",
45                        "repeat.repeat" );
46
47     private static final String OTHER_TEST_REPO = "other-repo";
48
49     public void testInstantiation()
50     {
51         assertFalse( action == (BrowseAction) getActionProxy( "/browse.action" ).getAction() );
52     }
53
54     public void testBrowse()
55         throws Exception
56     {
57         metadataResolver.setNamespaces( TEST_REPO, GROUPS );
58
59         String result = action.browse();
60         assertSuccessResult( result );
61
62         assertEquals( Arrays.asList( "com", "commons-lang", "org.apache", "repeat.repeat" ), action.getNamespaces() );
63         assertNull( action.getProjectIds() );
64         assertNull( action.getProjectVersions() );
65
66         assertNull( action.getGroupId() );
67         assertNull( action.getArtifactId() );
68         assertNull( action.getRepositoryId() );
69         assertNull( action.getSharedModel() );
70     }
71
72     public void testBrowseNoObservableRepos()
73         throws Exception
74     {
75         setObservableRepos( Collections.<String>emptyList() );
76
77         String result = action.browse();
78         assertNoAccessResult( result );
79
80         assertNoOutputVariables();
81     }
82
83     public void testBrowseGroupNoObservableRepos()
84         throws Exception
85     {
86         setObservableRepos( Collections.<String>emptyList() );
87         String selectedGroupId = "org";
88
89         action.setGroupId( selectedGroupId );
90         String result = action.browseGroup();
91         assertNoAccessResult( result );
92
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() );
100     }
101
102     public void testBrowseArtifactNoObservableRepos()
103         throws Exception
104     {
105         setObservableRepos( Collections.<String>emptyList() );
106         String selectedGroupId = "org.apache";
107         String selectedArtifactId = "apache";
108
109         action.setGroupId( selectedGroupId );
110         action.setArtifactId( selectedArtifactId );
111         String result = action.browseArtifact();
112         assertNoAccessResult( result );
113
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() );
121     }
122
123     public void testBrowseGroupNoGroupId()
124         throws Exception
125     {
126         String result = action.browseGroup();
127         assertErrorResult( result );
128         assertNoOutputVariables();
129     }
130
131     public void testBrowseGroupNoArtifacts()
132         throws Exception
133     {
134         String selectedGroupId = "org";
135         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
136
137         metadataResolver.setNamespaces( TEST_REPO, groups );
138         action.setGroupId( selectedGroupId );
139         String result = action.browseGroup();
140         assertSuccessResult( result );
141
142         assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
143         assertEquals( Collections.<String>emptyList(), action.getProjectIds() );
144         assertNull( action.getProjectVersions() );
145
146         assertEquals( selectedGroupId, action.getGroupId() );
147         assertNull( action.getArtifactId() );
148         assertNull( action.getRepositoryId() );
149         assertNull( action.getSharedModel() );
150     }
151
152     public void testBrowseGroupWithArtifacts()
153         throws Exception
154     {
155         String artifacts = "apache";
156         String selectedGroupId = "org.apache";
157         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache.maven" );
158
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 );
164
165         assertEquals( groups, action.getNamespaces() );
166         assertEquals( Collections.singletonList( artifacts ), 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 testBrowseWithCollapsedGroupsAndArtifacts()
176         throws Exception
177     {
178         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
179
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() );
183
184         String result = action.browse();
185         assertSuccessResult( result );
186
187         assertEquals( Collections.singletonList( "org.apache" ), action.getNamespaces() );
188         assertNull( action.getProjectIds() );
189         assertNull( action.getProjectVersions() );
190
191         assertNull( action.getGroupId() );
192         assertNull( action.getArtifactId() );
193         assertNull( action.getRepositoryId() );
194         assertNull( action.getSharedModel() );
195     }
196
197     public void testBrowseWithCollapsedGroupsAndArtifactsAcrossRepositories()
198         throws Exception
199     {
200         setObservableRepos( Arrays.asList( TEST_REPO, OTHER_TEST_REPO ) );
201
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" ) );
204
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" ), 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 testBrowseGroupWithCollapsedGroupsAndArtifacts()
222         throws Exception
223     {
224         String artifacts = "apache";
225         String selectedGroupId = "org.apache";
226         List<String> groups = Arrays.asList( "org.apache.archiva", "org.apache" );
227
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() );
231
232         action.setGroupId( selectedGroupId );
233         String result = action.browseGroup();
234         assertSuccessResult( result );
235
236         assertEquals( Collections.singletonList( "org.apache.archiva" ), action.getNamespaces() );
237         assertEquals( Collections.singletonList( artifacts ), action.getProjectIds() );
238         assertNull( action.getProjectVersions() );
239
240         assertEquals( selectedGroupId, action.getGroupId() );
241         assertNull( action.getArtifactId() );
242         assertNull( action.getRepositoryId() );
243         assertNull( action.getSharedModel() );
244     }
245
246     public void testBrowseArtifactNoGroupId()
247         throws Exception
248     {
249         String selectedArtifactId = "apache";
250
251         action.setArtifactId( selectedArtifactId );
252         String result = action.browseArtifact();
253         assertErrorResult( result );
254
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() );
262     }
263
264     public void testBrowseArtifactNoArtifactId()
265         throws Exception
266     {
267         String selectedGroupId = "org.apache";
268
269         action.setGroupId( selectedGroupId );
270         String result = action.browseArtifact();
271         assertErrorResult( result );
272
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() );
280     }
281
282     public void testBrowseArtifact()
283         throws Exception
284
285     {
286         String selectedGroupId = "org.apache";
287         String selectedArtifactId = "apache";
288
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" ) );
298
299         action.setGroupId( selectedGroupId );
300         action.setArtifactId( selectedArtifactId );
301         String result = action.browseArtifact();
302         assertSuccessResult( result );
303
304         assertEquals( selectedGroupId, action.getGroupId() );
305         assertEquals( selectedArtifactId, action.getArtifactId() );
306         assertNull( action.getRepositoryId() );
307
308         assertNull( action.getNamespaces() );
309         assertNull( action.getProjectIds() );
310         assertEquals( versions, action.getProjectVersions() );
311
312         ProjectVersionMetadata model = action.getSharedModel();
313         assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
314     }
315
316     public void testBrowseArtifactWithSnapshots()
317         throws Exception
318
319     {
320         String selectedGroupId = "org.apache";
321         String selectedArtifactId = "apache";
322
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" ) );
336
337         action.setGroupId( selectedGroupId );
338         action.setArtifactId( selectedArtifactId );
339         String result = action.browseArtifact();
340         assertSuccessResult( result );
341
342         assertEquals( selectedGroupId, action.getGroupId() );
343         assertEquals( selectedArtifactId, action.getArtifactId() );
344         assertNull( action.getRepositoryId() );
345
346         assertNull( action.getNamespaces() );
347         assertNull( action.getProjectIds() );
348         assertEquals( versions, action.getProjectVersions() );
349
350         ProjectVersionMetadata model = action.getSharedModel();
351         assertDefaultModel( model, selectedGroupId, selectedArtifactId, null );
352     }
353
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
357
358     private void assertNoAccessResult( String result )
359     {
360         assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result );
361         assertEquals( 0, action.getActionErrors().size() );
362         assertEquals( 0, action.getActionMessages().size() );
363     }
364
365     private void assertSuccessResult( String result )
366     {
367         assertEquals( Action.SUCCESS, result );
368         assertEquals( 0, action.getActionErrors().size() );
369         assertEquals( 0, action.getActionMessages().size() );
370     }
371
372     private void assertErrorResult( String result )
373     {
374         assertEquals( Action.ERROR, result );
375         assertEquals( 1, action.getActionErrors().size() );
376         assertEquals( 0, action.getActionMessages().size() );
377     }
378
379     private void assertNoOutputVariables()
380     {
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() );
388     }
389
390     protected void setUp()
391         throws Exception
392     {
393         super.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 );
403     }
404
405     protected void tearDown()
406         throws Exception
407     {
408         super.tearDown();
409         setObservableRepos( Lists.<String>newArrayList( "test-repo" ) );
410     }
411 }