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