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