]> source.dussan.org Git - archiva.git/blob
b784b29f4c52968cb7226c5bbb99f782a6511ad9
[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     @Test
133     public void deleteArtifact()
134         throws Exception
135     {
136         initSourceTargetRepo();
137
138         BrowseService browseService = getBrowseService( authorizationHeader, false );
139
140         List<Artifact> artifacts =
141             browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
142                                                     "2.2.2", SOURCE_REPO_ID );
143
144         log.info( "artifacts: {}", artifacts );
145
146         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
147
148         VersionsList versionsList =
149             browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
150                                            SOURCE_REPO_ID );
151         Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
152
153         log.info( "artifacts.size: {}", artifacts.size() );
154
155         try
156         {
157             File artifactFile = new File(
158                 "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" );
159
160             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
161
162             Artifact artifact = new Artifact();
163             artifact.setGroupId( "org.apache.karaf.features" );
164             artifact.setArtifactId( "org.apache.karaf.features.core" );
165             artifact.setVersion( "2.2.2" );
166             artifact.setPackaging( "jar" );
167             artifact.setContext( SOURCE_REPO_ID );
168
169             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
170
171             repositoriesService.deleteArtifact( artifact );
172
173             assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
174
175             artifacts =
176                 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
177                                                         "2.2.2", SOURCE_REPO_ID );
178
179             Assertions.assertThat( artifacts ).isNotNull().isEmpty();
180
181             versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
182                                                           SOURCE_REPO_ID );
183
184             Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
185
186         }
187         finally
188         {
189             cleanRepos();
190         }
191     }
192
193     @Test
194     public void deleteArtifactWithClassifier()
195         throws Exception
196     {
197         initSourceTargetRepo();
198
199         BrowseService browseService = getBrowseService( authorizationHeader, false );
200
201         List<Artifact> artifacts =
202             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
203
204         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
205
206         VersionsList versionsList =
207             browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
208         Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
209
210         log.info( "artifacts.size: {}", artifacts.size() );
211
212         try
213         {
214             File artifactFile = new File(
215                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
216
217             File artifactFilemd5 = new File(
218                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
219
220             File artifactFilesha1 = new File(
221                 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
222
223             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
224
225             assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
226             assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
227
228             Artifact artifact = new Artifact();
229             artifact.setGroupId( "commons-logging" );
230             artifact.setArtifactId( "commons-logging" );
231             artifact.setVersion( "1.0.1" );
232             artifact.setClassifier( "javadoc" );
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             assertFalse( "md5 still exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
242             assertFalse( "sha1 still exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
243
244             artifacts =
245                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
246
247             log.info( "artifact: {}", artifacts );
248
249             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
250
251             versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
252
253             log.info( "versionsList: {}", versionsList );
254
255             Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
256
257         }
258         finally
259         {
260             cleanRepos();
261         }
262     }
263
264
265     @Test
266     public void deleteGroupId()
267         throws Exception
268     {
269         initSourceTargetRepo();
270         try
271         {
272             BrowseService browseService = getBrowseService( authorizationHeader, false );
273
274             BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
275
276             assertNotNull( browseResult );
277
278             log.info( "browseResult: {}", browseResult );
279
280             Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
281                 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
282                 new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
283
284             File directory =
285                 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
286
287             assertTrue( "directory not exists", directory.exists() );
288
289             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
290             repositoriesService.deleteGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
291
292             assertFalse( "directory not exists", directory.exists() );
293
294             browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
295
296             assertNotNull( browseResult );
297
298             Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
299
300             log.info( "browseResult: {}", browseResult );
301         }
302         finally
303         {
304             cleanRepos();
305         }
306     }
307
308     @Test
309     public void authorizedToDeleteArtifacts()
310         throws Exception
311     {
312         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
313         try
314         {
315             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
316             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
317             assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
318         }
319         finally
320         {
321             cleanQuietlyRepo( managedRepository.getId() );
322         }
323     }
324
325     @Test
326     public void notAuthorizedToDeleteArtifacts()
327         throws Exception
328     {
329         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
330         try
331         {
332             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
333             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
334             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
335         }
336         finally
337         {
338             cleanQuietlyRepo( managedRepository.getId() );
339         }
340     }
341
342     protected void cleanQuietlyRepo( String id )
343     {
344         try
345         {
346             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
347         }
348         catch ( Exception e )
349         {
350             log.info( "ignore issue deleting test repo: {}", e.getMessage() );
351         }
352     }
353
354     @Test
355     public void deleteSnapshot()
356         throws Exception
357     {
358         File targetRepo = initSnapshotRepo();
359         try
360         {
361
362             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
363             //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
364
365             BrowseService browseService = getBrowseService( authorizationHeader, false );
366             List<Artifact> artifacts =
367                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
368                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
369
370             log.info( "artifacts: {}", artifacts );
371
372             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
373
374             File artifactFile = new File( targetRepo,
375                                           "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
376
377             File artifactFilemd5 = new File( targetRepo,
378                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
379
380             File artifactFilepom = new File( targetRepo,
381                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
382
383             Assertions.assertThat( artifactFile ).exists();
384             Assertions.assertThat( artifactFilemd5 ).exists();
385             Assertions.assertThat( artifactFilepom ).exists();
386
387             // we delete only one snapshot
388             Artifact artifact =
389                 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
390             artifact.setPackaging( "jar" );
391             artifact.setRepositoryId( SNAPSHOT_REPO_ID );
392             artifact.setContext( SNAPSHOT_REPO_ID );
393
394             repositoriesService.deleteArtifact( artifact );
395
396             artifacts =
397                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
398                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
399
400             log.info( "artifacts: {}", artifacts );
401
402             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
403
404             Assertions.assertThat( artifactFile ).doesNotExist();
405             Assertions.assertThat( artifactFilemd5 ).doesNotExist();
406             Assertions.assertThat( artifactFilepom ).doesNotExist();
407         }
408         catch ( Exception e )
409         {
410             log.error( e.getMessage(), e );
411             throw e;
412         }
413         finally
414         {
415             cleanSnapshotRepo();
416         }
417     }
418
419     protected File initSnapshotRepo()
420         throws Exception
421     {
422         File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
423         if ( targetRepo.exists() )
424         {
425             FileUtils.deleteDirectory( targetRepo );
426         }
427         assertFalse( targetRepo.exists() );
428
429         FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
430                                             targetRepo.getParentFile() );
431
432         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
433         {
434             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
435             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
436         }
437         ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
438         /*managedRepository.setId( SNAPSHOT_REPO_ID );
439         managedRepository.setLocation( );
440         managedRepository.setCronExpression( "* * * * * ?" );*/
441         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
442         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
443
444         return targetRepo;
445     }
446
447     protected void cleanSnapshotRepo()
448         throws Exception
449     {
450
451         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
452         {
453             try
454             {
455                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
456                 assertNull(
457                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
458             }
459             catch ( Exception e )
460             {
461                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
462             }
463         }
464
465     }
466
467     protected ManagedRepository getTestManagedRepository( String id, String path )
468     {
469         String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
470         return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
471                                       true, false );
472     }
473
474     protected ManagedRepository getTestManagedRepository()
475     {
476         return getTestManagedRepository( "TEST", "test-repo" );
477     }
478
479
480     static final String SNAPSHOT_REPO_ID = "snapshot-repo";
481
482
483 }