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.maven2.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.redback.rest.services.FakeCreateAdminService;
29 import org.apache.archiva.rest.api.model.SearchRequest;
30 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
31 import org.apache.archiva.rest.api.services.ProxyConnectorService;
32 import org.apache.archiva.rest.api.services.RepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoryGroupService;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
36 import org.apache.commons.io.FileUtils;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
44 import java.io.IOException;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.util.Arrays;
48 import java.util.List;
49 import java.util.Locale;
51 import static org.assertj.core.api.Assertions.assertThat;
54 * @author Olivier Lamy
56 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
57 public class DownloadMergedIndexTest
58 extends AbstractDownloadTest
61 private static Path appServerBase;
62 private Path indexDir;
65 public static void setAppServerBase()
68 previousAppServerBase = System.getProperty( "appserver.base" );
69 appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv4_" ).toAbsolutePath();
70 System.setProperty( "appserver.base", appServerBase.toString( ) );
74 public static void resetAppServerBase()
76 if (Files.exists(appServerBase)) {
77 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 FileUtils.deleteDirectory( indexDir.toFile() );
108 public void downloadMergedIndex()
111 String id = Long.toString( System.currentTimeMillis() );
112 Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
113 Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
114 FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
115 createdPaths.add( testRep );
118 ManagedRepository managedRepository = new ManagedRepository( Locale.getDefault());
119 managedRepository.setId( id );
120 managedRepository.setName( "name of " + id );
121 managedRepository.setLocation( testRep.toString() );
122 managedRepository.setIndexDirectory( indexDir.resolve( "index-" + id ).toString() );
124 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
126 if ( managedRepositoriesService.getManagedRepository( id ) != null )
128 managedRepositoriesService.deleteManagedRepository( id, false );
131 getManagedRepositoriesService().addManagedRepository( managedRepository );
133 RepositoriesService repositoriesService = getRepositoriesService();
135 repositoriesService.scanRepositoryNow( id, true );
137 // wait a bit to ensure index is finished
139 while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
145 RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
147 String repoGroupId = "test-group";
149 if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
151 repositoryGroupService.deleteRepositoryGroup( repoGroupId );
154 RepositoryGroup repositoryGroup = new RepositoryGroup();
155 repositoryGroup.setId( repoGroupId );
156 repositoryGroup.setRepositories( Arrays.asList( id ) );
158 repositoryGroupService.addRepositoryGroup( repositoryGroup );
160 // create a repo with a remote on the one with index
161 id = Long.toString( System.currentTimeMillis() );
163 Path srcRep2 = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
164 Path testRep2 = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
165 FileUtils.copyDirectory( srcRep2.toFile( ), testRep2.toFile( ) );
166 createdPaths.add( testRep2 );
168 managedRepository = new ManagedRepository(Locale.getDefault());
169 managedRepository.setId( id );
170 managedRepository.setName( "name of " + id );
171 managedRepository.setLocation( testRep2.toString() );
172 managedRepository.setIndexDirectory( indexDir.resolve( "index-" + id ).toString() );
174 if ( managedRepositoriesService.getManagedRepository( id ) != null )
176 managedRepositoriesService.deleteManagedRepository( id, false );
179 getManagedRepositoriesService().addManagedRepository( managedRepository );
181 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
182 remoteRepository.setId( "all-merged" );
183 remoteRepository.setName( "all-merged" );
184 remoteRepository.setDownloadRemoteIndex( true );
185 remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
186 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.index" );
187 remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
188 remoteRepository.setPassword( BaseSetup.getAdminPwd() );
190 if ( getRemoteRepositoriesService().getRemoteRepository( remoteRepository.getId() ) != null )
192 getRemoteRepositoriesService().deleteRemoteRepository( remoteRepository.getId() );
195 getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
197 ProxyConnectorService proxyConnectorService = getProxyConnectorService();
198 ProxyConnector proxyConnector = new ProxyConnector();
199 proxyConnector.setProxyId( "foo-bar1" );
200 proxyConnector.setSourceRepoId( id );
201 proxyConnector.setTargetRepoId( "all-merged" );
202 proxyConnectorService.addProxyConnector( proxyConnector );
204 repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
208 while ( timeout > 0 )
214 SearchService searchService = getSearchService();
216 SearchRequest request = new SearchRequest();
217 request.setRepositories( Arrays.asList( id ) );
218 request.setGroupId( "org.apache.felix" );
220 List<Artifact> artifacts = searchService.searchArtifacts( request );
221 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );