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.

MavenRepositoryProviderTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package org.apache.archiva.maven.repository;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.common.utils.FileUtils;
  20. import org.apache.archiva.configuration.model.ArchivaRuntimeConfiguration;
  21. import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
  22. import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
  23. import org.apache.archiva.configuration.model.RepositoryGroupConfiguration;
  24. import org.apache.archiva.repository.EditableRepositoryGroup;
  25. import org.apache.archiva.repository.ManagedRepository;
  26. import org.apache.archiva.repository.ReleaseScheme;
  27. import org.apache.archiva.repository.RemoteRepository;
  28. import org.apache.archiva.repository.RepositoryException;
  29. import org.apache.archiva.repository.RepositoryGroup;
  30. import org.apache.archiva.repository.RepositoryRegistry;
  31. import org.apache.archiva.repository.RepositoryType;
  32. import org.apache.archiva.repository.UnsupportedFeatureException;
  33. import org.apache.archiva.repository.base.PasswordCredentials;
  34. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  35. import org.apache.archiva.repository.features.IndexCreationFeature;
  36. import org.apache.archiva.repository.features.RemoteIndexFeature;
  37. import org.apache.archiva.repository.features.StagingRepositoryFeature;
  38. import org.apache.archiva.maven.repository.metadata.storage.mock.MockConfiguration;
  39. import org.junit.After;
  40. import org.junit.Before;
  41. import org.junit.Test;
  42. import java.io.IOException;
  43. import java.net.URI;
  44. import java.net.URISyntaxException;
  45. import java.nio.file.Files;
  46. import java.nio.file.Path;
  47. import java.nio.file.Paths;
  48. import java.time.Duration;
  49. import java.time.Period;
  50. import java.time.temporal.ChronoUnit;
  51. import java.util.ArrayList;
  52. import java.util.HashMap;
  53. import java.util.Map;
  54. import static org.junit.Assert.*;
  55. /**
  56. * @author Martin Stockhammer <martin_s@apache.org>
  57. */
  58. public class MavenRepositoryProviderTest
  59. {
  60. MavenRepositoryProvider provider;
  61. RepositoryRegistry reg;
  62. Path repoLocation;
  63. @Before
  64. public void setUp()
  65. throws Exception
  66. {
  67. provider = new MavenRepositoryProvider( );
  68. MockConfiguration mockConfiguration =new MockConfiguration();
  69. mockConfiguration.getConfiguration().setArchivaRuntimeConfiguration( new ArchivaRuntimeConfiguration() );
  70. mockConfiguration.getConfiguration().getArchivaRuntimeConfiguration().setRepositoryBaseDirectory( "repositories" );
  71. provider.setArchivaConfiguration( mockConfiguration );
  72. }
  73. @After
  74. public void cleanUp() {
  75. if (repoLocation!=null && Files.exists( repoLocation )) {
  76. FileUtils.deleteQuietly( repoLocation );
  77. }
  78. }
  79. @Test
  80. public void provides( ) throws Exception
  81. {
  82. assertEquals(1, provider.provides().size());
  83. assertEquals( RepositoryType.MAVEN, provider.provides().iterator().next());
  84. }
  85. @Test
  86. public void createManagedInstance( ) throws Exception
  87. {
  88. ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration( );
  89. repo.setId("testm001");
  90. repo.setName("Managed Test Repo 001");
  91. repo.setDescription( "This is a managed test" );
  92. repo.setRetentionPeriod( 37 );
  93. repoLocation = Files.createTempDirectory( "test-repo-001");
  94. repo.setLocation( repoLocation.toAbsolutePath().toString() );
  95. repo.setSnapshots( true );
  96. repo.setReleases( true );
  97. repo.setRefreshCronExpression( "4 0 0 ? * TUE" );
  98. repo.setScanned( true );
  99. repo.setBlockRedeployments( true );
  100. repo.setDeleteReleasedSnapshots( true );
  101. repo.setRetentionCount( 33 );
  102. repo.setSkipPackedIndexCreation( true );
  103. repo.setStageRepoNeeded( true );
  104. repo.setIndexDir( "testmanaged/.index" );
  105. repo.setLayout( "maven2" );
  106. repo.setType( RepositoryType.MAVEN.toString() );
  107. ManagedRepository mr = provider.createManagedInstance( repo );
  108. assertNotNull(mr.getLocation());
  109. String repoUri = repoLocation.toUri().toString();
  110. assertTrue(Files.exists(repoLocation));
  111. repoUri = repoUri.substring( 0, repoUri.length()-1 );
  112. assertEquals(repoUri, mr.getLocation().toString());
  113. assertEquals("This is a managed test", mr.getDescription());
  114. assertEquals("Managed Test Repo 001", mr.getName());
  115. assertEquals(2, mr.getActiveReleaseSchemes().size());
  116. assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ));
  117. assertTrue( mr.getActiveReleaseSchemes().contains( ReleaseScheme.SNAPSHOT));
  118. assertEquals("testm001", mr.getId());
  119. assertTrue(mr.blocksRedeployments());
  120. assertEquals("4 0 0 ? * TUE", mr.getSchedulingDefinition());
  121. assertTrue(mr.isScanned());
  122. ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class );
  123. assertEquals( Period.ofDays( 37), artifactCleanupFeature.getRetentionPeriod());
  124. assertTrue(artifactCleanupFeature.isDeleteReleasedSnapshots());
  125. assertEquals(33, artifactCleanupFeature.getRetentionCount());
  126. IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class );
  127. assertNotNull(indexCreationFeature.getIndexPath());
  128. assertEquals("testmanaged/.index", indexCreationFeature.getIndexPath().toString());
  129. assertFalse(indexCreationFeature.getIndexPath().isAbsolute());
  130. assertTrue(indexCreationFeature.isSkipPackedIndexCreation());
  131. StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class );
  132. assertTrue(stagingRepositoryFeature.isStageRepoNeeded());
  133. assertNull(stagingRepositoryFeature.getStagingRepository());
  134. }
  135. @Test
  136. public void createRemoteInstance( ) throws Exception
  137. {
  138. RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration( );
  139. repo.setUsername("testuser001");
  140. repo.setPassword( "pwd0000abc" );
  141. repo.setCheckPath( "test/check.html" );
  142. repo.setTimeout( 50 );
  143. repo.setUrl( "https://repo.maven.apache.org/maven2/test" );
  144. repo.setDownloadRemoteIndex( true );
  145. repo.setDownloadRemoteIndexOnStartup( true );
  146. Map<String,String> header = new HashMap<>( );
  147. header.put("header1","value1");
  148. header.put("header2","value2");
  149. repo.setExtraHeaders( header );
  150. Map<String,String> params = new HashMap<>( );
  151. params.put("param1","pval1");
  152. params.put("param2","pval2");
  153. repo.setExtraParameters( params );
  154. repo.setRefreshCronExpression( "0 1 07 ? * MON" );
  155. repo.setRemoteDownloadTimeout( 333 );
  156. repo.setRemoteIndexUrl( "testremote/.index" );
  157. repo.setDescription( "This is a test" );
  158. repo.setId( "test001" );
  159. repo.setName( "Remote Test Repo 001" );
  160. repo.setIndexDir( "testindex/.index" );
  161. repo.setLayout( "maven2" );
  162. repo.setType( RepositoryType.MAVEN.toString() );
  163. repo.setIndexDir( "local/.index" );
  164. RemoteRepository mr = provider.createRemoteInstance( repo );
  165. assertEquals("test001", mr.getId());
  166. assertEquals("This is a test", mr.getDescription());
  167. assertNotNull(mr.getLocation());
  168. assertEquals("https://repo.maven.apache.org/maven2/test", mr.getLocation().toString());
  169. assertEquals("Remote Test Repo 001", mr.getName());
  170. assertEquals("test001", mr.getId());
  171. assertEquals("0 1 07 ? * MON", mr.getSchedulingDefinition());
  172. assertEquals(50, mr.getTimeout().get( ChronoUnit.SECONDS ));
  173. assertTrue(mr.isScanned());
  174. assertNotNull(mr.getLoginCredentials());
  175. assertTrue(mr.getLoginCredentials() instanceof PasswordCredentials );
  176. PasswordCredentials creds = (PasswordCredentials) mr.getLoginCredentials();
  177. assertEquals("testuser001", creds.getUsername());
  178. assertEquals("pwd0000abc", new String(creds.getPassword()));
  179. assertEquals("value1", mr.getExtraHeaders().get("header1"));
  180. assertEquals("pval2", mr.getExtraParameters().get("param2"));
  181. assertEquals( "maven2", mr.getLayout());
  182. try
  183. {
  184. ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature( ArtifactCleanupFeature.class );
  185. throw new Exception("artifactCleanupFeature should not be available");
  186. } catch ( UnsupportedFeatureException e ) {
  187. // correct
  188. }
  189. IndexCreationFeature indexCreationFeature = mr.getFeature( IndexCreationFeature.class );
  190. assertEquals("local/.index", indexCreationFeature.getIndexPath().toString());
  191. try
  192. {
  193. StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature( StagingRepositoryFeature.class );
  194. throw new Exception("stagingRepositoryFeature should not be available");
  195. } catch (UnsupportedFeatureException e) {
  196. // correct
  197. }
  198. RemoteIndexFeature remoteIndexFeature = mr.getFeature( RemoteIndexFeature.class );
  199. assertNull(remoteIndexFeature.getProxyId());
  200. }
  201. @Test
  202. public void getManagedConfiguration() throws Exception {
  203. MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/repositories") );
  204. repo.setLocation( new URI("target/this.is/a/test") );
  205. repo.setScanned( true );
  206. repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
  207. repo.setLayout( "maven2" );
  208. repo.setBlocksRedeployment( true );
  209. repo.setName( repo.getPrimaryLocale(), "test0002" );
  210. repo.setSchedulingDefinition( "0 0 05 ? * WED" );
  211. repo.addActiveReleaseScheme( ReleaseScheme.RELEASE );
  212. repo.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
  213. StagingRepositoryFeature stagingFeat = repo.getFeature( StagingRepositoryFeature.class );
  214. stagingFeat.setStageRepoNeeded( true );
  215. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class );
  216. indexCreationFeature.setIndexPath( new URI("test/.indexes") );
  217. indexCreationFeature.setSkipPackedIndexCreation( true );
  218. ArtifactCleanupFeature artifactCleanupFeature = repo.getFeature( ArtifactCleanupFeature.class );
  219. artifactCleanupFeature.setRetentionPeriod( Period.ofDays( 5 ) );
  220. artifactCleanupFeature.setRetentionCount( 7 );
  221. artifactCleanupFeature.setDeleteReleasedSnapshots( true );
  222. ManagedRepositoryConfiguration cfg = provider.getManagedConfiguration( repo );
  223. assertEquals("target/this.is/a/test", cfg.getLocation());
  224. assertTrue(cfg.isScanned());
  225. assertEquals( "This is a description", cfg.getDescription() );
  226. assertEquals("maven2", cfg.getLayout());
  227. assertTrue(cfg.isBlockRedeployments());
  228. assertEquals("test0002", cfg.getName());
  229. assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
  230. assertTrue(cfg.isStageRepoNeeded());
  231. assertEquals("test/.indexes", cfg.getIndexDir());
  232. assertTrue(cfg.isSkipPackedIndexCreation());
  233. assertEquals(5, cfg.getRetentionPeriod());
  234. assertEquals(7, cfg.getRetentionCount());
  235. assertTrue(cfg.isDeleteReleasedSnapshots());
  236. assertTrue(cfg.isReleases());
  237. assertTrue(cfg.isSnapshots());
  238. assertTrue(cfg.isScanned());
  239. }
  240. @Test
  241. public void getRemoteConfiguration() throws Exception {
  242. MavenRemoteRepository repo = MavenRemoteRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/remotes") );
  243. repo.setLocation( new URI("https://this.is/a/test") );
  244. repo.setScanned( true );
  245. repo.setDescription( repo.getPrimaryLocale(), "This is a description" );
  246. repo.setLayout( "maven2" );
  247. repo.setName( repo.getPrimaryLocale(), "test0003" );
  248. repo.setSchedulingDefinition( "0 0 05 ? * WED" );
  249. RemoteIndexFeature remoteIndexFeature = repo.getFeature( RemoteIndexFeature.class );
  250. remoteIndexFeature.setProxyId( "proxyabc" );
  251. remoteIndexFeature.setDownloadTimeout( Duration.ofSeconds( 54 ) );
  252. remoteIndexFeature.setDownloadRemoteIndex( false );
  253. remoteIndexFeature.setIndexUri( new URI("/this/remote/.index") );
  254. remoteIndexFeature.setDownloadRemoteIndexOnStartup( true );
  255. IndexCreationFeature indexCreationFeature = repo.getFeature( IndexCreationFeature.class );
  256. indexCreationFeature.setIndexPath( new URI("/this/local/.index") );
  257. RemoteRepositoryConfiguration cfg = provider.getRemoteConfiguration( repo );
  258. assertEquals("https://this.is/a/test", cfg.getUrl());
  259. assertEquals( "This is a description", cfg.getDescription() );
  260. assertEquals("maven2", cfg.getLayout());
  261. assertEquals("test0003", cfg.getName());
  262. assertEquals("0 0 05 ? * WED", cfg.getRefreshCronExpression());
  263. assertEquals("/this/remote/.index", cfg.getRemoteIndexUrl());
  264. assertEquals("proxyabc", cfg.getRemoteDownloadNetworkProxyId());
  265. assertEquals(54, cfg.getRemoteDownloadTimeout());
  266. assertFalse(cfg.isDownloadRemoteIndex());
  267. assertTrue(cfg.isDownloadRemoteIndexOnStartup());
  268. assertEquals("/this/local/.index", cfg.getIndexDir());
  269. }
  270. @Test
  271. public void getRepositoryGroupConfiguration() throws RepositoryException, URISyntaxException, IOException {
  272. MavenRepositoryGroup repositoryGroup = MavenRepositoryGroup.newLocalInstance("group1","group1",Paths.get("target/groups"));
  273. MavenManagedRepository repo1 = MavenManagedRepository.newLocalInstance( "test01", "My Test repo", Paths.get("target/repositories") );
  274. MavenManagedRepository repo2 = MavenManagedRepository.newLocalInstance( "test02", "My Test repo", Paths.get("target/repositories") );
  275. repositoryGroup.setDescription(repositoryGroup.getPrimaryLocale(), "Repository group");
  276. repositoryGroup.setLayout("non-default");
  277. IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class );
  278. indexCreationFeature.setIndexPath( new URI(".index2") );
  279. repositoryGroup.setName(repositoryGroup.getPrimaryLocale(), "Repo Group 1");
  280. repositoryGroup.setMergedIndexTTL(1005);
  281. repositoryGroup.setSchedulingDefinition("0 0 04 ? * THU");
  282. repositoryGroup.addRepository(repo1);
  283. repositoryGroup.addRepository(repo2);
  284. RepositoryGroupConfiguration cfg = provider.getRepositoryGroupConfiguration(repositoryGroup);
  285. assertEquals("group1", cfg.getId());
  286. assertEquals(".index2", cfg.getMergedIndexPath());
  287. assertEquals("0 0 04 ? * THU", cfg.getCronExpression());
  288. assertEquals("Repo Group 1", cfg.getName());
  289. assertEquals(1005, cfg.getMergedIndexTtl());
  290. assertTrue(cfg.getRepositories().contains("test01"));
  291. assertTrue(cfg.getRepositories().contains("test02"));
  292. assertEquals(2, cfg.getRepositories().size());
  293. }
  294. @Test
  295. public void createRepositoryGroup() {
  296. EditableRepositoryGroup gr = provider.createRepositoryGroup("group1", "Group 1");
  297. assertEquals("group1",gr.getId());
  298. assertEquals("Group 1", gr.getName());
  299. assertEquals(MavenRepositoryGroup.class, gr.getClass());
  300. }
  301. @Test
  302. public void createRepositoryGroupWithCfg() throws RepositoryException {
  303. RepositoryGroupConfiguration cfg = new RepositoryGroupConfiguration();
  304. cfg.setId("group2");
  305. cfg.setName("Group 2");
  306. cfg.setCronExpression("0 0 03 ? * MON");
  307. cfg.setMergedIndexTtl(504);
  308. cfg.setMergedIndexPath(".index-abc");
  309. ArrayList<String> repos = new ArrayList<>();
  310. repos.add("test01");
  311. repos.add("test02");
  312. cfg.setRepositories(repos);
  313. RepositoryGroup grp = provider.createRepositoryGroup(cfg);
  314. assertEquals("group2", grp.getId());
  315. assertEquals("Group 2", grp.getName());
  316. assertEquals("0 0 03 ? * MON", grp.getSchedulingDefinition());
  317. IndexCreationFeature indexCreationFeature = grp.getFeature( IndexCreationFeature.class );
  318. try {
  319. assertEquals(new URI(".index-abc"), indexCreationFeature.getIndexPath());
  320. } catch (URISyntaxException e) {
  321. e.printStackTrace();
  322. }
  323. assertEquals(504, grp.getMergedIndexTTL());
  324. assertEquals(0, grp.getRepositories().size());
  325. // assertTrue(grp.getRepositories().stream().anyMatch(r -> "test01".equals(r.getId())));
  326. // assertTrue(grp.getRepositories().stream().anyMatch(r -> "test02".equals(r.getId())));
  327. }
  328. }