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