]> source.dussan.org Git - archiva.git/blob
aab1ea9621073300f7af3a883d702316e0ac7cb2
[archiva.git] /
1 package org.apache.archiva.rest.services;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.maven2.model.Artifact;
24 import org.apache.archiva.rest.api.model.BrowseResult;
25 import org.apache.archiva.rest.api.model.BrowseResultEntry;
26 import org.apache.archiva.rest.api.model.VersionsList;
27 import org.apache.archiva.rest.api.services.BrowseService;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.RepositoriesService;
30 import org.apache.commons.io.FileUtils;
31 import org.junit.Test;
32
33 import javax.ws.rs.BadRequestException;
34 import javax.ws.rs.ForbiddenException;
35 import javax.ws.rs.core.Response;
36 import java.io.File;
37 import java.util.List;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40
41 /**
42  * @author Olivier Lamy
43  */
44 public class RepositoriesServiceTest
45     extends AbstractArchivaRestTest
46 {
47
48     @Test( expected = ForbiddenException.class )
49     public void scanRepoKarmaFailed()
50         throws Exception
51     {
52         RepositoriesService service = getRepositoriesService();
53         try
54         {
55             service.scanRepository( "id", true );
56         }
57         catch ( ForbiddenException e )
58         {
59             assertEquals( 403, e.getResponse().getStatus() );
60             throw e;
61         }
62     }
63
64     @Test
65     public void scanRepo()
66         throws Exception
67     {
68         RepositoriesService service = getRepositoriesService( authorizationHeader );
69
70         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
71
72         String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
73
74         int timeout = 20000;
75         while ( timeout > 0 && service.alreadyScanning( repoId ) )
76         {
77             Thread.sleep( 500 );
78             timeout -= 500;
79         }
80
81         assertTrue( service.scanRepository( repoId, true ) );
82     }
83
84     @Test( expected = ForbiddenException.class )
85     public void deleteArtifactKarmaFailed()
86         throws Exception
87     {
88         try
89         {
90             Artifact artifact = new Artifact();
91             artifact.setGroupId( "commons-logging" );
92             artifact.setArtifactId( "commons-logging" );
93             artifact.setVersion( "1.0.1" );
94             artifact.setPackaging( "jar" );
95             artifact.setContext( SOURCE_REPO_ID );
96
97             RepositoriesService repositoriesService = getRepositoriesService( null );
98
99             repositoriesService.deleteArtifact( artifact );
100         }
101         catch ( ForbiddenException e )
102         {
103             assertEquals( 403, e.getResponse().getStatus() );
104             throw e;
105
106         }
107     }
108
109     @Test( expected = BadRequestException.class )
110     public void deleteWithRepoNull()
111         throws Exception
112     {
113         try
114         {
115
116             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
117
118             Artifact artifact = new Artifact();
119             artifact.setGroupId( "commons-logging" );
120             artifact.setArtifactId( "commons-logging" );
121             artifact.setVersion( "1.0.1" );
122             artifact.setPackaging( "jar" );
123
124             repositoriesService.deleteArtifact( artifact );
125         }
126         catch ( BadRequestException e )
127         {
128             assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
129                           Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
130             throw e;
131         }
132     }
133
134
135     /**
136      * delete a version of an artifact without packaging
137      *
138      * @throws Exception
139      */
140     @Test
141     public void deleteArtifactVersion()
142         throws Exception
143     {
144         initSourceTargetRepo();
145
146         BrowseService browseService = getBrowseService( authorizationHeader, false );
147
148         List<Artifact> artifacts =
149             browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
150                                                     "2.2.2", SOURCE_REPO_ID );
151
152         log.info( "artifacts: {}", artifacts );
153
154         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
155
156         VersionsList versionsList =
157             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
158                                            SOURCE_REPO_ID );
159         assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
160
161         log.info( "artifacts.size: {}", artifacts.size() );
162
163         try
164         {
165             File artifactFile = new File(
166                 "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
167
168             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
169
170             Artifact artifact = new Artifact();
171             artifact.setGroupId( "org.apache.karaf.features" );
172             artifact.setArtifactId( "org.apache.karaf.features.core" );
173             artifact.setVersion( "2.2.2" );
174             artifact.setContext( SOURCE_REPO_ID );
175
176             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
177
178             repositoriesService.deleteArtifact( artifact );
179
180             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
181
182             artifacts =
183                 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
184                                                         "2.2.2", SOURCE_REPO_ID );
185
186             assertThat( artifacts ).isNotNull().isEmpty();
187
188             versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
189                                                           SOURCE_REPO_ID );
190
191             assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
192
193         }
194         finally
195         {
196             cleanRepos();
197         }
198     }
199
200
201     @Test
202     public void deleteArtifact()
203         throws Exception
204     {
205         initSourceTargetRepo();
206
207         BrowseService browseService = getBrowseService( authorizationHeader, false );
208
209         List<Artifact> artifacts =
210             browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
211                                                     "2.2.2", SOURCE_REPO_ID );
212
213         log.info( "artifacts: {}", artifacts );
214
215         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
216
217         VersionsList versionsList =
218             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
219                                            SOURCE_REPO_ID );
220         assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
221
222         log.info( "artifacts.size: {}", artifacts.size() );
223
224         try
225         {
226             File artifactFile = new File(
227                 "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
228
229             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
230
231             Artifact artifact = new Artifact();
232             artifact.setGroupId( "org.apache.karaf.features" );
233             artifact.setArtifactId( "org.apache.karaf.features.core" );
234             artifact.setVersion( "2.2.2" );
235             artifact.setPackaging( "jar" );
236             artifact.setContext( SOURCE_REPO_ID );
237
238             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
239
240             repositoriesService.deleteArtifact( artifact );
241
242             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
243
244             artifacts =
245                 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
246                                                         "2.2.2", SOURCE_REPO_ID );
247
248             assertThat( artifacts ).isNotNull().isEmpty();
249
250             versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
251                                                           SOURCE_REPO_ID );
252
253             assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
254
255         }
256         finally
257         {
258             cleanRepos();
259         }
260     }
261
262     @Test
263     public void deleteArtifactWithClassifier()
264         throws Exception
265     {
266         initSourceTargetRepo();
267
268         BrowseService browseService = getBrowseService( authorizationHeader, false );
269
270         List<Artifact> artifacts =
271             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
272
273         assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
274
275         VersionsList versionsList =
276             browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
277         assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
278
279         log.info( "artifacts.size: {}", artifacts.size() );
280
281         try
282         {
283             File artifactFile = new File(
284                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
285
286             File artifactFilemd5 = new File(
287                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
288
289             File artifactFilesha1 = new File(
290                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
291
292             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
293
294             assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
295             assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
296
297             Artifact artifact = new Artifact();
298             artifact.setGroupId( "commons-logging" );
299             artifact.setArtifactId( "commons-logging" );
300             artifact.setVersion( "1.0.1" );
301             artifact.setClassifier( "javadoc" );
302             artifact.setPackaging( "jar" );
303             artifact.setContext( SOURCE_REPO_ID );
304
305             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
306
307             repositoriesService.deleteArtifact( artifact );
308
309             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
310             assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
311             assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
312
313             artifacts =
314                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
315
316             log.info( "artifact: {}", artifacts );
317
318             assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
319
320             versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
321
322             log.info( "versionsList: {}", versionsList );
323
324             assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
325
326         }
327         finally
328         {
329             cleanRepos();
330         }
331     }
332
333
334     @Test
335     public void deleteGroupId()
336         throws Exception
337     {
338         initSourceTargetRepo();
339         try
340         {
341             BrowseService browseService = getBrowseService( authorizationHeader, false );
342
343             BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
344
345             assertNotNull( browseResult );
346
347             log.info( "browseResult: {}", browseResult );
348
349             assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
350                 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
351                 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
352
353             File directory =
354                 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
355
356             assertTrue( "directory not exists", directory.exists() );
357
358             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
359             repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
360
361             assertFalse( "directory not exists", directory.exists() );
362
363             browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
364
365             assertNotNull( browseResult );
366
367             assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
368
369             browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
370
371             assertNotNull( browseResult );
372
373             assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
374
375             log.info( "browseResult empty: {}", browseResult );
376         }
377         finally
378         {
379             cleanRepos();
380         }
381     }
382
383     @Test
384     public void authorizedToDeleteArtifacts()
385         throws Exception
386     {
387         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
388         try
389         {
390             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
391             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
392             assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
393         }
394         finally
395         {
396             cleanQuietlyRepo( managedRepository.getId() );
397         }
398     }
399
400     @Test
401     public void notAuthorizedToDeleteArtifacts()
402         throws Exception
403     {
404         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
405         try
406         {
407             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
408             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
409             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
410         }
411         finally
412         {
413             cleanQuietlyRepo( managedRepository.getId() );
414         }
415     }
416
417     protected void cleanQuietlyRepo( String id )
418     {
419         try
420         {
421             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
422         }
423         catch ( Exception e )
424         {
425             log.info( "ignore issue deleting test repo: {}", e.getMessage() );
426         }
427     }
428
429     @Test
430     public void deleteSnapshot()
431         throws Exception
432     {
433         File targetRepo = initSnapshotRepo();
434         try
435         {
436
437             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
438             //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
439
440             BrowseService browseService = getBrowseService( authorizationHeader, false );
441             List<Artifact> artifacts =
442                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
443                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
444
445             log.info( "artifacts: {}", artifacts );
446
447             assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
448
449             File artifactFile = new File( targetRepo,
450                                           "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
451
452             File artifactFilemd5 = new File( targetRepo,
453                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
454
455             File artifactFilepom = new File( targetRepo,
456                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
457
458             assertThat( artifactFile ).exists();
459             assertThat( artifactFilemd5 ).exists();
460             assertThat( artifactFilepom ).exists();
461
462             // we delete only one snapshot
463             Artifact artifact =
464                 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
465             artifact.setPackaging( "jar" );
466             artifact.setRepositoryId( SNAPSHOT_REPO_ID );
467             artifact.setContext( SNAPSHOT_REPO_ID );
468
469             repositoriesService.deleteArtifact( artifact );
470
471             artifacts =
472                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
473                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
474
475             log.info( "artifacts: {}", artifacts );
476
477             assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
478
479             assertThat( artifactFile ).doesNotExist();
480             assertThat( artifactFilemd5 ).doesNotExist();
481             assertThat( artifactFilepom ).doesNotExist();
482         }
483         catch ( Exception e )
484         {
485             log.error( e.getMessage(), e );
486             throw e;
487         }
488         finally
489         {
490             cleanSnapshotRepo();
491         }
492     }
493
494     protected File initSnapshotRepo()
495         throws Exception
496     {
497         File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
498         if ( targetRepo.exists() )
499         {
500             FileUtils.deleteDirectory( targetRepo );
501         }
502         assertFalse( targetRepo.exists() );
503
504         FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
505                                             targetRepo.getParentFile() );
506
507         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
508         {
509             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
510             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
511         }
512         ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
513         /*managedRepository.setId( SNAPSHOT_REPO_ID );
514         managedRepository.setLocation( );
515         managedRepository.setCronExpression( "* * * * * ?" );*/
516         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
517         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
518
519         return targetRepo;
520     }
521
522     protected void cleanSnapshotRepo()
523         throws Exception
524     {
525
526         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
527         {
528             try
529             {
530                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
531                 assertNull(
532                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
533             }
534             catch ( Exception e )
535             {
536                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
537             }
538         }
539
540     }
541
542     protected ManagedRepository getTestManagedRepository( String id, String path )
543     {
544         String location = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/" + path ).getAbsolutePath();
545         return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
546                                       true, false );
547     }
548
549     @Override
550     protected ManagedRepository getTestManagedRepository()
551     {
552         return getTestManagedRepository( "TEST", "test-repo" );
553     }
554
555
556     static final String SNAPSHOT_REPO_ID = "snapshot-repo";
557
558
559 }