]> source.dussan.org Git - archiva.git/blob
213d19934f66785bcfd88d10be05c95fa8645442
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
43
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;
50
51 import static org.assertj.core.api.Assertions.assertThat;
52
53 /**
54  * @author Olivier Lamy
55  */
56 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
57 public class DownloadMergedIndexTest
58     extends AbstractDownloadTest
59 {
60
61     private static Path appServerBase;
62     private Path indexDir;
63
64     @BeforeClass
65     public static void setAppServerBase()
66         throws IOException
67     {
68         previousAppServerBase = System.getProperty( "appserver.base" );
69         appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv4_" ).toAbsolutePath();
70         System.setProperty( "appserver.base", appServerBase.toString( ) );
71     }
72
73     @AfterClass
74     public static void resetAppServerBase()
75     {
76         if (Files.exists(appServerBase)) {
77             FileUtils.deleteQuietly( appServerBase.toFile() );
78         }
79         System.setProperty( "appserver.base", previousAppServerBase );
80     }
81
82     @Override
83     protected String getSpringConfigLocation()
84     {
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";
87     }
88
89     @Before
90     public void init() throws IOException
91     {
92         indexDir = Files.createTempDirectory( "archiva-web-common-index" );
93     }
94
95     @After
96     public void cleanup()
97         throws Exception
98     {
99         super.tearDown();
100         if ( Files.exists( indexDir ) )
101         {
102             FileUtils.deleteDirectory( indexDir.toFile() );
103         }
104     }
105
106
107     @Test
108     public void downloadMergedIndex()
109         throws Exception
110     {
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 );
116
117
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() );
123
124         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
125
126         if ( managedRepositoriesService.getManagedRepository( id ) != null )
127         {
128             managedRepositoriesService.deleteManagedRepository( id, false );
129         }
130
131         getManagedRepositoriesService().addManagedRepository( managedRepository );
132
133         RepositoriesService repositoriesService = getRepositoriesService();
134
135         repositoriesService.scanRepositoryNow( id, true );
136
137         // wait a bit to ensure index is finished
138         int timeout = 20000;
139         while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
140         {
141             Thread.sleep( 500 );
142             timeout -= 500;
143         }
144
145         RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
146
147         String repoGroupId = "test-group";
148
149         if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
150         {
151             repositoryGroupService.deleteRepositoryGroup( repoGroupId );
152         }
153
154         RepositoryGroup repositoryGroup = new RepositoryGroup();
155         repositoryGroup.setId( repoGroupId );
156         repositoryGroup.setRepositories( Arrays.asList( id ) );
157
158         repositoryGroupService.addRepositoryGroup( repositoryGroup );
159
160         // create a repo with a remote on the one with index
161         id = Long.toString( System.currentTimeMillis() );
162
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 );
167
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() );
173
174         if ( managedRepositoriesService.getManagedRepository( id ) != null )
175         {
176             managedRepositoriesService.deleteManagedRepository( id, false );
177         }
178
179         getManagedRepositoriesService().addManagedRepository( managedRepository );
180
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() );
189
190         if ( getRemoteRepositoriesService().getRemoteRepository( remoteRepository.getId() ) != null )
191         {
192             getRemoteRepositoriesService().deleteRemoteRepository( remoteRepository.getId() );
193         }
194
195         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
196
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 );
203
204         repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
205
206         // wait a bit
207         timeout = 20000;
208         while ( timeout > 0 )
209         {
210             Thread.sleep( 500 );
211             timeout -= 500;
212         }
213
214         SearchService searchService = getSearchService();
215
216         SearchRequest request = new SearchRequest();
217         request.setRepositories( Arrays.asList( id ) );
218         request.setGroupId( "org.apache.felix" );
219
220         List<Artifact> artifacts = searchService.searchArtifacts( request );
221         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
222
223     }
224 }