1 package org.apache.archiva.admin.repository.managed;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
22 import org.apache.archiva.audit.AuditEvent;
23 import org.apache.archiva.security.common.ArchivaRoleConstants;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.codehaus.plexus.redback.role.RoleManager;
27 import org.junit.Test;
29 import javax.inject.Inject;
31 import java.util.List;
34 * @author Olivier Lamy
36 public class ManagedRepositoryAdminTest
37 extends AbstractRepositoryAdminTest
41 private ManagedRepositoryAdmin managedRepositoryAdmin;
46 protected RoleManager roleManager;
48 public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
51 public void getAllManagedRepos()
54 mockAuditListener.clearEvents();
55 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
56 assertNotNull( repos );
57 assertTrue( repos.size() > 0 );
58 log.info( "repos " + repos );
60 // check default internal
61 ManagedRepository internal = findManagedRepoById( repos, "internal" );
62 assertNotNull( internal );
63 assertTrue( internal.isReleases() );
64 assertFalse( internal.isSnapshots() );
65 mockAuditListener.clearEvents();
72 mockAuditListener.clearEvents();
73 ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
74 assertNotNull( repo );
75 mockAuditListener.clearEvents();
79 public void addDeleteManagedRepo()
82 mockAuditListener.clearEvents();
84 String repoId = "test-new-one";
86 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
88 File repoDir = clearRepoLocation( repoLocation );
90 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
91 assertNotNull( repos );
92 int initialSize = repos.size();
93 assertTrue( initialSize > 0 );
95 ManagedRepository repo = new ManagedRepository();
97 repo.setName( "test repo" );
98 repo.setLocation( repoLocation );
99 repo.setCronExpression( "0 0 * * * ?" );
100 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
101 repos = managedRepositoryAdmin.getManagedRepositories();
102 assertNotNull( repos );
103 assertEquals( initialSize + 1, repos.size() );
105 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
107 assertTemplateRoleExists( repoId );
109 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
111 // deleteContents false
112 assertTrue( repoDir.exists() );
114 repos = managedRepositoryAdmin.getManagedRepositories();
115 assertNotNull( repos );
116 assertEquals( initialSize, repos.size() );
118 assertTemplateRoleNotExists( repoId );
120 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
122 assertAuditListenerCallAddAndDelete();
124 mockAuditListener.clearEvents();
128 public void updateDeleteManagedRepo()
131 String repoId = "test-new-one";
133 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
135 File repoDir = clearRepoLocation( repoLocation );
137 mockAuditListener.clearEvents();
138 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
139 assertNotNull( repos );
140 int initialSize = repos.size();
141 assertTrue( initialSize > 0 );
143 ManagedRepository repo = new ManagedRepository();
144 repo.setId( repoId );
145 repo.setName( "test repo" );
146 repo.setLocation( repoLocation );
147 repo.setCronExpression( "0 0 * * * ?" );
148 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
150 assertTemplateRoleExists( repoId );
152 repos = managedRepositoryAdmin.getManagedRepositories();
153 assertNotNull( repos );
154 assertEquals( initialSize + 1, repos.size() );
156 String newName = "test repo update";
158 repo.setName( newName );
160 repo.setLocation( repoLocation );
161 repo.setCronExpression( "0 0 * * * ?" );
163 managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
165 repo = managedRepositoryAdmin.getManagedRepository( repoId );
166 assertNotNull( repo );
167 assertEquals( newName, repo.getName() );
168 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
169 assertTrue( new File( repoLocation ).exists() );
171 assertTemplateRoleExists( repoId );
173 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
175 // check deleteContents false
176 assertTrue( repoDir.exists() );
178 assertTemplateRoleNotExists( repoId );
180 assertAuditListenerCallAndUpdateAddAndDelete( false );
182 mockAuditListener.clearEvents();
188 public void addDeleteManagedRepoWithStaged()
192 String repoId = "test-new-one";
193 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
195 File repoDir = clearRepoLocation( repoLocation );
197 mockAuditListener.clearEvents();
198 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
199 assertNotNull( repos );
200 int initialSize = repos.size();
201 assertTrue( initialSize > 0 );
203 ManagedRepository repo = new ManagedRepository();
204 repo.setId( repoId );
205 repo.setName( "test repo" );
206 repo.setLocation( repoLocation );
207 repo.setCronExpression( "0 0 * * * ?" );
208 managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
209 repos = managedRepositoryAdmin.getManagedRepositories();
210 assertNotNull( repos );
211 assertEquals( initialSize + 2, repos.size() );
213 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
215 assertTemplateRoleExists( repoId );
217 assertTrue( repoDir.exists() );
219 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
221 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
223 assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
225 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
227 assertFalse( repoDir.exists() );
229 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
231 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
233 repos = managedRepositoryAdmin.getManagedRepositories();
234 assertNotNull( repos );
235 assertEquals( initialSize, repos.size() );
237 assertTemplateRoleNotExists( repoId );
239 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
241 mockAuditListener.clearEvents();
246 public void updateDeleteManagedRepoWithStagedRepo()
249 String repoId = "test-new-one";
251 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
253 String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
255 File repoDir = clearRepoLocation( repoLocation );
257 mockAuditListener.clearEvents();
258 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
259 assertNotNull( repos );
260 int initialSize = repos.size();
261 assertTrue( initialSize > 0 );
263 ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
267 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
269 assertTemplateRoleExists( repoId );
271 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
273 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
275 repos = managedRepositoryAdmin.getManagedRepositories();
276 assertNotNull( repos );
277 assertEquals( initialSize + 1, repos.size() );
279 repo = managedRepositoryAdmin.getManagedRepository( repoId );
281 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
283 String newName = "test repo update";
285 repo.setName( newName );
287 repo.setLocation( repoLocation );
289 managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
291 repo = managedRepositoryAdmin.getManagedRepository( repoId );
292 assertNotNull( repo );
293 assertEquals( newName, repo.getName() );
294 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
295 assertTrue( new File( repoLocation ).exists() );
296 assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
297 assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
298 assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
299 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
301 assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
302 assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
303 assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
304 repo.isDeleteReleasedSnapshots() );
306 assertTemplateRoleExists( repoId );
308 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
310 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
312 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
314 // check deleteContents false
315 assertTrue( repoDir.exists() );
317 assertTemplateRoleNotExists( repoId );
319 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
321 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
323 assertAuditListenerCallAndUpdateAddAndDelete( true );
325 mockAuditListener.clearEvents();
329 //----------------------------------
331 //----------------------------------
333 private void assertTemplateRoleExists( String repoId )
336 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
337 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
341 private void assertTemplateRoleNotExists( String repoId )
344 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
345 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
348 private void assertAuditListenerCallAddAndDelete()
350 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
352 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
353 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
354 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
356 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
357 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
360 private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
364 assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
365 mockAuditListener.getAuditEvents().size() );
369 assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
370 mockAuditListener.getAuditEvents().size() );
372 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
373 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
374 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
378 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
379 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
380 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
384 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
385 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
390 private File clearRepoLocation( String path )
393 File repoDir = new File( path );
394 if ( repoDir.exists() )
396 FileUtils.deleteDirectory( repoDir );
398 assertFalse( repoDir.exists() );
402 private ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
404 for ( ManagedRepository repo : repos )
406 if ( StringUtils.equals( id, repo.getId() ) )
416 ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
418 return new ManagedRepository( repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
419 repoLocation + "/.index", false, 1, 2, true );