1 package org.apache.archiva.repository.base.remote;
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.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;
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;
57 import static org.apache.archiva.repository.validation.ErrorKeys.ISEMPTY;
58 import static org.junit.jupiter.api.Assertions.*;
61 * @author Martin Stockhammer <martin_s@apache.org>
63 @ExtendWith( {MockitoExtension.class, SpringExtension.class} )
64 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-remote.xml"} )
65 class RemoteRepositoryHandlerTest
72 @Named( "repositoryRegistry" )
73 ArchivaRepositoryRegistry repositoryRegistry;
76 @Named( "repositoryContentFactory#default" )
77 RepositoryContentFactory repositoryContentFactory;
80 IndexManagerFactory indexManagerFactory;
83 ConfigurationHandler configurationHandler;
85 @SuppressWarnings( "unused" )
87 List<RepositoryValidator<? extends Repository>> repositoryValidatorList;
90 ArchivaConfiguration archivaConfiguration;
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 ) )
100 Path destConfig = baseDir.resolve( "target/test-classes/archiva-remote.xml" );
103 Files.copy( config, destConfig );
105 catch ( IOException e )
107 System.err.println( "Could not copy file: " + e.getMessage( ) );
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())) {
125 configurationHandler.save( configuration );
127 catch ( Throwable e )
129 System.err.println( "Could not remove repo from config "+id );
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( ) ) ) ;
139 private RemoteRepositoryHandler createHandler( )
141 RemoteRepositoryHandler repositoryHandler = new RemoteRepositoryHandler( repositoryRegistry, configurationHandler, indexManagerFactory, repositoryContentFactory );
142 repositoryHandler.init( );
143 return repositoryHandler;
146 private Path getRepoBaseDir() throws IOException
148 if (repoBaseDir==null) {
149 this.repoBaseDir = archivaConfiguration.getRepositoryBaseDir( ).resolve( "remote" );
150 Files.createDirectories( this.repoBaseDir );
155 protected EditableRemoteRepository createRepository( String id, String name, Path location ) throws IOException
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());
164 protected EditableRemoteRepository createRepository( String id, String name) throws IOException
166 Path dir = getRepoBaseDir( ).resolve( id );
167 Files.createDirectories( dir );
168 return createRepository( id, name, dir );
175 void initializeFromConfig( )
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() );
185 void activateRepository( ) throws RepositoryException
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 ) );
196 void newInstancesFromConfig( ) throws RepositoryException
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( ) );
209 void newInstance( ) throws RepositoryException
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 ) );
223 void put( ) throws IOException, RepositoryException
225 final String id = "test-repo-04";
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( ) );
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( ) );
243 assertTrue( hasRepositoryInConfig( id ));
245 removeRepositoryFromConfig( id );
250 void putWithConfiguration( ) throws RepositoryException
252 String id = "test-repo-05";
256 RemoteRepositoryHandler repoHandler = createHandler( );
257 RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
258 configuration.setId( id );
259 configuration.setName( "n-" + id );
260 repoHandler.put( configuration );
262 RemoteRepository repo = repoHandler.get( id );
263 assertNotNull( repo );
264 assertEquals( id, repo.getId( ) );
265 assertEquals( "n-" + id, repo.getName( ) );
266 assertTrue( hasRepositoryInConfig( id ) );
270 removeRepositoryFromConfig( id );
275 void testPutWithoutRegister( ) throws RepositoryException
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 );
285 RemoteRepository repo = repoHandler.get( id );
287 assertFalse( hasRepositoryInConfig( id ) );
288 assertTrue( aCfg.getRemoteRepositories( ).stream( ).anyMatch( g -> g!=null && id.equals( g.getId( ) ) ) );
293 void putWithCheck_invalid( ) throws RepositoryException
295 final String id = "test-repo-07";
296 final String name = "n-"+id;
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( "" ) );
315 removeRepositoryFromConfig( id );
320 void remove( ) throws RepositoryException
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 ) );
336 void removeWithoutSave( ) throws RepositoryException
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 ) );
355 void validateRepository( ) throws IOException
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( ) );
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" ) );
373 void validateRepositoryIfExisting( ) throws IOException, RepositoryException
375 final String id = "test-repo-11";
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( ) );
386 removeRepositoryFromConfig( id );
392 void validateRepositoryForUpdate( ) throws IOException, RepositoryException
394 final String id = "test-repo-12";
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( ) );
405 removeRepositoryFromConfig( id );
410 void has( ) throws IOException, RepositoryException
412 final String id = "test-repo-13";
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 ) );
422 removeRepositoryFromConfig( id );