]> source.dussan.org Git - archiva.git/blob
e3bb9404b8201bddb136f7fc660e43116e589ef9
[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.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;
42
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;
49
50 import static org.assertj.core.api.Assertions.assertThat;
51
52 /**
53  * @author Olivier Lamy
54  */
55 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
56 public class DownloadMergedIndexNonDefaultPathTest
57     extends AbstractDownloadTest
58 {
59
60     private static Path appServerBase;
61     private Path indexDir;
62
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_appsrv3_" ).toAbsolutePath();
70         System.setProperty( "appserver.base", appServerBase.toString( ) );
71     }
72
73     @AfterClass
74     public static void resetAppServerBase()
75     {
76         if (Files.exists(appServerBase)) {
77             org.apache.commons.io.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             org.apache.commons.io.FileUtils.deleteDirectory( indexDir.toFile() );
103         }
104     }
105
106     @Test
107     public void downloadMergedIndexWithNonDefaultPath()
108         throws Exception
109     {
110
111         Path indexBaseDir = indexDir.resolve("remotedownloadtest");
112         if (!Files.exists(indexBaseDir)) {
113             Files.createDirectories( indexBaseDir );
114         }
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 );
120
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() );
127
128
129         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
130
131         if ( managedRepositoriesService.getManagedRepository( id ) != null )
132         {
133             managedRepositoriesService.deleteManagedRepository( id, false );
134         }
135
136         getManagedRepositoriesService().addManagedRepository( managedRepository );
137
138         RepositoriesService repositoriesService = getRepositoriesService();
139
140         repositoriesService.scanRepositoryNow( id, true );
141
142         // wait a bit to ensure index is finished
143         int timeout = 20000;
144         while ( timeout > 0 && repositoriesService.getScanStatus( id ).isAlreadyScanning() )
145         {
146             Thread.sleep( 500 );
147             timeout -= 500;
148         }
149
150         RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
151
152         String repoGroupId = "test-group";
153
154         if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
155         {
156             repositoryGroupService.deleteRepositoryGroup( repoGroupId );
157         }
158
159         RepositoryGroup repositoryGroup = new RepositoryGroup();
160         repositoryGroup.setId( repoGroupId );
161         String path = ".fooooo";
162         repositoryGroup.setRepositories( Arrays.asList( id ) );
163         repositoryGroup.setMergedIndexPath( path );
164
165         repositoryGroupService.addRepositoryGroup( repositoryGroup );
166
167
168
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 );
175
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() );
182
183         if ( managedRepositoriesService.getManagedRepository( id ) != null )
184         {
185             managedRepositoriesService.deleteManagedRepository( id, false );
186         }
187
188         getManagedRepositoriesService().addManagedRepository( managedRepository );
189
190         String remoteId = Long.toString( System.currentTimeMillis() );
191
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() );
200
201         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
202
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 );
209
210         repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
211
212         // wait a bit
213         /*
214         timeout = 20000;
215         while ( timeout > 0 )
216         {
217             Thread.sleep( 500 );
218             timeout -= 500;
219         }*/
220         // wait the end
221         while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
222         {
223             Thread.sleep( 500 );
224             log.debug( "still running remote download" );
225         }
226
227         SearchService searchService = getSearchService();
228
229         SearchRequest request = new SearchRequest();
230         request.setRepositories( Arrays.asList( id ) );
231         request.setGroupId( "org.apache.felix" );
232
233         List<Artifact> artifacts = searchService.searchArtifacts( request );
234         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
235
236     }
237 }