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