]> source.dussan.org Git - archiva.git/blob
d4e87195e161bd57eee87a7876e2d036cf07e327
[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.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.RemoteRepository;
25 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.archiva.web.action.AbstractActionTestCase;
33 import org.codehaus.plexus.registry.RegistryException;
34 import org.easymock.MockControl;
35
36 import java.util.Collections;
37
38 /**
39  * DeleteRemoteRepositoryActionTest
40  *
41  * @version $Id$
42  */
43 public class DeleteRemoteRepositoryActionTest
44     extends AbstractActionTestCase
45 {
46     private static final String REPO_ID = "remote-repo-ident";
47
48     private DeleteRemoteRepositoryAction action;
49
50     private MockControl archivaConfigurationControl;
51
52     private ArchivaConfiguration archivaConfiguration;
53
54     protected void setUp()
55         throws Exception
56     {
57         super.setUp();
58
59         //action = (DeleteRemoteRepositoryAction) lookup( Action.class.getName(), "deleteRemoteRepositoryAction" );
60         action = (DeleteRemoteRepositoryAction) getActionProxy( "/admin/deleteRemoteRepository.action" ).getAction();
61
62         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
63         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
64
65         ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
66             archivaConfiguration );
67     }
68
69     public void testDeleteRemoteRepositoryConfirmation()
70         throws Exception
71     {
72         RemoteRepository originalRepository = createRepository();
73         Configuration configuration = createConfigurationForEditing( originalRepository );
74
75         archivaConfiguration.getConfiguration();
76         archivaConfigurationControl.setReturnValue( configuration );
77
78         archivaConfiguration.getConfiguration();
79         archivaConfigurationControl.setReturnValue( configuration );
80
81         archivaConfigurationControl.replay();
82
83         action.setRepoid( REPO_ID );
84
85         action.prepare();
86         assertEquals( REPO_ID, action.getRepoid() );
87         RemoteRepository repository = action.getRepository();
88         assertNotNull( repository );
89         assertRepositoryEquals( repository, createRepository() );
90
91         String status = action.confirmDelete();
92         assertEquals( Action.INPUT, status );
93         repository = action.getRepository();
94         assertRepositoryEquals( repository, createRepository() );
95         assertEquals( Collections.singletonList( originalRepository ),
96                       action.getRemoteRepositoryAdmin().getRemoteRepositories() );
97     }
98
99     public void testDeleteRemoteRepository()
100         throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
101     {
102         Configuration configuration = createConfigurationForEditing( createRepository() );
103         configuration.addManagedRepository( createManagedRepository( "internal", "target/repo/internal" ) );
104         configuration.addManagedRepository( createManagedRepository( "snapshots", "target/repo/snapshots" ) );
105         configuration.addProxyConnector( createProxyConnector( "internal", REPO_ID ) );
106
107         archivaConfiguration.getConfiguration();
108         archivaConfigurationControl.setReturnValue( configuration, 4 );
109
110         archivaConfiguration.save( configuration );
111         archivaConfigurationControl.replay();
112
113         action.setRepoid( REPO_ID );
114
115         action.prepare();
116         assertEquals( REPO_ID, action.getRepoid() );
117         RemoteRepository repository = action.getRepository();
118         assertNotNull( repository );
119         assertRepositoryEquals( repository, createRepository() );
120
121         assertEquals( 1, configuration.getProxyConnectors().size() );
122
123         String status = action.delete();
124         assertEquals( Action.SUCCESS, status );
125
126         assertTrue( configuration.getRemoteRepositories().isEmpty() );
127         assertEquals( 0, configuration.getProxyConnectors().size() );
128     }
129
130     public void testDeleteRemoteRepositoryCancelled()
131         throws Exception
132     {
133         RemoteRepository originalRepository = createRepository();
134         Configuration configuration = createConfigurationForEditing( originalRepository );
135
136         archivaConfiguration.getConfiguration();
137         archivaConfigurationControl.setReturnValue( configuration, 2 );
138
139         archivaConfiguration.save( configuration );
140         archivaConfigurationControl.replay();
141
142         action.setRepoid( REPO_ID );
143
144         action.prepare();
145         assertEquals( REPO_ID, action.getRepoid() );
146         RemoteRepository repositoryConfiguration = action.getRepository();
147         assertNotNull( repositoryConfiguration );
148         assertRepositoryEquals( repositoryConfiguration, createRepository() );
149
150         String status = action.execute();
151         assertEquals( Action.SUCCESS, status );
152
153         RemoteRepository repository = action.getRepository();
154         assertRepositoryEquals( repository, createRepository() );
155         assertEquals( Collections.singletonList( originalRepository ),
156                       action.getRemoteRepositoryAdmin().getRemoteRepositories() );
157     }
158
159     private Configuration createConfigurationForEditing( RemoteRepository repositoryConfiguration )
160     {
161         Configuration configuration = new Configuration();
162         RemoteRepositoryConfiguration conf = new RemoteRepositoryConfiguration();
163         conf.setId( repositoryConfiguration.getId() );
164         conf.setLayout( repositoryConfiguration.getLayout() );
165         conf.setUrl( repositoryConfiguration.getUrl() );
166         conf.setName( repositoryConfiguration.getName() );
167         configuration.addRemoteRepository( conf );
168         return configuration;
169     }
170
171     private RemoteRepository createRepository()
172     {
173         RemoteRepository r = new RemoteRepository();
174         r.setId( REPO_ID );
175         populateRepository( r );
176         return r;
177     }
178
179     private void assertRepositoryEquals( RemoteRepository expectedRepository, RemoteRepository actualRepository )
180     {
181         assertEquals( expectedRepository.getId(), actualRepository.getId() );
182         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
183         assertEquals( expectedRepository.getUrl(), actualRepository.getUrl() );
184         assertEquals( expectedRepository.getName(), actualRepository.getName() );
185     }
186
187     private ManagedRepositoryConfiguration createManagedRepository( String string, String testPath )
188     {
189         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
190         r.setId( REPO_ID );
191         r.setName( "repo name" );
192         r.setLocation( testPath );
193         r.setLayout( "default" );
194         r.setRefreshCronExpression( "* 0/5 * * * ?" );
195         r.setDaysOlder( 0 );
196         r.setRetentionCount( 0 );
197         r.setReleases( true );
198         r.setSnapshots( true );
199         r.setScanned( false );
200         r.setDeleteReleasedSnapshots( false );
201         return r;
202     }
203
204     private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
205     {
206         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
207         connector.setSourceRepoId( managedRepoId );
208         connector.setTargetRepoId( remoteRepoId );
209
210         return connector;
211     }
212
213     private void populateRepository( RemoteRepository repository )
214     {
215         repository.setId( REPO_ID );
216         repository.setName( "repo name" );
217         repository.setUrl( "url" );
218         repository.setLayout( "default" );
219     }
220
221     // TODO: what about removing proxied content if a proxy is removed?
222 }