]> source.dussan.org Git - archiva.git/blob
8493e47eec5c2ddf9c9996a53044a9866c0db146
[archiva.git] /
1 package org.apache.maven.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.remote.RemoteRepository;
25 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.maven.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         action.setArchivaConfiguration( archivaConfiguration );
65
66         ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
67             archivaConfiguration );
68     }
69
70     public void testDeleteRemoteRepositoryConfirmation()
71         throws Exception
72     {
73         RemoteRepository originalRepository = createRepository();
74         Configuration configuration = createConfigurationForEditing( originalRepository );
75
76         archivaConfiguration.getConfiguration();
77         archivaConfigurationControl.setReturnValue( configuration );
78
79         archivaConfiguration.getConfiguration();
80         archivaConfigurationControl.setReturnValue( configuration );
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     public void testDeleteRemoteRepository()
101         throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
102     {
103         Configuration configuration = createConfigurationForEditing( createRepository() );
104         configuration.addManagedRepository( createManagedRepository( "internal", "target/repo/internal" ) );
105         configuration.addManagedRepository( createManagedRepository( "snapshots", "target/repo/snapshots" ) );
106         configuration.addProxyConnector( createProxyConnector( "internal", REPO_ID ) );
107
108         archivaConfiguration.getConfiguration();
109         archivaConfigurationControl.setReturnValue( configuration, 4 );
110
111         archivaConfiguration.save( configuration );
112         archivaConfigurationControl.replay();
113
114         action.setRepoid( REPO_ID );
115
116         action.prepare();
117         assertEquals( REPO_ID, action.getRepoid() );
118         RemoteRepository repository = action.getRepository();
119         assertNotNull( repository );
120         assertRepositoryEquals( repository, createRepository() );
121
122         assertEquals( 1, configuration.getProxyConnectors().size() );
123
124         String status = action.delete();
125         assertEquals( Action.SUCCESS, status );
126
127         assertTrue( configuration.getRemoteRepositories().isEmpty() );
128         assertEquals( 0, configuration.getProxyConnectors().size() );
129     }
130
131     public void testDeleteRemoteRepositoryCancelled()
132         throws Exception
133     {
134         RemoteRepository originalRepository = createRepository();
135         Configuration configuration = createConfigurationForEditing( originalRepository );
136
137         archivaConfiguration.getConfiguration();
138         archivaConfigurationControl.setReturnValue( configuration, 2 );
139
140         archivaConfiguration.save( configuration );
141         archivaConfigurationControl.replay();
142
143         action.setRepoid( REPO_ID );
144
145         action.prepare();
146         assertEquals( REPO_ID, action.getRepoid() );
147         RemoteRepository repositoryConfiguration = action.getRepository();
148         assertNotNull( repositoryConfiguration );
149         assertRepositoryEquals( repositoryConfiguration, createRepository() );
150
151         String status = action.execute();
152         assertEquals( Action.SUCCESS, status );
153
154         RemoteRepository repository = action.getRepository();
155         assertRepositoryEquals( repository, createRepository() );
156         assertEquals( Collections.singletonList( originalRepository ),
157                       action.getRemoteRepositoryAdmin().getRemoteRepositories() );
158     }
159
160     private Configuration createConfigurationForEditing( RemoteRepository repositoryConfiguration )
161     {
162         Configuration configuration = new Configuration();
163         RemoteRepositoryConfiguration conf = new RemoteRepositoryConfiguration();
164         conf.setId( repositoryConfiguration.getId() );
165         conf.setLayout( repositoryConfiguration.getLayout() );
166         conf.setUrl( repositoryConfiguration.getUrl() );
167         conf.setName( repositoryConfiguration.getName() );
168         configuration.addRemoteRepository( conf );
169         return configuration;
170     }
171
172     private RemoteRepository createRepository()
173     {
174         RemoteRepository r = new RemoteRepository();
175         r.setId( REPO_ID );
176         populateRepository( r );
177         return r;
178     }
179
180     private void assertRepositoryEquals( RemoteRepository expectedRepository, RemoteRepository actualRepository )
181     {
182         assertEquals( expectedRepository.getId(), actualRepository.getId() );
183         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
184         assertEquals( expectedRepository.getUrl(), actualRepository.getUrl() );
185         assertEquals( expectedRepository.getName(), actualRepository.getName() );
186     }
187
188     private ManagedRepositoryConfiguration createManagedRepository( String string, String testPath )
189     {
190         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
191         r.setId( REPO_ID );
192         r.setName( "repo name" );
193         r.setLocation( testPath );
194         r.setLayout( "default" );
195         r.setRefreshCronExpression( "* 0/5 * * * ?" );
196         r.setDaysOlder( 0 );
197         r.setRetentionCount( 0 );
198         r.setReleases( true );
199         r.setSnapshots( true );
200         r.setScanned( false );
201         r.setDeleteReleasedSnapshots( false );
202         return r;
203     }
204
205     private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
206     {
207         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
208         connector.setSourceRepoId( managedRepoId );
209         connector.setTargetRepoId( remoteRepoId );
210
211         return connector;
212     }
213
214     private void populateRepository( RemoteRepository repository )
215     {
216         repository.setId( REPO_ID );
217         repository.setName( "repo name" );
218         repository.setUrl( "url" );
219         repository.setLayout( "default" );
220     }
221
222     // TODO: what about removing proxied content if a proxy is removed?
223 }