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