1 package org.apache.archiva.remotedownload;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * 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.admin.model.beans.ManagedRepository;
22 import org.apache.archiva.admin.model.beans.ProxyConnector;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.beans.RepositoryGroup;
25 import org.apache.archiva.maven.model.Artifact;
26 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
27 import org.apache.archiva.redback.rest.services.BaseSetup;
28 import org.apache.archiva.rest.api.model.SearchRequest;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.ProxyConnectorService;
31 import org.apache.archiva.rest.api.services.RepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoryGroupService;
33 import org.apache.archiva.rest.api.services.SearchService;
34 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
35 import org.apache.commons.io.FileUtils;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
43 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.util.Arrays;
47 import java.util.List;
48 import java.util.Locale;
50 import static org.assertj.core.api.Assertions.assertThat;
53 * @author Olivier Lamy
55 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
56 public class DownloadMergedIndexNonDefaultPathTest
57 extends AbstractDownloadTest
60 private static Path appServerBase;
61 private Path indexDir;
65 public static void setAppServerBase()
68 previousAppServerBase = System.getProperty( "appserver.base" );
69 appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv3_" ).toAbsolutePath();
70 System.setProperty( "appserver.base", appServerBase.toString( ) );
74 public static void resetAppServerBase()
76 if (Files.exists(appServerBase)) {
77 org.apache.commons.io.FileUtils.deleteQuietly( appServerBase.toFile() );
79 System.setProperty( "appserver.base", previousAppServerBase );
83 protected String getSpringConfigLocation()
85 System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
86 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
90 public void init() throws IOException
92 indexDir = Files.createTempDirectory( "archiva-web-common-index" );
100 if ( Files.exists( indexDir ) )
102 org.apache.commons.io.FileUtils.deleteDirectory( indexDir.toFile() );
107 public void downloadMergedIndexWithNonDefaultPath()
111 Path indexBaseDir = indexDir.resolve("remotedownloadtest");
112 if (!Files.exists(indexBaseDir)) {
113 Files.createDirectories( indexBaseDir );
115 String id = Long.toString( System.currentTimeMillis() );
116 Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
117 Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
118 FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
119 createdPaths.add( testRep );
121 ManagedRepository managedRepository = new ManagedRepository( Locale.getDefault());
122 managedRepository.setId( id );
123 managedRepository.setName( "name of " + id );
124 managedRepository.setLocation( testRep.toString() );
125 managedRepository.setIndexDirectory( indexBaseDir.resolve( "index-" + id ).toString() );
126 managedRepository.setPackedIndexDirectory( indexBaseDir.resolve( "indexPacked-" + id ).toString() );
129 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
131 if ( managedRepositoriesService.getManagedRepository( id ) != null )
133 managedRepositoriesService.deleteManagedRepository( id, false );
136 getManagedRepositoriesService().addManagedRepository( managedRepository );
138 RepositoriesService repositoriesService = getRepositoriesService();
140 repositoriesService.scanRepositoryNow( id, true );
142 // wait a bit to ensure index is finished
144 while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
150 RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
152 String repoGroupId = "test-group";
154 if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
156 repositoryGroupService.deleteRepositoryGroup( repoGroupId );
159 RepositoryGroup repositoryGroup = new RepositoryGroup();
160 repositoryGroup.setId( repoGroupId );
161 String path = ".fooooo";
162 repositoryGroup.setRepositories( Arrays.asList( id ) );
163 repositoryGroup.setMergedIndexPath( path );
165 repositoryGroupService.addRepositoryGroup( repositoryGroup );
169 // create a repo with a remote on the one with index
170 id = Long.toString( System.currentTimeMillis() );
171 Path srcRep2 = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
172 Path testRep2 = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
173 FileUtils.copyDirectory( srcRep2.toFile( ), testRep2.toFile( ) );
174 createdPaths.add( testRep2 );
176 managedRepository = new ManagedRepository(Locale.getDefault());
177 managedRepository.setId( id );
178 managedRepository.setName( "name of " + id );
179 managedRepository.setLocation( testRep2.toString() );
180 managedRepository.setIndexDirectory( indexBaseDir.resolve( "index-" + id ).toString() );
181 managedRepository.setPackedIndexDirectory( indexBaseDir.resolve( "indexpacked-" + id ).toString() );
183 if ( managedRepositoriesService.getManagedRepository( id ) != null )
185 managedRepositoriesService.deleteManagedRepository( id, false );
188 getManagedRepositoriesService().addManagedRepository( managedRepository );
190 String remoteId = Long.toString( System.currentTimeMillis() );
192 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
193 remoteRepository.setId( remoteId );
194 remoteRepository.setName( remoteId );
195 remoteRepository.setDownloadRemoteIndex( true );
196 remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
197 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
198 remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
199 remoteRepository.setPassword( BaseSetup.getAdminPwd() );
201 getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
203 ProxyConnectorService proxyConnectorService = getProxyConnectorService();
204 ProxyConnector proxyConnector = new ProxyConnector();
205 proxyConnector.setProxyId( "foo-bar2" );
206 proxyConnector.setSourceRepoId( id );
207 proxyConnector.setTargetRepoId( remoteId );
208 proxyConnectorService.addProxyConnector( proxyConnector );
210 repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
215 while ( timeout > 0 )
221 while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
224 log.debug( "still running remote download" );
227 SearchService searchService = getSearchService();
229 SearchRequest request = new SearchRequest();
230 request.setRepositories( Arrays.asList( id ) );
231 request.setGroupId( "org.apache.felix" );
233 List<Artifact> artifacts = searchService.searchArtifacts( request );
234 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );