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