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