]> source.dussan.org Git - archiva.git/blob
234df690316b86495c0a9aa9c697fc102344ef9e
[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.rest.api.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.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40
41 import java.io.File;
42 import java.util.Arrays;
43 import java.util.List;
44 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
45
46 /**
47  * @author Olivier Lamy
48  */
49 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
50 public class DownloadMergedIndexTest
51     extends AbstractDownloadTest
52 {
53
54     @BeforeClass
55     public static void setAppServerBase()
56     {
57         previousAppServerBase = System.getProperty( "appserver.base" );
58         System.setProperty( "appserver.base", "target/" + DownloadMergedIndexTest.class.getName() );
59     }
60
61     @AfterClass
62     public static void resetAppServerBase()
63     {
64         System.setProperty( "appserver.base", previousAppServerBase );
65     }
66
67     protected String getSpringConfigLocation()
68     {
69         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-merge-index-download.xml";
70     }
71
72     @After
73     public void cleanup()
74         throws Exception
75     {
76         super.tearDown();
77         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
78         if ( tmpIndexDir.exists() )
79         {
80             FileUtils.deleteDirectory( tmpIndexDir );
81         }
82     }
83
84
85     @Test
86     public void downloadMergedIndex()
87         throws Exception
88     {
89         File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
90         if ( tmpIndexDir.exists() )
91         {
92             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( "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         RepositoryGroup repositoryGroup = new RepositoryGroup();
125         repositoryGroup.setId( "test-group" );
126         repositoryGroup.setRepositories( Arrays.asList( id ) );
127
128         repositoryGroupService.addRepositoryGroup( repositoryGroup );
129
130         // create a repo with a remote on the one with index
131         id = Long.toString( System.currentTimeMillis() );
132         managedRepository = new ManagedRepository();
133         managedRepository.setId( id );
134         managedRepository.setName( "name of " + id );
135         managedRepository.setLocation( "src/test/repositories/test-repo" );
136         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
137
138         if ( managedRepositoriesService.getManagedRepository( id ) != null )
139         {
140             managedRepositoriesService.deleteManagedRepository( id, false );
141         }
142
143         getManagedRepositoriesService().addManagedRepository( managedRepository );
144
145         RemoteRepository remoteRepository = new RemoteRepository();
146         remoteRepository.setId( "all-merged" );
147         remoteRepository.setName( "all-merged" );
148         remoteRepository.setDownloadRemoteIndex( true );
149         remoteRepository.setUrl( "http://localhost:" + port + "/repository/test-group" );
150         remoteRepository.setRemoteIndexUrl( "http://localhost:" + port + "/repository/test-group/.indexer" );
151         remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
152         remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
153
154         getRemoteRepositoriesService().addRemoteRepository( remoteRepository );
155
156         ProxyConnectorService proxyConnectorService = getProxyConnectorService();
157         ProxyConnector proxyConnector = new ProxyConnector();
158         proxyConnector.setProxyId( "foo-bar" );
159         proxyConnector.setSourceRepoId( id );
160         proxyConnector.setTargetRepoId( "all-merged" );
161         proxyConnectorService.addProxyConnector( proxyConnector );
162
163         repositoriesService.scheduleDownloadRemoteIndex( "all-merged", true, true );
164
165         // wait a bit
166         timeout = 20000;
167         while ( timeout > 0 )
168         {
169             Thread.sleep( 500 );
170             timeout -= 500;
171         }
172
173         SearchService searchService = getSearchService();
174
175         SearchRequest request = new SearchRequest();
176         request.setRepositories( Arrays.asList( id ) );
177         request.setGroupId( "org.apache.felix" );
178
179         List<Artifact> artifacts = searchService.searchArtifacts( request );
180         assertFalse( artifacts.isEmpty() );
181         assertEquals( 1, artifacts.size() );
182     }
183 }