]> source.dussan.org Git - archiva.git/blob
df0e3bbb07d3ebe3e8cc320232ed2094fc8461de
[archiva.git] /
1 package org.apache.archiva.rest.services;
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
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
25 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
26 import org.apache.archiva.rest.api.services.NetworkProxyService;
27 import org.apache.archiva.rest.api.services.PingService;
28 import org.apache.archiva.rest.api.services.ProxyConnectorService;
29 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoryGroupService;
32 import org.apache.archiva.rest.api.services.SearchService;
33 import org.apache.archiva.security.common.ArchivaRoleConstants;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
37 import org.apache.cxf.jaxrs.client.WebClient;
38 import org.codehaus.redback.rest.services.AbstractRestServicesTest;
39 import org.junit.Before;
40
41 import javax.ws.rs.core.MediaType;
42 import java.io.File;
43 import java.util.Date;
44
45 /**
46  * @author Olivier Lamy
47  */
48 public abstract class AbstractArchivaRestTest
49     extends AbstractRestServicesTest
50 {
51
52     // START SNIPPET: authz-header
53     // guest with an empty password
54     public String guestAuthzHeader =
55         "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
56
57     // with an other login/password
58     //public String authzHeader =
59     //    "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
60
61     // END SNIPPET: authz-header
62
63
64     @Override
65     @Before
66     public void startServer()
67         throws Exception
68     {
69         File appServerBase = new File( System.getProperty( "appserver.base" ) );
70
71         File jcrDirectory = new File( appServerBase, "jcr" );
72
73         if ( jcrDirectory.exists() )
74         {
75             FileUtils.deleteDirectory( jcrDirectory );
76         }
77
78         super.startServer();
79     }
80
81     @Override
82     protected String getSpringConfigLocation()
83     {
84         return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
85     }
86
87     protected String getRestServicesPath()
88     {
89         return "restServices";
90     }
91
92     protected RepositoriesService getRepositoriesService()
93     {
94         return getRepositoriesService( null );
95     }
96
97
98     protected RepositoriesService getRepositoriesService( String authzHeader )
99     {
100         RepositoriesService service =
101             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
102                                        RepositoriesService.class );
103
104         if ( authzHeader != null )
105         {
106             WebClient.client( service ).header( "Authorization", authzHeader );
107         }
108         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
109         return service;
110
111     }
112
113     protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
114     {
115         ManagedRepositoriesService service =
116             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
117                                        ManagedRepositoriesService.class );
118
119         if ( authzHeader != null )
120         {
121             WebClient.client( service ).header( "Authorization", authzHeader );
122         }
123         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
124         return service;
125
126     }
127
128     protected PingService getPingService()
129     {
130         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
131                                           PingService.class );
132     }
133
134     protected RemoteRepositoriesService getRemoteRepositoriesService()
135     {
136         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
137                                           RemoteRepositoriesService.class );
138
139
140     }
141
142     protected RepositoryGroupService getRepositoryGroupService()
143     {
144         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
145                                           RepositoryGroupService.class );
146     }
147
148     protected ProxyConnectorService getProxyConnectorService()
149     {
150         ProxyConnectorService service =
151             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
152                                        ProxyConnectorService.class );
153
154         WebClient.client( service ).header( "Authorization", authorizationHeader );
155         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
156         return service;
157     }
158
159     protected NetworkProxyService getNetworkProxyService()
160     {
161         NetworkProxyService service =
162             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
163                                        NetworkProxyService.class );
164
165         WebClient.client( service ).header( "Authorization", authorizationHeader );
166         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
167         return service;
168     }
169
170     protected ArchivaAdministrationService getArchivaAdministrationService()
171     {
172         ArchivaAdministrationService service =
173             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
174                                        ArchivaAdministrationService.class );
175
176         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
177         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
178
179         WebClient.client( service ).header( "Authorization", authorizationHeader );
180         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
181         return service;
182     }
183
184     protected SearchService getSearchService( String authzHeader )
185     {
186         // START SNIPPET: cxf-searchservice-creation
187         SearchService service =
188             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
189                                        SearchService.class );
190
191         if ( authzHeader != null )
192         {
193             WebClient.client( service ).header( "Authorization", authzHeader );
194         }
195         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
196         return service;
197         // END SNIPPET: cxf-searchservice-creation
198
199     }
200
201     protected ManagedRepository getTestManagedRepository()
202     {
203         String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
204         return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
205                                       false, 2, 3, true, false );
206
207     }
208
209     protected String getBaseUrl()
210     {
211         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
212         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
213     }
214
215     //-----------------------------------------------------
216     // utilities to create repos for testing
217     //-----------------------------------------------------
218
219     static final String TARGET_REPO_ID = "test-copy-target";
220
221     static final String SOURCE_REPO_ID = "test-origin-repo";
222
223     protected void initSourceTargetRepo()
224         throws Exception
225     {
226         File targetRepo = new File( "target/test-repo-copy" );
227         if ( targetRepo.exists() )
228         {
229             FileUtils.deleteDirectory( targetRepo );
230         }
231         assertFalse( targetRepo.exists() );
232         targetRepo.mkdirs();
233
234         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
235         {
236             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
237             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
238         }
239         ManagedRepository managedRepository = getTestManagedRepository();
240         managedRepository.setId( TARGET_REPO_ID );
241         managedRepository.setLocation( targetRepo.getCanonicalPath() );
242         managedRepository.setCronExpression( "* * * * * ?" );
243         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
244         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
245
246         File originRepo = new File( "target/test-origin-repo" );
247         if ( originRepo.exists() )
248         {
249             FileUtils.deleteDirectory( originRepo );
250         }
251         assertFalse( originRepo.exists() );
252         FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
253
254         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
255         {
256             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
257             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
258         }
259
260         managedRepository = getTestManagedRepository();
261         managedRepository.setId( SOURCE_REPO_ID );
262         managedRepository.setLocation( originRepo.getCanonicalPath() );
263
264         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
265         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
266
267         getArchivaAdministrationService().addKnownContentConsumer( "create-missing-checksums" );
268         getArchivaAdministrationService().addKnownContentConsumer( "metadata-updater" );
269
270     }
271
272     protected void cleanRepos()
273         throws Exception
274     {
275
276         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
277         {
278             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
279             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
280         }
281         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
282         {
283             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
284             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
285         }
286
287     }
288
289     protected void createAndIndexRepo( String testRepoId, String repoPath )
290         throws Exception
291     {
292         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
293         {
294             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
295         }
296
297         ManagedRepository managedRepository = new ManagedRepository();
298         managedRepository.setId( testRepoId );
299         managedRepository.setName( "test repo" );
300
301         File badContent = new File( repoPath, "target" );
302         if ( badContent.exists() )
303         {
304             FileUtils.deleteDirectory( badContent );
305         }
306
307         managedRepository.setLocation( new File( repoPath ).getPath() );
308         managedRepository.setIndexDirectory(
309             System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
310
311         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
312         service.addManagedRepository( managedRepository );
313
314         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
315             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
316
317         getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
318
319     }
320
321     protected void deleteTestRepo( String id )
322         throws Exception
323     {
324         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
325         {
326             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
327         }
328
329     }
330 }