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