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 16KB

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