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