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.rest.api.model.SearchRequest;
27 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
28 import org.apache.archiva.rest.api.services.ProxyConnectorService;
29 import org.apache.archiva.rest.api.services.RepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoryGroupService;
31 import org.apache.archiva.rest.api.services.SearchService;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
34 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
43 import java.io.IOException;
44 import java.util.Arrays;
45 import java.util.List;
47 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
50 * @author Olivier Lamy
52 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
53 public class DownloadMergedIndexTest
54 extends AbstractDownloadTest
58 public static void setAppServerBase()
61 previousAppServerBase = System.getProperty( "appserver.base" );
62 System.setProperty( "appserver.base",
63 new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
64 + DownloadMergedIndexTest.class.getName() );
68 public static void resetAppServerBase()
70 System.setProperty( "appserver.base", previousAppServerBase );
74 protected String getSpringConfigLocation()
76 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
84 File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
85 if ( tmpIndexDir.exists() )
87 FileUtils.deleteDirectory( tmpIndexDir );
93 public void downloadMergedIndex()
96 File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
97 if ( tmpIndexDir.exists() )
99 FileUtils.deleteDirectory( tmpIndexDir );
101 String id = Long.toString( System.currentTimeMillis() );
102 ManagedRepository managedRepository = new ManagedRepository();
103 managedRepository.setId( id );
104 managedRepository.setName( "name of " + id );
105 managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
106 managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
108 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
110 if ( managedRepositoriesService.getManagedRepository( id ) != null )
112 managedRepositoriesService.deleteManagedRepository( id, false );
115 getManagedRepositoriesService().addManagedRepository( managedRepository );
117 RepositoriesService repositoriesService = getRepositoriesService();
119 repositoriesService.scanRepositoryNow( id, true );
121 // wait a bit to ensure index is finished
123 while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
129 RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
131 String repoGroupId = "test-group";
133 if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
135 repositoryGroupService.deleteRepositoryGroup( repoGroupId );
138 RepositoryGroup repositoryGroup = new RepositoryGroup();
139 repositoryGroup.setId( repoGroupId );
140 repositoryGroup.setRepositories( Arrays.asList( id ) );
142 repositoryGroupService.addRepositoryGroup( repositoryGroup );
144 // create a repo with a remote on the one with index
145 id = Long.toString( System.currentTimeMillis() );
146 managedRepository = new ManagedRepository();
147 managedRepository.setId( id );
148 managedRepository.setName( "name of " + id );
149 managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
150 managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
152 if ( managedRepositoriesService.getManagedRepository( id ) != null )
154 managedRepositoriesService.deleteManagedRepository( id, false );
157 getManagedRepositoriesService().addManagedRepository( managedRepository );
159 RemoteRepository remoteRepository = new RemoteRepository();
160 remoteRepository.setId( "all-merged" );
161 remoteRepository.setName( "all-merged" );
162 remoteRepository.setDownloadRemoteIndex( true );
163 remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
164 remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
165 remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
166 remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
168 getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
170 ProxyConnectorService proxyConnectorService = getProxyConnectorService();
171 ProxyConnector proxyConnector = new ProxyConnector();
172 proxyConnector.setProxyId( "foo-bar1" );
173 proxyConnector.setSourceRepoId( id );
174 proxyConnector.setTargetRepoId( "all-merged" );
175 proxyConnectorService.addProxyConnector( proxyConnector );
177 repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
181 while ( timeout > 0 )
187 SearchService searchService = getSearchService();
189 SearchRequest request = new SearchRequest();
190 request.setRepositories( Arrays.asList( id ) );
191 request.setGroupId( "org.apache.felix" );
193 List<Artifact> artifacts = searchService.searchArtifacts( request );
194 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );