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.junit.Test;
29 import java.util.List;
32 * @author Olivier Lamy
34 public class ManagedRepositoryAdminTest
35 extends AbstractRepositoryAdminTest
37 public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
40 public void getAllManagedRepos()
43 mockAuditListener.clearEvents();
44 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
45 assertNotNull( repos );
46 assertTrue( repos.size() > 0 );
47 log.info( "repos " + repos );
49 // check default internal
50 ManagedRepository internal = findManagedRepoById( repos, "internal" );
51 assertNotNull( internal );
52 assertTrue( internal.isReleases() );
53 assertFalse( internal.isSnapshots() );
54 mockAuditListener.clearEvents();
61 mockAuditListener.clearEvents();
62 ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
63 assertNotNull( repo );
64 mockAuditListener.clearEvents();
68 public void addDeleteManagedRepo()
71 mockAuditListener.clearEvents();
73 String repoId = "test-new-one";
75 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
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 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
90 repos = managedRepositoryAdmin.getManagedRepositories();
91 assertNotNull( repos );
92 assertEquals( initialSize + 1, repos.size() );
94 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
96 assertTemplateRoleExists( repoId );
98 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
100 // deleteContents false
101 assertTrue( repoDir.exists() );
103 repos = managedRepositoryAdmin.getManagedRepositories();
104 assertNotNull( repos );
105 assertEquals( initialSize, repos.size() );
107 assertTemplateRoleNotExists( repoId );
109 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
111 assertAuditListenerCallAddAndDelete();
113 mockAuditListener.clearEvents();
117 public void updateDeleteManagedRepo()
120 String repoId = "test-new-one";
122 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
124 File repoDir = clearRepoLocation( repoLocation );
126 mockAuditListener.clearEvents();
127 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
128 assertNotNull( repos );
129 int initialSize = repos.size();
130 assertTrue( initialSize > 0 );
132 ManagedRepository repo = new ManagedRepository();
133 repo.setId( repoId );
134 repo.setName( "test repo" );
135 repo.setLocation( repoLocation );
136 repo.setCronExpression( "0 0 * * * ?" );
137 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
139 assertTemplateRoleExists( repoId );
141 repos = managedRepositoryAdmin.getManagedRepositories();
142 assertNotNull( repos );
143 assertEquals( initialSize + 1, repos.size() );
145 String newName = "test repo update";
147 repo.setName( newName );
149 repo.setLocation( repoLocation );
150 repo.setCronExpression( "0 0 * * * ?" );
152 managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
154 repo = managedRepositoryAdmin.getManagedRepository( repoId );
155 assertNotNull( repo );
156 assertEquals( newName, repo.getName() );
157 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
158 assertTrue( new File( repoLocation ).exists() );
160 assertTemplateRoleExists( repoId );
162 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
164 // check deleteContents false
165 assertTrue( repoDir.exists() );
167 assertTemplateRoleNotExists( repoId );
169 assertAuditListenerCallAndUpdateAddAndDelete( false );
171 mockAuditListener.clearEvents();
177 public void addDeleteManagedRepoWithStaged()
181 String repoId = "test-new-one";
182 String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
184 File repoDir = clearRepoLocation( repoLocation );
186 mockAuditListener.clearEvents();
187 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
188 assertNotNull( repos );
189 int initialSize = repos.size();
190 assertTrue( initialSize > 0 );
192 ManagedRepository repo = new ManagedRepository();
193 repo.setId( repoId );
194 repo.setName( "test repo" );
195 repo.setLocation( repoLocation );
196 repo.setCronExpression( "0 0 * * * ?" );
197 managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
198 repos = managedRepositoryAdmin.getManagedRepositories();
199 assertNotNull( repos );
200 assertEquals( initialSize + 2, repos.size() );
202 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
204 assertTemplateRoleExists( repoId );
206 assertTrue( repoDir.exists() );
208 assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
210 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
212 assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
214 managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
216 assertFalse( repoDir.exists() );
218 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
220 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
222 repos = managedRepositoryAdmin.getManagedRepositories();
223 assertNotNull( repos );
224 assertEquals( initialSize, repos.size() );
226 assertTemplateRoleNotExists( repoId );
228 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
230 mockAuditListener.clearEvents();
235 public void updateDeleteManagedRepoWithStagedRepo()
238 String repoId = "test-new-one";
240 String repoLocation = APPSERVER_BASE_PATH + File.separator + "new-path";
242 String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
244 File repoDir = clearRepoLocation( repoLocation );
246 mockAuditListener.clearEvents();
247 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
248 assertNotNull( repos );
249 int initialSize = repos.size();
250 assertTrue( initialSize > 0 );
252 ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
254 managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
256 assertTemplateRoleExists( repoId );
258 assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
260 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
262 repos = managedRepositoryAdmin.getManagedRepositories();
263 assertNotNull( repos );
264 assertEquals( initialSize + 1, repos.size() );
266 repo = managedRepositoryAdmin.getManagedRepository( repoId );
268 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
270 String newName = "test repo update";
272 repo.setName( newName );
274 repo.setLocation( repoLocation );
276 managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
278 repo = managedRepositoryAdmin.getManagedRepository( repoId );
279 assertNotNull( repo );
280 assertEquals( newName, repo.getName() );
281 assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
282 assertTrue( new File( repoLocation ).exists() );
283 assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
284 assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
285 assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
286 assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
288 assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
289 assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
290 assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
291 repo.isDeleteReleasedSnapshots() );
293 assertTemplateRoleExists( repoId );
295 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
297 assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
299 managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
301 // check deleteContents false
302 assertTrue( repoDir.exists() );
304 assertTemplateRoleNotExists( repoId );
306 assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
308 assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
310 assertAuditListenerCallAndUpdateAddAndDelete( true );
312 mockAuditListener.clearEvents();
316 //----------------------------------
318 //----------------------------------
320 private void assertTemplateRoleExists( String repoId )
323 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
324 assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
328 private void assertTemplateRoleNotExists( String repoId )
331 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
332 assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
335 private void assertAuditListenerCallAddAndDelete()
337 assertEquals( 2, mockAuditListener.getAuditEvents().size() );
339 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
340 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
341 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
343 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
344 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
347 private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
351 assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
352 mockAuditListener.getAuditEvents().size() );
356 assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
357 mockAuditListener.getAuditEvents().size() );
359 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
360 assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
361 assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
365 assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
366 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
367 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
371 assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
372 assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );