]> source.dussan.org Git - archiva.git/blob
79a4b99b2d653de8a6f2cb9bcfd856cd3290517b
[archiva.git] /
1 package org.apache.archiva.web.action.admin.repositories;
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.opensymphony.xwork2.Action;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
26 import org.apache.struts2.StrutsSpringTestCase;
27 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
28 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
29 import org.easymock.MockControl;
30
31 /**
32  * SortRepositoriesActionTest
33  */
34 public class SortRepositoriesActionTest
35     extends StrutsSpringTestCase
36 {
37     private static final String REPO_GROUP_ID = "repo-group-ident";
38
39     private static final String REPO1_ID = "managed-repo-ident-1";
40
41     private static final String REPO2_ID = "managed-repo-ident-2";
42
43     private static final String REPO3_ID = "managed-repo-ident-3";
44
45     private MockControl archivaConfigurationControl;
46
47     private ArchivaConfiguration archivaConfiguration;
48
49     private ArchivaConfiguration originalArchivaConfiguration;
50
51     private SortRepositoriesAction action;
52
53     @Override
54     protected String[] getContextLocations()
55     {
56         return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
57     }
58
59     protected void setUp()
60         throws Exception
61     {
62         super.setUp();
63         action = (SortRepositoriesAction) getActionProxy( "/admin/sortDownRepositoryFromGroup.action" ).getAction();
64         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
65         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
66         originalArchivaConfiguration = action.archivaConfiguration;
67         action.setArchivaConfiguration( archivaConfiguration );
68     }
69
70     @Override
71     protected void tearDown()
72         throws Exception
73     {
74         super.tearDown();
75         action.archivaConfiguration = originalArchivaConfiguration;
76     }
77
78     public void testSecureActionBundle()
79         throws SecureActionException
80     {
81         archivaConfiguration.getConfiguration();
82         archivaConfigurationControl.setReturnValue( new Configuration() );
83         archivaConfigurationControl.replay();
84
85         SecureActionBundle bundle = action.getSecureActionBundle();
86         assertTrue( bundle.requiresAuthentication() );
87         assertEquals( 1, bundle.getAuthorizationTuples().size() );
88     }
89
90     public void testSortDownFirstRepository()
91         throws Exception
92     {
93         Configuration configuration = createInitialConfiguration();
94
95         archivaConfiguration.getConfiguration();
96         archivaConfigurationControl.setReturnValue( configuration, 4 );
97         archivaConfiguration.save( configuration );
98         archivaConfigurationControl.replay();
99
100         RepositoryGroupConfiguration repoGroup =
101             (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
102         java.util.List<String> repositories = repoGroup.getRepositories();
103
104         assertEquals( 3, repositories.size() );
105         assertEquals( REPO1_ID, repositories.get( 0 ) );
106         assertEquals( REPO2_ID, repositories.get( 1 ) );
107         assertEquals( REPO3_ID, repositories.get( 2 ) );
108
109         // sort down first repo
110         action.setRepoGroupId( repoGroup.getId() );
111         action.setTargetRepo( REPO1_ID );
112
113         String result = action.sortDown();
114         assertEquals( Action.SUCCESS, result );
115
116         repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
117         repositories = repoGroup.getRepositories();
118         assertEquals( 3, repositories.size() );
119         assertEquals( REPO2_ID, repositories.get( 0 ) );
120         assertEquals( REPO1_ID, repositories.get( 1 ) );
121         assertEquals( REPO3_ID, repositories.get( 2 ) );
122     }
123
124     public void testSortDownLastRepository()
125         throws Exception
126     {
127         Configuration configuration = createInitialConfiguration();
128
129         archivaConfiguration.getConfiguration();
130         archivaConfigurationControl.setReturnValue( configuration, 4 );
131         archivaConfiguration.save( configuration );
132         archivaConfigurationControl.replay();
133
134         RepositoryGroupConfiguration repoGroup =
135             (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
136         java.util.List<String> repositories = repoGroup.getRepositories();
137
138         assertEquals( 3, repositories.size() );
139         assertEquals( REPO1_ID, repositories.get( 0 ) );
140         assertEquals( REPO2_ID, repositories.get( 1 ) );
141         assertEquals( REPO3_ID, repositories.get( 2 ) );
142
143         // sort down last repo
144         action.setRepoGroupId( repoGroup.getId() );
145         action.setTargetRepo( REPO3_ID );
146
147         String result = action.sortDown();
148         assertEquals( Action.SUCCESS, result );
149
150         repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
151         repositories = repoGroup.getRepositories();
152         assertEquals( 3, repositories.size() );
153         assertEquals( REPO1_ID, repositories.get( 0 ) );
154         assertEquals( REPO2_ID, repositories.get( 1 ) );
155         assertEquals( REPO3_ID, repositories.get( 2 ) );
156     }
157
158     public void testSortUpLastRepository()
159         throws Exception
160     {
161         Configuration configuration = createInitialConfiguration();
162
163         archivaConfiguration.getConfiguration();
164         archivaConfigurationControl.setReturnValue( configuration, 4 );
165         archivaConfiguration.save( configuration );
166         archivaConfigurationControl.replay();
167
168         RepositoryGroupConfiguration repoGroup =
169             (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
170         java.util.List<String> repositories = repoGroup.getRepositories();
171
172         assertEquals( 3, repositories.size() );
173         assertEquals( REPO1_ID, repositories.get( 0 ) );
174         assertEquals( REPO2_ID, repositories.get( 1 ) );
175         assertEquals( REPO3_ID, repositories.get( 2 ) );
176
177         // sort up last repo
178         action.setRepoGroupId( repoGroup.getId() );
179         action.setTargetRepo( REPO3_ID );
180
181         String result = action.sortUp();
182         assertEquals( Action.SUCCESS, result );
183
184         repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
185         repositories = repoGroup.getRepositories();
186         assertEquals( 3, repositories.size() );
187         assertEquals( REPO1_ID, repositories.get( 0 ) );
188         assertEquals( REPO3_ID, repositories.get( 1 ) );
189         assertEquals( REPO2_ID, repositories.get( 2 ) );
190     }
191
192     public void testSortUpFirstRepository()
193         throws Exception
194     {
195         Configuration configuration = createInitialConfiguration();
196
197         archivaConfiguration.getConfiguration();
198         archivaConfigurationControl.setReturnValue( configuration, 4 );
199         archivaConfiguration.save( configuration );
200         archivaConfigurationControl.replay();
201
202         RepositoryGroupConfiguration repoGroup =
203             (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
204         java.util.List<String> repositories = repoGroup.getRepositories();
205
206         assertEquals( 3, repositories.size() );
207         assertEquals( REPO1_ID, repositories.get( 0 ) );
208         assertEquals( REPO2_ID, repositories.get( 1 ) );
209         assertEquals( REPO3_ID, repositories.get( 2 ) );
210
211         // sort up first repo
212         action.setRepoGroupId( repoGroup.getId() );
213         action.setTargetRepo( REPO1_ID );
214
215         String result = action.sortUp();
216         assertEquals( Action.SUCCESS, result );
217
218         repoGroup = (RepositoryGroupConfiguration) configuration.getRepositoryGroups().get( 0 );
219         repositories = repoGroup.getRepositories();
220         assertEquals( 3, repositories.size() );
221         assertEquals( REPO1_ID, repositories.get( 0 ) );
222         assertEquals( REPO2_ID, repositories.get( 1 ) );
223         assertEquals( REPO3_ID, repositories.get( 2 ) );
224     }
225
226     private Configuration createInitialConfiguration()
227     {
228         Configuration config = new Configuration();
229
230         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
231         repoGroup.setId( REPO_GROUP_ID );
232         repoGroup.addRepository( REPO1_ID );
233         repoGroup.addRepository( REPO2_ID );
234         repoGroup.addRepository( REPO3_ID );
235
236         config.addRepositoryGroup( repoGroup );
237
238         return config;
239     }
240 }