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