]> source.dussan.org Git - archiva.git/blob
cf8de1ce82e3892d62bd79f9e54c139e8ab4c8ad
[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.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
26 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
27 import org.apache.archiva.repository.EditableRepositoryGroup;
28 import org.apache.archiva.repository.Repository;
29 import org.apache.archiva.repository.RepositoryException;
30 import org.apache.archiva.repository.RepositoryGroup;
31 import org.apache.archiva.repository.RepositoryState;
32 import org.apache.archiva.repository.RepositoryType;
33 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
34 import org.apache.archiva.repository.base.ConfigurationHandler;
35 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
36 import org.apache.archiva.repository.validation.CheckedResult;
37 import org.apache.archiva.repository.validation.RepositoryValidator;
38 import org.apache.archiva.repository.validation.ValidationError;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit.jupiter.SpringExtension;
45
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import java.io.IOException;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.Map;
54
55 import static org.apache.archiva.repository.validation.ErrorKeys.ISEMPTY;
56 import static org.junit.jupiter.api.Assertions.*;
57 import static org.mockito.ArgumentMatchers.any;
58 import static org.mockito.ArgumentMatchers.eq;
59 import static org.mockito.Mockito.verify;
60
61 /**
62  * @author Martin Stockhammer <martin_s@apache.org>
63  */
64 @ExtendWith( {MockitoExtension.class, SpringExtension.class} )
65 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-group.xml"} )
66 class RepositoryGroupHandlerTest
67 {
68
69     @Inject
70     @Named( "repositoryRegistry" )
71     ArchivaRepositoryRegistry repositoryRegistry;
72
73     @Inject
74     ConfigurationHandler configurationHandler;
75
76     @Mock
77     // @Named( "mergedRemoteIndexesScheduler#default" )
78     MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
79
80     @Inject
81     List<RepositoryValidator<? extends Repository>> repositoryValidatorList;
82
83     @Inject
84     ArchivaConfiguration archivaConfiguration;
85
86     Path repoBaseDir;
87
88     private RepositoryGroupHandler createHandler( )
89     {
90         RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList );
91         groupHandler.init( );
92         return groupHandler;
93     }
94
95     private Path getRepoBaseDir() throws IOException
96     {
97         if (repoBaseDir==null) {
98             this.repoBaseDir = archivaConfiguration.getRepositoryBaseDir( ).resolve( "group" );
99             Files.createDirectories( this.repoBaseDir );
100         }
101         return repoBaseDir;
102     }
103
104
105
106     protected EditableRepositoryGroup createRepository( String id, String name, Path location ) throws IOException
107     {
108         FileLockManager lockManager = new DefaultFileLockManager();
109         FilesystemStorage storage = new FilesystemStorage(location.toAbsolutePath(), lockManager);
110         BasicRepositoryGroup repo = new BasicRepositoryGroup(id, name, storage);
111         repo.setLocation( location.toAbsolutePath().toUri());
112         return repo;
113     }
114
115     protected EditableRepositoryGroup createRepository( String id, String name) throws IOException
116     {
117         Path dir = getRepoBaseDir( ).resolve( id );
118         Files.createDirectories( dir );
119         return createRepository( id, name, dir );
120     }
121
122
123     @Test
124     void initializeFromConfig( )
125     {
126         RepositoryGroupHandler groupHandler = createHandler( );
127         groupHandler.initializeFromConfig( );
128         assertEquals( 1, groupHandler.getAll( ).size( ) );
129         assertNotNull( groupHandler.get( "test-group-01" ).getRepositories( ) );
130         assertEquals( "internal", groupHandler.get( "test-group-01" ).getRepositories( ).get( 0 ).getId( ) );
131     }
132
133     @Test
134     void activateRepository( ) throws RepositoryException
135     {
136         RepositoryGroupHandler groupHandler = createHandler( );
137         RepositoryGroup repo = groupHandler.newInstance( RepositoryType.MAVEN, "test-group-02" );
138         groupHandler.activateRepository( repo );
139         verify( mergedRemoteIndexesScheduler ).schedule( eq( repo ), any( ) );
140         assertEquals( RepositoryState.INITIALIZED, repo.getLastState( ) );
141     }
142
143     @Test
144     void newInstancesFromConfig( )
145     {
146         RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList );
147         Map<String, RepositoryGroup> instances = groupHandler.newInstancesFromConfig( );
148         assertFalse( groupHandler.hasRepository( "test-group-01" ) );
149         assertTrue( instances.containsKey( "test-group-01" ) );
150         assertEquals( RepositoryState.REFERENCES_SET, instances.get( "test-group-01" ).getLastState( ) );
151     }
152
153     @Test
154     void newInstance( ) throws RepositoryException
155     {
156         RepositoryGroupHandler groupHandler = createHandler( );
157         RepositoryGroup instance = groupHandler.newInstance( RepositoryType.MAVEN, "test-group-03" );
158         assertNotNull( instance );
159         assertEquals( "test-group-03", instance.getId( ) );
160         assertFalse( groupHandler.hasRepository( "test-group-03" ) );
161         assertEquals( RepositoryState.REFERENCES_SET, instance.getLastState( ) );
162     }
163
164
165     @Test
166     void put( ) throws IOException, RepositoryException
167     {
168         RepositoryGroupHandler groupHandler = createHandler( );
169         EditableRepositoryGroup repositoryGroup = createRepository( "test-group-04", "n-test-group-04" );
170         groupHandler.put( repositoryGroup );
171         RepositoryGroup storedGroup = groupHandler.get( "test-group-04" );
172         assertNotNull( storedGroup );
173         assertEquals( "test-group-04", storedGroup.getId( ) );
174         assertEquals( "n-test-group-04", storedGroup.getName( ) );
175
176         EditableRepositoryGroup repositoryGroup2 = createRepository( "test-group-04", "n2-test-group-04" );
177         groupHandler.put( repositoryGroup2 );
178         storedGroup = groupHandler.get( "test-group-04" );
179         assertNotNull( storedGroup );
180         assertEquals( "test-group-04", storedGroup.getId( ) );
181         assertEquals( "n2-test-group-04", storedGroup.getName( ) );
182
183         assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) );
184         assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-04".equals( g.getId( ) ) ) );
185     }
186
187     @Test
188     void testPut( ) throws RepositoryException
189     {
190         RepositoryGroupHandler groupHandler = createHandler( );
191         RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
192         configuration.setId( "test-group-05" );
193         configuration.setName( "n-test-group-05" );
194         ArrayList<String> repos = new ArrayList<>( );
195         repos.add( "internal" );
196         configuration.setRepositories( repos );
197         groupHandler.put( configuration );
198
199         RepositoryGroup repo = groupHandler.get( "test-group-05" );
200         assertNotNull( repo );
201         assertEquals( "test-group-05", repo.getId( ) );
202         assertEquals( "n-test-group-05", repo.getName( ) );
203
204         assertNotNull( repo.getRepositories( ) );
205         assertEquals( 1, repo.getRepositories( ).size( ) );
206         assertEquals( "internal", repo.getRepositories( ).get( 0 ).getId( ) );
207         assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) );
208         assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-05".equals( g.getId( ) ) ) );
209     }
210
211     @Test
212     void testPutWithoutRegister( ) throws RepositoryException
213     {
214         RepositoryGroupHandler groupHandler = createHandler( );
215         Configuration aCfg = new Configuration( );
216         RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
217         configuration.setId( "test-group-06" );
218         configuration.setName( "n-test-group-06" );
219         ArrayList<String> repos = new ArrayList<>( );
220         repos.add( "internal" );
221         configuration.setRepositories( repos );
222         groupHandler.put( configuration, aCfg );
223
224         RepositoryGroup repo = groupHandler.get( "test-group-06" );
225         assertNull( repo );
226         assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) );
227         assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).noneMatch( g -> g!=null && "test-group-06".equals( g.getId( ) ) ) );
228         assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-06".equals( g.getId( ) ) ) );
229
230     }
231
232     @Test
233     void putWithCheck( ) throws RepositoryException
234     {
235         RepositoryGroupHandler groupHandler = createHandler( );
236         BasicRepositoryGroupValidator checker = new BasicRepositoryGroupValidator( configurationHandler );
237         RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
238         configuration.setId( "" );
239         configuration.setName( "n-test-group-07" );
240         ArrayList<String> repos = new ArrayList<>( );
241         repos.add( "internal" );
242         configuration.setRepositories( repos );
243         CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.putWithCheck( configuration, checker );
244         assertNull( groupHandler.get( "test-group-07" ) );
245         assertNotNull( result.getResult( ) );
246         assertNotNull( result.getResult( ).get( "id" ) );
247         assertEquals( 1, result.getResult( ).get( "id" ).size( ) );
248         assertEquals( ISEMPTY, result.getResult( ).get( "id" ).get( 0 ).getType( ) );
249     }
250
251     @Test
252     void remove( ) throws RepositoryException
253     {
254         RepositoryGroupHandler groupHandler = createHandler( );
255         RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
256         configuration.setId( "test-group-08" );
257         configuration.setName( "n-test-group-08" );
258         groupHandler.put( configuration );
259         assertNotNull( groupHandler.get( "test-group-08" ) );
260         groupHandler.remove( "test-group-08" );
261         assertNull( groupHandler.get( "test-group-08" ) );
262     }
263
264     @Test
265     void testRemove( ) throws RepositoryException
266     {
267         RepositoryGroupHandler groupHandler = createHandler( );
268         Configuration aCfg = new Configuration( );
269         RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
270         configuration.setId( "test-group-09" );
271         configuration.setName( "n-test-group-09" );
272         ArrayList<String> repos = new ArrayList<>( );
273         repos.add( "internal" );
274         configuration.setRepositories( repos );
275         groupHandler.put( configuration, aCfg );
276         assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g != null && "test-group-09".equals( g.getId( ) ) ) );
277         groupHandler.remove( "test-group-09", aCfg );
278         assertNull( groupHandler.get( "test-group-09" ) );
279         assertTrue( aCfg.getRepositoryGroups( ).stream( ).noneMatch( g -> g != null && "test-group-09".equals( g.getId( ) ) ) );
280         assertNull( groupHandler.get( "test-group-09" ) );
281
282     }
283
284
285     @Test
286     void validateRepository( ) throws IOException
287     {
288         RepositoryGroupHandler groupHandler = createHandler( );
289         EditableRepositoryGroup repositoryGroup = createRepository( "test-group-10", "n-test-group-10" );
290         repositoryGroup.setMergedIndexTTL( 5 );
291         CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup );
292         assertNotNull( result );
293         assertEquals( 0, result.getResult( ).size( ) );
294
295         repositoryGroup = createRepository( "test-group-10", "n-test-group-10###" );
296         result = groupHandler.validateRepository( repositoryGroup );
297         assertNotNull( result );
298         assertEquals( 2, result.getResult( ).size( ) );
299         assertNotNull( result.getResult().get( "merged_index_ttl" ) );
300         assertNotNull( result.getResult().get( "name" ) );
301
302     }
303
304     @Test
305     void validateRepositoryIfExisting( ) throws IOException, RepositoryException
306     {
307         RepositoryGroupHandler groupHandler = createHandler( );
308         EditableRepositoryGroup repositoryGroup = createRepository( "test-group-11", "n-test-group-11" );
309         repositoryGroup.setMergedIndexTTL( 5 );
310         groupHandler.put( repositoryGroup );
311         CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup );
312         assertNotNull( result );
313         assertEquals( 1, result.getResult( ).size( ) );
314
315
316     }
317
318     @Test
319     void validateRepositoryForUpdate( ) throws IOException, RepositoryException
320     {
321         RepositoryGroupHandler groupHandler = createHandler( );
322         EditableRepositoryGroup repositoryGroup = createRepository( "test-group-12", "n-test-group-12" );
323         repositoryGroup.setMergedIndexTTL( 5 );
324         groupHandler.put( repositoryGroup );
325         CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepositoryForUpdate( repositoryGroup );
326         assertNotNull( result );
327         assertEquals( 0, result.getResult( ).size( ) );
328
329     }
330
331     @Test
332     void has( ) throws IOException, RepositoryException
333     {
334         RepositoryGroupHandler groupHandler = createHandler( );
335         EditableRepositoryGroup repositoryGroup = createRepository( "test-group-13", "n-test-group-13" );
336         repositoryGroup.setMergedIndexTTL( 5 );
337         assertFalse( groupHandler.hasRepository( "test-group-13" ) );
338         groupHandler.put( repositoryGroup );
339         assertTrue( groupHandler.hasRepository( "test-group-13" ) );
340     }
341
342 }