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