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