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.model.beans.ManagedRepository;
22 import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
23 import org.apache.archiva.metadata.model.facets.AuditEvent;
24 import org.apache.archiva.security.common.ArchivaRoleConstants;
25 import org.junit.Test;
28 import java.util.List;
31 * @author Olivier Lamy
33 public class ManagedRepositoryAdminTest
34 extends AbstractRepositoryAdminTest
36 public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
39 String repoId = "test-new-one";
41 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
44 public void getAllManagedRepos()
47 mockAuditListener.clearEvents();
48 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
49 assertNotNull( repos );
50 assertTrue( repos.size() > 0 );
51 log.info( "repos {}", repos );
53 // check default internal
54 ManagedRepository internal = findManagedRepoById( repos, "internal" );
55 assertNotNull( internal );
56 assertTrue( internal.isReleases() );
57 assertFalse( internal.isSnapshots() );
58 mockAuditListener.clearEvents();
65 mockAuditListener.clearEvents();
66 ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
67 assertNotNull( repo );
68 mockAuditListener.clearEvents();
72 public void addDeleteManagedRepo()
75 mockAuditListener.clearEvents();
77 File repoDir = clearRepoLocation( repoLocation );
79 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
80 assertNotNull( repos );
81 int initialSize = repos.size();
82 assertTrue( initialSize > 0 );
84 ManagedRepository repo = new ManagedRepository();
86 repo.setName( "test repo" );
87 repo.setLocation( repoLocation );
88 repo.setCronExpression( "0 0 * * * ?" );
89 repo.setDescription( "cool repo" );
90 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
91 repos = managedRepositoryAdmin.getManagedRepositories();
92 assertNotNull( repos );
93 assertEquals( initialSize + 1, repos.size() );
95 ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
97 assertNotNull( managedRepository );
99 assertEquals( "test repo", managedRepository.getName() );
101 assertEquals( "cool repo", managedRepository.getDescription() );
103 assertTemplateRoleExists( repoId );
105 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
107 // deleteContents false
108 assertTrue( repoDir.exists() );
110 repos = managedRepositoryAdmin.getManagedRepositories();
111 assertNotNull( repos );
112 assertEquals( initialSize, repos.size() );
114 assertTemplateRoleNotExists( repoId );
116 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
118 assertAuditListenerCallAddAndDelete();
120 mockAuditListener.clearEvents();
124 public void updateDeleteManagedRepo()
128 File repoDir = clearRepoLocation( repoLocation );
130 mockAuditListener.clearEvents();
131 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
132 assertNotNull( repos );
133 int initialSize = repos.size();
134 assertTrue( initialSize > 0 );
136 ManagedRepository repo = new ManagedRepository();
137 repo.setId( repoId );
138 repo.setName( "test repo" );
139 repo.setLocation( repoLocation );
140 repo.setCronExpression( "0 0 * * * ?" );
141 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
143 assertTemplateRoleExists( repoId );
145 repos = managedRepositoryAdmin.getManagedRepositories();
146 assertNotNull( repos );
147 assertEquals( initialSize + 1, repos.size() );
149 String newName = "test repo update";
151 repo.setName( newName );
153 String description = "so great repository";
155 repo.setDescription( description );
157 repo.setLocation( repoLocation );
158 repo.setCronExpression( "0 0 * * * ?" );
160 repo.setSkipPackedIndexCreation( true );
162 managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
164 repo = managedRepositoryAdmin.getManagedRepository( repoId );
165 assertNotNull( repo );
166 assertEquals( newName, repo.getName() );
167 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
168 assertTrue( new File( repoLocation ).exists() );
169 assertEquals( description, repo.getDescription() );
170 assertTrue( repo.isSkipPackedIndexCreation() );
172 assertTemplateRoleExists( repoId );
174 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
176 // check deleteContents false
177 assertTrue( repoDir.exists() );
179 assertTemplateRoleNotExists( repoId );
181 assertAuditListenerCallAndUpdateAddAndDelete( false );
183 mockAuditListener.clearEvents();
189 public void addDeleteManagedRepoWithStaged()
193 File repoDir = clearRepoLocation( repoLocation );
195 mockAuditListener.clearEvents();
196 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
197 assertNotNull( repos );
198 int initialSize = repos.size();
199 assertTrue( initialSize > 0 );
201 ManagedRepository repo = new ManagedRepository();
202 repo.setId( repoId );
203 repo.setName( "test repo" );
204 repo.setLocation( repoLocation );
205 repo.setCronExpression( "0 0 * * * ?" );
206 managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
207 repos = managedRepositoryAdmin.getManagedRepositories();
208 assertNotNull( repos );
209 assertEquals( initialSize + 2, repos.size() );
211 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
213 assertTemplateRoleExists( repoId );
215 assertTrue( repoDir.exists() );
217 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
219 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
221 assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
223 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
225 assertFalse( repoDir.exists() );
227 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
229 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
231 repos = managedRepositoryAdmin.getManagedRepositories();
232 assertNotNull( repos );
233 assertEquals( initialSize, repos.size() );
235 assertTemplateRoleNotExists( repoId );
237 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
239 mockAuditListener.clearEvents();
244 public void updateDeleteManagedRepoWithStagedRepo()
248 String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
250 File repoDir = clearRepoLocation( repoLocation );
252 mockAuditListener.clearEvents();
253 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
254 assertNotNull( repos );
255 int initialSize = repos.size();
256 assertTrue( initialSize > 0 );
258 ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
260 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
262 assertTemplateRoleExists( repoId );
264 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
266 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
268 repos = managedRepositoryAdmin.getManagedRepositories();
269 assertNotNull( repos );
270 assertEquals( initialSize + 1, repos.size() );
272 repo = managedRepositoryAdmin.getManagedRepository( repoId );
274 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
276 String newName = "test repo update";
278 repo.setName( newName );
280 repo.setLocation( repoLocation );
282 managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
284 repo = managedRepositoryAdmin.getManagedRepository( repoId );
285 assertNotNull( repo );
286 assertEquals( newName, repo.getName() );
287 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
288 assertTrue( new File( repoLocation ).exists() );
289 assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
290 assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
291 assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
292 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
294 assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
295 assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
296 assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
297 repo.isDeleteReleasedSnapshots() );
299 assertTemplateRoleExists( repoId );
301 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
303 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
305 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
307 // check deleteContents false
308 assertTrue( repoDir.exists() );
310 assertTemplateRoleNotExists( repoId );
312 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
314 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
316 assertAuditListenerCallAndUpdateAddAndDelete( true );
318 mockAuditListener.clearEvents();
320 new File( repoLocation + STAGE_REPO_ID_END ).delete();
321 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
324 //----------------------------------
326 //----------------------------------
328 private void assertTemplateRoleExists( String repoId )
331 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
332 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
336 private void assertTemplateRoleNotExists( String repoId )
339 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
340 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
343 private void assertAuditListenerCallAddAndDelete()
345 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
347 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
348 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
349 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
351 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
352 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
355 private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
359 assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
360 mockAuditListener.getAuditEvents().size() );
364 assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
365 mockAuditListener.getAuditEvents().size() );
367 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
368 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
369 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
373 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
374 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
375 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
379 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
380 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );