1 package org.apache.archiva.rest.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
37 import java.util.List;
40 * @author Olivier Lamy
42 public class RepositoriesServiceTest
43 extends AbstractArchivaRestTest
46 @Test ( expected = ServerWebApplicationException.class )
47 public void scanRepoKarmaFailed()
50 RepositoriesService service = getRepositoriesService();
53 service.scanRepository( "id", true );
55 catch ( ServerWebApplicationException e )
57 assertEquals( 403, e.getStatus() );
63 public void scanRepo()
66 RepositoriesService service = getRepositoriesService( authorizationHeader );
68 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
70 String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
73 while ( timeout > 0 && service.alreadyScanning( repoId ) )
79 assertTrue( service.scanRepository( repoId, true ) );
82 @Test ( expected = ServerWebApplicationException.class )
83 public void deleteArtifactKarmaFailed()
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 );
95 RepositoriesService repositoriesService = getRepositoriesService( null );
97 repositoriesService.deleteArtifact( artifact );
99 catch ( ServerWebApplicationException e )
101 assertEquals( 403, e.getStatus() );
107 @Test ( expected = ServerWebApplicationException.class )
108 public void deleteWithRepoNull()
114 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
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" );
122 repositoriesService.deleteArtifact( artifact );
124 catch ( ServerWebApplicationException e )
126 assertEquals( "not http 400 status", 400, e.getStatus() );
133 * delete a version of an artifact without packaging
138 public void deleteArtifactVersion()
141 initSourceTargetRepo();
143 BrowseService browseService = getBrowseService( authorizationHeader, false );
145 List<Artifact> artifacts =
146 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
147 "2.2.2", SOURCE_REPO_ID );
149 log.info( "artifacts: {}", artifacts );
151 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
153 VersionsList versionsList =
154 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
156 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
158 log.info( "artifacts.size: {}", artifacts.size() );
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" );
165 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
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 );
173 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
175 repositoriesService.deleteArtifact( artifact );
177 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
180 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
181 "2.2.2", SOURCE_REPO_ID );
183 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
185 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
188 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
199 public void deleteArtifact()
202 initSourceTargetRepo();
204 BrowseService browseService = getBrowseService( authorizationHeader, false );
206 List<Artifact> artifacts =
207 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
208 "2.2.2", SOURCE_REPO_ID );
210 log.info( "artifacts: {}", artifacts );
212 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
214 VersionsList versionsList =
215 browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
217 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
219 log.info( "artifacts.size: {}", artifacts.size() );
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" );
226 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
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 );
235 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
237 repositoriesService.deleteArtifact( artifact );
239 assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
242 browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
243 "2.2.2", SOURCE_REPO_ID );
245 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
247 versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
250 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
260 public void deleteArtifactWithClassifier()
263 initSourceTargetRepo();
265 BrowseService browseService = getBrowseService( authorizationHeader, false );
267 List<Artifact> artifacts =
268 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
270 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
272 VersionsList versionsList =
273 browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
274 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
276 log.info( "artifacts.size: {}", artifacts.size() );
280 File artifactFile = new File(
281 "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
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" );
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" );
289 assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
291 assertTrue( "md5 not exists:" + artifactFilemd5.getPath(), artifactFilemd5.exists() );
292 assertTrue( "sha1 not exists:" + artifactFilesha1.getPath(), artifactFilesha1.exists() );
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 );
302 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
304 repositoriesService.deleteArtifact( artifact );
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() );
311 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
313 log.info( "artifact: {}", artifacts );
315 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
317 versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
319 log.info( "versionsList: {}", versionsList );
321 Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
332 public void deleteGroupId()
335 initSourceTargetRepo();
338 BrowseService browseService = getBrowseService( authorizationHeader, false );
340 BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
342 assertNotNull( browseResult );
344 log.info( "browseResult: {}", browseResult );
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 ) );
351 new File( "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
353 assertTrue( "directory not exists", directory.exists() );
355 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
356 repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
358 assertFalse( "directory not exists", directory.exists() );
360 browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
362 assertNotNull( browseResult );
364 Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
366 browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
368 assertNotNull( browseResult );
370 Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
372 log.info( "browseResult empty: {}", browseResult );
381 public void authorizedToDeleteArtifacts()
384 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
387 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
388 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
389 assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
393 cleanQuietlyRepo( managedRepository.getId() );
398 public void notAuthorizedToDeleteArtifacts()
401 ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
404 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
405 RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
406 assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
410 cleanQuietlyRepo( managedRepository.getId() );
414 protected void cleanQuietlyRepo( String id )
418 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
420 catch ( Exception e )
422 log.info( "ignore issue deleting test repo: {}", e.getMessage() );
427 public void deleteSnapshot()
430 File targetRepo = initSnapshotRepo();
434 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
435 //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
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 );
442 log.info( "artifacts: {}", artifacts );
444 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
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" );
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" );
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" );
455 Assertions.assertThat( artifactFile ).exists();
456 Assertions.assertThat( artifactFilemd5 ).exists();
457 Assertions.assertThat( artifactFilepom ).exists();
459 // we delete only one snapshot
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 );
466 repositoriesService.deleteArtifact( artifact );
469 browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
470 "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
472 log.info( "artifacts: {}", artifacts );
474 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
476 Assertions.assertThat( artifactFile ).doesNotExist();
477 Assertions.assertThat( artifactFilemd5 ).doesNotExist();
478 Assertions.assertThat( artifactFilepom ).doesNotExist();
480 catch ( Exception e )
482 log.error( e.getMessage(), e );
491 protected File initSnapshotRepo()
494 File targetRepo = new File( getBasedir(), "target/repo-with-snapshots" );
495 if ( targetRepo.exists() )
497 FileUtils.deleteDirectory( targetRepo );
499 assertFalse( targetRepo.exists() );
501 FileUtils.copyDirectoryToDirectory( new File( getBasedir(), "src/test/repo-with-snapshots" ),
502 targetRepo.getParentFile() );
504 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
506 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
507 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
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 ) );
519 protected void cleanSnapshotRepo()
523 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
527 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
529 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
531 catch ( Exception e )
533 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
539 protected ManagedRepository getTestManagedRepository( String id, String path )
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,
546 protected ManagedRepository getTestManagedRepository()
548 return getTestManagedRepository( "TEST", "test-repo" );
552 static final String SNAPSHOT_REPO_ID = "snapshot-repo";