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.audit.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 public void getAllManagedRepos()
42 mockAuditListener.clearEvents();
43 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
44 assertNotNull( repos );
45 assertTrue( repos.size() > 0 );
46 log.info( "repos " + repos );
48 // check default internal
49 ManagedRepository internal = findManagedRepoById( repos, "internal" );
50 assertNotNull( internal );
51 assertTrue( internal.isReleases() );
52 assertFalse( internal.isSnapshots() );
53 mockAuditListener.clearEvents();
60 mockAuditListener.clearEvents();
61 ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
62 assertNotNull( repo );
63 mockAuditListener.clearEvents();
67 public void addDeleteManagedRepo()
70 mockAuditListener.clearEvents();
72 String repoId = "test-new-one";
74 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
76 File repoDir = clearRepoLocation( repoLocation );
78 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
79 assertNotNull( repos );
80 int initialSize = repos.size();
81 assertTrue( initialSize > 0 );
83 ManagedRepository repo = new ManagedRepository();
85 repo.setName( "test repo" );
86 repo.setLocation( repoLocation );
87 repo.setCronExpression( "0 0 * * * ?" );
88 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
89 repos = managedRepositoryAdmin.getManagedRepositories();
90 assertNotNull( repos );
91 assertEquals( initialSize + 1, repos.size() );
93 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
95 assertTemplateRoleExists( repoId );
97 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
99 // deleteContents false
100 assertTrue( repoDir.exists() );
102 repos = managedRepositoryAdmin.getManagedRepositories();
103 assertNotNull( repos );
104 assertEquals( initialSize, repos.size() );
106 assertTemplateRoleNotExists( repoId );
108 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
110 assertAuditListenerCallAddAndDelete();
112 mockAuditListener.clearEvents();
116 public void updateDeleteManagedRepo()
119 String repoId = "test-new-one";
121 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
123 File repoDir = clearRepoLocation( repoLocation );
125 mockAuditListener.clearEvents();
126 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
127 assertNotNull( repos );
128 int initialSize = repos.size();
129 assertTrue( initialSize > 0 );
131 ManagedRepository repo = new ManagedRepository();
132 repo.setId( repoId );
133 repo.setName( "test repo" );
134 repo.setLocation( repoLocation );
135 repo.setCronExpression( "0 0 * * * ?" );
136 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
138 assertTemplateRoleExists( repoId );
140 repos = managedRepositoryAdmin.getManagedRepositories();
141 assertNotNull( repos );
142 assertEquals( initialSize + 1, repos.size() );
144 String newName = "test repo update";
146 repo.setName( newName );
148 repo.setLocation( repoLocation );
149 repo.setCronExpression( "0 0 * * * ?" );
151 managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
153 repo = managedRepositoryAdmin.getManagedRepository( repoId );
154 assertNotNull( repo );
155 assertEquals( newName, repo.getName() );
156 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
157 assertTrue( new File( repoLocation ).exists() );
159 assertTemplateRoleExists( repoId );
161 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
163 // check deleteContents false
164 assertTrue( repoDir.exists() );
166 assertTemplateRoleNotExists( repoId );
168 assertAuditListenerCallAndUpdateAddAndDelete( false );
170 mockAuditListener.clearEvents();
176 public void addDeleteManagedRepoWithStaged()
180 String repoId = "test-new-one";
181 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
183 File repoDir = clearRepoLocation( repoLocation );
185 mockAuditListener.clearEvents();
186 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
187 assertNotNull( repos );
188 int initialSize = repos.size();
189 assertTrue( initialSize > 0 );
191 ManagedRepository repo = new ManagedRepository();
192 repo.setId( repoId );
193 repo.setName( "test repo" );
194 repo.setLocation( repoLocation );
195 repo.setCronExpression( "0 0 * * * ?" );
196 managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
197 repos = managedRepositoryAdmin.getManagedRepositories();
198 assertNotNull( repos );
199 assertEquals( initialSize + 2, repos.size() );
201 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
203 assertTemplateRoleExists( repoId );
205 assertTrue( repoDir.exists() );
207 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
209 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
211 assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
213 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
215 assertFalse( repoDir.exists() );
217 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
219 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
221 repos = managedRepositoryAdmin.getManagedRepositories();
222 assertNotNull( repos );
223 assertEquals( initialSize, repos.size() );
225 assertTemplateRoleNotExists( repoId );
227 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
229 mockAuditListener.clearEvents();
234 public void updateDeleteManagedRepoWithStagedRepo()
237 String repoId = "test-new-one";
239 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
241 String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
243 File repoDir = clearRepoLocation( repoLocation );
245 mockAuditListener.clearEvents();
246 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
247 assertNotNull( repos );
248 int initialSize = repos.size();
249 assertTrue( initialSize > 0 );
251 ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
253 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
255 assertTemplateRoleExists( repoId );
257 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
259 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
261 repos = managedRepositoryAdmin.getManagedRepositories();
262 assertNotNull( repos );
263 assertEquals( initialSize + 1, repos.size() );
265 repo = managedRepositoryAdmin.getManagedRepository( repoId );
267 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
269 String newName = "test repo update";
271 repo.setName( newName );
273 repo.setLocation( repoLocation );
275 managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
277 repo = managedRepositoryAdmin.getManagedRepository( repoId );
278 assertNotNull( repo );
279 assertEquals( newName, repo.getName() );
280 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
281 assertTrue( new File( repoLocation ).exists() );
282 assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
283 assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
284 assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
285 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
287 assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
288 assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
289 assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
290 repo.isDeleteReleasedSnapshots() );
292 assertTemplateRoleExists( repoId );
294 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
296 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
298 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
300 // check deleteContents false
301 assertTrue( repoDir.exists() );
303 assertTemplateRoleNotExists( repoId );
305 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
307 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
309 assertAuditListenerCallAndUpdateAddAndDelete( true );
311 mockAuditListener.clearEvents();
315 //----------------------------------
317 //----------------------------------
319 private void assertTemplateRoleExists( String repoId )
322 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
323 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
327 private void assertTemplateRoleNotExists( String repoId )
330 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
331 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
334 private void assertAuditListenerCallAddAndDelete()
336 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
338 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
339 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
340 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
342 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
343 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
346 private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
350 assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
351 mockAuditListener.getAuditEvents().size() );
355 assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
356 mockAuditListener.getAuditEvents().size() );
358 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
359 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
360 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
364 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
365 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
366 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
370 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
371 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );