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