]> source.dussan.org Git - archiva.git/blob
6eec7f84d2b5234525601b910cb6f946e44b9c19
[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.FakeCreateAdminService;
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 static org.assertj.core.api.Assertions.assertThat;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42
43 import java.io.File;
44 import java.util.Arrays;
45 import java.util.List;
46
47 /**
48  * @author Olivier Lamy
49  */
50 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
51 public class DownloadMergedIndexNonDefaultPathTest
52     extends AbstractDownloadTest
53 {
54
55     @BeforeClass
56     public static void setAppServerBase()
57     {
58         previousAppServerBase = System.getProperty( "appserver.base" );
59         System.setProperty( "appserver.base", System.getProperty( "basedir" ) + "/target/" + DownloadMergedIndexNonDefaultPathTest.class.getName() );
60     }
61
62     @AfterClass
63     public static void resetAppServerBase()
64     {
65         System.setProperty( "appserver.base", previousAppServerBase );
66     }
67
68     @Override
69     protected String getSpringConfigLocation()
70     {
71         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
72     }
73
74     @After
75     public void cleanup()
76         throws Exception
77     {
78         super.tearDown();
79         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
80         if ( tmpIndexDir.exists() )
81         {
82             FileUtils.deleteDirectory( tmpIndexDir );
83         }
84     }
85
86
87     @Test
88     public void downloadMergedIndexWithNonDefaultPath()
89         throws Exception
90     {
91         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
92         if ( tmpIndexDir.exists() )
93         {
94             FileUtils.deleteDirectory( tmpIndexDir );
95         }
96         String id = Long.toString( System.currentTimeMillis() );
97         ManagedRepository managedRepository = new ManagedRepository();
98         managedRepository.setId( id );
99         managedRepository.setName( "name of " + id );
100         managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
101         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
102
103         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
104
105         if ( managedRepositoriesService.getManagedRepository( id ) != null )
106         {
107             managedRepositoriesService.deleteManagedRepository( id, false );
108         }
109
110         getManagedRepositoriesService().addManagedRepository( managedRepository );
111
112         RepositoriesService repositoriesService = getRepositoriesService();
113
114         repositoriesService.scanRepositoryNow( id, true );
115
116         // wait a bit to ensure index is finished
117         int timeout = 20000;
118         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
119         {
120             Thread.sleep( 500 );
121             timeout -= 500;
122         }
123
124         RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
125
126         String repoGroupId = "test-group";
127
128         if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
129         {
130             repositoryGroupService.deleteRepositoryGroup( repoGroupId );
131         }
132
133         RepositoryGroup repositoryGroup = new RepositoryGroup();
134         repositoryGroup.setId( repoGroupId );
135         String path = ".fooooo";
136         repositoryGroup.setRepositories( Arrays.asList( id ) );
137         repositoryGroup.setMergedIndexPath( path );
138
139         repositoryGroupService.addRepositoryGroup( repositoryGroup );
140
141         // create a repo with a remote on the one with index
142         id = Long.toString( System.currentTimeMillis() );
143         managedRepository = new ManagedRepository();
144         managedRepository.setId( id );
145         managedRepository.setName( "name of " + id );
146         managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
147         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
148
149         if ( managedRepositoriesService.getManagedRepository( id ) != null )
150         {
151             managedRepositoriesService.deleteManagedRepository( id, false );
152         }
153
154         getManagedRepositoriesService().addManagedRepository( managedRepository );
155
156         String remoteId = Long.toString( System.currentTimeMillis() );
157
158         RemoteRepository remoteRepository = new RemoteRepository();
159         remoteRepository.setId( remoteId );
160         remoteRepository.setName( remoteId );
161         remoteRepository.setDownloadRemoteIndex( true );
162         remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
163         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
164         remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
165         remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
166
167         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
168
169         ProxyConnectorService proxyConnectorService = getProxyConnectorService();
170         ProxyConnector proxyConnector = new ProxyConnector();
171         proxyConnector.setProxyId( "foo-bar2" );
172         proxyConnector.setSourceRepoId( id );
173         proxyConnector.setTargetRepoId( remoteId );
174         proxyConnectorService.addProxyConnector( proxyConnector );
175
176         repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
177
178         // wait a bit
179         /*
180         timeout = 20000;
181         while ( timeout > 0 )
182         {
183             Thread.sleep( 500 );
184             timeout -= 500;
185         }*/
186         // wait the end
187         while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
188         {
189             Thread.sleep( 500 );
190             log.debug( "still running remote download" );
191         }
192
193         SearchService searchService = getSearchService();
194
195         SearchRequest request = new SearchRequest();
196         request.setRepositories( Arrays.asList( id ) );
197         request.setGroupId( "org.apache.felix" );
198
199         List<Artifact> artifacts = searchService.searchArtifacts( request );
200         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
201
202     }
203 }