]> source.dussan.org Git - archiva.git/blob
f596b8e23e5895080d58fe491ad0c08ff78089b5
[archiva.git] /
1 package org.apache.archiva.repository.base.remote;
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.filelock.DefaultFileLockManager;
22 import org.apache.archiva.common.filelock.FileLockManager;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.archiva.indexer.IndexManagerFactory;
28 import org.apache.archiva.repository.EditableRemoteRepository;
29 import org.apache.archiva.repository.RemoteRepository;
30 import org.apache.archiva.repository.Repository;
31 import org.apache.archiva.repository.RepositoryContentFactory;
32 import org.apache.archiva.repository.RepositoryException;
33 import org.apache.archiva.repository.RepositoryState;
34 import org.apache.archiva.repository.RepositoryType;
35 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
36 import org.apache.archiva.repository.base.ConfigurationHandler;
37 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
38 import org.apache.archiva.repository.validation.CheckedResult;
39 import org.apache.archiva.repository.validation.RepositoryValidator;
40 import org.apache.archiva.repository.validation.ValidationError;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.mockito.junit.jupiter.MockitoExtension;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
46
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 import java.io.IOException;
50 import java.nio.file.Files;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Map;
56
57 import static org.apache.archiva.repository.validation.ErrorKeys.ISEMPTY;
58 import static org.junit.jupiter.api.Assertions.*;
59
60 /**
61  * @author Martin Stockhammer <martin_s@apache.org>
62  */
63 @ExtendWith( {MockitoExtension.class, SpringExtension.class} )
64 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-remote.xml"} )
65 class RemoteRepositoryHandlerTest
66 {
67     static {
68         initialize();
69     }
70
71     @Inject
72     @Named( "repositoryRegistry" )
73     ArchivaRepositoryRegistry repositoryRegistry;
74
75     @Inject
76     @Named( "repositoryContentFactory#default" )
77     RepositoryContentFactory repositoryContentFactory;
78
79     @Inject
80     IndexManagerFactory indexManagerFactory;
81
82     @Inject
83     ConfigurationHandler configurationHandler;
84
85     @SuppressWarnings( "unused" )
86     @Inject
87     List<RepositoryValidator<? extends Repository>> repositoryValidatorList;
88
89     @Inject
90     ArchivaConfiguration archivaConfiguration;
91
92     Path repoBaseDir;
93
94
95     private static void initialize() {
96         Path baseDir = Paths.get( FileUtils.getBasedir( ) );
97         Path config = baseDir.resolve( "src/test/resources/archiva-remote.xml" );
98         if ( Files.exists( config ) )
99         {
100             Path destConfig = baseDir.resolve( "target/test-classes/archiva-remote.xml" );
101             try
102             {
103                 Files.copy( config, destConfig );
104             }
105             catch ( IOException e )
106             {
107                 System.err.println( "Could not copy file: " + e.getMessage( ) );
108             }
109         }
110     }
111
112     // Helper method that removes a repo from the configuration
113     private void removeRepositoryFromConfig( String id) {
114         Configuration configuration = configurationHandler.getBaseConfiguration( );
115         Iterator<RemoteRepositoryConfiguration> iter = configuration.getRemoteRepositories().iterator();
116         while(iter.hasNext()) {
117             RemoteRepositoryConfiguration repo = iter.next( );
118             if (id.equals(repo.getId())) {
119                 iter.remove();
120                 break;
121             }
122         }
123         try
124         {
125             configurationHandler.save( configuration );
126         }
127         catch ( Throwable e )
128         {
129             System.err.println( "Could not remove repo from config "+id );
130         }
131     }
132
133     private boolean hasRepositoryInConfig( String id) {
134         assertNotNull( configurationHandler.getBaseConfiguration( ).getRemoteRepositories() );
135         return configurationHandler.getBaseConfiguration( ).getRemoteRepositories( ).stream( ).anyMatch( g -> g != null && id.equals( g.getId( ) ) ) ;
136     }
137
138
139     private RemoteRepositoryHandler createHandler( )
140     {
141         RemoteRepositoryHandler repositoryHandler = new RemoteRepositoryHandler( repositoryRegistry, configurationHandler, indexManagerFactory, repositoryContentFactory );
142         repositoryHandler.init( );
143         return repositoryHandler;
144     }
145
146     private Path getRepoBaseDir() throws IOException
147     {
148         if (repoBaseDir==null) {
149             this.repoBaseDir = archivaConfiguration.getRepositoryBaseDir( ).resolve( "remote" );
150             Files.createDirectories( this.repoBaseDir );
151         }
152         return repoBaseDir;
153     }
154
155     protected EditableRemoteRepository createRepository( String id, String name, Path location ) throws IOException
156     {
157         FileLockManager lockManager = new DefaultFileLockManager();
158         FilesystemStorage storage = new FilesystemStorage(location.toAbsolutePath(), lockManager);
159         BasicRemoteRepository repo = new BasicRemoteRepository(id, name, storage);
160         repo.setLocation( location.toAbsolutePath().toUri());
161         return repo;
162     }
163
164     protected EditableRemoteRepository createRepository( String id, String name) throws IOException
165     {
166         Path dir = getRepoBaseDir( ).resolve( id );
167         Files.createDirectories( dir );
168         return createRepository( id, name, dir );
169     }
170
171
172
173
174     @Test
175     void initializeFromConfig( )
176     {
177         RemoteRepositoryHandler repoHandler = createHandler( );
178         repoHandler.initializeFromConfig( );
179         assertEquals( 2, repoHandler.getAll( ).size( ) );
180         assertNotNull( repoHandler.get( "test-repo-01" ) );
181         assertEquals( "Test Remote Repository", repoHandler.get( "test-repo-01" ).getName() );
182     }
183
184     @Test
185     void activateRepository( ) throws RepositoryException
186     {
187         String id = "test-repo-02";
188         RemoteRepositoryHandler repoHandler = createHandler( );
189         RemoteRepository repo = repoHandler.newInstance( RepositoryType.MAVEN, id );
190         repoHandler.activateRepository( repo );
191         assertFalse( hasRepositoryInConfig( id ));
192         assertNull( repoHandler.get( id ) );
193     }
194
195     @Test
196     void newInstancesFromConfig( ) throws RepositoryException
197     {
198         final String id = "test-repo-01";
199         RemoteRepositoryHandler repoHandler = createHandler( );
200         Configuration configuration = new Configuration( );
201         repoHandler.remove( "test-repo-01", configuration );
202         Map<String, RemoteRepository> instances = repoHandler.newInstancesFromConfig( );
203         assertFalse( repoHandler.hasRepository( id ) );
204         assertTrue( instances.containsKey( id ) );
205         assertEquals( RepositoryState.REFERENCES_SET, instances.get( id ).getLastState( ) );
206     }
207
208     @Test
209     void newInstance( ) throws RepositoryException
210     {
211         String id = "test-repo-03";
212         RemoteRepositoryHandler repoHandler = createHandler( );
213         RemoteRepository instance = repoHandler.newInstance( RepositoryType.MAVEN, id );
214         assertNotNull( instance );
215         assertEquals( id, instance.getId( ) );
216         assertFalse( repoHandler.hasRepository( id ) );
217         assertEquals( RepositoryState.REFERENCES_SET, instance.getLastState( ) );
218         assertFalse( hasRepositoryInConfig( id ) );
219     }
220
221
222     @Test
223     void put( ) throws IOException, RepositoryException
224     {
225         final String id = "test-repo-04";
226         try
227         {
228             RemoteRepositoryHandler repoHandler = createHandler( );
229             EditableRemoteRepository repository = createRepository( id, "n-"+id );
230             repoHandler.put( repository );
231             RemoteRepository storedRepository = repoHandler.get( id );
232             assertNotNull( storedRepository );
233             assertEquals( id, storedRepository.getId( ) );
234             assertEquals( "n-"+id, storedRepository.getName( ) );
235
236             EditableRemoteRepository repository2 = createRepository( id, "n2-"+id );
237             repoHandler.put( repository2 );
238             storedRepository = repoHandler.get( id );
239             assertNotNull( storedRepository );
240             assertEquals( id, storedRepository.getId( ) );
241             assertEquals( "n2-"+id, storedRepository.getName( ) );
242
243             assertTrue( hasRepositoryInConfig( id ));
244         } finally {
245             removeRepositoryFromConfig( id );
246         }
247     }
248
249     @Test
250     void putWithConfiguration( ) throws RepositoryException
251     {
252         String id = "test-repo-05";
253
254         try
255         {
256             RemoteRepositoryHandler repoHandler = createHandler( );
257             RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
258             configuration.setId( id );
259             configuration.setName( "n-" + id );
260             repoHandler.put( configuration );
261
262             RemoteRepository repo = repoHandler.get( id );
263             assertNotNull( repo );
264             assertEquals( id, repo.getId( ) );
265             assertEquals( "n-" + id, repo.getName( ) );
266             assertTrue( hasRepositoryInConfig( id ) );
267         }
268         finally
269         {
270             removeRepositoryFromConfig( id );
271         }
272     }
273
274     @Test
275     void testPutWithoutRegister( ) throws RepositoryException
276     {
277         final String id = "test-repo-06";
278         RemoteRepositoryHandler repoHandler = createHandler( );
279         Configuration aCfg = new Configuration( );
280         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
281         configuration.setId( id );
282         configuration.setName( "n-"+id );
283         repoHandler.put( configuration, aCfg );
284
285         RemoteRepository repo = repoHandler.get( id );
286         assertNull( repo );
287         assertFalse( hasRepositoryInConfig( id ) );
288         assertTrue( aCfg.getRemoteRepositories( ).stream( ).anyMatch( g -> g!=null && id.equals( g.getId( ) ) ) );
289
290     }
291
292     @Test
293     void putWithCheck_invalid( ) throws RepositoryException
294     {
295         final String id = "test-repo-07";
296         final String name = "n-"+id;
297         try
298         {
299             RemoteRepositoryHandler repoHandler = createHandler( );
300             BasicRemoteRepositoryValidator checker = new BasicRemoteRepositoryValidator( configurationHandler );
301             checker.setRepositoryRegistry( repositoryRegistry );
302             RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration();
303             configuration.setId( "" );
304             configuration.setName( name );
305             CheckedResult<RemoteRepository, Map<String, List<ValidationError>>> result = repoHandler.putWithCheck( configuration, checker );
306             assertNull( repoHandler.get( id ) );
307             assertNotNull( result.getResult( ) );
308             assertNotNull( result.getResult( ).get( "id" ) );
309             assertEquals( 2, result.getResult( ).get( "id" ).size( ) );
310             assertEquals( ISEMPTY, result.getResult( ).get( "id" ).get( 0 ).getType( ) );
311             assertFalse( hasRepositoryInConfig( id ) );
312             assertFalse( hasRepositoryInConfig( "" ) );
313         } finally
314         {
315             removeRepositoryFromConfig( id );
316         }
317     }
318
319     @Test
320     void remove( ) throws RepositoryException
321     {
322         final String id = "test-repo-08";
323         RemoteRepositoryHandler repoHandler = createHandler( );
324         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
325         configuration.setId( id );
326         configuration.setName( "n-"+id );
327         repoHandler.put( configuration );
328         assertTrue( hasRepositoryInConfig( id ) );
329         assertNotNull( repoHandler.get( id ) );
330         repoHandler.remove( id );
331         assertNull( repoHandler.get( id ) );
332         assertFalse( hasRepositoryInConfig( id ) );
333     }
334
335     @Test
336     void removeWithoutSave( ) throws RepositoryException
337     {
338         final String id = "test-repo-09";
339         RemoteRepositoryHandler repoHandler = createHandler( );
340         Configuration aCfg = new Configuration( );
341         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
342         configuration.setId( id );
343         configuration.setName( "n-"+id );
344         repoHandler.put( configuration, aCfg );
345         assertTrue( aCfg.getRemoteRepositories( ).stream( ).anyMatch( g -> g != null && id.equals( g.getId( ) ) ) );
346         repoHandler.remove( id, aCfg );
347         assertNull( repoHandler.get( id ) );
348         assertTrue( aCfg.getRemoteRepositories( ).stream( ).noneMatch( g -> g != null && id.equals( g.getId( ) ) ) );
349         assertNull( repoHandler.get( id ) );
350
351     }
352
353
354     @Test
355     void validateRepository( ) throws IOException
356     {
357         final String id = "test-repo-10";
358         RemoteRepositoryHandler repoHandler = createHandler( );
359         EditableRemoteRepository repository = createRepository( id, "n-"+id );
360         CheckedResult<RemoteRepository, Map<String, List<ValidationError>>> result = repoHandler.validateRepository( repository );
361         assertNotNull( result );
362         assertEquals( 0, result.getResult( ).size( ) );
363
364         repository = createRepository( id, "n-test-repo-10###" );
365         result = repoHandler.validateRepository( repository );
366         assertNotNull( result );
367         assertEquals( 1, result.getResult( ).size( ) );
368         assertNotNull( result.getResult().get( "name" ) );
369
370     }
371
372     @Test
373     void validateRepositoryIfExisting( ) throws IOException, RepositoryException
374     {
375         final String id = "test-repo-11";
376         try
377         {
378             RemoteRepositoryHandler repoHandler = createHandler( );
379             EditableRemoteRepository repository = createRepository( id, "n-" + id );
380             repoHandler.put( repository );
381             CheckedResult<RemoteRepository, Map<String, List<ValidationError>>> result = repoHandler.validateRepository( repository );
382             assertNotNull( result );
383             assertEquals( 1, result.getResult( ).size( ) );
384         } finally
385         {
386             removeRepositoryFromConfig( id );
387         }
388
389     }
390
391     @Test
392     void validateRepositoryForUpdate( ) throws IOException, RepositoryException
393     {
394         final String id = "test-repo-12";
395         try
396         {
397             RemoteRepositoryHandler repoHandler = createHandler( );
398             EditableRemoteRepository repository = createRepository( id, "n-" + id );
399             repoHandler.put( repository );
400             CheckedResult<RemoteRepository, Map<String, List<ValidationError>>> result = repoHandler.validateRepositoryForUpdate( repository );
401             assertNotNull( result );
402             assertEquals( 0, result.getResult( ).size( ) );
403         } finally
404         {
405             removeRepositoryFromConfig( id );
406         }
407     }
408
409     @Test
410     void has( ) throws IOException, RepositoryException
411     {
412         final String id = "test-repo-13";
413         try
414         {
415             RemoteRepositoryHandler repoHandler = createHandler( );
416             EditableRemoteRepository repository = createRepository( id, "n-" + id );
417             assertFalse( repoHandler.hasRepository( id ) );
418             repoHandler.put( repository );
419             assertTrue( repoHandler.hasRepository( id ) );
420         } finally
421         {
422             removeRepositoryFromConfig( id );
423         }
424     }
425
426 }