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