]> source.dussan.org Git - archiva.git/blob
f7aa3dfd61094831b533fecf8a2b81992bd9a77b
[archiva.git] /
1 package org.apache.archiva;
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 org.fest.assertions.api.Assertions;
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     protected String getSpringConfigLocation()
69     {
70         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
71     }
72
73     @After
74     public void cleanup()
75         throws Exception
76     {
77         super.tearDown();
78         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
79         if ( tmpIndexDir.exists() )
80         {
81             FileUtils.deleteDirectory( tmpIndexDir );
82         }
83     }
84
85
86     @Test
87     public void downloadMergedIndexWithNonDefaultPath()
88         throws Exception
89     {
90         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
91         if ( tmpIndexDir.exists() )
92         {
93             FileUtils.deleteDirectory( tmpIndexDir );
94         }
95         String id = Long.toString( System.currentTimeMillis() );
96         ManagedRepository managedRepository = new ManagedRepository();
97         managedRepository.setId( id );
98         managedRepository.setName( "name of " + id );
99         managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
100         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
101
102         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
103
104         if ( managedRepositoriesService.getManagedRepository( id ) != null )
105         {
106             managedRepositoriesService.deleteManagedRepository( id, false );
107         }
108
109         getManagedRepositoriesService().addManagedRepository( managedRepository );
110
111         RepositoriesService repositoriesService = getRepositoriesService();
112
113         repositoriesService.scanRepositoryNow( id, true );
114
115         // wait a bit to ensure index is finished
116         int timeout = 20000;
117         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
118         {
119             Thread.sleep( 500 );
120             timeout -= 500;
121         }
122
123         RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
124
125         String repoGroupId = "test-group";
126
127         if ( repositoryGroupService.getRepositoryGroup( repoGroupId ) != null )
128         {
129             repositoryGroupService.deleteRepositoryGroup( repoGroupId );
130         }
131
132         RepositoryGroup repositoryGroup = new RepositoryGroup();
133         repositoryGroup.setId( repoGroupId );
134         String path = ".fooooo";
135         repositoryGroup.setRepositories( Arrays.asList( id ) );
136         repositoryGroup.setMergedIndexPath( path );
137
138         repositoryGroupService.addRepositoryGroup( repositoryGroup );
139
140         // create a repo with a remote on the one with index
141         id = Long.toString( System.currentTimeMillis() );
142         managedRepository = new ManagedRepository();
143         managedRepository.setId( id );
144         managedRepository.setName( "name of " + id );
145         managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
146         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
147
148         if ( managedRepositoriesService.getManagedRepository( id ) != null )
149         {
150             managedRepositoriesService.deleteManagedRepository( id, false );
151         }
152
153         getManagedRepositoriesService().addManagedRepository( managedRepository );
154
155         String remoteId = Long.toString( System.currentTimeMillis() );
156
157         RemoteRepository remoteRepository = new RemoteRepository();
158         remoteRepository.setId( remoteId );
159         remoteRepository.setName( remoteId );
160         remoteRepository.setDownloadRemoteIndex( true );
161         remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
162         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/" + path );
163         remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
164         remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
165
166         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
167
168         ProxyConnectorService proxyConnectorService = getProxyConnectorService();
169         ProxyConnector proxyConnector = new ProxyConnector();
170         proxyConnector.setProxyId( "foo-bar2" );
171         proxyConnector.setSourceRepoId( id );
172         proxyConnector.setTargetRepoId( remoteId );
173         proxyConnectorService.addProxyConnector( proxyConnector );
174
175         repositoriesService.scheduleDownloadRemoteIndex( remoteId, true, true );
176
177         // wait a bit
178         /*
179         timeout = 20000;
180         while ( timeout > 0 )
181         {
182             Thread.sleep( 500 );
183             timeout -= 500;
184         }*/
185         // wait the end
186         while ( !repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty() )
187         {
188             Thread.sleep( 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         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
199
200     }
201 }