]> source.dussan.org Git - archiva.git/blob
89d6fdb0123af08f98c926866c870fa42c26a6f1
[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.rest.api.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.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             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
322                                                                                           true );
323         }
324     }
325
326     @Test
327     public void notAuthorizedToDeleteArtifacts()
328         throws Exception
329     {
330         ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
331         try
332         {
333             getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
334             RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
335             assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
336         }
337         finally
338         {
339             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( managedRepository.getId(),
340                                                                                           true );
341         }
342     }
343
344     @Test
345     public void deleteSnapshot()
346         throws Exception
347     {
348         File targetRepo = initSnapshotRepo();
349         try
350         {
351
352             RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
353             //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
354
355             BrowseService browseService = getBrowseService( authorizationHeader, false );
356             List<Artifact> artifacts =
357                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
358                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
359
360             log.info( "artifacts: {}", artifacts );
361
362             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
363
364             File artifactFile = new File( targetRepo,
365                                           "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
366
367             File artifactFilemd5 = new File( targetRepo,
368                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
369
370             File artifactFilepom = new File( targetRepo,
371                                              "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
372
373             Assertions.assertThat( artifactFile ).exists();
374             Assertions.assertThat( artifactFilemd5 ).exists();
375             Assertions.assertThat( artifactFilepom ).exists();
376
377             // we delete only one snapshot
378             Artifact artifact =
379                 new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
380             artifact.setPackaging( "jar" );
381             artifact.setRepositoryId( SNAPSHOT_REPO_ID );
382             artifact.setContext( SNAPSHOT_REPO_ID );
383
384             repositoriesService.deleteArtifact( artifact );
385
386
387             artifacts =
388                 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
389                                                         "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
390
391             log.info( "artifacts: {}", artifacts );
392
393             Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
394
395             Assertions.assertThat( artifactFile ).doesNotExist();
396             Assertions.assertThat( artifactFilemd5 ).doesNotExist();
397             Assertions.assertThat( artifactFilepom ).doesNotExist();
398         }
399         catch ( Exception e )
400         {
401             log.error( e.getMessage(), e );
402             throw e;
403         }
404         finally
405         {
406             cleanSnapshotRepo();
407         }
408     }
409
410     protected File initSnapshotRepo()
411         throws Exception
412     {
413         File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
414         if ( targetRepo.exists() )
415         {
416             FileUtils.deleteDirectory( targetRepo );
417         }
418         assertFalse( targetRepo.exists() );
419
420         FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
421                                             targetRepo.getParentFile() );
422
423         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
424         {
425             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
426             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
427         }
428         ManagedRepository managedRepository =
429             getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
430         /*managedRepository.setId( SNAPSHOT_REPO_ID );
431         managedRepository.setLocation( );
432         managedRepository.setCronExpression( "* * * * * ?" );*/
433         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
434         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
435
436         return targetRepo;
437     }
438
439     protected void cleanSnapshotRepo()
440         throws Exception
441     {
442
443         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
444         {
445             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
446             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
447         }
448
449     }
450
451     protected ManagedRepository getTestManagedRepository( String id, String path )
452     {
453         String location = new File( FileUtil.getBasedir(), "target/" + path ).getAbsolutePath();
454         return new ManagedRepository( id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
455                                       true, false );
456     }
457
458     protected ManagedRepository getTestManagedRepository()
459     {
460         return getTestManagedRepository( "TEST", "test-repo" );
461     }
462
463
464     static final String SNAPSHOT_REPO_ID = "snapshot-repo";
465
466
467 }