1 package org.apache.archiva.repository.base.group;
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
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
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;
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;
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;
62 * @author Martin Stockhammer <martin_s@apache.org>
64 @ExtendWith( {MockitoExtension.class, SpringExtension.class} )
65 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-group.xml"} )
66 class RepositoryGroupHandlerTest
70 @Named( "repositoryRegistry" )
71 ArchivaRepositoryRegistry repositoryRegistry;
74 ConfigurationHandler configurationHandler;
77 // @Named( "mergedRemoteIndexesScheduler#default" )
78 MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
81 List<RepositoryValidator<? extends Repository>> repositoryValidatorList;
84 ArchivaConfiguration archivaConfiguration;
88 private RepositoryGroupHandler createHandler( )
90 RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList );
95 private Path getRepoBaseDir() throws IOException
97 if (repoBaseDir==null) {
98 this.repoBaseDir = archivaConfiguration.getRepositoryBaseDir( ).resolve( "group" );
99 Files.createDirectories( this.repoBaseDir );
106 protected EditableRepositoryGroup createRepository( String id, String name, Path location ) throws IOException
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());
115 protected EditableRepositoryGroup createRepository( String id, String name) throws IOException
117 Path dir = getRepoBaseDir( ).resolve( id );
118 Files.createDirectories( dir );
119 return createRepository( id, name, dir );
124 void initializeFromConfig( )
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( ) );
134 void activateRepository( ) throws RepositoryException
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( ) );
144 void newInstancesFromConfig( )
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( ) );
154 void newInstance( ) throws RepositoryException
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( ) );
166 void put( ) throws IOException, RepositoryException
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( ) );
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( ) );
183 assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) );
184 assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-04".equals( g.getId( ) ) ) );
188 void testPut( ) throws RepositoryException
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 );
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( ) );
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( ) ) ) );
212 void testPutWithoutRegister( ) throws RepositoryException
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 );
224 RepositoryGroup repo = groupHandler.get( "test-group-06" );
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( ) ) ) );
233 void putWithCheck( ) throws RepositoryException
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( ) );
252 void remove( ) throws RepositoryException
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" ) );
265 void testRemove( ) throws RepositoryException
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" ) );
286 void validateRepository( ) throws IOException
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( ) );
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" ) );
305 void validateRepositoryIfExisting( ) throws IOException, RepositoryException
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( ) );
319 void validateRepositoryForUpdate( ) throws IOException, RepositoryException
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( ) );
332 void has( ) throws IOException, RepositoryException
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" ) );