You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagedRepositoryAdminTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. import org.apache.archiva.admin.model.beans.ManagedRepository;
  21. import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
  22. import org.apache.archiva.metadata.model.facets.AuditEvent;
  23. import org.apache.archiva.security.common.ArchivaRoleConstants;
  24. import org.junit.Test;
  25. import java.io.File;
  26. import java.util.List;
  27. /**
  28. * @author Olivier Lamy
  29. */
  30. public class ManagedRepositoryAdminTest
  31. extends AbstractRepositoryAdminTest
  32. {
  33. public static final String STAGE_REPO_ID_END = DefaultManagedRepositoryAdmin.STAGE_REPO_ID_END;
  34. String repoId = "test-new-one";
  35. String repoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
  36. @Test
  37. public void getAllManagedRepos()
  38. throws Exception
  39. {
  40. mockAuditListener.clearEvents();
  41. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  42. assertNotNull( repos );
  43. assertTrue( repos.size() > 0 );
  44. log.info( "repos {}", repos );
  45. // check default internal
  46. ManagedRepository internal = findManagedRepoById( repos, "internal" );
  47. assertNotNull( internal );
  48. assertTrue( internal.isReleases() );
  49. assertFalse( internal.isSnapshots() );
  50. mockAuditListener.clearEvents();
  51. }
  52. @Test
  53. public void getById()
  54. throws Exception
  55. {
  56. mockAuditListener.clearEvents();
  57. ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
  58. assertNotNull( repo );
  59. mockAuditListener.clearEvents();
  60. }
  61. @Test
  62. public void addDeleteManagedRepo()
  63. throws Exception
  64. {
  65. mockAuditListener.clearEvents();
  66. File repoDir = clearRepoLocation( repoLocation );
  67. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  68. assertNotNull( repos );
  69. int initialSize = repos.size();
  70. assertTrue( initialSize > 0 );
  71. ManagedRepository repo = new ManagedRepository();
  72. repo.setId( repoId );
  73. repo.setName( "test repo" );
  74. repo.setLocation( repoLocation );
  75. repo.setCronExpression( "0 0 * * * ?" );
  76. repo.setDescription( "cool repo" );
  77. managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
  78. repos = managedRepositoryAdmin.getManagedRepositories();
  79. assertNotNull( repos );
  80. assertEquals( initialSize + 1, repos.size() );
  81. ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
  82. assertNotNull( managedRepository );
  83. assertEquals( "test repo", managedRepository.getName() );
  84. assertEquals( "cool repo", managedRepository.getDescription() );
  85. assertTemplateRoleExists( repoId );
  86. managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), false );
  87. // deleteContents false
  88. assertTrue( repoDir.exists() );
  89. repos = managedRepositoryAdmin.getManagedRepositories();
  90. assertNotNull( repos );
  91. assertEquals( initialSize, repos.size() );
  92. assertTemplateRoleNotExists( repoId );
  93. assertEquals( 2, mockAuditListener.getAuditEvents().size() );
  94. assertAuditListenerCallAddAndDelete();
  95. mockAuditListener.clearEvents();
  96. }
  97. @Test
  98. public void updateDeleteManagedRepo()
  99. throws Exception
  100. {
  101. File repoDir = clearRepoLocation( repoLocation );
  102. mockAuditListener.clearEvents();
  103. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  104. assertNotNull( repos );
  105. int initialSize = repos.size();
  106. assertTrue( initialSize > 0 );
  107. ManagedRepository repo = new ManagedRepository();
  108. repo.setId( repoId );
  109. repo.setName( "test repo" );
  110. repo.setLocation( repoLocation );
  111. repo.setCronExpression( "0 0 * * * ?" );
  112. managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
  113. assertTemplateRoleExists( repoId );
  114. repos = managedRepositoryAdmin.getManagedRepositories();
  115. assertNotNull( repos );
  116. assertEquals( initialSize + 1, repos.size() );
  117. String newName = "test repo update";
  118. repo.setName( newName );
  119. String description = "so great repository";
  120. repo.setDescription( description );
  121. repo.setLocation( repoLocation );
  122. repo.setCronExpression( "0 0 * * * ?" );
  123. repo.setSkipPackedIndexCreation( true );
  124. managedRepositoryAdmin.updateManagedRepository( repo, false, getFakeAuditInformation(), false );
  125. repo = managedRepositoryAdmin.getManagedRepository( repoId );
  126. assertNotNull( repo );
  127. assertEquals( newName, repo.getName() );
  128. assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
  129. assertTrue( new File( repoLocation ).exists() );
  130. assertEquals( description, repo.getDescription() );
  131. assertTrue( repo.isSkipPackedIndexCreation() );
  132. assertTemplateRoleExists( repoId );
  133. managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
  134. // check deleteContents false
  135. assertTrue( repoDir.exists() );
  136. assertTemplateRoleNotExists( repoId );
  137. assertAuditListenerCallAndUpdateAddAndDelete( false );
  138. mockAuditListener.clearEvents();
  139. }
  140. @Test
  141. public void addDeleteManagedRepoWithStaged()
  142. throws Exception
  143. {
  144. File repoDir = clearRepoLocation( repoLocation );
  145. mockAuditListener.clearEvents();
  146. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  147. assertNotNull( repos );
  148. int initialSize = repos.size();
  149. assertTrue( initialSize > 0 );
  150. ManagedRepository repo = new ManagedRepository();
  151. repo.setId( repoId );
  152. repo.setName( "test repo" );
  153. repo.setLocation( repoLocation );
  154. repo.setCronExpression( "0 0 * * * ?" );
  155. managedRepositoryAdmin.addManagedRepository( repo, true, getFakeAuditInformation() );
  156. repos = managedRepositoryAdmin.getManagedRepositories();
  157. assertNotNull( repos );
  158. assertEquals( initialSize + 2, repos.size() );
  159. assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId ) );
  160. assertTemplateRoleExists( repoId );
  161. assertTrue( repoDir.exists() );
  162. assertNotNull( managedRepositoryAdmin.getManagedRepository( repoId + STAGE_REPO_ID_END ) );
  163. assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
  164. assertTrue( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
  165. managedRepositoryAdmin.deleteManagedRepository( repoId, getFakeAuditInformation(), true );
  166. assertFalse( repoDir.exists() );
  167. assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
  168. assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
  169. repos = managedRepositoryAdmin.getManagedRepositories();
  170. assertNotNull( repos );
  171. assertEquals( initialSize, repos.size() );
  172. assertTemplateRoleNotExists( repoId );
  173. assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
  174. mockAuditListener.clearEvents();
  175. }
  176. @Test
  177. public void updateDeleteManagedRepoWithStagedRepo()
  178. throws Exception
  179. {
  180. String stageRepoLocation = APPSERVER_BASE_PATH + File.separator + repoId;
  181. File repoDir = clearRepoLocation( repoLocation );
  182. mockAuditListener.clearEvents();
  183. List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
  184. assertNotNull( repos );
  185. int initialSize = repos.size();
  186. assertTrue( initialSize > 0 );
  187. ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
  188. managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
  189. assertTemplateRoleExists( repoId );
  190. assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
  191. assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
  192. repos = managedRepositoryAdmin.getManagedRepositories();
  193. assertNotNull( repos );
  194. assertEquals( initialSize + 1, repos.size() );
  195. repo = managedRepositoryAdmin.getManagedRepository( repoId );
  196. assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
  197. String newName = "test repo update";
  198. repo.setName( newName );
  199. repo.setLocation( repoLocation );
  200. managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false );
  201. repo = managedRepositoryAdmin.getManagedRepository( repoId );
  202. assertNotNull( repo );
  203. assertEquals( newName, repo.getName() );
  204. assertEquals( new File( repoLocation ).getCanonicalPath(), new File( repo.getLocation() ).getCanonicalPath() );
  205. assertTrue( new File( repoLocation ).exists() );
  206. assertEquals( getTestManagedRepository( repoId, repoLocation ).getCronExpression(), repo.getCronExpression() );
  207. assertEquals( getTestManagedRepository( repoId, repoLocation ).getLayout(), repo.getLayout() );
  208. assertEquals( getTestManagedRepository( repoId, repoLocation ).getId(), repo.getId() );
  209. assertEquals( getTestManagedRepository( repoId, repoLocation ).getIndexDirectory(), repo.getIndexDirectory() );
  210. assertEquals( getTestManagedRepository( repoId, repoLocation ).getDaysOlder(), repo.getDaysOlder() );
  211. assertEquals( getTestManagedRepository( repoId, repoLocation ).getRetentionCount(), repo.getRetentionCount() );
  212. assertEquals( getTestManagedRepository( repoId, repoLocation ).isDeleteReleasedSnapshots(),
  213. repo.isDeleteReleasedSnapshots() );
  214. assertTemplateRoleExists( repoId );
  215. assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
  216. assertTemplateRoleExists( repoId + STAGE_REPO_ID_END );
  217. managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation(), false );
  218. // check deleteContents false
  219. assertTrue( repoDir.exists() );
  220. assertTemplateRoleNotExists( repoId );
  221. assertTrue( new File( stageRepoLocation + STAGE_REPO_ID_END ).exists() );
  222. assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
  223. assertAuditListenerCallAndUpdateAddAndDelete( true );
  224. mockAuditListener.clearEvents();
  225. new File( repoLocation + STAGE_REPO_ID_END ).delete();
  226. assertFalse( new File( repoLocation + STAGE_REPO_ID_END ).exists() );
  227. }
  228. //----------------------------------
  229. // utility methods
  230. //----------------------------------
  231. private void assertTemplateRoleExists( String repoId )
  232. throws Exception
  233. {
  234. assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
  235. assertTrue( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
  236. }
  237. private void assertTemplateRoleNotExists( String repoId )
  238. throws Exception
  239. {
  240. assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) );
  241. assertFalse( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) );
  242. }
  243. private void assertAuditListenerCallAddAndDelete()
  244. {
  245. assertEquals( 2, mockAuditListener.getAuditEvents().size() );
  246. assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
  247. assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
  248. assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
  249. assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
  250. assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
  251. }
  252. private void assertAuditListenerCallAndUpdateAddAndDelete( boolean stageNeeded )
  253. {
  254. if ( stageNeeded )
  255. {
  256. assertEquals( "not 4 audit events " + mockAuditListener.getAuditEvents(), 4,
  257. mockAuditListener.getAuditEvents().size() );
  258. }
  259. else
  260. {
  261. assertEquals( "not 3 audit events " + mockAuditListener.getAuditEvents(), 3,
  262. mockAuditListener.getAuditEvents().size() );
  263. }
  264. assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
  265. assertEquals( "root", mockAuditListener.getAuditEvents().get( 0 ).getUserId() );
  266. assertEquals( "archiva-localhost", mockAuditListener.getAuditEvents().get( 0 ).getRemoteIP() );
  267. if ( stageNeeded )
  268. {
  269. assertEquals( AuditEvent.ADD_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
  270. assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
  271. assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 3 ).getAction() );
  272. }
  273. else
  274. {
  275. assertEquals( AuditEvent.MODIFY_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 1 ).getAction() );
  276. assertEquals( AuditEvent.DELETE_MANAGED_REPO, mockAuditListener.getAuditEvents().get( 2 ).getAction() );
  277. }
  278. }
  279. }