]> source.dussan.org Git - archiva.git/blob
10bbc7181f0f506d1b313dfb94693c7d277d48e6
[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.rest.api.model.SearchRequest;
27 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
28 import org.apache.archiva.rest.api.services.ProxyConnectorService;
29 import org.apache.archiva.rest.api.services.RepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoryGroupService;
31 import org.apache.archiva.rest.api.services.SearchService;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
34 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
35 import org.fest.assertions.api.Assertions;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41
42 import java.io.File;
43 import java.util.Arrays;
44 import java.util.List;
45
46 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
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     {
59         previousAppServerBase = System.getProperty( "appserver.base" );
60         System.setProperty( "appserver.base", "target/" + DownloadMergedIndexTest.class.getName() );
61     }
62
63     @AfterClass
64     public static void resetAppServerBase()
65     {
66         System.setProperty( "appserver.base", previousAppServerBase );
67     }
68
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 downloadMergedIndex()
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         repositoryGroup.setRepositories( Arrays.asList( id ) );
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         RemoteRepository remoteRepository = new RemoteRepository();
155         remoteRepository.setId( "all-merged" );
156         remoteRepository.setName( "all-merged" );
157         remoteRepository.setDownloadRemoteIndex( true );
158         remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
159         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
160         remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
161         remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
162
163         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
164
165         ProxyConnectorService proxyConnectorService = getProxyConnectorService();
166         ProxyConnector proxyConnector = new ProxyConnector();
167         proxyConnector.setProxyId( "foo-bar1" );
168         proxyConnector.setSourceRepoId( id );
169         proxyConnector.setTargetRepoId( "all-merged" );
170         proxyConnectorService.addProxyConnector( proxyConnector );
171
172         repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
173
174         // wait a bit
175         timeout = 20000;
176         while ( timeout > 0 )
177         {
178             Thread.sleep( 500 );
179             timeout -= 500;
180         }
181
182         SearchService searchService = getSearchService();
183
184         SearchRequest request = new SearchRequest();
185         request.setRepositories( Arrays.asList( id ) );
186         request.setGroupId( "org.apache.felix" );
187
188         List<Artifact> artifacts = searchService.searchArtifacts( request );
189         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
190
191     }
192 }