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