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