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