]> source.dussan.org Git - archiva.git/blob
eac8994ac49e30c8c1abde79c8e92b280e6a5d59
[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.redback.rest.services.AbstractRestServicesTest;
25 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
26 import org.apache.archiva.rest.api.services.BrowseService;
27 import org.apache.archiva.rest.api.services.CommonServices;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.NetworkProxyService;
30 import org.apache.archiva.rest.api.services.PingService;
31 import org.apache.archiva.rest.api.services.ProxyConnectorService;
32 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoriesService;
34 import org.apache.archiva.rest.api.services.RepositoryGroupService;
35 import org.apache.archiva.rest.api.services.SearchService;
36 import org.apache.archiva.security.common.ArchivaRoleConstants;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
40 import org.apache.cxf.jaxrs.client.WebClient;
41 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
42 import org.junit.Before;
43
44 import javax.ws.rs.core.MediaType;
45 import java.io.File;
46 import java.util.Collections;
47 import java.util.Date;
48 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
49 import org.junit.Assume;
50 import org.junit.BeforeClass;
51 import org.junit.runner.RunWith;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * @author Olivier Lamy
56  */
57 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
58 public abstract class AbstractArchivaRestTest
59     extends AbstractRestServicesTest
60 {
61
62     // START SNIPPET: authz-header
63     // guest with an empty password
64     public static String guestAuthzHeader =
65         "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
66
67     // with an other login/password
68     //public String authzHeader =
69     //    "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
70
71     // END SNIPPET: authz-header
72
73     @BeforeClass 
74     public static void chekRepo() {
75         Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains(" ") );
76         LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
77                 error( "Rest services unit test must be run in a folder with no space" );
78         // skygo: was not possible to fix path in this particular module
79         // Skip test if not in proper folder , otherwise test are not fair coz repository
80         // cannot have space in their name.
81     }
82     
83     @Override
84     @Before
85     public void startServer()
86         throws Exception
87     {        
88         File appServerBase = new File( System.getProperty( "appserver.base" ) );
89
90         File jcrDirectory = new File( appServerBase, "jcr" );
91         
92         if ( jcrDirectory.exists() )
93         {
94             FileUtils.deleteDirectory( jcrDirectory );
95         }
96
97         super.startServer();        
98     }
99
100     @Override
101     protected String getSpringConfigLocation()
102     {
103         return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
104     }
105
106     @Override
107     protected String getRestServicesPath()
108     {
109         return "restServices";
110     }
111
112     protected RepositoriesService getRepositoriesService()
113     {
114         return getRepositoriesService( null );
115     }
116
117
118     protected RepositoriesService getRepositoriesService( String authzHeader )
119     {
120         RepositoriesService service =
121             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
122                                        RepositoriesService.class,
123                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
124
125         if ( authzHeader != null )
126         {
127             WebClient.client( service ).header( "Authorization", authzHeader );
128         }
129         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
130         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
131         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
132         return service;
133
134     }
135
136     protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
137     {
138         ManagedRepositoriesService service =
139             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
140                                        ManagedRepositoriesService.class,
141                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
142
143         if ( authzHeader != null )
144         {
145             WebClient.client( service ).header( "Authorization", authzHeader );
146         }
147         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
148         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
149         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
150         return service;
151
152     }
153
154     protected PingService getPingService()
155     {
156         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
157                                           PingService.class,
158                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
159     }
160
161     protected RemoteRepositoriesService getRemoteRepositoriesService()
162     {
163         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
164                                           RemoteRepositoriesService.class,
165                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
166
167
168     }
169
170     protected RepositoryGroupService getRepositoryGroupService()
171     {
172         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
173                                           RepositoryGroupService.class,
174                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
175     }
176
177     protected ProxyConnectorService getProxyConnectorService()
178     {
179         ProxyConnectorService service =
180             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
181                                        ProxyConnectorService.class,
182                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
183
184         WebClient.client( service ).header( "Authorization", authorizationHeader );
185         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
186         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
187         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
188         return service;
189     }
190
191     protected NetworkProxyService getNetworkProxyService()
192     {
193         NetworkProxyService service =
194             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
195                                        NetworkProxyService.class,
196                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
197
198         WebClient.client( service ).header( "Authorization", authorizationHeader );
199         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
200         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
201         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
202         return service;
203     }
204
205     protected ArchivaAdministrationService getArchivaAdministrationService()
206     {
207         ArchivaAdministrationService service =
208             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
209                                        ArchivaAdministrationService.class,
210                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
211
212         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
213         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
214
215         WebClient.client( service ).header( "Authorization", authorizationHeader );
216         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
217         return service;
218     }
219
220     protected BrowseService getBrowseService( String authzHeader, boolean useXml )
221     {
222         BrowseService service =
223             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
224                                        BrowseService.class,
225                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
226         // to add authentification
227         if ( authzHeader != null )
228         {
229             WebClient.client( service ).header( "Authorization", authzHeader );
230         }
231
232         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
233         if ( useXml )
234         {
235             WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
236             WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
237         }
238         else
239         {
240             WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
241             WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
242         }
243         return service;
244
245     }
246
247     protected SearchService getSearchService( String authzHeader )
248     {
249         // START SNIPPET: cxf-searchservice-creation        
250         SearchService service =
251             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
252                                        SearchService.class,
253                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
254         // to add authentification
255         if ( authzHeader != null )
256         {
257             WebClient.client( service ).header( "Authorization", authzHeader );
258         }
259         // to configure read timeout
260         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
261         // if you want to use json as exchange format xml is supported too
262         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
263         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
264         return service;
265         // END SNIPPET: cxf-searchservice-creation
266
267     }
268
269     protected CommonServices getCommonServices( String authzHeader )
270     {
271         CommonServices service =
272             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
273                                        CommonServices.class,
274                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
275
276         if ( authzHeader != null )
277         {
278             WebClient.client( service ).header( "Authorization", authzHeader );
279         }
280         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
281         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
282         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
283         return service;
284     }
285
286     protected ManagedRepository getTestManagedRepository()
287     {
288         String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
289         return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
290                                       false, 2, 3, true, false );
291
292     }
293
294     protected String getBaseUrl()
295     {
296         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
297         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
298     }
299
300     //-----------------------------------------------------
301     // utilities to create repos for testing
302     //-----------------------------------------------------
303
304     static final String TARGET_REPO_ID = "test-copy-target";
305
306     static final String SOURCE_REPO_ID = "test-origin-repo";
307
308     protected void initSourceTargetRepo()
309         throws Exception
310     {
311         File targetRepo = new File( "target/test-repo-copy" );
312         if ( targetRepo.exists() )
313         {
314             FileUtils.deleteDirectory( targetRepo );
315         }
316         assertFalse( targetRepo.exists() );
317         targetRepo.mkdirs();
318
319         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
320         {
321             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
322             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
323         }
324         ManagedRepository managedRepository = getTestManagedRepository();
325         managedRepository.setId( TARGET_REPO_ID );
326         managedRepository.setLocation( targetRepo.getCanonicalPath() );
327         managedRepository.setCronExpression( "* * * * * ?" );
328         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
329         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
330
331         File originRepo = new File( "target/test-origin-repo" );
332         if ( originRepo.exists() )
333         {
334             FileUtils.deleteDirectory( originRepo );
335         }
336         assertFalse( originRepo.exists() );
337         FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
338
339         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
340         {
341             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
342             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
343         }
344
345         managedRepository = getTestManagedRepository();
346         managedRepository.setId( SOURCE_REPO_ID );
347         managedRepository.setLocation( originRepo.getCanonicalPath() );
348
349         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
350         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
351
352         getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
353         getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
354
355     }
356
357     protected void cleanRepos()
358         throws Exception
359     {
360
361         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
362         {
363             try
364             {
365                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
366                 assertNull(
367                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
368             }
369             catch ( Exception e )
370             {
371                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
372             }
373         }
374         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
375         {
376             try
377             {
378                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
379                 assertNull(
380                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
381             }
382             catch ( Exception e )
383             {
384                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
385             }
386         }
387
388     }
389     
390     protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
391         throws Exception
392     {
393         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
394         {
395             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
396         }
397
398         ManagedRepository managedRepository = new ManagedRepository();
399         managedRepository.setId( testRepoId );
400         managedRepository.setName( "test repo" );
401
402         File badContent = new File( repoPath, "target" );
403         if ( badContent.exists() )
404         {
405             FileUtils.deleteDirectory( badContent );
406         }
407         
408         managedRepository.setLocation( new File( repoPath ).getPath() );
409         managedRepository.setIndexDirectory(
410             System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
411
412         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
413         service.addManagedRepository( managedRepository );
414
415         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
416             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
417
418         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
419             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
420         if ( scan )
421         {
422             getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
423         }
424
425     }
426
427     protected void createAndIndexRepo( String testRepoId, String repoPath )
428         throws Exception
429     {
430         createAndIndexRepo( testRepoId, repoPath, true );
431     }
432
433     protected void deleteTestRepo( String id )
434         throws Exception
435     {
436         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
437         {
438             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
439         }
440
441     }
442
443     public String getBasedir()
444     {
445         return System.getProperty( "basedir" );
446     }
447 }