View Javadoc
1   package org.apache.archiva.admin.repository.managed;
2   /*
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
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
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
18   * under the License.
19   */
20  
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;
26  
27  import java.io.File;
28  import java.util.List;
29  
30  /**
31   * @author Olivier Lamy
32   */
33  public class ManagedRepositoryAdminTest
34      extends AbstractRepositoryAdminTest
35  {
36      public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
37  
38  
39      String repoId = "test-new-one";
40  
41      String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
42  
43      @Test
44      public void getAllManagedRepos()
45          throws Exception
46      {
47          mockAuditListener.clearEvents();
48          List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
49          assertNotNull( repos );
50          assertTrue( repos.size() > 0 );
51          log.info( "repos {}", repos );
52  
53          // check default internal
54          ManagedRepository internal = findManagedRepoById( repos, "internal" );
55          assertNotNull( internal );
56          assertTrue( internal.isReleases() );
57          assertFalse( internal.isSnapshots() );
58          mockAuditListener.clearEvents();
59      }
60  
61      @Test
62      public void getById()
63          throws Exception
64      {
65          mockAuditListener.clearEvents();
66          ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
67          assertNotNull( repo );
68          mockAuditListener.clearEvents();
69      }
70  
71      @Test
72      public void addDeleteManagedRepo()
73          throws Exception
74      {
75          mockAuditListener.clearEvents();
76  
77          File repoDir = clearRepoLocation( repoLocation );
78  
79          List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
80          assertNotNull( repos );
81          int initialSize = repos.size();
82          assertTrue( initialSize > 0 );
83  
84          ManagedRepository repo = new ManagedRepository();
85          repo.setId( repoId );
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() );
94  
95          ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
96  
97          assertNotNull( managedRepository );
98  
99          assertEquals( "test repo", managedRepository.getName() );
100 
101         assertEquals( "cool repo", managedRepository.getDescription() );
102 
103         assertTemplateRoleExists( repoId );
104 
105         managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
106 
107         // deleteContents false
108         assertTrue( repoDir.exists() );
109 
110         repos = managedRepositoryAdmin.getManagedRepositories();
111         assertNotNull( repos );
112         assertEquals( initialSize, repos.size() );
113 
114         assertTemplateRoleNotExists( repoId );
115 
116         assertEquals( 2, mockAuditListener.getAuditEvents().size() );
117 
118         assertAuditListenerCallAddAndDelete();
119 
120         mockAuditListener.clearEvents();
121     }
122 
123     @Test
124     public void updateDeleteManagedRepo()
125         throws Exception
126     {
127 
128         File repoDir = clearRepoLocation( repoLocation );
129 
130         mockAuditListener.clearEvents();
131         List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
132         assertNotNull( repos );
133         int initialSize = repos.size();
134         assertTrue( initialSize > 0 );
135 
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() );
142 
143         assertTemplateRoleExists( repoId );
144 
145         repos = managedRepositoryAdmin.getManagedRepositories();
146         assertNotNull( repos );
147         assertEquals( initialSize + 1, repos.size() );
148 
149         String newName = "test repo update";
150 
151         repo.setName( newName );
152 
153         String description = "so great repository";
154 
155         repo.setDescription( description );
156 
157         repo.setLocation( repoLocation );
158         repo.setCronExpression( "0 0 * * * ?" );
159 
160         repo.setSkipPackedIndexCreation( true );
161 
162         managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
163 
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() );
171 
172         assertTemplateRoleExists( repoId );
173 
174         managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
175 
176         // check deleteContents false
177         assertTrue( repoDir.exists() );
178 
179         assertTemplateRoleNotExists( repoId );
180 
181         assertAuditListenerCallAndUpdateAddAndDelete( false );
182 
183         mockAuditListener.clearEvents();
184 
185     }
186 
187 
188     @Test
189     public void addDeleteManagedRepoWithStaged()
190         throws Exception
191     {
192 
193         File repoDir = clearRepoLocation( repoLocation );
194 
195         mockAuditListener.clearEvents();
196         List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
197         assertNotNull( repos );
198         int initialSize = repos.size();
199         assertTrue( initialSize > 0 );
200 
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() );
210 
211         assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
212 
213         assertTemplateRoleExists( repoId );
214 
215         assertTrue( repoDir.exists() );
216 
217         assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
218 
219         assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
220 
221         assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
222 
223         managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
224 
225         assertFalse( repoDir.exists() );
226 
227         assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
228 
229         assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
230 
231         repos = managedRepositoryAdmin.getManagedRepositories();
232         assertNotNull( repos );
233         assertEquals( initialSize, repos.size() );
234 
235         assertTemplateRoleNotExists( repoId );
236 
237         assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
238 
239         mockAuditListener.clearEvents();
240 
241     }
242 
243     @Test
244     public void updateDeleteManagedRepoWithStagedRepo()
245         throws Exception
246     {
247 
248         String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
249 
250         File repoDir = clearRepoLocation( repoLocation );
251 
252         mockAuditListener.clearEvents();
253         List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
254         assertNotNull( repos );
255         int initialSize = repos.size();
256         assertTrue( initialSize > 0 );
257 
258         ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
259 
260         managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
261 
262         assertTemplateRoleExists( repoId );
263 
264         assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
265 
266         assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
267 
268         repos = managedRepositoryAdmin.getManagedRepositories();
269         assertNotNull( repos );
270         assertEquals( initialSize + 1, repos.size() );
271 
272         repo = managedRepositoryAdmin.getManagedRepository( repoId );
273 
274         assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
275 
276         String newName = "test repo update";
277 
278         repo.setName( newName );
279 
280         repo.setLocation( repoLocation );
281 
282         managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
283 
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() );
293 
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() );
298 
299         assertTemplateRoleExists( repoId );
300 
301         assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
302 
303         assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
304 
305         managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
306 
307         // check deleteContents false
308         assertTrue( repoDir.exists() );
309 
310         assertTemplateRoleNotExists( repoId );
311 
312         assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
313 
314         assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
315 
316         assertAuditListenerCallAndUpdateAddAndDelete( true );
317 
318         mockAuditListener.clearEvents();
319 
320         new File( repoLocation + STAGE_REPO_ID_END ).delete();
321         assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
322     }
323 
324     //----------------------------------
325     // utility methods
326     //----------------------------------
327 
328     private void assertTemplateRoleExists( String repoId )
329         throws Exception
330     {
331         assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
332         assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
333     }
334 
335 
336     private void assertTemplateRoleNotExists( String repoId )
337         throws Exception
338     {
339         assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
340         assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
341     }
342 
343     private void assertAuditListenerCallAddAndDelete()
344     {
345         assertEquals( 2, mockAuditListener.getAuditEvents().size() );
346 
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() );
350 
351         assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
352         assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
353     }
354 
355     private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
356     {
357         if ( stageNeeded )
358         {
359             assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
360                           mockAuditListener.getAuditEvents().size() );
361         }
362         else
363         {
364             assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
365                           mockAuditListener.getAuditEvents().size() );
366         }
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() );
370 
371         if ( stageNeeded )
372         {
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() );
376         }
377         else
378         {
379             assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
380             assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
381         }
382 
383     }
384 
385 
386 }