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