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