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