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.AuditInformation;
22 import org.apache.archiva.admin.mock.MockAuditListener;
23 import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.security.ArchivaRoleConstants;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.codehaus.plexus.redback.role.RoleManager;
29 import org.codehaus.plexus.redback.users.User;
30 import org.codehaus.plexus.redback.users.memory.SimpleUser;
31 import org.junit.Test;
33 import javax.inject.Inject;
35 import java.util.List;
38 * @author Olivier Lamy
40 public class ManagedRepositoryAdminTest
41 extends AbstractRepositoryAdminTest
45 private ManagedRepositoryAdmin managedRepositoryAdmin;
48 private MockAuditListener mockAuditListener;
51 protected RoleManager roleManager;
53 public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
56 public void getAllManagedRepos()
59 mockAuditListener.clearEvents();
60 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
61 assertNotNull( repos );
62 assertTrue( repos.size() > 0 );
63 log.info( "repos " + repos );
65 // check default internal
66 ManagedRepository internal = findManagedRepoById( repos, "internal" );
67 assertNotNull( internal );
68 assertTrue( internal.isReleases() );
69 assertFalse( internal.isSnapshots() );
70 mockAuditListener.clearEvents();
77 mockAuditListener.clearEvents();
78 ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
79 assertNotNull( repo );
80 mockAuditListener.clearEvents();
84 public void addDeleteManagedRepo()
87 mockAuditListener.clearEvents();
89 String repoId = "test-new-one";
91 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
93 File repoDir = clearRepoLocation( repoLocation );
95 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
96 assertNotNull( repos );
97 int initialSize = repos.size();
98 assertTrue( initialSize > 0 );
100 ManagedRepository repo = new ManagedRepository();
101 repo.setId( repoId );
102 repo.setName( "test repo" );
103 repo.setLocation( repoLocation );
104 repo.setCronExpression( "0 0 * * * ?" );
105 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
106 repos = managedRepositoryAdmin.getManagedRepositories();
107 assertNotNull( repos );
108 assertEquals( initialSize + 1, repos.size() );
110 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
112 assertTemplateRoleExists( repoId );
114 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
116 // deleteContents false
117 assertTrue( repoDir.exists() );
119 repos = managedRepositoryAdmin.getManagedRepositories();
120 assertNotNull( repos );
121 assertEquals( initialSize, repos.size() );
123 assertTemplateRoleNotExists( repoId );
125 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
127 assertAuditListenerCallAddAndDelete();
129 mockAuditListener.clearEvents();
133 public void updateDeleteManagedRepo()
136 String repoId = "test-new-one";
138 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
140 File repoDir = clearRepoLocation( repoLocation );
142 mockAuditListener.clearEvents();
143 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
144 assertNotNull( repos );
145 int initialSize = repos.size();
146 assertTrue( initialSize > 0 );
148 ManagedRepository repo = new ManagedRepository();
149 repo.setId( repoId );
150 repo.setName( "test repo" );
151 repo.setLocation( repoLocation );
152 repo.setCronExpression( "0 0 * * * ?" );
153 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
155 assertTemplateRoleExists( repoId );
157 repos = managedRepositoryAdmin.getManagedRepositories();
158 assertNotNull( repos );
159 assertEquals( initialSize + 1, repos.size() );
161 String newName = "test repo update";
163 repo.setName( newName );
165 repo.setLocation( repoLocation );
166 repo.setCronExpression( "0 0 * * * ?" );
168 managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
170 repo = managedRepositoryAdmin.getManagedRepository( repoId );
171 assertNotNull( repo );
172 assertEquals( newName, repo.getName() );
173 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
174 assertTrue( new File( repoLocation ).exists() );
176 assertTemplateRoleExists( repoId );
178 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
180 // check deleteContents false
181 assertTrue( repoDir.exists() );
183 assertTemplateRoleNotExists( repoId );
185 assertAuditListenerCallAndUpdateAddAndDelete( false );
187 mockAuditListener.clearEvents();
193 public void addDeleteManagedRepoWithStaged()
197 String repoId = "test-new-one";
198 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
200 File repoDir = clearRepoLocation( repoLocation );
202 mockAuditListener.clearEvents();
203 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
204 assertNotNull( repos );
205 int initialSize = repos.size();
206 assertTrue( initialSize > 0 );
208 ManagedRepository repo = new ManagedRepository();
209 repo.setId( repoId );
210 repo.setName( "test repo" );
211 repo.setLocation( repoLocation );
212 repo.setCronExpression( "0 0 * * * ?" );
213 managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
214 repos = managedRepositoryAdmin.getManagedRepositories();
215 assertNotNull( repos );
216 assertEquals( initialSize + 2, repos.size() );
218 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
220 assertTemplateRoleExists( repoId );
222 assertTrue( repoDir.exists() );
224 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
226 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
228 assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
230 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
232 assertFalse( repoDir.exists() );
234 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
236 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
238 repos = managedRepositoryAdmin.getManagedRepositories();
239 assertNotNull( repos );
240 assertEquals( initialSize, repos.size() );
242 assertTemplateRoleNotExists( repoId );
244 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
246 mockAuditListener.clearEvents();
251 public void updateDeleteManagedRepoWithStagedRepo()
254 String repoId = "test-new-one";
256 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
258 String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
260 File repoDir = clearRepoLocation( repoLocation );
262 mockAuditListener.clearEvents();
263 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
264 assertNotNull( repos );
265 int initialSize = repos.size();
266 assertTrue( initialSize > 0 );
268 ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
272 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
274 assertTemplateRoleExists( repoId );
276 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
278 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
280 repos = managedRepositoryAdmin.getManagedRepositories();
281 assertNotNull( repos );
282 assertEquals( initialSize + 1, repos.size() );
284 repo = managedRepositoryAdmin.getManagedRepository( repoId );
286 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
288 String newName = "test repo update";
290 repo.setName( newName );
292 repo.setLocation( repoLocation );
294 managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
296 repo = managedRepositoryAdmin.getManagedRepository( repoId );
297 assertNotNull( repo );
298 assertEquals( newName, repo.getName() );
299 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
300 assertTrue( new File( repoLocation ).exists() );
301 assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
302 assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
303 assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
304 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
306 assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
307 assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
308 assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
309 repo.isDeleteReleasedSnapshots() );
311 assertTemplateRoleExists( repoId );
313 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
315 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
317 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
319 // check deleteContents false
320 assertTrue( repoDir.exists() );
322 assertTemplateRoleNotExists( repoId );
324 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
326 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
328 assertAuditListenerCallAndUpdateAddAndDelete( true );
330 mockAuditListener.clearEvents();
334 //----------------------------------
336 //----------------------------------
338 private void assertTemplateRoleExists( String repoId )
341 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
342 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
346 private void assertTemplateRoleNotExists( String repoId )
349 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
350 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
353 private void assertAuditListenerCallAddAndDelete()
355 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
357 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
358 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
359 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
361 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
362 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
365 private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
369 assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
370 mockAuditListener.getAuditEvents().size() );
374 assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
375 mockAuditListener.getAuditEvents().size() );
377 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
378 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
379 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
383 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
384 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
385 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
389 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
390 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
395 private File clearRepoLocation( String path )
398 File repoDir = new File( path );
399 if ( repoDir.exists() )
401 FileUtils.deleteDirectory( repoDir );
403 assertFalse( repoDir.exists() );
407 private ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
409 for ( ManagedRepository repo : repos )
411 if ( StringUtils.equals( id, repo.getId() ) )
419 AuditInformation getFakeAuditInformation()
421 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
422 return auditInformation;
427 SimpleUser user = new SimpleUser()
430 public Object getPrincipal()
437 user.setUsername( "root" );
438 user.setFullName( "The top user" );
442 ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
444 return new ManagedRepository( repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
445 repoLocation + "/.index", false, 1, 2, true );