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