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